OrdinaryGetOwnProperty.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. var $gOPD = require('gopd');
  3. var $TypeError = require('es-errors/type');
  4. var callBound = require('call-bind/callBound');
  5. var $isEnumerable = callBound('Object.prototype.propertyIsEnumerable');
  6. var hasOwn = require('hasown');
  7. var IsArray = require('./IsArray');
  8. var IsPropertyKey = require('./IsPropertyKey');
  9. var IsRegExp = require('./IsRegExp');
  10. var ToPropertyDescriptor = require('./ToPropertyDescriptor');
  11. var Type = require('./Type');
  12. // https://262.ecma-international.org/6.0/#sec-ordinarygetownproperty
  13. module.exports = function OrdinaryGetOwnProperty(O, P) {
  14. if (Type(O) !== 'Object') {
  15. throw new $TypeError('Assertion failed: O must be an Object');
  16. }
  17. if (!IsPropertyKey(P)) {
  18. throw new $TypeError('Assertion failed: P must be a Property Key');
  19. }
  20. if (!hasOwn(O, P)) {
  21. return void 0;
  22. }
  23. if (!$gOPD) {
  24. // ES3 / IE 8 fallback
  25. var arrayLength = IsArray(O) && P === 'length';
  26. var regexLastIndex = IsRegExp(O) && P === 'lastIndex';
  27. return {
  28. '[[Configurable]]': !(arrayLength || regexLastIndex),
  29. '[[Enumerable]]': $isEnumerable(O, P),
  30. '[[Value]]': O[P],
  31. '[[Writable]]': true
  32. };
  33. }
  34. return ToPropertyDescriptor($gOPD(O, P));
  35. };