tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WeakMap.js (2520B)


      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 /**
     31 * Upsert proposal
     32 *
     33 * WeakMap.prototype.getOrInsertComputed ( key, callbackfn )
     34 *
     35 * https://tc39.es/proposal-upsert/
     36 */
     37 function WeakMapGetOrInsertComputed(key, callbackfn) {
     38  // Step 1.  Let M be the this value.
     39  var M = this;
     40 
     41  // Step 2.  Perform ? RequireInternalSlot(M, [[WeakMapData]]).
     42  if (!IsObject(M) || (M = GuardToWeakMapObject(M)) === null) {
     43    return callFunction(
     44      CallWeakMapMethodIfWrapped,
     45      this,
     46      key,
     47      callbackfn,
     48      "WeakMapGetOrInsertComputed"
     49    );
     50  }
     51 
     52  // Step 3.  If IsCallable(callbackfn) is false, throw a TypeError exception.
     53  if (!IsCallable(callbackfn)) {
     54    ThrowTypeError(JSMSG_NOT_FUNCTION, DecompileArg(1, callbackfn));
     55  }
     56 
     57  // Step 4.  If CanBeHeldWeakly(key) is false, throw a TypeError exception.
     58  if (!CanBeHeldWeakly(key)) {
     59    ThrowTypeError(JSMSG_WEAKMAP_KEY_CANT_BE_HELD_WEAKLY, DecompileArg(0, key));
     60  }
     61 
     62  // Step 5.  For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
     63  // Step 5.a.  If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
     64  if (callFunction(std_WeakMap_has, M, key)) {
     65    return callFunction(std_WeakMap_get, M, key);
     66  }
     67 
     68  // Step 6.  Let value be ? Call(callbackfn, undefined, « key »).
     69  var value = callContentFunction(callbackfn, undefined, key);
     70 
     71  // Step 7.  For each Record { [[Key]], [[Value]] } p of M.[[WeakMapData]], do
     72  // Step 7.a.  If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
     73  // Step 7.a.i.  Set p.[[Value]] to value.
     74  // Step 8.  Let p be the Record { [[Key]]: key, [[Value]]: value }.
     75  // Step 9.  Append p to M.[[WeakMapData]].
     76  callFunction(std_WeakMap_set, M, key, value);
     77 
     78  // Step 7.a.ii, 10. Return value.
     79  return value;
     80 }