-
Notifications
You must be signed in to change notification settings - Fork 0
/
return-override-weakmap.js
109 lines (104 loc) · 2 KB
/
return-override-weakmap.js
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
class Trojan {
constructor(key) {
return key;
}
}
const makePrivateTagger = () => {
const tombstone = Symbol('deleted');
return class PrivateTagger extends Trojan {
#value
constructor(key, value) {
super(key);
this.#value = value;
}
/**
* @param {any} key
* @returns {boolean}
*/
static has(key) {
try {
return key.#value !== tombstone;
} catch {
return false;
}
}
/**
* @param {PrivateTagger} key
*/
static get(key) {
try {
const value = key.#value;
return value === tombstone ? undefined : value;
} catch {
return undefined;
}
}
/**
* @param {PrivateTagger} key
* @param {any} value
*/
static set(key, value) {
new PrivateTagger(key, value);
}
/**
* @param {PrivateTagger} key
*/
static delete(key) {
try {
key.#value = tombstone;
} catch {}
}
};
}
/**
* A `WeakMap`-like abstraction built using the `class` syntax and its support
* for the return-override-mistake.
*
* Because the browser global WindowProxy object is exempt from the
* return-override-mistake by special dispensation, currently, that object
* alone cannot be used as a key in a `WeakishMap`.
*
* @template {object} K
* @template {object} V
*/
export class WeakishMap {
#tagger
constructor() {
this.#tagger = makePrivateTagger();
}
/**
* @param {any} key
* @returns {boolean}
*/
has(key) {
return this.#tagger.has(key);
}
/**
* @param {K} key
* @returns {V}
*/
get(key) {
return this.#tagger.get(key);
}
/**
* @param {K} key
* @param {V} value
*/
set(key, value) {
this.#tagger.set(key, value);
}
/**
* @param {K} key
*/
delete(key) {
return this.#tagger.delete(key);
}
}
Object.freeze(Object.prototype);
function Point(x, y) {
this.x = y; this.y = y;
}
Point.prototype.toString =
function () {
return `<${this.x},${this.y}`;
};