-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathweb.js
More file actions
66 lines (61 loc) · 1.75 KB
/
Copy pathweb.js
File metadata and controls
66 lines (61 loc) · 1.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
// Shared Wasm externs
_jsPrint=(text, len)=>{
BaseWebGPU.print(text, len);
};
_jsPrintFlush=()=>{
BaseWebGPU.printFlush();
};
// Main object, connect with shared exports
// and externs, + tools.
const BaseWebGPU = (() => {
let title="JS_BaseWebGPU";
let instance;
const print = {
buffer: "",
decoder: null,
}
const methods = {
log(message) { console.log(title + ' : ' + message); },
print(ptr, len) { this.print.buffer += this.getString(ptr, len); },
printFlush() {
try {
eval(this.print.buffer);
} catch (e) {
console.error("Log eval error:", e);
console.error("Failed script:", this.print.buffer);
}
this.print.buffer = "";
},
construct() { this.log('Constructed'); },
// Emscripten
loadModules() {
Object.assign(this, { ...Module.wasmExports, ...Module.wasmImports, ...Module.memory });
this.log('Loaded module with exports and imports');
},
// Wasm
init() {
this.print.buffer = "";
this.print.decoder = new TextDecoder();
this.loadModules();
this.loadEvents();
this.log('Initialized');
if (this.main) this.main();
},
update() { this.log('Update'); },
// WebGPU
loadEvents() {
this.log('Loaded events with externs.');
},
// Helpers
getString(ptr, len) {
if (!wasmMemory || !wasmMemory.buffer) {
return "<memory not ready>";
}
return this.print.decoder.decode(new Uint8Array(wasmMemory.buffer, ptr, len));
// return this.print.decoder.decode(new Uint8Array(BaseWebGPU.memory.buffer, ptr, len));
},
};
return { getInstance() {
return instance ? instance : (instance = methods, instance.construct(), instance); },
};
})().getInstance();