CreateIteratorFromClosure.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. var $TypeError = require('es-errors/type');
  3. var GeneratorStart = require('./GeneratorStart');
  4. var IsArray = require('es-abstract/2023/IsArray');
  5. var IsCallable = require('es-abstract/2023/IsCallable');
  6. var OrdinaryObjectCreate = require('es-abstract/2023/OrdinaryObjectCreate');
  7. var every = require('es-abstract/helpers/every');
  8. var SLOT = require('internal-slot');
  9. var safeConcat = require('safe-array-concat');
  10. var isString = function isString(slot) {
  11. return typeof slot === 'string';
  12. };
  13. module.exports = function CreateIteratorFromClosure(closure, generatorBrand, proto) {
  14. if (!IsCallable(closure)) {
  15. throw new $TypeError('`closure` must be a function');
  16. }
  17. if (typeof generatorBrand !== 'string') {
  18. throw new $TypeError('`generatorBrand` must be a string');
  19. }
  20. var extraSlots = arguments.length > 3 ? arguments[3] : [];
  21. if (arguments.length > 3) {
  22. if (!IsArray(extraSlots) || !every(extraSlots, isString)) {
  23. throw new $TypeError('`extraSlots` must be a List of String internal slot names');
  24. }
  25. }
  26. var internalSlotsList = safeConcat(extraSlots, ['[[GeneratorContext]]', '[[GeneratorBrand]]', '[[GeneratorState]]']); // step 3
  27. var generator = OrdinaryObjectCreate(proto, internalSlotsList); // steps 4, 6
  28. SLOT.set(generator, '[[GeneratorBrand]]', generatorBrand); // step 5
  29. SLOT.assert(closure, '[[Sentinel]]'); // our userland slot
  30. SLOT.set(generator, '[[Sentinel]]', SLOT.get(closure, '[[Sentinel]]')); // our userland slot
  31. SLOT.assert(closure, '[[CloseIfAbrupt]]'); // our second userland slot
  32. SLOT.set(generator, '[[CloseIfAbrupt]]', SLOT.get(closure, '[[CloseIfAbrupt]]')); // our second userland slot
  33. GeneratorStart(generator, closure); // step 13
  34. return generator; // step 15
  35. };