JintStatementList.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 readonly 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. if (context.Engine._error is not null)
  83. {
  84. return HandleError(context.Engine, s);
  85. }
  86. }
  87. if (c.Type != CompletionType.Normal)
  88. {
  89. return new Completion(c.Type, c.Value ?? sl.Value!, c._source);
  90. }
  91. sl = c;
  92. if (c.Value is not null)
  93. {
  94. lastValue = c.Value;
  95. }
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. if (ex is JintException)
  101. {
  102. return HandleException(context, ex, s);
  103. }
  104. throw;
  105. }
  106. return new Completion(c.Type, lastValue ?? JsValue.Undefined, c._source!);
  107. }
  108. private static Completion HandleException(EvaluationContext context, Exception exception, JintStatement? s)
  109. {
  110. if (exception is JavaScriptException javaScriptException)
  111. {
  112. return CreateThrowCompletion(s, javaScriptException);
  113. }
  114. if (exception is TypeErrorException typeErrorException)
  115. {
  116. var node = typeErrorException.Node ?? s!._statement;
  117. return CreateThrowCompletion(context.Engine.Realm.Intrinsics.TypeError, typeErrorException, node);
  118. }
  119. if (exception is RangeErrorException rangeErrorException)
  120. {
  121. return CreateThrowCompletion(context.Engine.Realm.Intrinsics.RangeError, rangeErrorException, s!._statement);
  122. }
  123. // should not happen unless there's problem in the engine
  124. throw exception;
  125. }
  126. private static Completion HandleError(Engine engine, JintStatement? s)
  127. {
  128. var error = engine._error!;
  129. engine._error = null;
  130. return CreateThrowCompletion(error.ErrorConstructor, error.Message, engine._lastSyntaxElement ?? s!._statement);
  131. }
  132. private static Completion CreateThrowCompletion(ErrorConstructor errorConstructor, string? message, SyntaxElement s)
  133. {
  134. var error = errorConstructor.Construct(message);
  135. return new Completion(CompletionType.Throw, error, s);
  136. }
  137. private static Completion CreateThrowCompletion(ErrorConstructor errorConstructor, Exception e, SyntaxElement s)
  138. {
  139. var error = errorConstructor.Construct(e.Message);
  140. return new Completion(CompletionType.Throw, error, s);
  141. }
  142. private static Completion CreateThrowCompletion(JintStatement? s, JavaScriptException v)
  143. {
  144. SyntaxElement source = s!._statement;
  145. if (v.Location != default)
  146. {
  147. source = EsprimaExtensions.CreateLocationNode(v.Location);
  148. }
  149. return new Completion(CompletionType.Throw, v.Error, source);
  150. }
  151. /// <summary>
  152. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  153. /// </summary>
  154. internal static void BlockDeclarationInstantiation(
  155. Engine engine,
  156. EnvironmentRecord env,
  157. List<Declaration> declarations)
  158. {
  159. var privateEnv = env._engine.ExecutionContext.PrivateEnvironment;
  160. var boundNames = new List<string>();
  161. for (var i = 0; i < declarations.Count; i++)
  162. {
  163. var d = declarations[i];
  164. boundNames.Clear();
  165. d.GetBoundNames(boundNames);
  166. for (var j = 0; j < boundNames.Count; j++)
  167. {
  168. var dn = boundNames[j];
  169. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  170. {
  171. env.CreateImmutableBinding(dn, strict: true);
  172. }
  173. else
  174. {
  175. env.CreateMutableBinding(dn, canBeDeleted: false);
  176. }
  177. }
  178. if (d is FunctionDeclaration functionDeclaration)
  179. {
  180. var definition = new JintFunctionDefinition(functionDeclaration);
  181. var fn = definition.Name!;
  182. var fo = env._engine.Realm.Intrinsics.Function.InstantiateFunctionObject(definition, env, privateEnv);
  183. env.InitializeBinding(fn, fo);
  184. }
  185. }
  186. }
  187. }
  188. }