arrayIterator.cjs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.safeIfNeeded = safeIfNeeded;
  6. exports.wrap = wrap;
  7. var _primordials = require("./primordials.cjs");
  8. const arrayIterators = new _primordials.NativeWeakMap();
  9. const SafeIteratorPrototype = (0, _primordials.ObjectCreate)(null, {
  10. next: {
  11. value: function next() {
  12. const arrayIterator = (0, _primordials.WeakMapPrototypeGet)(arrayIterators, this);
  13. return (0, _primordials.ArrayIteratorPrototypeNext)(arrayIterator);
  14. }
  15. },
  16. [_primordials.SymbolIterator]: {
  17. value: function values() {
  18. return this;
  19. }
  20. }
  21. });
  22. function safeIfNeeded(array) {
  23. if (array[_primordials.SymbolIterator] === _primordials.NativeArrayPrototypeSymbolIterator && _primordials.ArrayIteratorPrototype.next === _primordials.ArrayIteratorPrototypeNext) {
  24. return array;
  25. }
  26. const safe = (0, _primordials.ObjectCreate)(SafeIteratorPrototype);
  27. (0, _primordials.WeakMapPrototypeSet)(arrayIterators, safe, (0, _primordials.ArrayPrototypeSymbolIterator)(array));
  28. return safe;
  29. }
  30. const generators = new _primordials.NativeWeakMap();
  31. const DummyArrayIteratorPrototype = (0, _primordials.ObjectCreate)(_primordials.IteratorPrototype, {
  32. next: {
  33. value: function next() {
  34. const generator = (0, _primordials.WeakMapPrototypeGet)(generators, this);
  35. return (0, _primordials.GeneratorPrototypeNext)(generator);
  36. },
  37. writable: true,
  38. configurable: true
  39. }
  40. });
  41. for (const key of (0, _primordials.ReflectOwnKeys)(_primordials.ArrayIteratorPrototype)) {
  42. if (key === "next") {
  43. continue;
  44. }
  45. (0, _primordials.ObjectDefineProperty)(DummyArrayIteratorPrototype, key, (0, _primordials.ReflectGetOwnPropertyDescriptor)(_primordials.ArrayIteratorPrototype, key));
  46. }
  47. function wrap(generator) {
  48. const dummy = (0, _primordials.ObjectCreate)(DummyArrayIteratorPrototype);
  49. (0, _primordials.WeakMapPrototypeSet)(generators, dummy, generator);
  50. return dummy;
  51. }