43 lines
1.0 KiB
JavaScript
43 lines
1.0 KiB
JavaScript
|
|
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)
|
|
);
|
|
}
|