es.weak-map.constructor.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var FREEZING = require('../internals/freezing');
  3. var globalThis = require('../internals/global-this');
  4. var uncurryThis = require('../internals/function-uncurry-this');
  5. var defineBuiltIns = require('../internals/define-built-ins');
  6. var InternalMetadataModule = require('../internals/internal-metadata');
  7. var collection = require('../internals/collection');
  8. var collectionWeak = require('../internals/collection-weak');
  9. var isObject = require('../internals/is-object');
  10. var enforceInternalState = require('../internals/internal-state').enforce;
  11. var fails = require('../internals/fails');
  12. var NATIVE_WEAK_MAP = require('../internals/weak-map-basic-detection');
  13. var $Object = Object;
  14. // eslint-disable-next-line es/no-array-isarray -- safe
  15. var isArray = Array.isArray;
  16. // eslint-disable-next-line es/no-object-isextensible -- safe
  17. var isExtensible = $Object.isExtensible;
  18. // eslint-disable-next-line es/no-object-isfrozen -- safe
  19. var isFrozen = $Object.isFrozen;
  20. // eslint-disable-next-line es/no-object-issealed -- safe
  21. var isSealed = $Object.isSealed;
  22. // eslint-disable-next-line es/no-object-freeze -- safe
  23. var freeze = $Object.freeze;
  24. // eslint-disable-next-line es/no-object-seal -- safe
  25. var seal = $Object.seal;
  26. var IS_IE11 = !globalThis.ActiveXObject && 'ActiveXObject' in globalThis;
  27. var InternalWeakMap;
  28. var wrapper = function (init) {
  29. return function WeakMap() {
  30. return init(this, arguments.length ? arguments[0] : undefined);
  31. };
  32. };
  33. // `WeakMap` constructor
  34. // https://tc39.es/ecma262/#sec-weakmap-constructor
  35. var $WeakMap = collection('WeakMap', wrapper, collectionWeak);
  36. var WeakMapPrototype = $WeakMap.prototype;
  37. var nativeSet = uncurryThis(WeakMapPrototype.set);
  38. // Chakra Edge bug: adding frozen arrays to WeakMap unfreeze them
  39. var hasMSEdgeFreezingBug = function () {
  40. return FREEZING && fails(function () {
  41. var frozenArray = freeze([]);
  42. nativeSet(new $WeakMap(), frozenArray, 1);
  43. return !isFrozen(frozenArray);
  44. });
  45. };
  46. // IE11 WeakMap frozen keys fix
  47. // We can't use feature detection because it crash some old IE builds
  48. // https://github.com/zloirock/core-js/issues/485
  49. if (NATIVE_WEAK_MAP) if (IS_IE11) {
  50. InternalWeakMap = collectionWeak.getConstructor(wrapper, 'WeakMap', true);
  51. InternalMetadataModule.enable();
  52. var nativeDelete = uncurryThis(WeakMapPrototype['delete']);
  53. var nativeHas = uncurryThis(WeakMapPrototype.has);
  54. var nativeGet = uncurryThis(WeakMapPrototype.get);
  55. defineBuiltIns(WeakMapPrototype, {
  56. 'delete': function (key) {
  57. if (isObject(key) && !isExtensible(key)) {
  58. var state = enforceInternalState(this);
  59. if (!state.frozen) state.frozen = new InternalWeakMap();
  60. return nativeDelete(this, key) || state.frozen['delete'](key);
  61. } return nativeDelete(this, key);
  62. },
  63. has: function has(key) {
  64. if (isObject(key) && !isExtensible(key)) {
  65. var state = enforceInternalState(this);
  66. if (!state.frozen) state.frozen = new InternalWeakMap();
  67. return nativeHas(this, key) || state.frozen.has(key);
  68. } return nativeHas(this, key);
  69. },
  70. get: function get(key) {
  71. if (isObject(key) && !isExtensible(key)) {
  72. var state = enforceInternalState(this);
  73. if (!state.frozen) state.frozen = new InternalWeakMap();
  74. return nativeHas(this, key) ? nativeGet(this, key) : state.frozen.get(key);
  75. } return nativeGet(this, key);
  76. },
  77. set: function set(key, value) {
  78. if (isObject(key) && !isExtensible(key)) {
  79. var state = enforceInternalState(this);
  80. if (!state.frozen) state.frozen = new InternalWeakMap();
  81. nativeHas(this, key) ? nativeSet(this, key, value) : state.frozen.set(key, value);
  82. } else nativeSet(this, key, value);
  83. return this;
  84. }
  85. });
  86. // Chakra Edge frozen keys fix
  87. } else if (hasMSEdgeFreezingBug()) {
  88. defineBuiltIns(WeakMapPrototype, {
  89. set: function set(key, value) {
  90. var arrayIntegrityLevel;
  91. if (isArray(key)) {
  92. if (isFrozen(key)) arrayIntegrityLevel = freeze;
  93. else if (isSealed(key)) arrayIntegrityLevel = seal;
  94. }
  95. nativeSet(this, key, value);
  96. if (arrayIntegrityLevel) arrayIntegrityLevel(key);
  97. return this;
  98. }
  99. });
  100. }