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