IteratorProtocol.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using Jint.Native.Object;
  2. using Jint.Runtime;
  3. namespace Jint.Native.Iterator
  4. {
  5. /// <summary>
  6. /// Handles looping of iterator values, sub-classes can use to implement wanted actions.
  7. /// </summary>
  8. internal abstract class IteratorProtocol
  9. {
  10. private readonly Engine _engine;
  11. private readonly IteratorInstance _iterator;
  12. private readonly int _argCount;
  13. protected IteratorProtocol(
  14. Engine engine,
  15. IteratorInstance iterator,
  16. int argCount)
  17. {
  18. _engine = engine;
  19. _iterator = iterator;
  20. _argCount = argCount;
  21. }
  22. internal bool Execute()
  23. {
  24. var args = _engine._jsValueArrayPool.RentArray(_argCount);
  25. var done = false;
  26. try
  27. {
  28. while (ShouldContinue)
  29. {
  30. if (!_iterator.TryIteratorStep(out var item))
  31. {
  32. done = true;
  33. break;
  34. }
  35. var currentValue = item.Get(CommonProperties.Value);
  36. ProcessItem(args, currentValue);
  37. }
  38. }
  39. catch
  40. {
  41. IteratorClose(CompletionType.Throw);
  42. throw;
  43. }
  44. finally
  45. {
  46. _engine._jsValueArrayPool.ReturnArray(args);
  47. }
  48. IterationEnd();
  49. return done;
  50. }
  51. protected void IteratorClose(CompletionType completionType)
  52. {
  53. _iterator.Close(completionType);
  54. }
  55. protected virtual bool ShouldContinue => true;
  56. protected virtual void IterationEnd()
  57. {
  58. }
  59. protected abstract void ProcessItem(JsValue[] arguments, JsValue currentValue);
  60. internal static void AddEntriesFromIterable(ObjectInstance target, IteratorInstance iterable, object adder)
  61. {
  62. var callable = adder as ICallable;
  63. if (callable is null)
  64. {
  65. ExceptionHelper.ThrowTypeError(target.Engine.Realm, "adder must be callable");
  66. }
  67. var args = target.Engine._jsValueArrayPool.RentArray(2);
  68. var skipClose = true;
  69. try
  70. {
  71. do
  72. {
  73. if (!iterable.TryIteratorStep(out var nextItem))
  74. {
  75. return;
  76. }
  77. var temp = nextItem.Get(CommonProperties.Value);
  78. skipClose = false;
  79. var oi = temp as ObjectInstance;
  80. if (oi is null)
  81. {
  82. ExceptionHelper.ThrowTypeError(target.Engine.Realm, "iterator's value must be an object");
  83. }
  84. var k = oi.Get(JsString.NumberZeroString);
  85. var v = oi.Get(JsString.NumberOneString);
  86. args[0] = k;
  87. args[1] = v;
  88. callable.Call(target, args);
  89. } while (true);
  90. }
  91. catch
  92. {
  93. if (!skipClose)
  94. {
  95. iterable.Close(CompletionType.Throw);
  96. }
  97. throw;
  98. }
  99. finally
  100. {
  101. target.Engine._jsValueArrayPool.ReturnArray(args);
  102. }
  103. }
  104. }
  105. }