JintStatementList.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. }
  83. if (c.Type != CompletionType.Normal)
  84. {
  85. return new Completion(
  86. c.Type,
  87. c.Value ?? sl.Value!,
  88. c.Target,
  89. 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.Target, 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. return CreateThrowCompletion(context.Engine.Realm.Intrinsics.TypeError, typeErrorException, s!);
  117. }
  118. if (exception is RangeErrorException rangeErrorException)
  119. {
  120. return CreateThrowCompletion(context.Engine.Realm.Intrinsics.RangeError, rangeErrorException, s!);
  121. }
  122. // should not happen unless there's problem in the engine
  123. throw exception;
  124. }
  125. private static Completion CreateThrowCompletion(ErrorConstructor errorConstructor, Exception e, JintStatement s)
  126. {
  127. var error = errorConstructor.Construct(new JsValue[] { e.Message });
  128. return new Completion(CompletionType.Throw, error, null, s._statement);
  129. }
  130. private static Completion CreateThrowCompletion(JintStatement? s, JavaScriptException v)
  131. {
  132. SyntaxElement source = s!._statement;
  133. if (v.Location != default)
  134. {
  135. source = EsprimaExtensions.CreateLocationNode(v.Location);
  136. }
  137. return new Completion(CompletionType.Throw, v.Error, null, source);
  138. }
  139. /// <summary>
  140. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  141. /// </summary>
  142. internal static void BlockDeclarationInstantiation(
  143. Engine engine,
  144. EnvironmentRecord env,
  145. List<Declaration> declarations)
  146. {
  147. var privateEnv = env._engine.ExecutionContext.PrivateEnvironment;
  148. var boundNames = new List<string>();
  149. for (var i = 0; i < declarations.Count; i++)
  150. {
  151. var d = declarations[i];
  152. boundNames.Clear();
  153. d.GetBoundNames(boundNames);
  154. for (var j = 0; j < boundNames.Count; j++)
  155. {
  156. var dn = boundNames[j];
  157. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  158. {
  159. env.CreateImmutableBinding(dn, strict: true);
  160. }
  161. else
  162. {
  163. env.CreateMutableBinding(dn, canBeDeleted: false);
  164. }
  165. }
  166. if (d is FunctionDeclaration functionDeclaration)
  167. {
  168. var definition = new JintFunctionDefinition(functionDeclaration);
  169. var fn = definition.Name!;
  170. var fo = env._engine.Realm.Intrinsics.Function.InstantiateFunctionObject(definition, env, privateEnv);
  171. env.InitializeBinding(fn, fo);
  172. }
  173. }
  174. }
  175. }
  176. }