assertRecord.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var GetIntrinsic = require('../GetIntrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var $SyntaxError = GetIntrinsic('%SyntaxError%');
  5. var has = require('has');
  6. var predicates = {
  7. // https://ecma-international.org/ecma-262/6.0/#sec-property-descriptor-specification-type
  8. 'Property Descriptor': function isPropertyDescriptor(ES, Desc) {
  9. if (ES.Type(Desc) !== 'Object') {
  10. return false;
  11. }
  12. var allowed = {
  13. '[[Configurable]]': true,
  14. '[[Enumerable]]': true,
  15. '[[Get]]': true,
  16. '[[Set]]': true,
  17. '[[Value]]': true,
  18. '[[Writable]]': true
  19. };
  20. for (var key in Desc) { // eslint-disable-line
  21. if (has(Desc, key) && !allowed[key]) {
  22. return false;
  23. }
  24. }
  25. var isData = has(Desc, '[[Value]]');
  26. var IsAccessor = has(Desc, '[[Get]]') || has(Desc, '[[Set]]');
  27. if (isData && IsAccessor) {
  28. throw new $TypeError('Property Descriptors may not be both accessor and data descriptors');
  29. }
  30. return true;
  31. }
  32. };
  33. module.exports = function assertRecord(ES, recordType, argumentName, value) {
  34. var predicate = predicates[recordType];
  35. if (typeof predicate !== 'function') {
  36. throw new $SyntaxError('unknown record type: ' + recordType);
  37. }
  38. if (!predicate(ES, value)) {
  39. throw new $TypeError(argumentName + ' must be a ' + recordType);
  40. }
  41. console.log(predicate(ES, value), value);
  42. };