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