CompletionRecord.js 1.5 KB

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