-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.js
More file actions
34 lines (24 loc) · 940 Bytes
/
implementation.js
File metadata and controls
34 lines (24 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
var callBound = require('call-bound');
var $SyntaxError = require('es-errors/syntax');
var $TypeError = require('es-errors/type');
var CanBeHeldWeakly = require('es-abstract/2025/CanBeHeldWeakly');
var $weakMapHas = callBound('WeakMap.prototype.has', true);
var $weakMapGet = callBound('WeakMap.prototype.get', true);
var $weakMapSet = callBound('WeakMap.prototype.set', true);
module.exports = function getOrInsert(key, value) {
if (!$weakMapHas) {
throw new $SyntaxError('`getOrInsert` is not supported unless a native WeakMap exists');
}
var M = this; // step 1
// 2. Perform ? RequireInternalSlot(M, [[WeakMapData]]).
$weakMapHas(M, M); // step 2
if (!CanBeHeldWeakly(key)) {
throw new $TypeError('Key can not be held weakly: ' + key); // step 3
}
if ($weakMapHas(M, key)) { // step 4
return $weakMapGet(M, key); // step 4.a
}
$weakMapSet(M, key, value); // step 5, 6
return value; // step 7
};