JintStatementList.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 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. // When in debug mode, don't do FastResolve: Stepping requires each statement to be actually executed.
  36. Value = _engine._isDebugMode ? null : JintStatement.FastResolve(esprimaStatement)
  37. };
  38. }
  39. _jintStatements = jintStatements;
  40. }
  41. public Completion Execute()
  42. {
  43. if (!_initialized)
  44. {
  45. Initialize();
  46. _initialized = true;
  47. }
  48. if (_statement != null)
  49. {
  50. _engine._lastSyntaxNode = _statement;
  51. _engine.RunBeforeExecuteStatementChecks(_statement);
  52. }
  53. JintStatement s = null;
  54. var c = new Completion(CompletionType.Normal, null, null, _engine._lastSyntaxNode?.Location ?? default);
  55. Completion sl = c;
  56. // The value of a StatementList is the value of the last value-producing item in the StatementList
  57. JsValue lastValue = null;
  58. try
  59. {
  60. foreach (var pair in _jintStatements)
  61. {
  62. s = pair.Statement;
  63. c = pair.Value ?? s.Execute();
  64. if (c.Type != CompletionType.Normal)
  65. {
  66. return new Completion(
  67. c.Type,
  68. c.Value ?? sl.Value,
  69. c.Identifier,
  70. c.Location);
  71. }
  72. sl = c;
  73. lastValue = c.Value ?? lastValue;
  74. }
  75. }
  76. catch (JavaScriptException v)
  77. {
  78. var location = v.Location == default ? s.Location : v.Location;
  79. var completion = new Completion(CompletionType.Throw, v.Error, null, location);
  80. return completion;
  81. }
  82. catch (TypeErrorException e)
  83. {
  84. var error = _engine.TypeError.Construct(new JsValue[]
  85. {
  86. e.Message
  87. });
  88. return new Completion(CompletionType.Throw, error, null, s.Location);
  89. }
  90. catch (RangeErrorException e)
  91. {
  92. var error = _engine.RangeError.Construct(new JsValue[]
  93. {
  94. e.Message
  95. });
  96. c = new Completion(CompletionType.Throw, error, null, s.Location);
  97. }
  98. return new Completion(c.Type, lastValue ?? JsValue.Undefined, c.Identifier, c.Location);
  99. }
  100. /// <summary>
  101. /// https://tc39.es/ecma262/#sec-blockdeclarationinstantiation
  102. /// </summary>
  103. internal static void BlockDeclarationInstantiation(
  104. LexicalEnvironment env,
  105. List<Declaration> declarations)
  106. {
  107. var envRec = env._record;
  108. var boundNames = new List<string>();
  109. for (var i = 0; i < declarations.Count; i++)
  110. {
  111. var d = declarations[i];
  112. boundNames.Clear();
  113. d.GetBoundNames(boundNames);
  114. for (var j = 0; j < boundNames.Count; j++)
  115. {
  116. var dn = boundNames[j];
  117. if (d is VariableDeclaration { Kind: VariableDeclarationKind.Const })
  118. {
  119. envRec.CreateImmutableBinding(dn, strict: true);
  120. }
  121. else
  122. {
  123. envRec.CreateMutableBinding(dn, canBeDeleted: false);
  124. }
  125. }
  126. if (d is FunctionDeclaration functionDeclaration)
  127. {
  128. var fn = functionDeclaration.Id!.Name;
  129. var fo = env._engine.Function.InstantiateFunctionObject(functionDeclaration, env);
  130. envRec.InitializeBinding(fn, fo);
  131. }
  132. }
  133. }
  134. }
  135. }