JintStatementList.cs 6.7 KB

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