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
|
function decodeString(base, length) {
const slice = new Uint8Array(wasm.memory.buffer, base, length);
return new TextDecoder().decode(slice);
}
class ObjectStore {
static id_name = "+-store-id";
static types = Object.freeze({
0: "uninit",
1: "object",
2: "string",
3: "int",
4: "uint",
5: "float",
6: "undefined",
"uninit": 0,
"object": 1,
"string": 2,
"int": 3,
"uint": 4,
"float": 5,
"undefined": 6,
});
static methods = Object.freeze({
0: 'free',
1: 'getProp',
2: 'setProp',
3: 'delProp',
4: 'callMethod',
5: 'call',
});
static store = { 0: null, 1: window };
static generateFreeId() {
let id = 0;
while (id in ObjectStore.store) {
id = Math.floor(Math.random() * (2**32 - 1))
}
return id;
}
static free(id) {
delete ObjectStore.store[id][ObjectStore.id_name];
delete ObjectStore.store[id];
}
static getProp(target, key) {
return target[key];
}
static setProp(target, key, value) {
target[key] = value;
}
static delProp(target, key) {
delete target[key];
}
static callMethod(target, method, ...args) {
return target[method](...args);
}
static call(target, args) {
return target(args);
}
static decodeValue(view, offset) {
if (ObjectStore.types[kind] === 'string') {
const ptr = view.getUint32((offset * 4) * 2, true);
const len = view.getUint32((offset * 4) * 2 + 4, true);
return decodeString(ptr, len);
}
}
static decodeCall(view, offset) {
const kind = ObjectStore.methods[view.getUint8(offset, true)]
if (!kind) throw "unknown method, cannot decode";
return {
kind: kind,
num_args: view.getUint8(offset + 1, true),
}
}
static decodeValue(view, offset) {
const kind = ObjectStore.types[view.getUint8(offset, true)];
if (!kind) throw "unknown type, cannot decode";
if (kind === 'string') {
const ptr = view.getUint32(offset + 8, true)
const len = view.getUint32(offset + 12, true)
return decodeString(ptr, len);
}
if (kind === 'int') {
return view.getInt32(offset + 8, true);
}
if (kind === 'uint') {
return view.getUint32(offset + 8, true);
}
if (kind === 'object') {
const id = view.getUint32(offset + 8, true);
return ObjectStore.store[id];
}
if (kind === 'float') {
return view.getFloat64(offset + 8, true);
}
}
static writeValue(ptr, value) {
const mem = new DataView(wasm.memory.buffer, ptr);
if (typeof value === 'object') {
mem.setUint8(0, ObjectStore.types['object'], true);
let id = value == null ? 0 : value[ObjectStore.id_name];
if (id === undefined) {
id = ObjectStore.generateFreeId();
ObjectStore.store[id] = value;
value[ObjectStore.id_name] = id;
}
mem.setUint32(8, id, true);
return;
}
if (typeof value === 'number' && Number.isInteger(value) && value >= 0) {
// uint
throw "unimplemented";
return;
}
if (typeof value === 'number' && Number.isInteger(value)) {
// int
throw "unimplemented";
return;
}
if (typeof value === 'number') {
// float
throw "unimplemented";
return;
}
if (typeof value === 'string') {
// string
throw "unimplemented";
return;
}
if (typeof value === 'undefined') {
// undefined
throw "unimplemented";
return;
}
}
static processWasm(
calls_base,
values_base,
result_base,
length,
) {
const offset = {
call: 0,
value: 0,
result: 0,
}
const calls_view = new DataView(wasm.memory.buffer, calls_base);
const values_view = new DataView(wasm.memory.buffer, values_base);
const results_view = new DataView(wasm.memory.buffer, result_base);
for (let i = 0; i < length; ++i) {
const method = ObjectStore.decodeCall(calls_view, offset.call);
offset.call += 2;
let args = new Array(method.num_args).fill().map(_ => {
const value = ObjectStore.decodeValue(values_view, offset.value);
offset.value += 16;
return value;
});
const result = ObjectStore[method.kind](...args);
const result_ptr = results_view.getUint32(offset.result, true);
offset.result += 4;
ObjectStore.writeValue(result_ptr, result);
}
}
}
const imports = {
object_store: {
process: ObjectStore.processWasm,
},
console: {
log(buffer, length) {
console.log(decodeString(buffer, length))
},
warn(buffer, length) {
console.warn(decodeString(buffer, length))
},
error(buffer, length) {
console.error(decodeString(buffer, length))
},
debug(buffer, length) {
console.debug(decodeString(buffer, length))
},
}
}
WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
({ instance, module }) => {
const is64 = new Uint8Array(
instance.exports.memory.buffer,
instance.exports.bit_length,
1,
)[0] == 8
window.wasm = {
exports: instance.exports,
memory: instance.exports.memory,
is64: is64,
UsizeArray: is64 ? BigUint64Array : Uint32Array,
IsizeArray: is64 ? BigInt64Array : Int32Array,
}
window[ObjectStore.id_name] = 1;
wasm.exports.run();
},
);
|