Skip to content

Commit

Permalink
Make ScriptExecutionContext a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumDev committed Sep 24, 2023
1 parent 274b0e0 commit bbe9fec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/WattleScript.Interpreter/Execution/DynamicExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ internal DynamicExpression(Script S, string strExpr, DynValue constant)
/// </summary>
/// <param name="context">The context.</param>
/// <returns></returns>
public DynValue Evaluate(ScriptExecutionContext context = null)
public DynValue Evaluate(ScriptExecutionContext? context = null)
{
context = context ?? OwnerScript.CreateDynamicExecutionContext();
var ctx = context ?? OwnerScript.CreateDynamicExecutionContext();

this.CheckScriptOwnership(context.GetScript());
this.CheckScriptOwnership(ctx.GetScript());

if (!m_Constant.IsNil())
return m_Constant;

return m_Exp.Eval(context);
return m_Exp.Eval(ctx);
}

/// <summary>
Expand Down
37 changes: 22 additions & 15 deletions src/WattleScript.Interpreter/Execution/ScriptExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,47 @@ namespace WattleScript.Interpreter
/// <summary>
/// Class giving access to details of the environment where the script is executing
/// </summary>
public class ScriptExecutionContext : IScriptPrivateResource
public struct ScriptExecutionContext : IScriptPrivateResource
{
Processor m_Processor;
CallbackFunction m_Callback;

internal bool CanAwait { get; set; }
private SourceRef m_CallingLocation;
private int m_Flags;

private const int FLAG_ISDYNAMIC = (1 << 0);
private const int FLAG_CANAWAIT = (1 << 1);

internal bool CanAwait
{
get => (m_Flags & FLAG_CANAWAIT) != 0;
set
{
if (value)
m_Flags |= FLAG_CANAWAIT;
else
m_Flags &= ~FLAG_CANAWAIT;
}
}

internal ScriptExecutionContext(Processor p, CallbackFunction callBackFunction, SourceRef sourceRef, bool isDynamic = false)
{
IsDynamicExecution = isDynamic;
m_Flags = isDynamic ? FLAG_ISDYNAMIC : 0;
m_Processor = p;
m_Callback = callBackFunction;
CallingLocation = sourceRef;
m_CallingLocation = sourceRef;
}

/// <summary>
/// Gets a value indicating whether this instance is running a dynamic execution.
/// Under a dynamic execution, most methods of ScriptExecutionContext are not reliable as the
/// processing engine of the script is not "really" running or is not available.
/// </summary>
public bool IsDynamicExecution
{
get;
private set;
}
public bool IsDynamicExecution => (m_Flags & FLAG_ISDYNAMIC) != 0;

/// <summary>
/// Gets the location of the code calling back
/// </summary>
public SourceRef CallingLocation
{
get;
private set;
}
public SourceRef CallingLocation => m_CallingLocation;

/// <summary>
/// Gets or sets the additional data associated to this CLR function call.
Expand Down

0 comments on commit bbe9fec

Please sign in to comment.