CompletionRecord.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 === 'normal') {
  26. return value;
  27. }
  28. if (type === 'throw') {
  29. throw value;
  30. }
  31. throw new $SyntaxError('Completion Record is not of type "normal" or "throw": other types not supported');
  32. };
  33. CompletionRecord.prototype['!'] = function assert() {
  34. var type = SLOT.get(this, '[[Type]]');
  35. if (type !== 'normal') {
  36. throw new $SyntaxError('Assertion failed: Completion Record is not of type "normal"');
  37. }
  38. return SLOT.get(this, '[[Value]]');
  39. };
  40. module.exports = CompletionRecord;