2
0

JintBlockStatement.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Esprima.Ast;
  2. using Jint.Runtime.Environments;
  3. namespace Jint.Runtime.Interpreter.Statements
  4. {
  5. internal sealed class JintBlockStatement : JintStatement<BlockStatement>
  6. {
  7. private JintStatementList _statementList;
  8. private List<Declaration> _lexicalDeclarations;
  9. public JintBlockStatement(BlockStatement blockStatement) : base(blockStatement)
  10. {
  11. }
  12. protected override void Initialize(EvaluationContext context)
  13. {
  14. _statementList = new JintStatementList(_statement, _statement.Body);
  15. _lexicalDeclarations = HoistingScope.GetLexicalDeclarations(_statement);
  16. }
  17. internal override bool SupportsResume => true;
  18. protected override Completion ExecuteInternal(EvaluationContext context)
  19. {
  20. EnvironmentRecord oldEnv = null;
  21. var engine = context.Engine;
  22. if (_lexicalDeclarations != null)
  23. {
  24. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  25. var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, engine.ExecutionContext.LexicalEnvironment);
  26. JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, _lexicalDeclarations);
  27. engine.UpdateLexicalEnvironment(blockEnv);
  28. }
  29. var blockValue = _statementList.Execute(context);
  30. if (oldEnv is not null)
  31. {
  32. engine.UpdateLexicalEnvironment(oldEnv);
  33. }
  34. return blockValue;
  35. }
  36. }
  37. }