| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | var $TypeError = require('es-errors/type');
|
|---|
| 4 |
|
|---|
| 5 | var CompletionRecord = require('./CompletionRecord');
|
|---|
| 6 | var CreateIteratorResultObject = require('./CreateIteratorResultObject');
|
|---|
| 7 | var GeneratorValidate = require('./GeneratorValidate');
|
|---|
| 8 | var NormalCompletion = require('./NormalCompletion');
|
|---|
| 9 |
|
|---|
| 10 | var SLOT = require('internal-slot');
|
|---|
| 11 |
|
|---|
| 12 | // https://262.ecma-international.org/16.0/#sec-generatorresumeabrupt
|
|---|
| 13 |
|
|---|
| 14 | module.exports = function GeneratorResumeAbrupt(generator, abruptCompletion, generatorBrand) {
|
|---|
| 15 | if (
|
|---|
| 16 | !(abruptCompletion instanceof CompletionRecord)
|
|---|
| 17 | || (abruptCompletion.type() !== 'return' && abruptCompletion.type() !== 'throw')
|
|---|
| 18 | ) {
|
|---|
| 19 | throw new $TypeError('Assertion failed: abruptCompletion must be a `return` or `throw` Completion Record');
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | var state = GeneratorValidate(generator, generatorBrand); // step 1
|
|---|
| 23 |
|
|---|
| 24 | if (state === 'SUSPENDED-START') { // step 2
|
|---|
| 25 | SLOT.set(generator, '[[GeneratorState]]', 'COMPLETED'); // step 2.a
|
|---|
| 26 | SLOT.set(generator, '[[GeneratorContext]]', null); // step 2.b
|
|---|
| 27 | state = 'COMPLETED'; // step 2.c
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | var value = abruptCompletion.value();
|
|---|
| 31 |
|
|---|
| 32 | if (state === 'COMPLETED') { // step 3
|
|---|
| 33 | if (abruptCompletion.type() === 'return') { // step 3.a
|
|---|
| 34 | return CreateIteratorResultObject(value, true); // step 3.a.i
|
|---|
| 35 | }
|
|---|
| 36 | return abruptCompletion['?'](); // step 3.b
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | if (state !== 'SUSPENDED-YIELD') {
|
|---|
| 40 | throw new $TypeError('Assertion failed: generator state is unexpected: ' + state); // step 4
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | var genContext = SLOT.get(generator, '[[GeneratorContext]]'); // step 5
|
|---|
| 44 |
|
|---|
| 45 | SLOT.set(generator, '[[GeneratorState]]', 'EXECUTING'); // step 8
|
|---|
| 46 |
|
|---|
| 47 | if (abruptCompletion.type() === 'return') {
|
|---|
| 48 | // due to representing `GeneratorContext` as a function, we can't safely re-invoke it, so we can't support sending it a return completion
|
|---|
| 49 | return CreateIteratorResultObject(SLOT.get(generator, '[[CloseIfAbrupt]]')(NormalCompletion(value)), true);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | var result = genContext(value); // steps 6-7, 9-11
|
|---|
| 53 |
|
|---|
| 54 | return result; // step 12
|
|---|
| 55 | };
|
|---|