diff --git a/alg/pre.wx.js b/alg/pre.wx.js index edc8bd6..0a381f0 100644 --- a/alg/pre.wx.js +++ b/alg/pre.wx.js @@ -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}; + } +} diff --git a/scanner/pages/emblemscanner/qrprocessor.js b/scanner/pages/emblemscanner/qrprocessor.js index 9ce736e..e936565 100644 --- a/scanner/pages/emblemscanner/qrprocessor.js +++ b/scanner/pages/emblemscanner/qrprocessor.js @@ -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; diff --git a/scanner/pages/emblemscanner/qrtool.wx.js b/scanner/pages/emblemscanner/qrtool.wx.js index 2f5ae1c..b668186 100644 --- a/scanner/pages/emblemscanner/qrtool.wx.js +++ b/scanner/pages/emblemscanner/qrtool.wx.js @@ -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 diff --git a/scanner/pages/emblemscanner/worker/index.js b/scanner/pages/emblemscanner/worker/index.js index 7b883b8..3b11cc0 100644 --- a/scanner/pages/emblemscanner/worker/index.js +++ b/scanner/pages/emblemscanner/worker/index.js @@ -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); } diff --git a/scanner/pages/emblemscanner/worker/qrtool.wx.js b/scanner/pages/emblemscanner/worker/qrtool.wx.js index 2f5ae1c..b668186 100644 --- a/scanner/pages/emblemscanner/worker/qrtool.wx.js +++ b/scanner/pages/emblemscanner/worker/qrtool.wx.js @@ -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 diff --git a/scanner/precheck.js b/scanner/precheck.js index 8616270..3e3a80d 100644 --- a/scanner/precheck.js +++ b/scanner/precheck.js @@ -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;