JintSwitchBlock.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using Esprima;
  2. using Esprima.Ast;
  3. using Jint.Native;
  4. using Jint.Runtime.Environments;
  5. using Jint.Runtime.Interpreter.Expressions;
  6. namespace Jint.Runtime.Interpreter.Statements
  7. {
  8. internal sealed class JintSwitchBlock
  9. {
  10. private readonly NodeList<SwitchCase> _switchBlock;
  11. private JintSwitchCase[] _jintSwitchBlock = Array.Empty<JintSwitchCase>();
  12. private bool _initialized;
  13. public JintSwitchBlock(NodeList<SwitchCase> switchBlock)
  14. {
  15. _switchBlock = switchBlock;
  16. }
  17. private void Initialize(EvaluationContext context)
  18. {
  19. var engine = context.Engine;
  20. _jintSwitchBlock = new JintSwitchCase[_switchBlock.Count];
  21. for (var i = 0; i < _jintSwitchBlock.Length; i++)
  22. {
  23. _jintSwitchBlock[i] = new JintSwitchCase(engine, _switchBlock[i]);
  24. }
  25. }
  26. public Completion Execute(EvaluationContext context, JsValue input)
  27. {
  28. if (!_initialized)
  29. {
  30. Initialize(context);
  31. _initialized = true;
  32. }
  33. var engine = context.Engine;
  34. JsValue v = Undefined.Instance;
  35. SyntaxElement l = context.LastSyntaxElement;
  36. JintSwitchCase? defaultCase = null;
  37. bool hit = false;
  38. for (var i = 0; i < (uint) _jintSwitchBlock.Length; i++)
  39. {
  40. var clause = _jintSwitchBlock[i];
  41. EnvironmentRecord? oldEnv = null;
  42. if (clause.LexicalDeclarations != null)
  43. {
  44. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  45. var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
  46. JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, clause.LexicalDeclarations);
  47. engine.UpdateLexicalEnvironment(blockEnv);
  48. }
  49. if (clause.Test == null)
  50. {
  51. defaultCase = clause;
  52. }
  53. else
  54. {
  55. var clauseSelector = clause.Test.GetValue(context).Value;
  56. if (clauseSelector == input)
  57. {
  58. hit = true;
  59. }
  60. }
  61. if (hit && clause.Consequent != null)
  62. {
  63. var r = clause.Consequent.Execute(context);
  64. if (oldEnv is not null)
  65. {
  66. engine.UpdateLexicalEnvironment(oldEnv);
  67. }
  68. if (r.Type != CompletionType.Normal)
  69. {
  70. return r;
  71. }
  72. l = r._source;
  73. v = r.Value ?? Undefined.Instance;
  74. }
  75. }
  76. // do we need to execute the default case ?
  77. if (hit == false && defaultCase != null)
  78. {
  79. EnvironmentRecord? oldEnv = null;
  80. if (defaultCase.LexicalDeclarations != null)
  81. {
  82. oldEnv = engine.ExecutionContext.LexicalEnvironment;
  83. var blockEnv = JintEnvironment.NewDeclarativeEnvironment(engine, oldEnv);
  84. JintStatementList.BlockDeclarationInstantiation(engine, blockEnv, defaultCase.LexicalDeclarations);
  85. engine.UpdateLexicalEnvironment(blockEnv);
  86. }
  87. var r = defaultCase.Consequent.Execute(context);
  88. if (oldEnv is not null)
  89. {
  90. engine.UpdateLexicalEnvironment(oldEnv);
  91. }
  92. if (r.Type != CompletionType.Normal)
  93. {
  94. return r;
  95. }
  96. l = r._source;
  97. v = r.Value ?? Undefined.Instance;
  98. }
  99. return new Completion(CompletionType.Normal, v, null, l);
  100. }
  101. private sealed class JintSwitchCase
  102. {
  103. internal readonly JintStatementList Consequent;
  104. internal readonly JintExpression? Test;
  105. internal readonly List<Declaration>? LexicalDeclarations;
  106. public JintSwitchCase(Engine engine, SwitchCase switchCase)
  107. {
  108. Consequent = new JintStatementList(null, switchCase.Consequent);
  109. LexicalDeclarations = HoistingScope.GetLexicalDeclarations(switchCase);
  110. if (switchCase.Test != null)
  111. {
  112. Test = JintExpression.Build(engine, switchCase.Test);
  113. }
  114. }
  115. }
  116. }
  117. }