collection-weak.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var redefineAll = require('../internals/redefine-all');
  4. var getWeakData = require('../internals/internal-metadata').getWeakData;
  5. var anObject = require('../internals/an-object');
  6. var isObject = require('../internals/is-object');
  7. var anInstance = require('../internals/an-instance');
  8. var iterate = require('../internals/iterate');
  9. var ArrayIterationModule = require('../internals/array-iteration');
  10. var hasOwn = require('../internals/has-own-property');
  11. var InternalStateModule = require('../internals/internal-state');
  12. var setInternalState = InternalStateModule.set;
  13. var internalStateGetterFor = InternalStateModule.getterFor;
  14. var find = ArrayIterationModule.find;
  15. var findIndex = ArrayIterationModule.findIndex;
  16. var splice = uncurryThis([].splice);
  17. var id = 0;
  18. // fallback for uncaught frozen keys
  19. var uncaughtFrozenStore = function (store) {
  20. return store.frozen || (store.frozen = new UncaughtFrozenStore());
  21. };
  22. var UncaughtFrozenStore = function () {
  23. this.entries = [];
  24. };
  25. var findUncaughtFrozen = function (store, key) {
  26. return find(store.entries, function (it) {
  27. return it[0] === key;
  28. });
  29. };
  30. UncaughtFrozenStore.prototype = {
  31. get: function (key) {
  32. var entry = findUncaughtFrozen(this, key);
  33. if (entry) return entry[1];
  34. },
  35. has: function (key) {
  36. return !!findUncaughtFrozen(this, key);
  37. },
  38. set: function (key, value) {
  39. var entry = findUncaughtFrozen(this, key);
  40. if (entry) entry[1] = value;
  41. else this.entries.push([key, value]);
  42. },
  43. 'delete': function (key) {
  44. var index = findIndex(this.entries, function (it) {
  45. return it[0] === key;
  46. });
  47. if (~index) splice(this.entries, index, 1);
  48. return !!~index;
  49. }
  50. };
  51. module.exports = {
  52. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  53. var Constructor = wrapper(function (that, iterable) {
  54. anInstance(that, Prototype);
  55. setInternalState(that, {
  56. type: CONSTRUCTOR_NAME,
  57. id: id++,
  58. frozen: undefined
  59. });
  60. if (iterable != undefined) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  61. });
  62. var Prototype = Constructor.prototype;
  63. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  64. var define = function (that, key, value) {
  65. var state = getInternalState(that);
  66. var data = getWeakData(anObject(key), true);
  67. if (data === true) uncaughtFrozenStore(state).set(key, value);
  68. else data[state.id] = value;
  69. return that;
  70. };
  71. redefineAll(Prototype, {
  72. // `{ WeakMap, WeakSet }.prototype.delete(key)` methods
  73. // https://tc39.es/ecma262/#sec-weakmap.prototype.delete
  74. // https://tc39.es/ecma262/#sec-weakset.prototype.delete
  75. 'delete': function (key) {
  76. var state = getInternalState(this);
  77. if (!isObject(key)) return false;
  78. var data = getWeakData(key);
  79. if (data === true) return uncaughtFrozenStore(state)['delete'](key);
  80. return data && hasOwn(data, state.id) && delete data[state.id];
  81. },
  82. // `{ WeakMap, WeakSet }.prototype.has(key)` methods
  83. // https://tc39.es/ecma262/#sec-weakmap.prototype.has
  84. // https://tc39.es/ecma262/#sec-weakset.prototype.has
  85. has: function has(key) {
  86. var state = getInternalState(this);
  87. if (!isObject(key)) return false;
  88. var data = getWeakData(key);
  89. if (data === true) return uncaughtFrozenStore(state).has(key);
  90. return data && hasOwn(data, state.id);
  91. }
  92. });
  93. redefineAll(Prototype, IS_MAP ? {
  94. // `WeakMap.prototype.get(key)` method
  95. // https://tc39.es/ecma262/#sec-weakmap.prototype.get
  96. get: function get(key) {
  97. var state = getInternalState(this);
  98. if (isObject(key)) {
  99. var data = getWeakData(key);
  100. if (data === true) return uncaughtFrozenStore(state).get(key);
  101. return data ? data[state.id] : undefined;
  102. }
  103. },
  104. // `WeakMap.prototype.set(key, value)` method
  105. // https://tc39.es/ecma262/#sec-weakmap.prototype.set
  106. set: function set(key, value) {
  107. return define(this, key, value);
  108. }
  109. } : {
  110. // `WeakSet.prototype.add(value)` method
  111. // https://tc39.es/ecma262/#sec-weakset.prototype.add
  112. add: function add(value) {
  113. return define(this, value, true);
  114. }
  115. });
  116. return Constructor;
  117. }
  118. };