Set up a basic "front-end" for the webassembly module to be tested locally from a data folder (I use python -m http.server). The WebAssembly application is now responsible for all setup/rendering through the WebGL2 context.
This commit is contained in:
42
data/wasm_functions.js
Normal file
42
data/wasm_functions.js
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
import { appGlobals } from './globals.js'
|
||||
|
||||
export async function loadWasmModule(filePath, environment)
|
||||
{
|
||||
let result = null;
|
||||
try
|
||||
{
|
||||
const fetchPromise = fetch(filePath);
|
||||
const wasmModule = await WebAssembly.instantiateStreaming(
|
||||
fetchPromise,
|
||||
{ env: environment }
|
||||
);
|
||||
result = wasmModule.instance;
|
||||
}
|
||||
catch (exception)
|
||||
{
|
||||
console.error("Failed to load WASM module from \"" + filePath + "\":", exception);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
//TODO: We should do some performance measurements of wasmPntrToJsString vs wasmPntrAndLengthToJsString!
|
||||
export function wasmPntrToJsString(ptr)
|
||||
{
|
||||
let cIndex = ptr;
|
||||
while (cIndex < appGlobals.memDataView.byteLength)
|
||||
{
|
||||
let byteValue = appGlobals.memDataView.getUint8(cIndex, true);
|
||||
if (byteValue == 0) { break; }
|
||||
cIndex++;
|
||||
}
|
||||
return appGlobals.textDecoder.decode(
|
||||
appGlobals.memDataView.buffer.slice(ptr, cIndex)
|
||||
);
|
||||
}
|
||||
export function wasmPntrAndLengthToJsString(ptr, length)
|
||||
{
|
||||
return appGlobals.textDecoder.decode(
|
||||
appGlobals.memDataView.buffer.slice(ptr, ptr + length)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user