GeneratorResumeAbrupt.js 1.6 KB

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