JintForStatement.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using Jint.Native;
  2. using Jint.Runtime.Environments;
  3. using Jint.Runtime.Interpreter.Expressions;
  4. using Environment = Jint.Runtime.Environments.Environment;
  5. namespace Jint.Runtime.Interpreter.Statements;
  6. /// <summary>
  7. /// https://tc39.es/ecma262/#sec-forbodyevaluation
  8. /// </summary>
  9. internal sealed class JintForStatement : JintStatement<ForStatement>
  10. {
  11. private JintVariableDeclaration? _initStatement;
  12. private JintExpression? _initExpression;
  13. private JintExpression? _test;
  14. private JintExpression? _increment;
  15. private ProbablyBlockStatement _body;
  16. private List<Key>? _boundNames;
  17. private bool _shouldCreatePerIterationEnvironment;
  18. public JintForStatement(ForStatement statement) : base(statement)
  19. {
  20. }
  21. protected override void Initialize(EvaluationContext context)
  22. {
  23. _body = new ProbablyBlockStatement(_statement.Body);
  24. if (_statement.Init != null)
  25. {
  26. if (_statement.Init.Type == NodeType.VariableDeclaration)
  27. {
  28. var d = (VariableDeclaration) _statement.Init;
  29. if (d.Kind != VariableDeclarationKind.Var)
  30. {
  31. _boundNames = new List<Key>();
  32. d.GetBoundNames(_boundNames);
  33. }
  34. _initStatement = new JintVariableDeclaration(d);
  35. _shouldCreatePerIterationEnvironment = d.Kind == VariableDeclarationKind.Let;
  36. }
  37. else
  38. {
  39. _initExpression = JintExpression.Build((Expression) _statement.Init);
  40. }
  41. }
  42. if (_statement.Test != null)
  43. {
  44. _test = JintExpression.Build(_statement.Test);
  45. }
  46. if (_statement.Update != null)
  47. {
  48. _increment = JintExpression.Build(_statement.Update);
  49. }
  50. }
  51. protected override Completion ExecuteInternal(EvaluationContext context)
  52. {
  53. Environment? oldEnv = null;
  54. DeclarativeEnvironment? loopEnv = null;
  55. var engine = context.Engine;
  56. if (_boundNames != null)
  57. {
  58. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  59. loopEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
  60. var loopEnvRec = loopEnv;
  61. var kind = _initStatement!._statement.Kind;
  62. for (var i = 0; i < _boundNames.Count; i++)
  63. {
  64. var name = _boundNames[i];
  65. if (kind == VariableDeclarationKind.Const)
  66. {
  67. loopEnvRec.CreateImmutableBinding(name);
  68. }
  69. else
  70. {
  71. loopEnvRec.CreateMutableBinding(name);
  72. }
  73. }
  74. engine.UpdateLexicalEnvironment(loopEnv);
  75. }
  76. var completion = Completion.Empty();
  77. try
  78. {
  79. if (_initExpression != null)
  80. {
  81. _initExpression?.GetValue(context);
  82. }
  83. else
  84. {
  85. _initStatement?.Execute(context);
  86. }
  87. completion = ForBodyEvaluation(context);
  88. return completion;
  89. }
  90. finally
  91. {
  92. if (oldEnv is not null)
  93. {
  94. loopEnv!.DisposeResources(completion);
  95. engine.UpdateLexicalEnvironment(oldEnv);
  96. }
  97. }
  98. }
  99. /// <summary>
  100. /// https://tc39.es/ecma262/#sec-forbodyevaluation
  101. /// </summary>
  102. private Completion ForBodyEvaluation(EvaluationContext context)
  103. {
  104. var v = JsValue.Undefined;
  105. if (_shouldCreatePerIterationEnvironment)
  106. {
  107. CreatePerIterationEnvironment(context);
  108. }
  109. var debugHandler = context.DebugMode ? context.Engine.Debugger : null;
  110. while (true)
  111. {
  112. if (_test != null)
  113. {
  114. debugHandler?.OnStep(_test._expression);
  115. if (!TypeConverter.ToBoolean(_test.GetValue(context)))
  116. {
  117. return new Completion(CompletionType.Normal, v, ((JintStatement) this)._statement);
  118. }
  119. }
  120. var result = _body.Execute(context);
  121. if (!result.Value.IsEmpty)
  122. {
  123. v = result.Value;
  124. }
  125. if (result.Type == CompletionType.Break && (context.Target == null || string.Equals(context.Target, _statement?.LabelSet?.Name, StringComparison.Ordinal)))
  126. {
  127. return new Completion(CompletionType.Normal, result.Value, ((JintStatement) this)._statement);
  128. }
  129. if (result.Type != CompletionType.Continue || (context.Target != null && !string.Equals(context.Target, _statement?.LabelSet?.Name, StringComparison.Ordinal)))
  130. {
  131. if (result.Type != CompletionType.Normal)
  132. {
  133. return result;
  134. }
  135. }
  136. if (_shouldCreatePerIterationEnvironment)
  137. {
  138. CreatePerIterationEnvironment(context);
  139. }
  140. if (_increment != null)
  141. {
  142. debugHandler?.OnStep(_increment._expression);
  143. _increment.Evaluate(context);
  144. }
  145. }
  146. }
  147. private void CreatePerIterationEnvironment(EvaluationContext context)
  148. {
  149. var engine = context.Engine;
  150. var lastIterationEnv = (DeclarativeEnvironment) engine.ExecutionContext.LexicalEnvironment;
  151. var thisIterationEnv = JintEnvironment.NewDeclarativeEnvironment(engine, lastIterationEnv._outerEnv);
  152. lastIterationEnv.TransferTo(_boundNames!, thisIterationEnv);
  153. engine.UpdateLexicalEnvironment(thisIterationEnv);
  154. }
  155. }