JintWhileStatement.cs 2.1 KB

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