EvaluationContext.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Runtime.CompilerServices;
  2. using Esprima.Ast;
  3. namespace Jint.Runtime.Interpreter;
  4. /// <summary>
  5. /// Per Engine.Evaluate() call context.
  6. /// </summary>
  7. internal sealed class EvaluationContext
  8. {
  9. private readonly bool _shouldRunBeforeExecuteStatementChecks;
  10. public EvaluationContext(Engine engine, in Completion? resumedCompletion = null)
  11. {
  12. Engine = engine;
  13. ResumedCompletion = resumedCompletion ?? default; // TODO later
  14. OperatorOverloadingAllowed = engine.Options.Interop.AllowOperatorOverloading;
  15. _shouldRunBeforeExecuteStatementChecks = engine._constraints.Length > 0 || engine._isDebugMode;
  16. }
  17. // for fast evaluation checks only
  18. public EvaluationContext()
  19. {
  20. Engine = null!;
  21. ResumedCompletion = default; // TODO later
  22. OperatorOverloadingAllowed = false;
  23. _shouldRunBeforeExecuteStatementChecks = false;
  24. }
  25. public readonly Engine Engine;
  26. public readonly Completion ResumedCompletion;
  27. public bool DebugMode => Engine._isDebugMode;
  28. public SyntaxElement LastSyntaxElement = null!;
  29. public readonly bool OperatorOverloadingAllowed;
  30. // completion record information
  31. public string? Target;
  32. public CompletionType Completion;
  33. public void RunBeforeExecuteStatementChecks(Statement statement)
  34. {
  35. if (_shouldRunBeforeExecuteStatementChecks)
  36. {
  37. Engine.RunBeforeExecuteStatementChecks(statement);
  38. }
  39. }
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public void PrepareFor(Node node)
  42. {
  43. LastSyntaxElement = node;
  44. Target = null;
  45. Completion = CompletionType.Normal;
  46. }
  47. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  48. public bool IsAbrupt() => Completion != CompletionType.Normal;
  49. }