-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathsymbol.ts
More file actions
114 lines (91 loc) · 3.1 KB
/
symbol.ts
File metadata and controls
114 lines (91 loc) · 3.1 KB
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
import { Map } from "./map";
// @ts-ignore: decorator
@lazy let stringToId: Map<string, usize> = new Map();
// @ts-ignore: decorator
@lazy let idToString: Map<usize, string> = new Map();
// @ts-ignore: decorator
@lazy let nextId: usize = 12; // Symbol.unscopables + 1
@unmanaged @final abstract class _Symbol {
// TODO: all of the following default symbols are unused currently yet add to
// binary size if #toString becomes compiled. Ultimately we'll most likely want
// to remove the unsupported ones and only keep what's actually supported.
// @ts-ignore: decorator
@lazy
static readonly hasInstance: symbol = changetype<symbol>(1);
// @ts-ignore: decorator
@lazy
static readonly isConcatSpreadable: symbol = changetype<symbol>(2);
// @ts-ignore: decorator
@lazy
static readonly isRegExp: symbol = changetype<symbol>(3);
// @ts-ignore: decorator
@lazy
static readonly iterator: symbol = changetype<symbol>(3);
// @ts-ignore: decorator
@lazy
static readonly match: symbol = changetype<symbol>(4);
// @ts-ignore: decorator
@lazy
static readonly replace: symbol = changetype<symbol>(5);
// @ts-ignore: decorator
@lazy
static readonly search: symbol = changetype<symbol>(6);
// @ts-ignore: decorator
@lazy
static readonly species: symbol = changetype<symbol>(7);
// @ts-ignore: decorator
@lazy
static readonly split: symbol = changetype<symbol>(8);
// @ts-ignore: decorator
@lazy
static readonly toPrimitive: symbol = changetype<symbol>(9);
// @ts-ignore: decorator
@lazy
static readonly toStringTag: symbol = changetype<symbol>(10);
// @ts-ignore: decorator
@lazy
static readonly unscopables: symbol = changetype<symbol>(11);
static for(key: string): symbol {
if (stringToId.has(key)) return changetype<symbol>(stringToId.get(key));
let id = nextId++;
if (!id) unreachable(); // out of ids
stringToId.set(key, id);
idToString.set(id, key);
return changetype<symbol>(id);
}
static keyFor(sym: symbol): string | null {
return idToString.has(changetype<usize>(sym))
? idToString.get(changetype<usize>(sym))
: null;
}
toString(): string {
let id = changetype<usize>(this);
let str = "";
switch (<u32>id) {
case 1: { str = "hasInstance"; break; }
case 2: { str = "isConcatSpreadable"; break; }
case 3: { str = "isRegExp"; break; }
case 4: { str = "match"; break; }
case 5: { str = "replace"; break; }
case 6: { str = "search"; break; }
case 7: { str = "species"; break; }
case 8: { str = "split"; break; }
case 9: { str = "toPrimitive"; break; }
case 10: { str = "toStringTag"; break; }
case 11: { str = "unscopables"; break; }
default: {
if (idToString != null && idToString.has(id)) str = idToString.get(id);
break;
}
}
return "Symbol(" + str + ")";
}
}
export function Symbol(description: string | null = null): symbol {
let id = nextId++;
if (!id) unreachable(); // out of ids
return changetype<symbol>(id);
}
export type Symbol = _Symbol;
// @ts-ignore: nolib
export type symbol = _Symbol;