es.weak-map.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. var isExtensible = Object.isExtensible;
  12. var InternalWeakMap;
  13. var wrapper = function (init) {
  14. return function WeakMap() {
  15. return init(this, arguments.length ? arguments[0] : undefined);
  16. };
  17. };
  18. // `WeakMap` constructor
  19. // https://tc39.github.io/ecma262/#sec-weakmap-constructor
  20. var $WeakMap = module.exports = collection('WeakMap', wrapper, collectionWeak);
  21. // IE11 WeakMap frozen keys fix
  22. // We can't use feature detection because it crash some old IE builds
  23. // https://github.com/zloirock/core-js/issues/485
  24. if (NATIVE_WEAK_MAP && IS_IE11) {
  25. InternalWeakMap = collectionWeak.getConstructor(wrapper, 'WeakMap', true);
  26. InternalMetadataModule.REQUIRED = true;
  27. var WeakMapPrototype = $WeakMap.prototype;
  28. var nativeDelete = WeakMapPrototype['delete'];
  29. var nativeHas = WeakMapPrototype.has;
  30. var nativeGet = WeakMapPrototype.get;
  31. var nativeSet = WeakMapPrototype.set;
  32. redefineAll(WeakMapPrototype, {
  33. 'delete': function (key) {
  34. if (isObject(key) && !isExtensible(key)) {
  35. var state = enforceIternalState(this);
  36. if (!state.frozen) state.frozen = new InternalWeakMap();
  37. return nativeDelete.call(this, key) || state.frozen['delete'](key);
  38. } return nativeDelete.call(this, key);
  39. },
  40. has: function has(key) {
  41. if (isObject(key) && !isExtensible(key)) {
  42. var state = enforceIternalState(this);
  43. if (!state.frozen) state.frozen = new InternalWeakMap();
  44. return nativeHas.call(this, key) || state.frozen.has(key);
  45. } return nativeHas.call(this, key);
  46. },
  47. get: function get(key) {
  48. if (isObject(key) && !isExtensible(key)) {
  49. var state = enforceIternalState(this);
  50. if (!state.frozen) state.frozen = new InternalWeakMap();
  51. return nativeHas.call(this, key) ? nativeGet.call(this, key) : state.frozen.get(key);
  52. } return nativeGet.call(this, key);
  53. },
  54. set: function set(key, value) {
  55. if (isObject(key) && !isExtensible(key)) {
  56. var state = enforceIternalState(this);
  57. if (!state.frozen) state.frozen = new InternalWeakMap();
  58. nativeHas.call(this, key) ? nativeSet.call(this, key, value) : state.frozen.set(key, value);
  59. } else nativeSet.call(this, key, value);
  60. return this;
  61. }
  62. });
  63. }