implementation.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var IsCallable = require('es-abstract/2023/IsCallable');
  4. var Type = require('es-abstract/2023/Type');
  5. var whichBuiltinType = require('which-builtin-type');
  6. var $TypeError = require('es-errors/type');
  7. var $gPO = GetIntrinsic('%Object.getPrototypeOf%', true);
  8. var $ObjectPrototype = GetIntrinsic('%Object.prototype%');
  9. var hasProto = [].__proto__ === Array.prototype; // eslint-disable-line no-proto
  10. module.exports = function getPrototypeOf(O) {
  11. if (Type(O) !== 'Object') {
  12. throw new $TypeError('Reflect.getPrototypeOf called on non-object');
  13. }
  14. if ($gPO) {
  15. return $gPO(O);
  16. }
  17. if (hasProto) {
  18. // eslint-disable-next-line no-proto
  19. var proto = O.__proto__;
  20. if (proto || proto === null) {
  21. return proto;
  22. }
  23. }
  24. var type = whichBuiltinType(O);
  25. if (type) {
  26. var intrinsic = GetIntrinsic('%' + type + '.prototype%', true);
  27. if (intrinsic) {
  28. return intrinsic;
  29. }
  30. }
  31. if (IsCallable(O.constructor)) {
  32. return O.constructor.prototype;
  33. }
  34. if (O instanceof Object) {
  35. return $ObjectPrototype;
  36. }
  37. /*
  38. * Correctly return null for Objects created with `Object.create(null)` (shammed or native) or `{ __proto__: null}`. Also returns null for
  39. * cross-realm objects on browsers that lack `__proto__` support (like IE <11), but that's the best we can do.
  40. */
  41. return null;
  42. };