-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWasmLoader.js
151 lines (124 loc) · 3.7 KB
/
WasmLoader.js
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
"use strict";
(class WasmLoader extends Base {
initPrototype () {
this.newSlot("path", "./iovm.js")
this.newSlot("wasmHash", null)
this.newSlot("module", null)
this.newSlot("delegate", null)
}
init () {
super.init()
this.setModule({})
this.setupModule()
}
wasmPath () {
const parts = this.path().split(".")
parts.pop()
return parts.join(".") + ".wasm"
}
setupModule () {
const m = this.module()
m.printErr = (text) => {
if (arguments.length > 1) {
text = Array.prototype.slice.call(arguments).join(' ');
}
const d = this.delegate()
d.onStandardError(text)
}
m.print = (text) => {
if (arguments.length > 1) {
text = Array.prototype.slice.call(arguments).join(' ');
}
const d = this.delegate()
d.onStandardOutput(text)
}
m.onRuntimeInitialized = () => {
const d = this.delegate()
d.onWasmLoaded.apply(d, arguments)
}
}
load () {
this.loadUrl(this.path())
this.loadWasm()
}
loadUrl (url) {
import(url).then((m) => {
m.default(this.module())
}).catch((error) => {
this.debugLog("error:" + error);
})
}
loadWasm () {
fetch(this.wasmPath()).then((response) => {
if (!response.ok) {
this.debugLog("fetch error: " + response.status)
throw new Error(response.status);
}
return response.arrayBuffer();
}).then((buffer) => {
//this.debugLog("loaded " + this.wasmPath() + " into ArrayBuffer of " + buffer.byteLength + " bytes")
this.onFetchWasmBuffer(buffer)
})
}
onFetchWasmBuffer (arrayBuffer) {
//this.debugLog(this.type() + " onFetchBuffer ")
//const s = String.fromCharCode.apply(null, new Uint16Array(arrayBuffer));
const s = btoa(arrayBuffer);
const hash = this.hashOfString(s)
//this.debugLog("hash of wasm buffer: " + hash)
this.setWasmHash(hash)
}
hashOfString (s) {
let h = 0xdeadbeef;
for(let i = 0; i < s.length; i++) {
h = Math.imul(h ^ s.charCodeAt(i), 2654435761);
}
return (h ^ h >>> 16) >>> 0;
}
debugLog (s) {
console.log(s)
alert(s)
}
}.initThisClass());
/*
window.IoWASM = {
preRun: [],
postRun: [],
'printErr': function (text) {
console.log('stderr: ' + text)
},
print: function (text) {
if (arguments.length > 1) {
text = Array.prototype.slice.call(arguments).join(' ');
}
// These replacements are necessary if you render to raw HTML
text = text.replace(/&/g, "&");
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
text = text.replace('\n', '<br>', 'g');
console.log(text);
Repl.shared().addOutput(text)
},
canvas: (function() {
var canvas = document.getElementById('canvas');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function (text) {
console.log("setStatus:", text);
},
totalDependencies: 0,
monitorRunDependencies: function (left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
MyModule.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
},
onRuntimeInitialized: function () {
window.main(this)
}
};
import initModule from "./iovm.js";
initModule(IoWASM);
*/