JintStatementList.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Runtime.Environments;
  4. using Jint.Runtime.Interpreter.Statements;
  5. namespace Jint.Runtime.Interpreter
  6. {
  7. internal sealed class JintStatementList
  8. {
  9. private sealed class Pair
  10. {
  11. internal JintStatement Statement = null!;
  12. internal Completion? Value;
  13. }
  14. private readonly Statement? _statement;
  15. private readonly NodeList<Statement> _statements;
  16. private Pair[]? _jintStatements;
  17. private bool _initialized;
  18. private uint _index;
  19. private readonly bool _generator;
  20. public JintStatementList(IFunction function)
  21. : this((BlockStatement) function.Body)
  22. {
  23. _generator = function.Generator;
  24. }
  25. public JintStatementList(BlockStatement blockStatement)
  26. : this(blockStatement, blockStatement.Body)
  27. {
  28. }
  29. public JintStatementList(Program program)
  30. : this(null, program.Body)
  31. {
  32. }
  33. public JintStatementList(Statement? statement, in NodeList<Statement> statements)
  34. {
  35. _statement = statement;
  36. _statements = statements;
  37. }
  38. private void Initialize(EvaluationContext context)
  39. {
  40. var jintStatements = new Pair[_statements.Count];
  41. for (var i = 0; i < jintStatements.Length; i++)
  42. {
  43. var esprimaStatement = _statements[i];
  44. var statement = JintStatement.Build(esprimaStatement);
  45. // When in debug mode, don't do FastResolve: Stepping requires each statement to be actually executed.
  46. var value = context.DebugMode ? null : JintStatement.FastResolve(esprimaStatement);
  47. jintStatements[i] = new Pair
  48. {
  49. Statement = statement,
  50. Value = value
  51. };
  52. }
  53. _jintStatements = jintStatements;
  54. }
  55. public Completion Execute(EvaluationContext context)
  56. {
  57. if (!_initialized)
  58. {
  59. Initialize(context);
  60. _initialized = true;
  61. }
  62. var engine = context.Engine;
  63. if (_statement != null)
  64. {
  65. context.LastSyntaxNode = _statement;
  66. engine.RunBeforeExecuteStatementChecks(_statement);
  67. }
  68. JintStatement? s = null;
  69. var c = new Completion(CompletionType.Normal, null!, null, context.LastSyntaxNode?.Location ?? default);
  70. Completion sl = c;
  71. // The value of a StatementList is the value of the last value-producing item in the StatementList
  72. JsValue? lastValue = null;
  73. try
  74. {
  75. foreach (var pair in _jintStatements!)
  76. {
  77. s = pair.Statement;
  78. c = pair.Value ?? s.Execute(context);
  79. if (c.Type != CompletionType.Normal)
  80. {
  81. return new Completion(
  82. c.Type,
  83. c.Value ?? sl.Value,
  84. c.Target,
  85. c.Location);
  86. }
  87. sl = c;
  88. lastValue = c.Value ?? lastValue;
  89. }
  90. }
  91. catch (JavaScriptException v)
  92. {
  93. var location = v.Location == default ? s!.Location : v.Location;
  94. var completion = new Completion(CompletionType.Throw, v.Error, null, location);
  95. return completion;
  96. }
  97. catch (TypeErrorException e)
  98. {
  99. var error = engine.Realm.Intrinsics.TypeError.Construct(new JsValue[]
  100. {
  101. e.Message
  102. });
  103. return new Completion(CompletionType.Throw, error, null, s!.Location);
  104. }
  105. catch (RangeErrorException e)
  106. {
  107. var error = engine.Realm.Intrinsics.RangeError.Construct(new JsValue[]
  108. {
  109. e.Message
  110. });
  111. c = new Completion(CompletionType.Throw, error, null, s!.Location);
  112. }
  113. return new Completion(c.Type, lastValue ?? JsValue.Undefined, c.Target, c.Location);
  114. }
  115. /// <summary>
  116. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  117. /// </summary>
  118. internal static void BlockDeclarationInstantiation(
  119. Engine engine,
  120. EnvironmentRecord env,
  121. List<Declaration> declarations)
  122. {
  123. var privateEnv = env._engine.ExecutionContext.PrivateEnvironment;
  124. var boundNames = new List<string>();
  125. for (var i = 0; i < declarations.Count; i++)
  126. {
  127. var d = declarations[i];
  128. boundNames.Clear();
  129. d.GetBoundNames(boundNames);
  130. for (var j = 0; j < boundNames.Count; j++)
  131. {
  132. var dn = boundNames[j];
  133. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  134. {
  135. env.CreateImmutableBinding(dn, strict: true);
  136. }
  137. else
  138. {
  139. env.CreateMutableBinding(dn, canBeDeleted: false);
  140. }
  141. }
  142. if (d is FunctionDeclaration functionDeclaration)
  143. {
  144. var definition = new JintFunctionDefinition(engine, functionDeclaration);
  145. var fn = definition.Name!;
  146. var fo = env._engine.Realm.Intrinsics.Function.InstantiateFunctionObject(definition, env, privateEnv);
  147. env.InitializeBinding(fn, fo);
  148. }
  149. }
  150. }
  151. }
  152. }