EvaluationContext.cs 1.8 KB

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