set.js 1.1 KB

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. var callBind = require('call-bind-apply-helpers');
  3. var gOPD = require('gopd');
  4. var $TypeError = require('es-errors/type');
  5. /** @type {{ __proto__?: object | null }} */
  6. var obj = {};
  7. obj.__proto__ = null; // eslint-disable-line no-proto
  8. var hasProtoMutator = !('toString' in obj);
  9. // eslint-disable-next-line no-extra-parens
  10. var desc = gOPD && gOPD(Object.prototype, /** @type {keyof typeof Object.prototype} */ ('__proto__'));
  11. /** @type {import('./set')} */
  12. module.exports = hasProtoMutator && (
  13. // eslint-disable-next-line no-extra-parens
  14. (!!desc && typeof desc.set === 'function' && /** @type {import('./set')} */ (callBind([desc.set])))
  15. || /** @type {import('./set')} */ function setDunder(object, proto) {
  16. // this is node v0.10 or older, which doesn't have Object.setPrototypeOf and has undeniable __proto__
  17. if (object == null) { // eslint-disable-line eqeqeq
  18. throw new $TypeError('set Object.prototype.__proto__ called on null or undefined');
  19. }
  20. // eslint-disable-next-line no-proto, no-param-reassign, no-extra-parens
  21. /** @type {{ __proto__?: object | null }} */ (object).__proto__ = proto;
  22. return proto;
  23. }
  24. );