IteratorProtocol.cs 3.8 KB

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