aboutsummaryrefslogtreecommitdiff
path: root/src/z/App/main.js
blob: 7ac2cdb6d9d892d52377e007af1a4413c31e74f3 (plain)
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
const imports = {
	object_store: {
		process: ObjectStore.processWasm,
	},
	console: {
		log(buffer, length) {
			console.log(ObjectStore.decodeString(buffer, length))
		},
		warn(buffer, length) {
			console.warn(ObjectStore.decodeString(buffer, length))
		},
		err(buffer, length) {
			console.error(ObjectStore.decodeString(buffer, length))
		},
		debug(buffer, length) {
			console.debug(ObjectStore.decodeString(buffer, length))
		},
	}
}

async function wasm_handle_event(name, event, pass_event) {
	await wasm_ready;

	if (pass_event) {
		const id = ObjectStore.generateFreeId();
		ObjectStore.registerObject(id, event);
		wasm.exports[name](this["@ptr"], id);
		ObjectStore.free(event);
	} else {
		wasm.exports[name](this["@ptr"]);
	}
}

const wasm_ready = WebAssembly.instantiateStreaming(fetch("index.wasm"), imports).then(
	({ instance, module }) => {
		wasm = {
			exports: instance.exports,
			memory: instance.exports.memory,
		}
	},
);