Debian Codegraph @

js/src/builtin/WeakMap.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 // ES2017 draft rev 0e10c9f29fca1385980c08a7d5e7bb3eb775e2e4
6 // 23.3.1.1 WeakMap, steps 6-8
7 function WeakMapConstructorInit(iterable) {
8 var map = this;
9
10 // Step 6.a.
11 var adder = map.set;
12
13 // Step 6.b.
14 if (!IsCallable(adder)) {
15 ThrowTypeError(JSMSG_NOT_FUNCTION, typeof adder);
16 }
17
18 // Steps 6.c-8.
19 for (var nextItem of allowContentIter(iterable)) {
20 // Step 8.d.
21 if (!IsObject(nextItem)) {
22 ThrowTypeError(JSMSG_INVALID_MAP_ITERABLE, "WeakMap");
23 }
24
25 // Steps 8.e-j.
26 callContentFunction(adder, map, nextItem[0], nextItem[1]);
27 }
28 }
29
30 #ifdef NIGHTLY_BUILD
31 /**
32 * Upsert proposal
33 *
34 * WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
35 *
36 * https://tc39.es/proposal-upsert/
37 */
38 function WeakMapGetOrInsertComputed(key, callbackfn) {
39 // Step 1. Let M be the this value.
40 var M = this;
41
42 // Step 2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
43 if (!IsObject(M) || (M = GuardToWeakMapObject(M)) === null) {
44 return callFunction(
45 CallWeakMapMethodIfWrapped,
46 this,
47 key,
48 callbackfn,
49 "WeakMapGetOrInsertComputed"
50 );
51 }
52
53 // Step 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
54 if (!IsCallable(callbackfn)) {
55 ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(1, callbackfn));
56 }
57
58 // Step 4. If CanBeHeldWeakly(key) is false, throw a TypeError exception.
59 // Step 5. For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
60 // Step 5.a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
61 if (callFunction(std_WeakMap_has, M, key)) {
62 return callFunction(std_WeakMap_get, M, key);
63 }
64
65 // Step 6. Let value be ? Call(callbackfn, undefined, « key »).
66 var value = callContentFunction(callbackfn, undefined, key);
67
68 // Step 7. For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
69 // Step 7.a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
70 // Step 7.a.i. Set p.[[Value]] to value.
71 // Step 8. Let p be the Record { [[Key]]: key, [[Value]]: value }.
72 // Step 9. Append p to M.[[WeakMapData]].
73 callFunction(std_WeakMap_set, M, key, value);
74
75 // Step 7.a.ii, 10. Return value.
76 return value;
77 }
78 #endif // #ifdef NIGHTLY_BUILD