1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var CompletionRecord = require('es-abstract/2024/CompletionRecord');
|
---|
6 | var CreateIterResultObject = require('es-abstract/2024/CreateIterResultObject');
|
---|
7 | var GeneratorValidate = require('./GeneratorValidate');
|
---|
8 | var NormalCompletion = require('es-abstract/2024/NormalCompletion');
|
---|
9 |
|
---|
10 | var SLOT = require('internal-slot');
|
---|
11 |
|
---|
12 | module.exports = function GeneratorResumeAbrupt(generator, abruptCompletion, generatorBrand) {
|
---|
13 | if (!(abruptCompletion instanceof CompletionRecord)) {
|
---|
14 | throw new $TypeError('Assertion failed: abruptCompletion must be a Completion Record');
|
---|
15 | }
|
---|
16 |
|
---|
17 | var state = GeneratorValidate(generator, generatorBrand); // step 1
|
---|
18 |
|
---|
19 | if (state === 'suspendedStart') { // step 2
|
---|
20 | SLOT.set(generator, '[[GeneratorState]]', 'completed'); // step 3.a
|
---|
21 | SLOT.set(generator, '[[GeneratorContext]]', null); // step 3.b
|
---|
22 | state = 'completed'; // step 3.c
|
---|
23 | }
|
---|
24 |
|
---|
25 | var value = abruptCompletion.value();
|
---|
26 |
|
---|
27 | if (state === 'completed') { // step 3
|
---|
28 | return CreateIterResultObject(value, true); // steps 3.a-b
|
---|
29 | }
|
---|
30 |
|
---|
31 | if (state !== 'suspendedYield') {
|
---|
32 | throw new $TypeError('Assertion failed: generator state is unexpected: ' + state); // step 4
|
---|
33 | }
|
---|
34 | if (abruptCompletion.type() === 'return') {
|
---|
35 | // due to representing `GeneratorContext` as a function, we can't safely re-invoke it, so we can't support sending it a return completion
|
---|
36 | return CreateIterResultObject(SLOT.get(generator, '[[CloseIfAbrupt]]')(NormalCompletion(abruptCompletion.value())), true);
|
---|
37 | }
|
---|
38 |
|
---|
39 | var genContext = SLOT.get(generator, '[[GeneratorContext]]'); // step 5
|
---|
40 |
|
---|
41 | SLOT.set(generator, '[[GeneratorState]]', 'executing'); // step 8
|
---|
42 |
|
---|
43 | var result = genContext(value); // steps 6-7, 8-11
|
---|
44 |
|
---|
45 | return result; // step 12
|
---|
46 | };
|
---|