JintBlockStatement.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Jint.Runtime.Environments;
  2. using Environment = Jint.Runtime.Environments.Environment;
  3. namespace Jint.Runtime.Interpreter.Statements;
  4. internal sealed class JintBlockStatement : JintStatement<NestedBlockStatement>
  5. {
  6. private JintStatementList? _statementList;
  7. private JintStatement? _singleStatement;
  8. private DeclarationCache _lexicalDeclarations;
  9. public JintBlockStatement(NestedBlockStatement blockStatement) : base(blockStatement)
  10. {
  11. }
  12. protected override void Initialize(EvaluationContext context)
  13. {
  14. _lexicalDeclarations = (DeclarationCache) (_statement.UserData ??= BuildState(_statement));
  15. if (_statement.Body.Count == 1)
  16. {
  17. _singleStatement = Build(_statement.Body[0]);
  18. }
  19. else
  20. {
  21. _statementList = new JintStatementList(_statement, _statement.Body);
  22. }
  23. }
  24. internal static DeclarationCache BuildState(BlockStatement blockStatement)
  25. {
  26. return DeclarationCacheBuilder.Build(blockStatement);
  27. }
  28. /// <summary>
  29. /// Optimized for direct access without virtual dispatch.
  30. /// </summary>
  31. public Completion ExecuteBlock(EvaluationContext context)
  32. {
  33. if (_statementList is null && _singleStatement is null)
  34. {
  35. Initialize(context);
  36. }
  37. DeclarativeEnvironment? blockEnv = null;
  38. Environment? oldEnv = null;
  39. var engine = context.Engine;
  40. if (_lexicalDeclarations.Declarations.Count > 0)
  41. {
  42. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  43. blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment);
  44. JintStatementList.BlockDeclarationInstantiation(blockEnv, _lexicalDeclarations);
  45. engine.UpdateLexicalEnvironment(blockEnv);
  46. }
  47. Completion blockValue;
  48. if (_singleStatement is not null)
  49. {
  50. blockValue = ExecuteSingle(context);
  51. }
  52. else
  53. {
  54. blockValue = _statementList!.Execute(context);
  55. }
  56. if (blockEnv != null)
  57. {
  58. blockValue = blockEnv.DisposeResources(blockValue);
  59. }
  60. if (oldEnv is not null)
  61. {
  62. engine.UpdateLexicalEnvironment(oldEnv);
  63. }
  64. return blockValue;
  65. }
  66. private Completion ExecuteSingle(EvaluationContext context)
  67. {
  68. Completion blockValue;
  69. try
  70. {
  71. blockValue = _singleStatement!.Execute(context);
  72. if (context.Engine._error is not null)
  73. {
  74. blockValue = JintStatementList.HandleError(context.Engine, _singleStatement);
  75. }
  76. }
  77. catch (Exception ex)
  78. {
  79. if (ex is JintException)
  80. {
  81. blockValue = JintStatementList.HandleException(context, ex, _singleStatement);
  82. }
  83. else
  84. {
  85. throw;
  86. }
  87. }
  88. return blockValue;
  89. }
  90. protected override Completion ExecuteInternal(EvaluationContext context)
  91. {
  92. return ExecuteBlock(context);
  93. }
  94. }