es.weak-map.constructor.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. var global = require('../internals/global');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var redefineAll = require('../internals/redefine-all');
  5. var InternalMetadataModule = require('../internals/internal-metadata');
  6. var collection = require('../internals/collection');
  7. var collectionWeak = require('../internals/collection-weak');
  8. var isObject = require('../internals/is-object');
  9. var isExtensible = require('../internals/object-is-extensible');
  10. var enforceInternalState = require('../internals/internal-state').enforce;
  11. var NATIVE_WEAK_MAP = require('../internals/native-weak-map');
  12. var IS_IE11 = !global.ActiveXObject && 'ActiveXObject' in global;
  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 = 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.enable();
  28. var WeakMapPrototype = $WeakMap.prototype;
  29. var nativeDelete = uncurryThis(WeakMapPrototype['delete']);
  30. var nativeHas = uncurryThis(WeakMapPrototype.has);
  31. var nativeGet = uncurryThis(WeakMapPrototype.get);
  32. var nativeSet = uncurryThis(WeakMapPrototype.set);
  33. redefineAll(WeakMapPrototype, {
  34. 'delete': function (key) {
  35. if (isObject(key) && !isExtensible(key)) {
  36. var state = enforceInternalState(this);
  37. if (!state.frozen) state.frozen = new InternalWeakMap();
  38. return nativeDelete(this, key) || state.frozen['delete'](key);
  39. } return nativeDelete(this, key);
  40. },
  41. has: function has(key) {
  42. if (isObject(key) && !isExtensible(key)) {
  43. var state = enforceInternalState(this);
  44. if (!state.frozen) state.frozen = new InternalWeakMap();
  45. return nativeHas(this, key) || state.frozen.has(key);
  46. } return nativeHas(this, key);
  47. },
  48. get: function get(key) {
  49. if (isObject(key) && !isExtensible(key)) {
  50. var state = enforceInternalState(this);
  51. if (!state.frozen) state.frozen = new InternalWeakMap();
  52. return nativeHas(this, key) ? nativeGet(this, key) : state.frozen.get(key);
  53. } return nativeGet(this, key);
  54. },
  55. set: function set(key, value) {
  56. if (isObject(key) && !isExtensible(key)) {
  57. var state = enforceInternalState(this);
  58. if (!state.frozen) state.frozen = new InternalWeakMap();
  59. nativeHas(this, key) ? nativeSet(this, key, value) : state.frozen.set(key, value);
  60. } else nativeSet(this, key, value);
  61. return this;
  62. }
  63. });
  64. }