JintStatementList.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. public class JintStatementList
  9. {
  10. private class Pair
  11. {
  12. internal JintStatement Statement;
  13. internal Completion? Value;
  14. }
  15. private readonly Engine _engine;
  16. private readonly Statement _statement;
  17. private readonly NodeList<Statement> _statements;
  18. private Pair[] _jintStatements;
  19. private bool _initialized;
  20. public JintStatementList(Engine engine, Statement statement, NodeList<Statement> statements)
  21. {
  22. _engine = engine;
  23. _statement = statement;
  24. _statements = statements;
  25. }
  26. private void Initialize()
  27. {
  28. var jintStatements = new Pair[_statements.Count];
  29. for (var i = 0; i < jintStatements.Length; i++)
  30. {
  31. var esprimaStatement = _statements[i];
  32. jintStatements[i] = new Pair
  33. {
  34. Statement = JintStatement.Build(_engine, esprimaStatement),
  35. Value = JintStatement.FastResolve(esprimaStatement)
  36. };
  37. }
  38. _jintStatements = jintStatements;
  39. }
  40. public Completion Execute()
  41. {
  42. if (!_initialized)
  43. {
  44. Initialize();
  45. _initialized = true;
  46. }
  47. if (_statement != null)
  48. {
  49. _engine._lastSyntaxNode = _statement;
  50. _engine.RunBeforeExecuteStatementChecks(_statement);
  51. }
  52. JintStatement s = null;
  53. var c = new Completion(CompletionType.Normal, null, null, _engine._lastSyntaxNode?.Location ?? default);
  54. Completion sl = c;
  55. // The value of a StatementList is the value of the last value-producing item in the StatementList
  56. JsValue lastValue = null;
  57. try
  58. {
  59. foreach (var pair in _jintStatements)
  60. {
  61. s = pair.Statement;
  62. c = pair.Value ?? s.Execute();
  63. if (c.Type != CompletionType.Normal)
  64. {
  65. return new Completion(
  66. c.Type,
  67. c.Value ?? sl.Value,
  68. c.Identifier,
  69. c.Location);
  70. }
  71. sl = c;
  72. lastValue = c.Value ?? lastValue;
  73. }
  74. }
  75. catch (JavaScriptException v)
  76. {
  77. var location = v.Location == default ? s.Location : v.Location;
  78. var completion = new Completion(CompletionType.Throw, v.Error, null, location);
  79. return completion;
  80. }
  81. catch (TypeErrorException e)
  82. {
  83. var error = _engine.TypeError.Construct(new JsValue[]
  84. {
  85. e.Message
  86. });
  87. return new Completion(CompletionType.Throw, error, null, s.Location);
  88. }
  89. catch (RangeErrorException e)
  90. {
  91. var error = _engine.RangeError.Construct(new JsValue[]
  92. {
  93. e.Message
  94. });
  95. c = new Completion(CompletionType.Throw, error, null, s.Location);
  96. }
  97. return new Completion(c.Type, lastValue ?? JsValue.Undefined, c.Identifier, c.Location);
  98. }
  99. /// <summary>
  100. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  101. /// </summary>
  102. internal static void BlockDeclarationInstantiation(
  103. LexicalEnvironment env,
  104. List<VariableDeclaration> lexicalDeclarations)
  105. {
  106. var envRec = env._record;
  107. var boundNames = new List<string>();
  108. for (var i = 0; i < lexicalDeclarations.Count; i++)
  109. {
  110. var variableDeclaration = lexicalDeclarations[i];
  111. boundNames.Clear();
  112. variableDeclaration.GetBoundNames(boundNames);
  113. for (var j = 0; j < boundNames.Count; j++)
  114. {
  115. var dn = boundNames[j];
  116. if (variableDeclaration.Kind == VariableDeclarationKind.Const)
  117. {
  118. envRec.CreateImmutableBinding(dn, strict: true);
  119. }
  120. else
  121. {
  122. envRec.CreateMutableBinding(dn, canBeDeleted: false);
  123. }
  124. }
  125. /* If d is a FunctionDeclaration, a GeneratorDeclaration, an AsyncFunctionDeclaration, or an AsyncGeneratorDeclaration, then
  126. * Let fn be the sole element of the BoundNames of d.
  127. * Let fo be the result of performing InstantiateFunctionObject for d with argument env.
  128. * Perform envRec.InitializeBinding(fn, fo).
  129. */
  130. }
  131. }
  132. }
  133. }