JintWhileStatement.cs 2.2 KB

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