GeneratorValidate.js 748 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var SLOT = require('internal-slot');
  5. module.exports = function GeneratorValidate(generator, generatorBrand) {
  6. SLOT.assert(generator, '[[GeneratorState]]'); // step 1
  7. SLOT.assert(generator, '[[GeneratorBrand]]'); // step 2
  8. var brand = SLOT.get(generator, '[[GeneratorBrand]]');
  9. if (brand !== generatorBrand) {
  10. throw new $TypeError('Assertion failed: generator brand is unexpected: ' + brand);
  11. }
  12. SLOT.assert(generator, '[[GeneratorContext]]'); // step 4
  13. var state = SLOT.get(generator, '[[GeneratorState]]'); // step 5
  14. if (state === 'executing') {
  15. throw new $TypeError('generator is executing');
  16. }
  17. return state; // step 7
  18. };