BadScript 2
Loading...
Searching...
No Matches
BadTask.cs
Go to the documentation of this file.
1using System.Text;
2
10
12
16public class BadTask : BadObject
17{
22 "Task",
23 (ctx, args) =>
24 {
25 if (args.Length != 2)
26 {
27 throw new BadRuntimeException("Task constructor takes 2 arguments");
28 }
29
30 if (args[0] is not IBadString name)
31 {
32 throw new BadRuntimeException("Task constructor takes a string as first argument");
33 }
34
35 if (args[1] is not BadFunction f)
36 {
37 throw new BadRuntimeException("Task constructor takes a function as second argument");
38 }
39
40 return new BadTask(BadRunnable.Create(f, ctx), name.Value);
41 }
42 );
43
47 private readonly List<BadTask> m_ContinuationTasks = new List<BadTask>();
48
52 private readonly Dictionary<string, BadObjectReference> m_Properties =
53 new Dictionary<string, BadObjectReference>();
54
60 public BadTask(BadRunnable runnable, string name)
61 {
62 Name = name;
63 Runnable = runnable;
64 m_Properties.Add(
65 "Name",
66 BadObjectReference.Make("Task.Name", () => Name, (o, _) => Name = o is IBadString s ? s.Value : Name)
67 );
68 m_Properties.Add("IsCompleted", BadObjectReference.Make("Task.IsCompleted", () => IsFinished));
69 m_Properties.Add("IsInactive", BadObjectReference.Make("Task.IsInactive", () => IsInactive));
70 m_Properties.Add("IsPaused", BadObjectReference.Make("Task.IsPaused", () => IsPaused));
71 m_Properties.Add("IsRunning", BadObjectReference.Make("Task.IsRunning", () => IsRunning));
72
73 BadFunction continueFunc =
78 m_Properties.Add("ContinueWith", BadObjectReference.Make("Task.ContinueWith", () => continueFunc));
79 m_Properties.Add("Pause", BadObjectReference.Make("Task.Pause", () => pauseFunc));
80 m_Properties.Add("Resume", BadObjectReference.Make("Task.Resume", () => resumeFunc));
81 m_Properties.Add("Cancel", BadObjectReference.Make("Task.Cancel", () => cancelFunc));
82 m_Properties.Add(
83 "RunSynchronously",
85 "Task.RunSynchronously",
86 () => new BadEnumerableInteropFunction("RunSynchronously", (_, _) => Enumerate(runnable), false, BadAnyPrototype.Instance)
87 )
88 );
89 m_Properties.Add(
90 "Run",
92 "Task.Run",
94 )
95 );
96 }
97
98
102 public BadRunnable Runnable { get; }
103
107 public IEnumerable<BadTask> ContinuationTasks => m_ContinuationTasks;
108
112 public BadTask? Creator { get; private set; }
113
117 public string Name { get; set; }
118
122 public bool IsInactive => !IsRunning && !IsFinished && !IsPaused;
123
127 public bool IsRunning { get; private set; }
128
132 public bool IsFinished { get; private set; }
133
137 public bool IsPaused { get; private set; }
138
139 private void Run(BadExecutionContext context)
140 {
141 if (IsFinished)
142 {
143 throw BadRuntimeException.Create(context.Scope, "Task is already finished");
144 }
145
146 if (IsRunning)
147 {
148 throw BadRuntimeException.Create(context.Scope, "Task is already running");
149 }
150
151 BadTaskRunner runner = context.Scope.GetSingleton<BadTaskRunner>() ?? throw BadRuntimeException.Create(context.Scope, "Task Runner not found");
152 if (IsPaused)
153 {
154 IsPaused = false;
155 }
156
157 if (!runner.IsTaskAdded(this))
158 {
159 runner.AddTask(this, true);
160 }
161 }
162
163 private IEnumerable<BadObject> Enumerate(BadRunnable runnable)
164 {
165 IEnumerator<BadObject> e = runnable.Enumerator;
166 while (e.MoveNext())
167 {
168 yield return e.Current ?? Null;
169 }
170 }
171
176 public void AddContinuation(BadTask task)
177 {
178 m_ContinuationTasks.Add(task);
179 }
180
185 public void SetCreator(BadTask? task)
186 {
187 Creator = task;
188 }
189
193 public void Start()
194 {
195 IsRunning = true;
196 }
197
201 public void Stop()
202 {
203 IsFinished = true;
204 IsRunning = false;
205 IsPaused = false;
206 }
207
212 public void Cancel()
213 {
214 if (!IsRunning)
215 {
216 throw new BadRuntimeException("Task is not running");
217 }
218
219 IsPaused = false;
220 IsFinished = true;
221 }
222
223
228 public void Resume()
229 {
230 if (!IsRunning)
231 {
232 throw new BadRuntimeException("Task is not running");
233 }
234
235 if (!IsPaused)
236 {
237 throw new BadRuntimeException("Task is not paused");
238 }
239
240 IsPaused = false;
241 }
242
243
248 public void Pause()
249 {
250 if (!IsRunning)
251 {
252 throw new BadRuntimeException("Task is not running");
253 }
254
255 if (IsPaused)
256 {
257 throw new BadRuntimeException("Task is already paused");
258 }
259
260 IsPaused = true;
261 }
262
263
271 {
272 if (IsFinished)
273 {
274 throw new BadRuntimeException("Task is already finished");
275 }
276
277 AddContinuation(task);
278
279 return Null;
280 }
281
290 public static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
291 {
292 return new BadTask(BadRunnable.Create(f, caller, args), name ?? f.ToString());
293 }
294
296 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
297 {
298 return m_Properties.TryGetValue(propName, out BadObjectReference? property) ? property : base.GetProperty(propName, caller);
299 }
300
302 public override string ToSafeString(List<BadObject> done)
303 {
304 StringBuilder sb = new StringBuilder();
305 sb.AppendLine("{");
306
307 foreach (KeyValuePair<string, BadObjectReference> props in m_Properties)
308 {
309 sb.AppendLine($"\t{props.Key}: {props.Value.Dereference()}");
310 }
311
312 sb.AppendLine("}");
313
314 return sb.ToString();
315 }
316
319 {
320 return Prototype;
321 }
322
324 public override bool HasProperty(string propName, BadScope? caller = null)
325 {
326 return m_Properties.ContainsKey(propName) ||
327 base.HasProperty(propName, caller);
328 }
329}
Implements a Runnable Object.
static BadRunnable Create(IEnumerable< BadObject > e)
Creates a Runnable from an Enumeration.
IEnumerator< BadObject > Enumerator
The Enumerator of the Runnable.
Implements a Task Object.
Definition BadTask.cs:17
void Resume()
Resumes the Task.
Definition BadTask.cs:228
void SetCreator(BadTask? task)
Sets the Creator of this Task.
Definition BadTask.cs:185
BadTask(BadRunnable runnable, string name)
Constructs a new Task.
Definition BadTask.cs:60
void AddContinuation(BadTask task)
Adds a Continuation Task.
Definition BadTask.cs:176
bool IsPaused
Is true if the task is running.
Definition BadTask.cs:137
static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
Creates a new Task from a Function.
Definition BadTask.cs:290
override bool HasProperty(string propName, BadScope? caller=null)
Returns true if the object contains a given property or there exists an extension for the current Ins...
Definition BadTask.cs:324
void Start()
Starts the Task.
Definition BadTask.cs:193
override string ToSafeString(List< BadObject > done)
Definition BadTask.cs:302
IEnumerable< BadObject > Enumerate(BadRunnable runnable)
Definition BadTask.cs:163
bool IsInactive
Is True if the Task is not running, finished or paused.
Definition BadTask.cs:122
void Run(BadExecutionContext context)
Definition BadTask.cs:139
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadTask.cs:296
void Pause()
Pauses the Task.
Definition BadTask.cs:248
static readonly BadClassPrototype Prototype
The BadTask Prototype.
Definition BadTask.cs:21
BadTask? Creator
The Creator of this Task.
Definition BadTask.cs:112
BadObject ContinueWith(BadTask task)
Continues the Task with another Task.
Definition BadTask.cs:270
bool IsFinished
Is true if the task is finished.
Definition BadTask.cs:132
IEnumerable< BadTask > ContinuationTasks
Enumeration of Continuation Tasks.
Definition BadTask.cs:107
void Cancel()
Cancels the Task.
Definition BadTask.cs:212
readonly Dictionary< string, BadObjectReference > m_Properties
The Task Properties.
Definition BadTask.cs:52
readonly List< BadTask > m_ContinuationTasks
The List of Continuation Tasks.
Definition BadTask.cs:47
string Name
The Name of the Task.
Definition BadTask.cs:117
bool IsRunning
Is true if the task is running.
Definition BadTask.cs:127
override BadClassPrototype GetPrototype()
Definition BadTask.cs:318
BadRunnable Runnable
Runnable of the Task.
Definition BadTask.cs:102
void AddTask(BadTask task, bool runImmediately=false)
Adds a Task to the Task Runner.
bool IsTaskAdded(BadTask task)
Checks if a Task is added to the Task Runner.
The Execution Context. Every execution of a script needs a context the script is running in....
BadScope Scope
The Root Scope of the Context.
Implements the Scope for the Script Engine.
Definition BadScope.cs:238
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
override string ToString()
Returns a String Representation of this Object.
Definition BadObject.cs:210
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:129
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Implements the base functionality for a BadScript Reference.
static BadObjectReference Make(string refText, Func< BadObject > getter, Action< BadObject, BadPropertyInfo?>? setter=null, Action? delete=null)
Creates a new Reference Object.
Implements a function that can be called from the script.
The Any Prototype, Base type for all types.
static readonly BadAnyPrototype Instance
The Instance of the BadAnyPrototype.
Implements a Class Prototype for the BadScript Language.
Implements the Interface for Native Strings.
Definition IBadString.cs:7
new string Value
The String Value.
Definition IBadString.cs:11
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Interop Function Classes for the BadScript2 Language.
Contains Runtime Function Objects.
Contains the Native Runtime Objects.
Definition BadBoolean.cs:6
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.