ProbablyBlockStatement.cs 1.1 KB

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