JintBlockStatement.cs 1.6 KB

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