fix wasm path parameter

This commit is contained in:
Fam Zheng 2025-10-15 16:15:51 +01:00
parent 80f72d0a7f
commit dd2c4614ba
6 changed files with 117 additions and 33 deletions

View File

@ -5,19 +5,44 @@ WebAssembly.RuntimeErrror = Error;
var performance = {
now: Date.now,
};
Module['instantiateWasm'] = (info, receiveInstance) => {
console.log("loading wasm...", info);
// Use global wasmFilePath (should always be set by the main app)
var wasmPath = global.wasmFilePath;
if (!wasmPath) {
throw new Error("wasmFilePath must be provided via global.wasmFilePath");
// Local variable to store WASM path
var wasmFilePath = null;
// Store instantiateWasm parameters for later use
var pendingInstantiation = null;
// Exported function to set WASM path
Module['setWasmPath'] = (path) => {
console.log("Setting WASM path:", path);
wasmFilePath = path;
// If instantiateWasm was called before the path was set, call it now
if (pendingInstantiation) {
var {info, receiveInstance} = pendingInstantiation;
pendingInstantiation = null;
doInstantiateWasm(info, receiveInstance);
}
console.log("Using WASM path:", wasmPath);
};
WebAssembly.instantiate(wasmPath, info).then((result) => {
function doInstantiateWasm(info, receiveInstance) {
console.log("loading wasm...", info);
console.log("Using WASM path:", wasmFilePath);
WebAssembly.instantiate(wasmFilePath, info).then((result) => {
console.log("result:", result);
var inst = result['instance'];
receiveInstance(inst);
});
}
Module['instantiateWasm'] = (info, receiveInstance) => {
// If WASM path is already set, proceed immediately
if (wasmFilePath) {
doInstantiateWasm(info, receiveInstance);
} else {
// Otherwise, store parameters for later use
console.log("WASM path not set yet, deferring instantiation...");
pendingInstantiation = {info, receiveInstance};
}
}

View File

@ -8,12 +8,12 @@ var qrtool_ready = false;
* Load qrtool WASM module
*/
function load_qrtool(wasmFilePath) {
// Set global WASM file path before loading the module
global.wasmFilePath = wasmFilePath;
// Load the WASM module
// Load the WASM module first
var m = require('./qrtool.wx.js');
// Set WASM file path after loading the module (required parameter)
m['setWasmPath'](wasmFilePath);
m.onRuntimeInitialized = () => {
qrtool_ready = true;
qrtool = m;

View File

@ -32,19 +32,48 @@ WebAssembly.RuntimeErrror = Error;
var performance = {
now: Date.now
};
Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info);
// Use global wasmFilePath (should always be set by the main app)
var wasmPath = global.wasmFilePath;
if (!wasmPath) {
throw new Error("wasmFilePath must be provided via global.wasmFilePath");
// Local variable to store WASM path
var wasmFilePath = null;
// Store instantiateWasm parameters for later use
var pendingInstantiation = null;
// Exported function to set WASM path
Module["setWasmPath"] = path => {
console.log("Setting WASM path:", path);
wasmFilePath = path;
// If instantiateWasm was called before the path was set, call it now
if (pendingInstantiation) {
var {
info,
receiveInstance
} = pendingInstantiation;
pendingInstantiation = null;
doInstantiateWasm(info, receiveInstance);
}
console.log("Using WASM path:", wasmPath);
WebAssembly.instantiate(wasmPath, info).then(result => {
};
function doInstantiateWasm(info, receiveInstance) {
console.log("loading wasm...", info);
console.log("Using WASM path:", wasmFilePath);
WebAssembly.instantiate(wasmFilePath, info).then(result => {
console.log("result:", result);
var inst = result["instance"];
receiveInstance(inst);
});
}
Module["instantiateWasm"] = (info, receiveInstance) => {
// If WASM path is already set, proceed immediately
if (wasmFilePath) {
doInstantiateWasm(info, receiveInstance);
} else {
// Otherwise, store parameters for later use
console.log("WASM path not set yet, deferring instantiation...");
pendingInstantiation = {
info,
receiveInstance
};
}
};
// end include: /Users/fam/src/themblem/alg/pre.wx.js

View File

@ -1,12 +1,11 @@
console.log("hello from emblemscanner worker");
// Set global WASM file path before loading qrtool
// Load qrtool module first, then set WASM path
// The wasmFilePath will be passed from the main thread when the worker is created
let qrtool = require('./qrtool.wx.js');
if (typeof worker !== "undefined" && worker.wasmFilePath) {
global.wasmFilePath = worker.wasmFilePath;
let qrtool = require('./qrtool.wx.js');
} else {
let qrtool = require('./qrtool.wx.js');
qrtool['setWasmPath'](worker.wasmFilePath);
}

View File

@ -32,19 +32,48 @@ WebAssembly.RuntimeErrror = Error;
var performance = {
now: Date.now
};
Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info);
// Use global wasmFilePath (should always be set by the main app)
var wasmPath = global.wasmFilePath;
if (!wasmPath) {
throw new Error("wasmFilePath must be provided via global.wasmFilePath");
// Local variable to store WASM path
var wasmFilePath = null;
// Store instantiateWasm parameters for later use
var pendingInstantiation = null;
// Exported function to set WASM path
Module["setWasmPath"] = path => {
console.log("Setting WASM path:", path);
wasmFilePath = path;
// If instantiateWasm was called before the path was set, call it now
if (pendingInstantiation) {
var {
info,
receiveInstance
} = pendingInstantiation;
pendingInstantiation = null;
doInstantiateWasm(info, receiveInstance);
}
console.log("Using WASM path:", wasmPath);
WebAssembly.instantiate(wasmPath, info).then(result => {
};
function doInstantiateWasm(info, receiveInstance) {
console.log("loading wasm...", info);
console.log("Using WASM path:", wasmFilePath);
WebAssembly.instantiate(wasmFilePath, info).then(result => {
console.log("result:", result);
var inst = result["instance"];
receiveInstance(inst);
});
}
Module["instantiateWasm"] = (info, receiveInstance) => {
// If WASM path is already set, proceed immediately
if (wasmFilePath) {
doInstantiateWasm(info, receiveInstance);
} else {
// Otherwise, store parameters for later use
console.log("WASM path not set yet, deferring instantiation...");
pendingInstantiation = {
info,
receiveInstance
};
}
};
// end include: /Users/fam/src/themblem/alg/pre.wx.js

View File

@ -3,6 +3,8 @@ var qrtool_ready = false;
function load_qrtool() {
var m = require('/assets/qrtool.wx.js');
// Set WASM file path for web context
m['setWasmPath']('/assets/qrtool.wx.wasm');
m.onRuntimeInitialized = () => {
console.log("runtime initialized");
qrtool_ready = true;