collection-strong.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict';
  2. var defineProperty = require('../internals/object-define-property').f;
  3. var create = require('../internals/object-create');
  4. var defineBuiltIns = require('../internals/define-built-ins');
  5. var bind = require('../internals/function-bind-context');
  6. var anInstance = require('../internals/an-instance');
  7. var isNullOrUndefined = require('../internals/is-null-or-undefined');
  8. var iterate = require('../internals/iterate');
  9. var defineIterator = require('../internals/iterator-define');
  10. var createIterResultObject = require('../internals/create-iter-result-object');
  11. var setSpecies = require('../internals/set-species');
  12. var DESCRIPTORS = require('../internals/descriptors');
  13. var fastKey = require('../internals/internal-metadata').fastKey;
  14. var InternalStateModule = require('../internals/internal-state');
  15. var setInternalState = InternalStateModule.set;
  16. var internalStateGetterFor = InternalStateModule.getterFor;
  17. module.exports = {
  18. getConstructor: function (wrapper, CONSTRUCTOR_NAME, IS_MAP, ADDER) {
  19. var Constructor = wrapper(function (that, iterable) {
  20. anInstance(that, Prototype);
  21. setInternalState(that, {
  22. type: CONSTRUCTOR_NAME,
  23. index: create(null),
  24. first: undefined,
  25. last: undefined,
  26. size: 0
  27. });
  28. if (!DESCRIPTORS) that.size = 0;
  29. if (!isNullOrUndefined(iterable)) iterate(iterable, that[ADDER], { that: that, AS_ENTRIES: IS_MAP });
  30. });
  31. var Prototype = Constructor.prototype;
  32. var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
  33. var define = function (that, key, value) {
  34. var state = getInternalState(that);
  35. var entry = getEntry(that, key);
  36. var previous, index;
  37. // change existing entry
  38. if (entry) {
  39. entry.value = value;
  40. // create new entry
  41. } else {
  42. state.last = entry = {
  43. index: index = fastKey(key, true),
  44. key: key,
  45. value: value,
  46. previous: previous = state.last,
  47. next: undefined,
  48. removed: false
  49. };
  50. if (!state.first) state.first = entry;
  51. if (previous) previous.next = entry;
  52. if (DESCRIPTORS) state.size++;
  53. else that.size++;
  54. // add to index
  55. if (index !== 'F') state.index[index] = entry;
  56. } return that;
  57. };
  58. var getEntry = function (that, key) {
  59. var state = getInternalState(that);
  60. // fast case
  61. var index = fastKey(key);
  62. var entry;
  63. if (index !== 'F') return state.index[index];
  64. // frozen object case
  65. for (entry = state.first; entry; entry = entry.next) {
  66. if (entry.key == key) return entry;
  67. }
  68. };
  69. defineBuiltIns(Prototype, {
  70. // `{ Map, Set }.prototype.clear()` methods
  71. // https://tc39.es/ecma262/#sec-map.prototype.clear
  72. // https://tc39.es/ecma262/#sec-set.prototype.clear
  73. clear: function clear() {
  74. var that = this;
  75. var state = getInternalState(that);
  76. var data = state.index;
  77. var entry = state.first;
  78. while (entry) {
  79. entry.removed = true;
  80. if (entry.previous) entry.previous = entry.previous.next = undefined;
  81. delete data[entry.index];
  82. entry = entry.next;
  83. }
  84. state.first = state.last = undefined;
  85. if (DESCRIPTORS) state.size = 0;
  86. else that.size = 0;
  87. },
  88. // `{ Map, Set }.prototype.delete(key)` methods
  89. // https://tc39.es/ecma262/#sec-map.prototype.delete
  90. // https://tc39.es/ecma262/#sec-set.prototype.delete
  91. 'delete': function (key) {
  92. var that = this;
  93. var state = getInternalState(that);
  94. var entry = getEntry(that, key);
  95. if (entry) {
  96. var next = entry.next;
  97. var prev = entry.previous;
  98. delete state.index[entry.index];
  99. entry.removed = true;
  100. if (prev) prev.next = next;
  101. if (next) next.previous = prev;
  102. if (state.first == entry) state.first = next;
  103. if (state.last == entry) state.last = prev;
  104. if (DESCRIPTORS) state.size--;
  105. else that.size--;
  106. } return !!entry;
  107. },
  108. // `{ Map, Set }.prototype.forEach(callbackfn, thisArg = undefined)` methods
  109. // https://tc39.es/ecma262/#sec-map.prototype.foreach
  110. // https://tc39.es/ecma262/#sec-set.prototype.foreach
  111. forEach: function forEach(callbackfn /* , that = undefined */) {
  112. var state = getInternalState(this);
  113. var boundFunction = bind(callbackfn, arguments.length > 1 ? arguments[1] : undefined);
  114. var entry;
  115. while (entry = entry ? entry.next : state.first) {
  116. boundFunction(entry.value, entry.key, this);
  117. // revert to the last existing entry
  118. while (entry && entry.removed) entry = entry.previous;
  119. }
  120. },
  121. // `{ Map, Set}.prototype.has(key)` methods
  122. // https://tc39.es/ecma262/#sec-map.prototype.has
  123. // https://tc39.es/ecma262/#sec-set.prototype.has
  124. has: function has(key) {
  125. return !!getEntry(this, key);
  126. }
  127. });
  128. defineBuiltIns(Prototype, IS_MAP ? {
  129. // `Map.prototype.get(key)` method
  130. // https://tc39.es/ecma262/#sec-map.prototype.get
  131. get: function get(key) {
  132. var entry = getEntry(this, key);
  133. return entry && entry.value;
  134. },
  135. // `Map.prototype.set(key, value)` method
  136. // https://tc39.es/ecma262/#sec-map.prototype.set
  137. set: function set(key, value) {
  138. return define(this, key === 0 ? 0 : key, value);
  139. }
  140. } : {
  141. // `Set.prototype.add(value)` method
  142. // https://tc39.es/ecma262/#sec-set.prototype.add
  143. add: function add(value) {
  144. return define(this, value = value === 0 ? 0 : value, value);
  145. }
  146. });
  147. if (DESCRIPTORS) defineProperty(Prototype, 'size', {
  148. get: function () {
  149. return getInternalState(this).size;
  150. }
  151. });
  152. return Constructor;
  153. },
  154. setStrong: function (Constructor, CONSTRUCTOR_NAME, IS_MAP) {
  155. var ITERATOR_NAME = CONSTRUCTOR_NAME + ' Iterator';
  156. var getInternalCollectionState = internalStateGetterFor(CONSTRUCTOR_NAME);
  157. var getInternalIteratorState = internalStateGetterFor(ITERATOR_NAME);
  158. // `{ Map, Set }.prototype.{ keys, values, entries, @@iterator }()` methods
  159. // https://tc39.es/ecma262/#sec-map.prototype.entries
  160. // https://tc39.es/ecma262/#sec-map.prototype.keys
  161. // https://tc39.es/ecma262/#sec-map.prototype.values
  162. // https://tc39.es/ecma262/#sec-map.prototype-@@iterator
  163. // https://tc39.es/ecma262/#sec-set.prototype.entries
  164. // https://tc39.es/ecma262/#sec-set.prototype.keys
  165. // https://tc39.es/ecma262/#sec-set.prototype.values
  166. // https://tc39.es/ecma262/#sec-set.prototype-@@iterator
  167. defineIterator(Constructor, CONSTRUCTOR_NAME, function (iterated, kind) {
  168. setInternalState(this, {
  169. type: ITERATOR_NAME,
  170. target: iterated,
  171. state: getInternalCollectionState(iterated),
  172. kind: kind,
  173. last: undefined
  174. });
  175. }, function () {
  176. var state = getInternalIteratorState(this);
  177. var kind = state.kind;
  178. var entry = state.last;
  179. // revert to the last existing entry
  180. while (entry && entry.removed) entry = entry.previous;
  181. // get next entry
  182. if (!state.target || !(state.last = entry = entry ? entry.next : state.state.first)) {
  183. // or finish the iteration
  184. state.target = undefined;
  185. return createIterResultObject(undefined, true);
  186. }
  187. // return step by kind
  188. if (kind == 'keys') return createIterResultObject(entry.key, false);
  189. if (kind == 'values') return createIterResultObject(entry.value, false);
  190. return createIterResultObject([entry.key, entry.value], false);
  191. }, IS_MAP ? 'entries' : 'values', !IS_MAP, true);
  192. // `{ Map, Set }.prototype[@@species]` accessors
  193. // https://tc39.es/ecma262/#sec-get-map-@@species
  194. // https://tc39.es/ecma262/#sec-get-set-@@species
  195. setSpecies(CONSTRUCTOR_NAME);
  196. }
  197. };