You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

assertRecord.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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(Type, Desc) {
  9. if (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(Type, 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(Type, value)) {
  39. throw new $TypeError(argumentName + ' must be a ' + recordType);
  40. }
  41. };