-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathobject-heap.ts
More file actions
113 lines (101 loc) · 3.75 KB
/
object-heap.ts
File metadata and controls
113 lines (101 loc) · 3.75 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
import { globalVariable } from "./find-global.js";
import { ref } from "./types.js";
const SLOT_BITS = 22;
const SLOT_MASK = (1 << SLOT_BITS) - 1;
const GEN_MASK = (1 << (32 - SLOT_BITS)) - 1;
export class JSObjectSpace {
private _slotByValue: Map<any, number>;
private _values: (any | undefined)[];
private _stateBySlot: number[];
private _freeSlotStack: number[];
constructor() {
this._slotByValue = new Map();
this._values = [];
this._stateBySlot = [];
this._freeSlotStack = [];
this._values[0] = undefined;
this._values[1] = globalVariable;
this._slotByValue.set(globalVariable, 1);
this._stateBySlot[1] = 1; // gen=0, rc=1
}
retain(value: any) {
const slot = this._slotByValue.get(value);
if (slot !== undefined) {
const state = this._stateBySlot[slot]!;
const nextState = (state + 1) >>> 0;
if ((nextState & SLOT_MASK) === 0) {
throw new RangeError(
`Reference count overflow at slot ${slot}`,
);
}
this._stateBySlot[slot] = nextState;
return ((nextState & ~SLOT_MASK) | slot) >>> 0;
}
let newSlot: number;
let state: number;
if (this._freeSlotStack.length > 0) {
newSlot = this._freeSlotStack.pop()!;
const gen = this._stateBySlot[newSlot]! >>> SLOT_BITS;
state = ((gen << SLOT_BITS) | 1) >>> 0;
} else {
newSlot = this._values.length;
if (newSlot > SLOT_MASK) {
throw new RangeError(
`Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`,
);
}
state = 1;
}
this._stateBySlot[newSlot] = state;
this._values[newSlot] = value;
this._slotByValue.set(value, newSlot);
return ((state & ~SLOT_MASK) | newSlot) >>> 0;
}
retainByRef(reference: ref) {
const state = this._getValidatedSlotState(reference);
const slot = reference & SLOT_MASK;
const nextState = (state + 1) >>> 0;
if ((nextState & SLOT_MASK) === 0) {
throw new RangeError(`Reference count overflow at slot ${slot}`);
}
this._stateBySlot[slot] = nextState;
return reference;
}
release(reference: ref) {
const state = this._getValidatedSlotState(reference);
const slot = reference & SLOT_MASK;
if ((state & SLOT_MASK) > 1) {
this._stateBySlot[slot] = (state - 1) >>> 0;
return;
}
this._slotByValue.delete(this._values[slot]);
this._values[slot] = undefined;
const nextGen = ((state >>> SLOT_BITS) + 1) & GEN_MASK;
this._stateBySlot[slot] = (nextGen << SLOT_BITS) >>> 0;
this._freeSlotStack.push(slot);
}
getObject(reference: ref) {
this._getValidatedSlotState(reference);
return this._values[reference & SLOT_MASK];
}
// Returns the packed state for the slot, after validating the reference.
private _getValidatedSlotState(reference: ref): number {
const slot = reference & SLOT_MASK;
if (slot === 0)
throw new ReferenceError(
"Attempted to use invalid reference " + reference,
);
const state = this._stateBySlot[slot];
if (state === undefined || (state & SLOT_MASK) === 0) {
throw new ReferenceError(
"Attempted to use invalid reference " + reference,
);
}
if (state >>> SLOT_BITS !== reference >>> SLOT_BITS) {
throw new ReferenceError(
"Attempted to use stale reference " + reference,
);
}
return state;
}
}