EvaluationContext.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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
  29. {
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. get => Engine.GetLastSyntaxElement();
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. set => Engine._lastSyntaxElement = value;
  34. }
  35. public readonly bool OperatorOverloadingAllowed;
  36. // completion record information
  37. public string? Target;
  38. public CompletionType Completion;
  39. public void RunBeforeExecuteStatementChecks(StatementListItem statement)
  40. {
  41. if (_shouldRunBeforeExecuteStatementChecks)
  42. {
  43. Engine.RunBeforeExecuteStatementChecks(statement);
  44. }
  45. }
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public void PrepareFor(Node node)
  48. {
  49. LastSyntaxElement = node;
  50. Target = null;
  51. Completion = CompletionType.Normal;
  52. }
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public bool IsAbrupt() => Completion != CompletionType.Normal;
  55. }