2
0

JintDoWhileStatement.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Runtime.Interpreter.Expressions;
  4. namespace Jint.Runtime.Interpreter.Statements
  5. {
  6. /// <summary>
  7. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.1
  8. /// </summary>
  9. internal sealed class JintDoWhileStatement : JintStatement<DoWhileStatement>
  10. {
  11. private readonly JintStatement _body;
  12. private readonly string _labelSetName;
  13. private readonly JintExpression _test;
  14. public JintDoWhileStatement(Engine engine, DoWhileStatement statement) : base(engine, statement)
  15. {
  16. _body = Build(_engine, statement.Body);
  17. _test = JintExpression.Build(engine, statement.Test);
  18. _labelSetName = statement.LabelSet?.Name;
  19. }
  20. protected override Completion ExecuteInternal()
  21. {
  22. JsValue v = Undefined.Instance;
  23. bool iterating;
  24. do
  25. {
  26. var completion = _body.Execute();
  27. if (!ReferenceEquals(completion.Value, null))
  28. {
  29. v = completion.Value;
  30. }
  31. if (completion.Type != CompletionType.Continue || completion.Identifier != _labelSetName)
  32. {
  33. if (completion.Type == CompletionType.Break && (completion.Identifier == null || completion.Identifier == _labelSetName))
  34. {
  35. return new Completion(CompletionType.Normal, v, null, Location);
  36. }
  37. if (completion.Type != CompletionType.Normal)
  38. {
  39. return completion;
  40. }
  41. }
  42. iterating = TypeConverter.ToBoolean(_test.GetValue());
  43. } while (iterating);
  44. return new Completion(CompletionType.Normal, v, null, Location);
  45. }
  46. }
  47. }