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 (ctx, args) =>
23 {
24 if (args.Length != 2)
25 {
26 throw new BadRuntimeException("Task constructor takes 2 arguments");
27 }
28
29 if (args[0] is not IBadString name)
30 {
31 throw new BadRuntimeException("Task constructor takes a string as first argument");
32 }
33
34 if (args[1] is not BadFunction f)
35 {
36 throw new BadRuntimeException("Task constructor takes a function as second argument");
37 }
38
39 return new BadTask(BadRunnable.Create(f, ctx), name.Value);
40 },
41 null
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
65 m_Properties.Add("Name",
66 BadObjectReference.Make("Task.Name",
67 (p) => Name,
68 (o, _, _) => Name = o is IBadString s ? s.Value : Name
69 )
70 );
71 m_Properties.Add("IsCompleted", BadObjectReference.Make("Task.IsCompleted", (p) => IsFinished));
72 m_Properties.Add("IsInactive", BadObjectReference.Make("Task.IsInactive", (p) => IsInactive));
73 m_Properties.Add("IsPaused", BadObjectReference.Make("Task.IsPaused", (p) => IsPaused));
74 m_Properties.Add("IsRunning", BadObjectReference.Make("Task.IsRunning", (p) => IsRunning));
75
76 BadFunction continueFunc =
77 new BadDynamicInteropFunction<BadTask>("ContinueWith", (_, t) => ContinueWith(t), BadAnyPrototype.Instance, new BadFunctionParameter("task", false, true, false, null, Prototype));
81 m_Properties.Add("ContinueWith", BadObjectReference.Make("Task.ContinueWith", (p) => continueFunc));
82 m_Properties.Add("Pause", BadObjectReference.Make("Task.Pause", (p) => pauseFunc));
83 m_Properties.Add("Resume", BadObjectReference.Make("Task.Resume", (p) => resumeFunc));
84 m_Properties.Add("Cancel", BadObjectReference.Make("Task.Cancel", (p) => cancelFunc));
85
86 m_Properties.Add("RunSynchronously",
87 BadObjectReference.Make("Task.RunSynchronously",
88 (p) => new BadEnumerableInteropFunction("RunSynchronously",
89 (_, _) => Enumerate(runnable),
90 false,
92 )
93 )
94 );
95
96 m_Properties.Add("Run",
97 BadObjectReference.Make("Task.Run",
98 (p) => new BadDynamicInteropFunction("Run",
99 Run,
101 )
102 )
103 );
104 }
105
106
110 public BadRunnable Runnable { get; }
111
115 public IEnumerable<BadTask> ContinuationTasks => m_ContinuationTasks;
116
120 public BadTask? Creator { get; private set; }
121
125 public string Name { get; set; }
126
130 public bool IsInactive => !IsRunning && !IsFinished && !IsPaused;
131
135 public bool IsRunning { get; private set; }
136
140 public bool IsFinished { get; private set; }
141
145 public bool IsPaused { get; private set; }
146
147 private void Run(BadExecutionContext context)
148 {
149 if (IsFinished)
150 {
151 throw BadRuntimeException.Create(context.Scope, "Task is already finished");
152 }
153
154 if (IsRunning)
155 {
156 throw BadRuntimeException.Create(context.Scope, "Task is already running");
157 }
158
159 BadTaskRunner runner = context.Scope.GetSingleton<BadTaskRunner>() ??
160 throw BadRuntimeException.Create(context.Scope, "Task Runner not found");
161
162 if (IsPaused)
163 {
164 IsPaused = false;
165 }
166
167 if (!runner.IsTaskAdded(this))
168 {
169 runner.AddTask(this, true);
170 }
171 }
172
173 private IEnumerable<BadObject> Enumerate(BadRunnable runnable)
174 {
175 IEnumerator<BadObject> e = runnable.Enumerator;
176
177 while (e.MoveNext())
178 {
179 yield return e.Current ?? Null;
180 }
181 }
182
187 public void AddContinuation(BadTask task)
188 {
189 m_ContinuationTasks.Add(task);
190 }
191
196 public void SetCreator(BadTask? task)
197 {
198 Creator = task;
199 }
200
204 public void Start()
205 {
206 IsRunning = true;
207 }
208
212 public void Stop()
213 {
214 IsFinished = true;
215 IsRunning = false;
216 IsPaused = false;
217 }
218
223 public void Cancel()
224 {
225 if (!IsRunning)
226 {
227 throw new BadRuntimeException("Task is not running");
228 }
229
230 IsPaused = false;
231 IsFinished = true;
232 }
233
234
239 public void Resume()
240 {
241 if (!IsRunning)
242 {
243 throw new BadRuntimeException("Task is not running");
244 }
245
246 if (!IsPaused)
247 {
248 throw new BadRuntimeException("Task is not paused");
249 }
250
251 IsPaused = false;
252 }
253
254
259 public void Pause()
260 {
261 if (!IsRunning)
262 {
263 throw new BadRuntimeException("Task is not running");
264 }
265
266 if (IsPaused)
267 {
268 throw new BadRuntimeException("Task is already paused");
269 }
270
271 IsPaused = true;
272 }
273
274
282 {
283 if (IsFinished)
284 {
285 throw new BadRuntimeException("Task is already finished");
286 }
287
288 AddContinuation(task);
289
290 return Null;
291 }
292
301 public static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
302 {
303 return new BadTask(BadRunnable.Create(f, caller, args), name ?? f.ToString());
304 }
305
307 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
308 {
309 return m_Properties.TryGetValue(propName, out BadObjectReference? property)
310 ? property
311 : base.GetProperty(propName, caller);
312 }
313
315 public override string ToSafeString(List<BadObject> done)
316 {
317 StringBuilder sb = new StringBuilder();
318 sb.AppendLine("{");
319
320 foreach (KeyValuePair<string, BadObjectReference> props in m_Properties)
321 {
322 sb.AppendLine($"\t{props.Key}: {props.Value.Dereference(null)}");
323 }
324
325 sb.AppendLine("}");
326
327 return sb.ToString();
328 }
329
332 {
333 return Prototype;
334 }
335
337 public override bool HasProperty(string propName, BadScope? caller = null)
338 {
339 return m_Properties.ContainsKey(propName) ||
340 base.HasProperty(propName, caller);
341 }
342}
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:239
void SetCreator(BadTask? task)
Sets the Creator of this Task.
Definition BadTask.cs:196
BadTask(BadRunnable runnable, string name)
Constructs a new Task.
Definition BadTask.cs:60
void AddContinuation(BadTask task)
Adds a Continuation Task.
Definition BadTask.cs:187
bool IsPaused
Is true if the task is running.
Definition BadTask.cs:145
static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
Creates a new Task from a Function.
Definition BadTask.cs:301
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:337
void Start()
Starts the Task.
Definition BadTask.cs:204
override string ToSafeString(List< BadObject > done)
Definition BadTask.cs:315
IEnumerable< BadObject > Enumerate(BadRunnable runnable)
Definition BadTask.cs:173
bool IsInactive
Is True if the Task is not running, finished or paused.
Definition BadTask.cs:130
void Run(BadExecutionContext context)
Definition BadTask.cs:147
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadTask.cs:307
void Pause()
Pauses the Task.
Definition BadTask.cs:259
static readonly BadClassPrototype Prototype
The BadTask Prototype.
Definition BadTask.cs:21
BadTask? Creator
The Creator of this Task.
Definition BadTask.cs:120
BadObject ContinueWith(BadTask task)
Continues the Task with another Task.
Definition BadTask.cs:281
bool IsFinished
Is true if the task is finished.
Definition BadTask.cs:140
IEnumerable< BadTask > ContinuationTasks
Enumeration of Continuation Tasks.
Definition BadTask.cs:115
void Cancel()
Cancels the Task.
Definition BadTask.cs:223
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:125
bool IsRunning
Is true if the task is running.
Definition BadTask.cs:135
override BadClassPrototype GetPrototype()
Definition BadTask.cs:331
BadRunnable Runnable
Runnable of the Task.
Definition BadTask.cs:110
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:16
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:224
virtual BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.
Definition BadObject.cs:141
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< BadSourcePosition?, BadObject > getter, Action< BadObject, BadSourcePosition?, BadPropertyInfo?>? setter=null, Action< BadSourcePosition?>? 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.