CompletionRecord.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. var $SyntaxError = require('es-errors/syntax');
  3. var SLOT = require('internal-slot');
  4. // https://262.ecma-international.org/7.0/#sec-completion-record-specification-type
  5. var CompletionRecord = function CompletionRecord(type, value) {
  6. if (!(this instanceof CompletionRecord)) {
  7. return new CompletionRecord(type, value);
  8. }
  9. if (type !== 'normal' && type !== 'break' && type !== 'continue' && type !== 'return' && type !== 'throw') {
  10. throw new $SyntaxError('Assertion failed: `type` must be one of "normal", "break", "continue", "return", or "throw"');
  11. }
  12. SLOT.set(this, '[[Type]]', type);
  13. SLOT.set(this, '[[Value]]', value);
  14. // [[Target]] slot?
  15. };
  16. CompletionRecord.prototype.type = function Type() {
  17. return SLOT.get(this, '[[Type]]');
  18. };
  19. CompletionRecord.prototype.value = function Value() {
  20. return SLOT.get(this, '[[Value]]');
  21. };
  22. CompletionRecord.prototype['?'] = function ReturnIfAbrupt() {
  23. var type = SLOT.get(this, '[[Type]]');
  24. var value = SLOT.get(this, '[[Value]]');
  25. if (type === 'throw') {
  26. throw value;
  27. }
  28. return value;
  29. };
  30. CompletionRecord.prototype['!'] = function assert() {
  31. var type = SLOT.get(this, '[[Type]]');
  32. if (type !== 'normal') {
  33. throw new $SyntaxError('Assertion failed: Completion Record is not of type "normal"');
  34. }
  35. return SLOT.get(this, '[[Value]]');
  36. };
  37. module.exports = CompletionRecord;