JintWhileStatement.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.2
  8. /// </summary>
  9. internal sealed class JintWhileStatement : JintStatement<WhileStatement>
  10. {
  11. private readonly string _labelSetName;
  12. private readonly JintStatement _body;
  13. private readonly JintExpression _test;
  14. public JintWhileStatement(Engine engine, WhileStatement statement) : base(engine, statement)
  15. {
  16. _labelSetName = _statement?.LabelSet?.Name;
  17. _body = Build(engine, statement.Body);
  18. _test = JintExpression.Build(engine, statement.Test);
  19. }
  20. protected override Completion ExecuteInternal()
  21. {
  22. var v = Undefined.Instance;
  23. while (true)
  24. {
  25. var jsValue = _test.GetValue();
  26. if (!TypeConverter.ToBoolean(jsValue))
  27. {
  28. return new Completion(CompletionType.Normal, v, null, Location);
  29. }
  30. var completion = _body.Execute();
  31. if (!ReferenceEquals(completion.Value, null))
  32. {
  33. v = completion.Value;
  34. }
  35. if (completion.Type != CompletionType.Continue || completion.Identifier != _labelSetName)
  36. {
  37. if (completion.Type == CompletionType.Break && (completion.Identifier == null || completion.Identifier == _labelSetName))
  38. {
  39. return new Completion(CompletionType.Normal, v, null, Location);
  40. }
  41. if (completion.Type != CompletionType.Normal)
  42. {
  43. return completion;
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }