JintSwitchStatement.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using Esprima.Ast;
  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.11
  7. /// </summary>
  8. internal sealed class JintSwitchStatement : JintStatement<SwitchStatement>
  9. {
  10. private JintSwitchBlock _switchBlock = null!;
  11. private JintExpression _discriminant = null!;
  12. public JintSwitchStatement(SwitchStatement statement) : base(statement)
  13. {
  14. }
  15. protected override void Initialize(EvaluationContext context)
  16. {
  17. var engine = context.Engine;
  18. _switchBlock = new JintSwitchBlock(_statement.Cases);
  19. _discriminant = JintExpression.Build(engine, _statement.Discriminant);
  20. }
  21. protected override Completion ExecuteInternal(EvaluationContext context)
  22. {
  23. var value = _discriminant.GetValue(context);
  24. var r = _switchBlock.Execute(context, value);
  25. if (r.Type == CompletionType.Break && context.Target == _statement.LabelSet?.Name)
  26. {
  27. return new Completion(CompletionType.Normal, r.Value, ((JintStatement) this)._statement);
  28. }
  29. return r;
  30. }
  31. }
  32. }