2
0

JintDoWhileStatement.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ProbablyBlockStatement _body;
  12. private string? _labelSetName;
  13. private JintExpression _test = null!;
  14. public JintDoWhileStatement(DoWhileStatement statement) : base(statement)
  15. {
  16. }
  17. protected override void Initialize(EvaluationContext context)
  18. {
  19. _body = new ProbablyBlockStatement(_statement.Body);
  20. _test = JintExpression.Build(context.Engine, _statement.Test);
  21. _labelSetName = _statement.LabelSet?.Name;
  22. }
  23. protected override Completion ExecuteInternal(EvaluationContext context)
  24. {
  25. JsValue v = Undefined.Instance;
  26. bool iterating;
  27. do
  28. {
  29. var completion = _body.Execute(context);
  30. if (!ReferenceEquals(completion.Value, null))
  31. {
  32. v = completion.Value;
  33. }
  34. if (completion.Type != CompletionType.Continue || completion.Target != _labelSetName)
  35. {
  36. if (completion.Type == CompletionType.Break && (completion.Target == null || completion.Target == _labelSetName))
  37. {
  38. return new Completion(CompletionType.Normal, v, null, _statement);
  39. }
  40. if (completion.Type != CompletionType.Normal)
  41. {
  42. return completion;
  43. }
  44. }
  45. if (context.DebugMode)
  46. {
  47. context.Engine.DebugHandler.OnStep(_test._expression);
  48. }
  49. iterating = TypeConverter.ToBoolean(_test.GetValue(context).Value);
  50. } while (iterating);
  51. return new Completion(CompletionType.Normal, v, ((JintStatement) this)._statement);
  52. }
  53. }
  54. }