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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! §6.1.5 and §20.4 — the one primitive whose identity is itself.
//!
//! Checked against V8 first. Nearly every row here is about a Symbol *not* doing something: not
//! being equal to another Symbol, not turning into text, not appearing in a list of keys. That is
//! the type — it is useful precisely because of what it refuses.
use super::*;
#[test]
fn two_symbols_are_never_equal_however_alike_they_look() {
// §7.2.12 — a Symbol is compared by identity and its description takes no part. This is the
// whole type in one row: it is what makes a Symbol a key nothing else can collide with.
assert_eq!(run("Symbol('a') === Symbol('a')"), "false");
assert_eq!(run("Symbol() == Symbol()"), "false");
assert_eq!(
run("(function () { var s = Symbol('a'); return s === s; })()"),
"true"
);
assert_eq!(
run("(function () { var s = Symbol(); return s == s; })()"),
"true"
);
assert_eq!(run("typeof Symbol()"), "symbol");
// §7.2.14 — a Symbol is equal to nothing of another type, and `==` does not throw finding out.
for other in ["1", "'a'", "null", "undefined", "true"] {
assert_eq!(run(&format!("Symbol() == {other}")), "false");
}
// …but a Symbol *wrapper* is, because steps 13 and 14 name Symbol among the types an object
// is converted for. `Object(s).valueOf()` is `s`, so the two ends meet.
assert_eq!(
run("(function () { var s = Symbol(); return Object(s) == s; })()"),
"true"
);
assert_eq!(
run("(function () { var s = Symbol(); return Object(s) === s; })()"),
"false"
);
}
#[test]
fn a_symbol_will_not_become_text_by_accident() {
// §7.1.17 step 2 and §7.1.4 step 3 — both conversions **throw**. That is the point: a Symbol
// in a template or an addition is always a mistake, and the specification says so rather than
// producing something unhelpful in the middle of a string.
for attempt in ["'' + Symbol()", "Symbol() + 1", "+Symbol()", "Symbol() * 2"] {
assert_eq!(
run(&format!(
"(function () {{ try {{ return {attempt}; }} \
catch (e) {{ return e.constructor.name; }} }})()"
)),
"TypeError"
);
}
// §22.1.1.1 step 2 is the one door out, and `Symbol.prototype.toString` is the other. Both
// have to be asked for by name.
assert_eq!(run("String(Symbol('a'))"), "Symbol(a)");
assert_eq!(run("Symbol('a').toString()"), "Symbol(a)");
// A Symbol with no description and one described as the empty String print alike and are not
// the same thing — which is what `description` keeps and `toString` throws away.
assert_eq!(run("Symbol().toString()"), "Symbol()");
assert_eq!(run("Symbol('').toString()"), "Symbol()");
assert_eq!(run("typeof Symbol().description"), "undefined");
assert_eq!(run("Symbol('').description === ''"), "true");
assert_eq!(run("Symbol('a').description"), "a");
// §7.1.2 — always true, description or not. There is no empty Symbol for a rule to be about.
assert_eq!(run("Boolean(Symbol())"), "true");
assert_eq!(run("!Symbol()"), "false");
}
#[test]
fn a_symbol_key_is_a_property_that_no_listing_of_names_shows() {
assert_eq!(
run("(function () { var o = {}, s = Symbol('k'); o[s] = 1; return o[s]; })()"),
"1"
);
assert_eq!(
run("(function () { var o = {}, s = Symbol('k'); o[s] = 1; return s in o; })()"),
"true"
);
assert_eq!(
run("(function () { var o = {}, s = Symbol('k'); o[s] = 1; return delete o[s]; })()"),
"true"
);
// Not hidden — `getOwnPropertySymbols` lists it — but not among the *names*, and not walked
// by `for`-`in` or `Object.keys`. Every operation that lists keys picks one of the two lists.
let hidden = "(function () { var o = {}, s = Symbol('k'); o[s] = 1; return ";
assert_eq!(run(&format!("{hidden}Object.keys(o).length; }})()")), "0");
assert_eq!(
run(&format!(
"{hidden}Object.getOwnPropertyNames(o).length; }})()"
)),
"0"
);
assert_eq!(
run(&format!(
"{hidden}Object.getOwnPropertySymbols(o).length; }})()"
)),
"1"
);
assert_eq!(
run(&format!(
"{hidden}Object.getOwnPropertySymbols(o)[0] === s; }})()"
)),
"true"
);
assert_eq!(
run(
"(function () { var o = {}, s = Symbol('k'); o[s] = 1; var r = ''; \
for (var k in o) { r += k; } return r; })()"
),
""
);
// …and `getOwnPropertyDescriptor` answers about it, because that one is asked about a key
// rather than for a list. `ToPropertyKey` of a Symbol is the Symbol, never its text.
assert_eq!(
run("(function () { var s = Symbol('k'); var o = {}; o[s] = 1; \
return Object.getOwnPropertyDescriptor(o, s).value; })()"),
"1"
);
// §10.1.11 step 4 — Symbol keys come after every String key, whatever order they were added.
assert_eq!(
run(
"(function () { var o = {a: 1}; var s = Symbol('s'); o[s] = 2; o.b = 3; o[1] = 4; \
return Object.getOwnPropertyNames(o).join(','); })()"
),
"1,a,b"
);
// §20.1.2.1 copies a Symbol-keyed property, because `assign` walks own *enumerable* keys and
// that is a question about the attribute rather than about the kind of key.
assert_eq!(
run("(function () { var o = {}; var s = Symbol(); o[s] = 1; \
return Object.assign({}, o)[s]; })()"),
"1"
);
}
#[test]
fn the_registry_is_the_one_way_to_ask_for_a_symbol_that_already_exists() {
assert_eq!(run("Symbol.for('k') === Symbol.for('k')"), "true");
assert_eq!(run("Symbol.for('') === Symbol.for('')"), "true");
assert_eq!(run("Symbol.for('a') === Symbol.for('b')"), "false");
assert_eq!(run("Symbol.for('k') === Symbol('k')"), "false");
assert_eq!(run("Symbol.keyFor(Symbol.for('k'))"), "k");
// §20.4.2.7 — `undefined` for a Symbol that was not registered, which is the only way to tell
// a registered Symbol from an ordinary one with the same description.
assert_eq!(run("typeof Symbol.keyFor(Symbol('k'))"), "undefined");
// The key is `ToString`ed, so `Symbol.for(1)` and `Symbol.for("1")` are one Symbol.
assert_eq!(run("Symbol.for(1) === Symbol.for('1')"), "true");
assert_eq!(run("Symbol.keyFor(Symbol.for('1'))"), "1");
}
#[test]
fn the_well_known_symbols_exist_and_are_the_same_ones_every_time() {
for name in [
"asyncIterator",
"hasInstance",
"isConcatSpreadable",
"iterator",
"match",
"matchAll",
"replace",
"search",
"species",
"split",
"toPrimitive",
"toStringTag",
"unscopables",
] {
assert_eq!(run(&format!("typeof Symbol.{name}")), "symbol");
assert_eq!(run(&format!("Symbol.{name} === Symbol.{name}")), "true");
}
// §6.1.5.1 gives each a description, which is how one shows up in a message.
assert_eq!(run("String(Symbol.iterator)"), "Symbol(Symbol.iterator)");
// §20.4.2 — not writable and not configurable, so a script cannot move what the engine will
// reach for when `for`-`of` arrives.
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol, 'iterator').writable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol, 'iterator').configurable"),
"false"
);
// …and none of them is in the registry: a well-known Symbol is not one `Symbol.for` made.
assert_eq!(run("typeof Symbol.keyFor(Symbol.iterator)"), "undefined");
}
#[test]
fn to_string_tag_renames_what_object_prototype_to_string_reports() {
// §20.1.3.6 step 15 — the first well-known Symbol ViperJS acts on, and the supported way for a
// script to name its own type here.
assert_eq!(
run(
"(function () { var o = {}; o[Symbol.toStringTag] = 'Mine'; \
return Object.prototype.toString.call(o); })()"
),
"[object Mine]"
);
// A tag that is not a String is ignored, and the table's answer stands.
assert_eq!(
run("(function () { var o = {}; o[Symbol.toStringTag] = 5; \
return Object.prototype.toString.call(o); })()"),
"[object Object]"
);
// A get and not an own-property read, so an inherited tag counts — which is how one property
// on `Symbol.prototype` tags every Symbol without anything being put on each of them.
assert_eq!(
run(
"(function () { function F() {} F.prototype[Symbol.toStringTag] = 'F'; \
return Object.prototype.toString.call(new F()); })()"
),
"[object F]"
);
assert_eq!(
run("Object.prototype.toString.call(Symbol())"),
"[object Symbol]"
);
// …which is a property on `Symbol.prototype` and not a row in the table, so it has the three
// attributes §20.4.3.5 gives it — and being *configurable* is the one that shows: deleting it
// makes a Symbol tag as an ordinary object again.
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, Symbol.toStringTag).value"),
"Symbol"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, Symbol.toStringTag).writable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, Symbol.toStringTag).enumerable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, Symbol.toStringTag).configurable"),
"true"
);
assert_eq!(
run(
"(function () { delete Symbol.prototype[Symbol.toStringTag]; return Object.prototype.toString.call(Symbol()); })()"
),
"[object Object]"
);
// …and the rows that were already there still answer from the table.
assert_eq!(run("Object.prototype.toString.call({})"), "[object Object]");
assert_eq!(run("Object.prototype.toString.call([])"), "[object Array]");
assert_eq!(run("Object.prototype.toString.call(null)"), "[object Null]");
}
#[test]
fn symbol_is_the_one_constructor_that_refuses_itself() {
// §20.4.1 step 1. A Symbol wrapper would be an object, and an object is equal to nothing but
// itself — so `new Symbol("a")` would look like a Symbol and silently fail to be usable as
// the key it was made to be. The specification refuses rather than hand that back.
assert_eq!(
run("(function () { try { new Symbol(); return 'ok'; } \
catch (e) { return e.constructor.name; } })()"),
"TypeError"
);
// §20.4.3.2 — `description` is an accessor, not enumerable and *configurable*: a script may
// delete it, which is the one of the three attributes that surprises.
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, 'description').enumerable"),
"false"
);
assert_eq!(
run("Object.getOwnPropertyDescriptor(Symbol.prototype, 'description').configurable"),
"true"
);
assert_eq!(
run("typeof Object.getOwnPropertyDescriptor(Symbol.prototype, 'description').get"),
"function"
);
// The wrapper still exists, because `ToObject` has to answer something. It just cannot be
// asked for directly.
assert_eq!(run("typeof Object(Symbol())"), "object");
assert_eq!(run("Object(Symbol('a')).description"), "a");
assert_eq!(
run("(function () { var s = Symbol(); \
return Symbol.prototype.valueOf.call(Object(s)) === s; })()"),
"true"
);
// `thisSymbolValue` refuses anything else, which is what keeps these two methods honest.
assert_eq!(
run(
"(function () { try { return Symbol.prototype.toString.call('x'); } \
catch (e) { return e.constructor.name; } })()"
),
"TypeError"
);
assert_eq!(
run(
"(function () { try { return Symbol.prototype.description; } \
catch (e) { return e.constructor.name; } })()"
),
"TypeError"
);
}