JintForOfStatement.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Native.Iterator;
  4. using Jint.Runtime.Interpreter.Expressions;
  5. using Jint.Runtime.References;
  6. namespace Jint.Runtime.Interpreter.Statements
  7. {
  8. /// <summary>
  9. /// https://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements
  10. /// </summary>
  11. internal sealed class JintForOfStatement : JintStatement<ForOfStatement>
  12. {
  13. private JintStatement _body;
  14. private JintExpression _identifier;
  15. private BindingPattern _leftPattern;
  16. private JintExpression _right;
  17. public JintForOfStatement(Engine engine, ForOfStatement statement) : base(engine, statement)
  18. {
  19. _initialized = false;
  20. }
  21. protected override void Initialize()
  22. {
  23. if (_statement.Left is VariableDeclaration variableDeclaration)
  24. {
  25. var element = variableDeclaration.Declarations[0].Id;
  26. if (element is BindingPattern bindingPattern)
  27. {
  28. _leftPattern = bindingPattern;
  29. }
  30. else
  31. {
  32. _identifier = JintExpression.Build(_engine, (Identifier) element);
  33. }
  34. }
  35. else if (_statement.Left is BindingPattern bindingPattern)
  36. {
  37. _leftPattern = bindingPattern;
  38. }
  39. else
  40. {
  41. _identifier = JintExpression.Build(_engine, (Expression) _statement.Left);
  42. }
  43. _body = Build(_engine, _statement.Body);
  44. _right = JintExpression.Build(_engine, _statement.Right);
  45. }
  46. protected override Completion ExecuteInternal()
  47. {
  48. var experValue = _right.GetValue();
  49. if (!(experValue is IIterator iterator))
  50. {
  51. var obj = TypeConverter.ToObject(_engine, experValue);
  52. obj.TryGetIterator(_engine, out iterator);
  53. }
  54. if (iterator is null)
  55. {
  56. return ExceptionHelper.ThrowTypeError<Completion>(_engine, _identifier + " is not iterable");
  57. }
  58. var completionType = CompletionType.Normal;
  59. var v = JsValue.Undefined;
  60. var close = false;
  61. try
  62. {
  63. do
  64. {
  65. iterator.TryIteratorStep(out var item);
  66. var done = item.Get(CommonProperties.Done);
  67. if (TypeConverter.ToBoolean(done))
  68. {
  69. // we can close after checks pass
  70. close = true;
  71. break;
  72. }
  73. var currentValue = item.Get(CommonProperties.Value);
  74. // we can close after checks pass
  75. close = true;
  76. if (_leftPattern != null)
  77. {
  78. BindingPatternAssignmentExpression.ProcessPatterns(
  79. _engine,
  80. _leftPattern,
  81. currentValue,
  82. checkReference: !(_statement.Left is VariableDeclaration));
  83. }
  84. else
  85. {
  86. var varRef = (Reference) _identifier.Evaluate();
  87. _engine.PutValue(varRef, currentValue);
  88. }
  89. var stmt = _body.Execute();
  90. if (!ReferenceEquals(stmt.Value, null))
  91. {
  92. v = stmt.Value;
  93. }
  94. if (stmt.Type == CompletionType.Break && (stmt.Identifier == null || stmt.Identifier == _statement?.LabelSet?.Name))
  95. {
  96. return new Completion(CompletionType.Normal, stmt.Value, null, Location);
  97. }
  98. if (stmt.Type != CompletionType.Continue || ((stmt.Identifier != null) && stmt.Identifier != _statement?.LabelSet?.Name))
  99. {
  100. if (stmt.Type != CompletionType.Normal)
  101. {
  102. return stmt;
  103. }
  104. }
  105. } while (true);
  106. }
  107. catch
  108. {
  109. completionType = CompletionType.Throw;
  110. throw;
  111. }
  112. finally
  113. {
  114. if (close)
  115. {
  116. iterator.Close(completionType);
  117. }
  118. }
  119. return new Completion(CompletionType.Normal, v, null, Location);
  120. }
  121. }
  122. }