-
Notifications
You must be signed in to change notification settings - Fork 14
/
spec.emu
83 lines (77 loc) · 3.86 KB
/
spec.emu
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!doctype html>
<meta charset="utf8">
<link rel="stylesheet" href="./spec.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/github.min.css">
<script src="./spec.js"></script>
<pre class="metadata">
title: Map.prototype.getOrInsert
stage: 2
contributors: Jonas Haukenes, Daniel Minor
</pre>
<emu-intro id="intro">
<h1>Introduction</h1>
<p>Given a `key` and a value, the `getOrInsert` method will return the existing value if it exists, or otherwise
insert the provided default value and return that value.</p>
<p>Similarly, given a `key` and a callback function, the `getOrInsert` method will return the existing value if it
exists, or otherwise insert the returned value of the callback function, and return that value.</p>
</emu-intro>
<emu-clause id="sec-map.prototype.getOrInsert">
<h1>Map.prototype.getOrInsert ( _key_, _value_ )</h1>
<p>When the getOrInsert method is called the following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[MapData]]).
1. Set _key_ to CanonicalizeKeyedCollectionKey(_key_).
1. For each Record { [[Key]], [[Value]] } _p_ of _M_.[[MapData]], do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return _p_.[[Value]].
1. Let _p_ be the Record { [[Key]]: _key_, [[Value]]: _value_ }.
1. Append _p_ to _M_.[[MapData]].
1. Return _value_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-map.prototype.getOrInsertComputed">
<h1>Map.prototype.getOrInsertComputed ( _key_, _callbackfn_ )</h1>
<p>When the getOrInsertComputed method is called the following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[MapData]]).
1. If IsCallable(_callbackfn_) is *false*, throw a *TypeError* exception.
1. Set _key_ to CanonicalizeKeyedCollectionKey(_key_).
1. For each Record { [[Key]], [[Value]] } _p_ of _M_.[[MapData]], do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return _p_.[[Value]].
1. Let _value_ be ? Call(_callbackfn_, *undefined*, « _key_ »).
1. Let _p_ be the Record { [[Key]]: _key_, [[Value]]: _value_ }.
1. Append _p_ to _M_.[[MapData]].
1. Return _value_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakmap.prototype.getOrInsert">
<h1>WeakMap.prototype.getOrInsert ( _key_, _value_ )</h1>
<p>When the getOrInsert method is called the following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
1. For each Record { [[Key]], [[Value]] } _p_ of _M_.[[WeakMapData]], do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return _p_.[[Value]].
1. Let _p_ be the Record { [[Key]]: _key_, [[Value]]: _value_ }.
1. Append _p_ to _M_.[[WeakMapData]].
1. Return _value_.
</emu-alg>
</emu-clause>
<emu-clause id="sec-weakmap.prototype.getOrInsertComputed">
<h1>WeakMap.prototype.getOrInsertComputed ( _key_, _callbackfn_ )</h1>
<p>When the getOrInsertComputed method is called the following steps are taken:</p>
<emu-alg>
1. Let _M_ be the *this* value.
1. Perform ? RequireInternalSlot(_M_, [[WeakMapData]]).
1. If IsCallable(_callbackfn_) is *false*, throw a *TypeError* exception.
1. If CanBeHeldWeakly(_key_) is *false*, throw a *TypeError* exception.
1. For each Record { [[Key]], [[Value]] } _p_ of _M_.[[WeakMapData]], do
1. If _p_.[[Key]] is not ~empty~ and SameValue(_p_.[[Key]], _key_) is *true*, return _p_.[[Value]].
1. Let _value_ be ? Call(_callbackfn_, *undefined*, « _key_ »).
1. Let _p_ be the Record { [[Key]]: _key_, [[Value]]: _value_ }.
1. Append _p_ to _M_.[[WeakMapData]].
1. Return _value_.
</emu-alg>
</emu-clause>