EvaluationContext.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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)
  11. {
  12. Engine = engine;
  13. OperatorOverloadingAllowed = engine.Options.Interop.AllowOperatorOverloading;
  14. _shouldRunBeforeExecuteStatementChecks = engine._constraints.Length > 0 || engine._isDebugMode;
  15. }
  16. // for fast evaluation checks only
  17. public EvaluationContext()
  18. {
  19. Engine = null!;
  20. OperatorOverloadingAllowed = false;
  21. _shouldRunBeforeExecuteStatementChecks = false;
  22. }
  23. public readonly Engine Engine;
  24. public bool DebugMode => Engine._isDebugMode;
  25. public SyntaxElement LastSyntaxElement
  26. {
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. get => Engine.GetLastSyntaxElement();
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. set => Engine._lastSyntaxElement = value;
  31. }
  32. public readonly bool OperatorOverloadingAllowed;
  33. // completion record information
  34. public string? Target;
  35. public CompletionType Completion;
  36. public void RunBeforeExecuteStatementChecks(StatementListItem statement)
  37. {
  38. if (_shouldRunBeforeExecuteStatementChecks)
  39. {
  40. Engine.RunBeforeExecuteStatementChecks(statement);
  41. }
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. public void PrepareFor(Node node)
  45. {
  46. LastSyntaxElement = node;
  47. Target = null;
  48. Completion = CompletionType.Normal;
  49. }
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public bool IsAbrupt() => Completion != CompletionType.Normal;
  52. }