JintBlockStatement.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. Environment? oldEnv = null;
  38. var engine = context.Engine;
  39. if (_lexicalDeclarations.Declarations.Count > 0)
  40. {
  41. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  42. var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment);
  43. JintStatementList.BlockDeclarationInstantiation(blockEnv, _lexicalDeclarations);
  44. engine.UpdateLexicalEnvironment(blockEnv);
  45. }
  46. Completion blockValue;
  47. if (_singleStatement is not null)
  48. {
  49. blockValue = ExecuteSingle(context);
  50. }
  51. else
  52. {
  53. blockValue = _statementList!.Execute(context);
  54. }
  55. if (oldEnv is not null)
  56. {
  57. engine.UpdateLexicalEnvironment(oldEnv);
  58. }
  59. return blockValue;
  60. }
  61. private Completion ExecuteSingle(EvaluationContext context)
  62. {
  63. Completion blockValue;
  64. try
  65. {
  66. blockValue = _singleStatement!.Execute(context);
  67. if (context.Engine._error is not null)
  68. {
  69. blockValue = JintStatementList.HandleError(context.Engine, _singleStatement);
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. if (ex is JintException)
  75. {
  76. blockValue = JintStatementList.HandleException(context, ex, _singleStatement);
  77. }
  78. else
  79. {
  80. throw;
  81. }
  82. }
  83. return blockValue;
  84. }
  85. protected override Completion ExecuteInternal(EvaluationContext context)
  86. {
  87. return ExecuteBlock(context);
  88. }
  89. }