JintDoWhileStatement.cs 1.9 KB

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