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 );
42
46 private readonly List<BadTask> m_ContinuationTasks = new List<BadTask>();
47
51 private readonly Dictionary<string, BadObjectReference> m_Properties =
52 new Dictionary<string, BadObjectReference>();
53
59 public BadTask(BadRunnable runnable, string name)
60 {
61 Name = name;
62 Runnable = runnable;
63
64 m_Properties.Add("Name",
65 BadObjectReference.Make("Task.Name",
66 (p) => Name,
67 (o, _, _) => Name = o is IBadString s ? s.Value : Name
68 )
69 );
70 m_Properties.Add("IsCompleted", BadObjectReference.Make("Task.IsCompleted", (p) => IsFinished));
71 m_Properties.Add("IsInactive", BadObjectReference.Make("Task.IsInactive", (p) => IsInactive));
72 m_Properties.Add("IsPaused", BadObjectReference.Make("Task.IsPaused", (p) => IsPaused));
73 m_Properties.Add("IsRunning", BadObjectReference.Make("Task.IsRunning", (p) => IsRunning));
74
75 BadFunction continueFunc =
80 m_Properties.Add("ContinueWith", BadObjectReference.Make("Task.ContinueWith", (p) => continueFunc));
81 m_Properties.Add("Pause", BadObjectReference.Make("Task.Pause", (p) => pauseFunc));
82 m_Properties.Add("Resume", BadObjectReference.Make("Task.Resume", (p) => resumeFunc));
83 m_Properties.Add("Cancel", BadObjectReference.Make("Task.Cancel", (p) => cancelFunc));
84
85 m_Properties.Add("RunSynchronously",
86 BadObjectReference.Make("Task.RunSynchronously",
87 (p) => new BadEnumerableInteropFunction("RunSynchronously",
88 (_, _) => Enumerate(runnable),
89 false,
91 )
92 )
93 );
94
95 m_Properties.Add("Run",
96 BadObjectReference.Make("Task.Run",
97 (p) => new BadDynamicInteropFunction("Run",
98 Run,
100 )
101 )
102 );
103 }
104
105
109 public BadRunnable Runnable { get; }
110
114 public IEnumerable<BadTask> ContinuationTasks => m_ContinuationTasks;
115
119 public BadTask? Creator { get; private set; }
120
124 public string Name { get; set; }
125
129 public bool IsInactive => !IsRunning && !IsFinished && !IsPaused;
130
134 public bool IsRunning { get; private set; }
135
139 public bool IsFinished { get; private set; }
140
144 public bool IsPaused { get; private set; }
145
146 private void Run(BadExecutionContext context)
147 {
148 if (IsFinished)
149 {
150 throw BadRuntimeException.Create(context.Scope, "Task is already finished");
151 }
152
153 if (IsRunning)
154 {
155 throw BadRuntimeException.Create(context.Scope, "Task is already running");
156 }
157
158 BadTaskRunner runner = context.Scope.GetSingleton<BadTaskRunner>() ??
159 throw BadRuntimeException.Create(context.Scope, "Task Runner not found");
160
161 if (IsPaused)
162 {
163 IsPaused = false;
164 }
165
166 if (!runner.IsTaskAdded(this))
167 {
168 runner.AddTask(this, true);
169 }
170 }
171
172 private IEnumerable<BadObject> Enumerate(BadRunnable runnable)
173 {
174 IEnumerator<BadObject> e = runnable.Enumerator;
175
176 while (e.MoveNext())
177 {
178 yield return e.Current ?? Null;
179 }
180 }
181
186 public void AddContinuation(BadTask task)
187 {
188 m_ContinuationTasks.Add(task);
189 }
190
195 public void SetCreator(BadTask? task)
196 {
197 Creator = task;
198 }
199
203 public void Start()
204 {
205 IsRunning = true;
206 }
207
211 public void Stop()
212 {
213 IsFinished = true;
214 IsRunning = false;
215 IsPaused = false;
216 }
217
222 public void Cancel()
223 {
224 if (!IsRunning)
225 {
226 throw new BadRuntimeException("Task is not running");
227 }
228
229 IsPaused = false;
230 IsFinished = true;
231 }
232
233
238 public void Resume()
239 {
240 if (!IsRunning)
241 {
242 throw new BadRuntimeException("Task is not running");
243 }
244
245 if (!IsPaused)
246 {
247 throw new BadRuntimeException("Task is not paused");
248 }
249
250 IsPaused = false;
251 }
252
253
258 public void Pause()
259 {
260 if (!IsRunning)
261 {
262 throw new BadRuntimeException("Task is not running");
263 }
264
265 if (IsPaused)
266 {
267 throw new BadRuntimeException("Task is already paused");
268 }
269
270 IsPaused = true;
271 }
272
273
281 {
282 if (IsFinished)
283 {
284 throw new BadRuntimeException("Task is already finished");
285 }
286
287 AddContinuation(task);
288
289 return Null;
290 }
291
300 public static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
301 {
302 return new BadTask(BadRunnable.Create(f, caller, args), name ?? f.ToString());
303 }
304
306 public override BadObjectReference GetProperty(string propName, BadScope? caller = null)
307 {
308 return m_Properties.TryGetValue(propName, out BadObjectReference? property)
309 ? property
310 : base.GetProperty(propName, caller);
311 }
312
314 public override string ToSafeString(List<BadObject> done)
315 {
316 StringBuilder sb = new StringBuilder();
317 sb.AppendLine("{");
318
319 foreach (KeyValuePair<string, BadObjectReference> props in m_Properties)
320 {
321 sb.AppendLine($"\t{props.Key}: {props.Value.Dereference(null)}");
322 }
323
324 sb.AppendLine("}");
325
326 return sb.ToString();
327 }
328
331 {
332 return Prototype;
333 }
334
336 public override bool HasProperty(string propName, BadScope? caller = null)
337 {
338 return m_Properties.ContainsKey(propName) ||
339 base.HasProperty(propName, caller);
340 }
341}
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:238
void SetCreator(BadTask? task)
Sets the Creator of this Task.
Definition BadTask.cs:195
BadTask(BadRunnable runnable, string name)
Constructs a new Task.
Definition BadTask.cs:59
void AddContinuation(BadTask task)
Adds a Continuation Task.
Definition BadTask.cs:186
bool IsPaused
Is true if the task is running.
Definition BadTask.cs:144
static BadTask Create(BadFunction f, BadExecutionContext caller, string? name, params BadObject[] args)
Creates a new Task from a Function.
Definition BadTask.cs:300
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:336
void Start()
Starts the Task.
Definition BadTask.cs:203
override string ToSafeString(List< BadObject > done)
Definition BadTask.cs:314
IEnumerable< BadObject > Enumerate(BadRunnable runnable)
Definition BadTask.cs:172
bool IsInactive
Is True if the Task is not running, finished or paused.
Definition BadTask.cs:129
void Run(BadExecutionContext context)
Definition BadTask.cs:146
override BadObjectReference GetProperty(string propName, BadScope? caller=null)
Returns a Reference to the Property with the given Name.The Property Reference
Definition BadTask.cs:306
void Pause()
Pauses the Task.
Definition BadTask.cs:258
static readonly BadClassPrototype Prototype
The BadTask Prototype.
Definition BadTask.cs:21
BadTask? Creator
The Creator of this Task.
Definition BadTask.cs:119
BadObject ContinueWith(BadTask task)
Continues the Task with another Task.
Definition BadTask.cs:280
bool IsFinished
Is true if the task is finished.
Definition BadTask.cs:139
IEnumerable< BadTask > ContinuationTasks
Enumeration of Continuation Tasks.
Definition BadTask.cs:114
void Cancel()
Cancels the Task.
Definition BadTask.cs:222
readonly Dictionary< string, BadObjectReference > m_Properties
The Task Properties.
Definition BadTask.cs:51
readonly List< BadTask > m_ContinuationTasks
The List of Continuation Tasks.
Definition BadTask.cs:46
string Name
The Name of the Task.
Definition BadTask.cs:124
bool IsRunning
Is true if the task is running.
Definition BadTask.cs:134
override BadClassPrototype GetPrototype()
Definition BadTask.cs:330
BadRunnable Runnable
Runnable of the Task.
Definition BadTask.cs:109
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.