Skip to content

Commit e45f7a4

Browse files
committed
read slides
1 parent eedae38 commit e45f7a4

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class ReturnOverrideClass {
2+
constructor(key) { return key; }
3+
}
4+
5+
class HiddenWeakMap extends ReturnOverrideClass {
6+
#value;
7+
8+
constructor(key, value) {
9+
super(key);
10+
this.#value = value;
11+
}
12+
13+
static get(hwm) {
14+
return hwn.#value;
15+
}
16+
}
17+
harden(HiddenWeakMap);
18+
19+
// HardenedJS builtins all frozen
20+
new HiddenWeakMap(Object, Date);
21+
22+
// in separate compartment
23+
HiddenWeakMap.get(Object); // Date or not.
24+
// Where's the mutability?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Trojan {
2+
constructor(key) {
3+
return key;
4+
}
5+
}
6+
7+
const makeHiddenWeakMap = () => {
8+
const tombstone = Symbol('deleted');
9+
class PrivateTagger extends Trojan {
10+
#value;
11+
constructor(key, value) {
12+
super(key);
13+
this.#value = value;
14+
}
15+
16+
static HiddenWeakMap = class {
17+
set(key, value) { new PrivateTagger(key, value); }
18+
19+
has(key) {
20+
try {
21+
return key.#value !== tombstone;
22+
} catch { return false; }
23+
}
24+
get(key) {
25+
try {
26+
const value = key.#value;
27+
return value === tombstone ? undefined : value;
28+
} catch { return undefined; }
29+
}
30+
delete(key) {
31+
if (this.has(key)) {
32+
key.#value = tombstone;
33+
return true;
34+
}
35+
return false;
36+
}
37+
}
38+
};
39+
return PrivateTagger.HiddenWeakMap;
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Trojan {
2+
constructor(key) { return key; }
3+
}
4+
5+
class Tagger extends Trojan {
6+
#value;
7+
8+
constructor(key, value) {
9+
super(key);
10+
this.#value = value;
11+
}
12+
}
13+
14+
// struct instances born sealed
15+
new Tagger(struct, 'a'); // adds #value to struct anyway
1.03 MB
Binary file not shown.
594 KB
Binary file not shown.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class NonExtensibleBase {
2+
constructor() {
3+
Object.preventExtensions(this);
4+
}
5+
}
6+
7+
class ClassWithPrivateField
8+
extends NonExtensibleBase {
9+
#val;
10+
11+
constructor(v) {
12+
super();
13+
this.#val = v;
14+
}
15+
}
16+
17+
new ClassWithPrivateField(42); // doesn't throw
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class FrozenBase {
2+
constructor() {
3+
Object.freeze(this);
4+
}
5+
}
6+
7+
class AddsProperty extends FrozenBase {
8+
_val;
9+
10+
constructor(v) {
11+
super();
12+
this._val = v;
13+
}
14+
}
15+
16+
// throws TypeError
17+
new AddsProperty(42);
18+
19+
class AddsPrivateField extends FrozenBase {
20+
#val;
21+
22+
constructor(v) {
23+
super();
24+
this.#val = v;
25+
}
26+
}
27+
28+
// doesn’t throw
29+
new AddsPrivateField(42);

0 commit comments

Comments
 (0)