ProbablyBlockStatement.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Runtime.CompilerServices;
  2. using System.Runtime.InteropServices;
  3. using Esprima.Ast;
  4. namespace Jint.Runtime.Interpreter.Statements;
  5. /// <summary>
  6. /// Helper to remove virtual dispatch from block statements when it's most common target.
  7. /// This is especially true for things like for statements body
  8. /// </summary>
  9. [StructLayout(LayoutKind.Auto)]
  10. internal readonly struct ProbablyBlockStatement
  11. {
  12. private readonly JintStatement? _statement = null;
  13. private readonly JintBlockStatement? _blockStatement = null;
  14. public ProbablyBlockStatement(Statement statement)
  15. {
  16. if (statement is BlockStatement blockStatement)
  17. {
  18. _blockStatement = new JintBlockStatement(blockStatement);
  19. }
  20. else
  21. {
  22. _statement = JintStatement.Build(statement);
  23. }
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public Completion Execute(EvaluationContext context)
  27. {
  28. if (_blockStatement is not null)
  29. {
  30. return _blockStatement.ExecuteBlock(context);
  31. }
  32. return _statement!.Execute(context);
  33. }
  34. }