es.weak-map.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. var global = require('../internals/global');
  3. var redefineAll = require('../internals/redefine-all');
  4. var InternalMetadataModule = require('../internals/internal-metadata');
  5. var collection = require('../internals/collection');
  6. var collectionWeak = require('../internals/collection-weak');
  7. var isObject = require('../internals/is-object');
  8. var enforceIternalState = require('../internals/internal-state').enforce;
  9. var NATIVE_WEAK_MAP = require('../internals/native-weak-map');
  10. var IS_IE11 = !global.ActiveXObject && 'ActiveXObject' in global;
  11. // eslint-disable-next-line es/no-object-isextensible -- safe
  12. var isExtensible = Object.isExtensible;
  13. var InternalWeakMap;
  14. var wrapper = function (init) {
  15. return function WeakMap() {
  16. return init(this, arguments.length ? arguments[0] : undefined);
  17. };
  18. };
  19. // `WeakMap` constructor
  20. // https://tc39.es/ecma262/#sec-weakmap-constructor
  21. var $WeakMap = module.exports = collection('WeakMap', wrapper, collectionWeak);
  22. // IE11 WeakMap frozen keys fix
  23. // We can't use feature detection because it crash some old IE builds
  24. // https://github.com/zloirock/core-js/issues/485
  25. if (NATIVE_WEAK_MAP && IS_IE11) {
  26. InternalWeakMap = collectionWeak.getConstructor(wrapper, 'WeakMap', true);
  27. InternalMetadataModule.REQUIRED = true;
  28. var WeakMapPrototype = $WeakMap.prototype;
  29. var nativeDelete = WeakMapPrototype['delete'];
  30. var nativeHas = WeakMapPrototype.has;
  31. var nativeGet = WeakMapPrototype.get;
  32. var nativeSet = WeakMapPrototype.set;
  33. redefineAll(WeakMapPrototype, {
  34. 'delete': function (key) {
  35. if (isObject(key) && !isExtensible(key)) {
  36. var state = enforceIternalState(this);
  37. if (!state.frozen) state.frozen = new InternalWeakMap();
  38. return nativeDelete.call(this, key) || state.frozen['delete'](key);
  39. } return nativeDelete.call(this, key);
  40. },
  41. has: function has(key) {
  42. if (isObject(key) && !isExtensible(key)) {
  43. var state = enforceIternalState(this);
  44. if (!state.frozen) state.frozen = new InternalWeakMap();
  45. return nativeHas.call(this, key) || state.frozen.has(key);
  46. } return nativeHas.call(this, key);
  47. },
  48. get: function get(key) {
  49. if (isObject(key) && !isExtensible(key)) {
  50. var state = enforceIternalState(this);
  51. if (!state.frozen) state.frozen = new InternalWeakMap();
  52. return nativeHas.call(this, key) ? nativeGet.call(this, key) : state.frozen.get(key);
  53. } return nativeGet.call(this, key);
  54. },
  55. set: function set(key, value) {
  56. if (isObject(key) && !isExtensible(key)) {
  57. var state = enforceIternalState(this);
  58. if (!state.frozen) state.frozen = new InternalWeakMap();
  59. nativeHas.call(this, key) ? nativeSet.call(this, key, value) : state.frozen.set(key, value);
  60. } else nativeSet.call(this, key, value);
  61. return this;
  62. }
  63. });
  64. }