-
Notifications
You must be signed in to change notification settings - Fork 0
/
webworker.js
36 lines (36 loc) · 1.76 KB
/
webworker.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
var g_importObject = {
'env': {
'memoryBase': 0,
'tableBase': 0,
'memory': new WebAssembly.Memory({ initial: 256 }),
'table': new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
'abort': function(){},
'setTempRet0': function(){},
'getTempRet0': function(){}
}
};
// The WebAssembly module instance that we'll be working with
var g_objInstance = null;
// Listen for messages from the main thread. Because all messages to this thread come through
// this method, we need a way to know what is being asked of us which is why we included the
// MessagePurpose property.
self.onmessage = function (evt) {
// If we've been asked to call the module's Add method then...
var objData = evt.data;
var sMessagePurpose = objData.MessagePurpose;
if (sMessagePurpose === "AddValues") {
// Call the add method in the WebAssembly module and pass the result back to the main thread
var iResult = g_objInstance.exports._mmixal_wasm(objData.Val1);
self.postMessage(`This is the Web Worker...The result of ${objData.Val1.toString()} `);
} // If we've been passed a compiled WebAssembly module then...
else if (sMessagePurpose === "CompiledModule") {
// NOTE: Unlike when we pass in the bytes to instantiate, we don't have a separate 'instance'
// and 'modules' object returned in this case since we started out with the module object.
// We're only passed back the instance in this case.
// WebAssembly.instantiateStreaming(fetch("mmix.wasm"), g_importObject).then(instance => g_objInstance = instance);
WebAssembly.instantiate(objData.WasmModule, g_importObject).then(instance =>
g_objInstance = instance,
reason => console.debug(reason)// Hold onto the module's instance so that we can reuse it
);
}
}