JintStatementList.cs 7.3 KB

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