2
0

JintStatementList.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections.Generic;
  2. using Esprima.Ast;
  3. using Jint.Native;
  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 class Pair
  11. {
  12. internal JintStatement Statement;
  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. public JintStatementList(BlockStatement blockStatement)
  20. : this(blockStatement, blockStatement.Body)
  21. {
  22. }
  23. public JintStatementList(Program program)
  24. : this(null, program.Body)
  25. {
  26. }
  27. public JintStatementList(Statement statement, in NodeList<Statement> statements)
  28. {
  29. _statement = statement;
  30. _statements = statements;
  31. }
  32. private void Initialize(EvaluationContext context)
  33. {
  34. var jintStatements = new Pair[_statements.Count];
  35. for (var i = 0; i < jintStatements.Length; i++)
  36. {
  37. var esprimaStatement = _statements[i];
  38. var statement = JintStatement.Build(esprimaStatement);
  39. // When in debug mode, don't do FastResolve: Stepping requires each statement to be actually executed.
  40. var value = context.DebugMode ? null : JintStatement.FastResolve(esprimaStatement);
  41. jintStatements[i] = new Pair
  42. {
  43. Statement = statement,
  44. Value = value
  45. };
  46. }
  47. _jintStatements = jintStatements;
  48. }
  49. public Completion Execute(EvaluationContext context)
  50. {
  51. if (!_initialized)
  52. {
  53. Initialize(context);
  54. _initialized = true;
  55. }
  56. var engine = context.Engine;
  57. if (_statement != null)
  58. {
  59. context.LastSyntaxNode = _statement;
  60. engine.RunBeforeExecuteStatementChecks(_statement);
  61. }
  62. JintStatement s = null;
  63. var c = new Completion(CompletionType.Normal, null, null, context.LastSyntaxNode?.Location ?? default);
  64. Completion sl = c;
  65. // The value of a StatementList is the value of the last value-producing item in the StatementList
  66. JsValue lastValue = null;
  67. try
  68. {
  69. foreach (var pair in _jintStatements)
  70. {
  71. s = pair.Statement;
  72. c = pair.Value ?? s.Execute(context);
  73. if (c.Type != CompletionType.Normal)
  74. {
  75. return new Completion(
  76. c.Type,
  77. c.Value ?? sl.Value,
  78. c.Target,
  79. c.Location);
  80. }
  81. sl = c;
  82. lastValue = c.Value ?? lastValue;
  83. }
  84. }
  85. catch (JavaScriptException v)
  86. {
  87. var location = v.Location == default ? s.Location : v.Location;
  88. var completion = new Completion(CompletionType.Throw, v.Error, null, location);
  89. return completion;
  90. }
  91. catch (TypeErrorException e)
  92. {
  93. var error = engine.Realm.Intrinsics.TypeError.Construct(new JsValue[]
  94. {
  95. e.Message
  96. });
  97. return new Completion(CompletionType.Throw, error, null, s.Location);
  98. }
  99. catch (RangeErrorException e)
  100. {
  101. var error = engine.Realm.Intrinsics.RangeError.Construct(new JsValue[]
  102. {
  103. e.Message
  104. });
  105. c = new Completion(CompletionType.Throw, error, null, s.Location);
  106. }
  107. return new Completion(c.Type, lastValue ?? JsValue.Undefined, c.Target, c.Location);
  108. }
  109. /// <summary>
  110. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  111. /// </summary>
  112. internal static void BlockDeclarationInstantiation(
  113. Engine engine,
  114. EnvironmentRecord env,
  115. List<Declaration> declarations)
  116. {
  117. var envRec = env;
  118. var boundNames = new List<string>();
  119. for (var i = 0; i < declarations.Count; i++)
  120. {
  121. var d = declarations[i];
  122. boundNames.Clear();
  123. d.GetBoundNames(boundNames);
  124. for (var j = 0; j < boundNames.Count; j++)
  125. {
  126. var dn = boundNames[j];
  127. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  128. {
  129. envRec.CreateImmutableBinding(dn, strict: true);
  130. }
  131. else
  132. {
  133. envRec.CreateMutableBinding(dn, canBeDeleted: false);
  134. }
  135. }
  136. if (d is FunctionDeclaration functionDeclaration)
  137. {
  138. var fn = functionDeclaration.Id!.Name;
  139. var functionDefinition = new JintFunctionDefinition(engine, functionDeclaration);
  140. var fo = env._engine.Realm.Intrinsics.Function.InstantiateFunctionObject(functionDefinition, env);
  141. envRec.InitializeBinding(fn, fo);
  142. }
  143. }
  144. }
  145. }
  146. }