BadScript 2
Loading...
Searching...
No Matches
BadAwaitExpression.cs
Go to the documentation of this file.
6
11
16{
20 private readonly BadExpression m_TaskExpr;
21
27 public BadAwaitExpression(BadExpression expr, BadSourcePosition position) : base(false, position)
28 {
29 m_TaskExpr = expr;
30 }
31
33 public override IEnumerable<BadExpression> GetDescendants()
34 {
36 }
37
39 protected override IEnumerable<BadObject> InnerExecute(BadExecutionContext context)
40 {
42
43 foreach (BadObject o in m_TaskExpr.Execute(context))
44 {
45 obj = o;
46
47 yield return o;
48 }
49
50 obj = obj.Dereference();
51
52 if (obj is not BadTask task)
53 {
54 throw new BadRuntimeException("await can only be used on a task", Position);
55 }
56
57 if (task.IsFinished)
58 {
59 yield return task.Runnable.GetReturn();
60
61 yield break;
62 }
63
64 BadTaskRunner runner = context.Scope.GetSingleton<BadTaskRunner>() ??
65 throw BadRuntimeException.Create(context.Scope, "Task Runner not found", Position);
66
67 //Run Task
68 //Add current to continuation
69 task.AddContinuation(runner.Current ?? throw new BadRuntimeException("Current task is null", Position));
70 runner.Current?.Pause();
71
72 if (task.IsInactive)
73 {
74 runner.AddTask(task, true);
75 }
76
77 yield return BadObject.Null; //Should pause Here
78
79 if (task.Runnable.Error != null)
80 {
81 throw new BadRuntimeErrorException(task.Runnable.Error);
82 }
83 else
84 {
85 yield return task.Runnable.GetReturn();
86 }
87 }
88}
Describes a specific position inside a source file.
override IEnumerable< BadExpression > GetDescendants()
override IEnumerable< BadObject > InnerExecute(BadExecutionContext context)
readonly BadExpression m_TaskExpr
The Task Expression.
BadAwaitExpression(BadExpression expr, BadSourcePosition position)
Constructs a new Await Expression.
Implements a Task Object.
Definition BadTask.cs:17
void Pause()
Pauses the Task.
Definition BadTask.cs:248
void AddTask(BadTask task, bool runImmediately=false)
Adds a Task to the Task Runner.
Base Implementation for all Expressions used inside the Script.
IEnumerable< BadExpression > GetDescendantsAndSelf()
Returns all Descendants of the Expression and the Expression itself.
BadSourcePosition Position
The source Position of the Expression.
IEnumerable< BadObject > Execute(BadExecutionContext context)
Evaluates the Expression within the current Execution Context.
The Execution Context. Every execution of a script needs a context the script is running in....
Gets thrown if the runtime encounters an error.
static BadRuntimeException Create(BadScope? scope, string message)
Creates a new BadScriptException.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Contains Shared Data Structures and Functionality.
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the Expressions for the BadScript2 Language.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains the Runtime Implementation.