[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var CreateIterResultObject = require('es-abstract/2024/CreateIterResultObject');
|
---|
| 6 | var IsCallable = require('es-abstract/2024/IsCallable');
|
---|
| 7 | var Type = require('es-abstract/2024/Type');
|
---|
| 8 |
|
---|
| 9 | var SLOT = require('internal-slot');
|
---|
| 10 |
|
---|
| 11 | module.exports = function GeneratorStart(generator, closure) {
|
---|
| 12 | SLOT.assert(generator, '[[GeneratorState]]');
|
---|
| 13 | SLOT.assert(generator, '[[GeneratorContext]]');
|
---|
| 14 | SLOT.assert(generator, '[[GeneratorBrand]]');
|
---|
| 15 | SLOT.assert(generator, '[[Sentinel]]'); // our userland slot
|
---|
| 16 | SLOT.assert(generator, '[[CloseIfAbrupt]]'); // our second userland slot
|
---|
| 17 |
|
---|
| 18 | if (!IsCallable(closure) || closure.length !== 0) {
|
---|
| 19 | throw new $TypeError('`closure` must be a function that takes no arguments');
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | var sentinel = SLOT.get(closure, '[[Sentinel]]');
|
---|
| 23 | if (Type(sentinel) !== 'Object') {
|
---|
| 24 | throw new $TypeError('`closure.[[Sentinel]]` must be an object');
|
---|
| 25 | }
|
---|
| 26 | SLOT.set(generator, '[[GeneratorContext]]', function () { // steps 2-5
|
---|
| 27 | try {
|
---|
| 28 | var result = closure();
|
---|
| 29 | if (result === sentinel) {
|
---|
| 30 | SLOT.set(generator, '[[GeneratorState]]', 'completed');
|
---|
| 31 | SLOT.set(generator, '[[GeneratorContext]]', null);
|
---|
| 32 | return CreateIterResultObject(void undefined, true);
|
---|
| 33 | }
|
---|
| 34 | SLOT.set(generator, '[[GeneratorState]]', 'suspendedYield');
|
---|
| 35 | return CreateIterResultObject(result, false);
|
---|
| 36 | } catch (e) {
|
---|
| 37 | SLOT.set(generator, '[[GeneratorState]]', 'completed');
|
---|
| 38 | SLOT.set(generator, '[[GeneratorContext]]', null);
|
---|
| 39 | throw e;
|
---|
| 40 | }
|
---|
| 41 | });
|
---|
| 42 |
|
---|
| 43 | SLOT.set(generator, '[[GeneratorState]]', 'suspendedStart'); // step 6
|
---|
| 44 | };
|
---|