OrdinaryGetOwnProperty.js 1.2 KB

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