JintStatementList.cs 5.5 KB

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