JintStatementList.cs 7.5 KB

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