JintStatementList.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System.Runtime.CompilerServices;
  2. using Esprima.Ast;
  3. using Jint.Native;
  4. using Jint.Native.Error;
  5. using Jint.Runtime.Interpreter.Statements;
  6. using Environment = Jint.Runtime.Environments.Environment;
  7. namespace Jint.Runtime.Interpreter
  8. {
  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 readonly uint _index;
  17. private readonly bool _generator;
  18. public JintStatementList(IFunction function)
  19. : this((BlockStatement) 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 = 0; 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 (c.Type != CompletionType.Normal)
  87. {
  88. return c.UpdateEmpty(sl.Value);
  89. }
  90. sl = c;
  91. if (!c.Value.IsEmpty)
  92. {
  93. lastValue = c.Value;
  94. }
  95. }
  96. }
  97. catch (Exception ex)
  98. {
  99. if (ex is JintException)
  100. {
  101. c = HandleException(context, ex, temp[i].Statement);
  102. }
  103. else
  104. {
  105. throw;
  106. }
  107. }
  108. return c.UpdateEmpty(lastValue).UpdateEmpty(JsValue.Undefined);
  109. }
  110. internal 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. internal 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(Environment env, List<Declaration> declarations)
  157. {
  158. var privateEnv = env._engine.ExecutionContext.PrivateEnvironment;
  159. var boundNames = new List<Key>();
  160. for (var i = 0; i < declarations.Count; i++)
  161. {
  162. var d = declarations[i];
  163. boundNames.Clear();
  164. d.GetBoundNames(boundNames);
  165. for (var j = 0; j < boundNames.Count; j++)
  166. {
  167. var dn = boundNames[j];
  168. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  169. {
  170. env.CreateImmutableBinding(dn, strict: true);
  171. }
  172. else
  173. {
  174. env.CreateMutableBinding(dn, canBeDeleted: false);
  175. }
  176. }
  177. if (d is FunctionDeclaration functionDeclaration)
  178. {
  179. var definition = new JintFunctionDefinition(functionDeclaration);
  180. var fn = definition.Name!;
  181. var fo = env._engine.Realm.Intrinsics.Function.InstantiateFunctionObject(definition, env, privateEnv);
  182. env.InitializeBinding(fn, fo);
  183. }
  184. }
  185. }
  186. }
  187. }