JintStatementList.cs 7.9 KB

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