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 = { var performance = {
now: Date.now, now: Date.now,
}; };
Module['instantiateWasm'] = (info, receiveInstance) => {
console.log("loading wasm...", info);
// Use global wasmFilePath (should always be set by the main app) // Local variable to store WASM path
var wasmPath = global.wasmFilePath; var wasmFilePath = null;
if (!wasmPath) {
throw new Error("wasmFilePath must be provided via global.wasmFilePath"); // 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); console.log("result:", result);
var inst = result['instance']; var inst = result['instance'];
receiveInstance(inst); 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 * Load qrtool WASM module
*/ */
function load_qrtool(wasmFilePath) { function load_qrtool(wasmFilePath) {
// Set global WASM file path before loading the module // Load the WASM module first
global.wasmFilePath = wasmFilePath;
// Load the WASM module
var m = require('./qrtool.wx.js'); var m = require('./qrtool.wx.js');
// Set WASM file path after loading the module (required parameter)
m['setWasmPath'](wasmFilePath);
m.onRuntimeInitialized = () => { m.onRuntimeInitialized = () => {
qrtool_ready = true; qrtool_ready = true;
qrtool = m; qrtool = m;

View File

@ -32,19 +32,48 @@ WebAssembly.RuntimeErrror = Error;
var performance = { var performance = {
now: Date.now now: Date.now
}; };
Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info); // Local variable to store WASM path
// Use global wasmFilePath (should always be set by the main app) var wasmFilePath = null;
var wasmPath = global.wasmFilePath;
if (!wasmPath) { // Store instantiateWasm parameters for later use
throw new Error("wasmFilePath must be provided via global.wasmFilePath"); 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); console.log("result:", result);
var inst = result["instance"]; var inst = result["instance"];
receiveInstance(inst); 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 // end include: /Users/fam/src/themblem/alg/pre.wx.js

View File

@ -1,12 +1,11 @@
console.log("hello from emblemscanner worker"); 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 // 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) { if (typeof worker !== "undefined" && worker.wasmFilePath) {
global.wasmFilePath = worker.wasmFilePath; qrtool['setWasmPath'](worker.wasmFilePath);
let qrtool = require('./qrtool.wx.js');
} else {
let qrtool = require('./qrtool.wx.js');
} }

View File

@ -32,19 +32,48 @@ WebAssembly.RuntimeErrror = Error;
var performance = { var performance = {
now: Date.now now: Date.now
}; };
Module["instantiateWasm"] = (info, receiveInstance) => {
console.log("loading wasm...", info); // Local variable to store WASM path
// Use global wasmFilePath (should always be set by the main app) var wasmFilePath = null;
var wasmPath = global.wasmFilePath;
if (!wasmPath) { // Store instantiateWasm parameters for later use
throw new Error("wasmFilePath must be provided via global.wasmFilePath"); 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); console.log("result:", result);
var inst = result["instance"]; var inst = result["instance"];
receiveInstance(inst); 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 // end include: /Users/fam/src/themblem/alg/pre.wx.js

View File

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