FromPropertyDescriptor.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var Type = require('./Type');
  5. var IsDataDescriptor = require('./IsDataDescriptor');
  6. var IsAccessorDescriptor = require('./IsAccessorDescriptor');
  7. var assertRecord = require('../helpers/assertRecord');
  8. // https://262.ecma-international.org/5.1/#sec-8.10.4
  9. module.exports = function FromPropertyDescriptor(Desc) {
  10. if (typeof Desc === 'undefined') {
  11. return Desc;
  12. }
  13. assertRecord(Type, 'Property Descriptor', 'Desc', Desc);
  14. if (IsDataDescriptor(Desc)) {
  15. return {
  16. value: Desc['[[Value]]'],
  17. writable: !!Desc['[[Writable]]'],
  18. enumerable: !!Desc['[[Enumerable]]'],
  19. configurable: !!Desc['[[Configurable]]']
  20. };
  21. } else if (IsAccessorDescriptor(Desc)) {
  22. return {
  23. get: Desc['[[Get]]'],
  24. set: Desc['[[Set]]'],
  25. enumerable: !!Desc['[[Enumerable]]'],
  26. configurable: !!Desc['[[Configurable]]']
  27. };
  28. }
  29. throw new $TypeError('FromPropertyDescriptor must be called with a fully populated Property Descriptor');
  30. };