diff --git a/loader/src/formats/wasm64-emscripten.cjs b/loader/src/formats/wasm64-emscripten.cjs index 59008a458..2a038d977 100644 --- a/loader/src/formats/wasm64-emscripten.cjs +++ b/loader/src/formats/wasm64-emscripten.cjs @@ -630,73 +630,73 @@ var Module = (() => { missingGlobal("asm", "Please use wasmExports instead"); -function missingLibrarySymbol(sym) { - if (typeof globalThis != "undefined" && !Object.getOwnPropertyDescriptor(globalThis, sym)) { - Object.defineProperty(globalThis, sym, { - configurable: true, - get() { - var msg = `\`${sym}\` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line`; - var librarySymbol = sym; - if (!librarySymbol.startsWith("_")) { - librarySymbol = "$" + sym; - } - msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`; - if (isExportedByForceFilesystem(sym)) { - msg += ". Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you"; - } - warnOnce(msg); - return undefined; - } - }); - } - unexportedRuntimeSymbol(sym); -} - -function unexportedRuntimeSymbol(sym) { - if (!Object.getOwnPropertyDescriptor(Module, sym)) { - Object.defineProperty(Module, sym, { - configurable: true, - get() { - var msg = `'${sym}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the Emscripten FAQ)`; - if (isExportedByForceFilesystem(sym)) { - msg += ". Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you"; - } - abort(msg); - } - }); - } -} - -function __asyncjs__weavedrive_open(c_filename, mode) { - return Asyncify.handleAsync(async () => { - const filename = UTF8ToString(Number(c_filename)); - if (!Module.WeaveDrive) { - return Promise.resolve(null); - } - const drive = Module.WeaveDrive(Module, FS); - const driveResponse = await drive.open(filename); - if (typeof driveResponse === 'string') { - throw new Error('HALT: FILE NOT FOUND') - } - return driveResponse - }); -} - -function __asyncjs__weavedrive_read(fd, dst_ptr, length) { - return Asyncify.handleAsync(async () => { - const drive = Module.WeaveDrive(Module, FS); - return Promise.resolve(await drive.read(fd, dst_ptr, length)); - }); -} -function __asyncjs__weavedrive_close(fd) { - return Asyncify.handleAsync(async () => { - const drive = Module.WeaveDrive(Module, FS); - return drive.close(fd); - }); -} -function metering_gasUsed() { - return Module.gas.used; -} + function missingLibrarySymbol(sym) { + if (typeof globalThis != "undefined" && !Object.getOwnPropertyDescriptor(globalThis, sym)) { + Object.defineProperty(globalThis, sym, { + configurable: true, + get() { + var msg = `\`${sym}\` is a library symbol and not included by default; add it to your library.js __deps or to DEFAULT_LIBRARY_FUNCS_TO_INCLUDE on the command line`; + var librarySymbol = sym; + if (!librarySymbol.startsWith("_")) { + librarySymbol = "$" + sym; + } + msg += ` (e.g. -sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE='${librarySymbol}')`; + if (isExportedByForceFilesystem(sym)) { + msg += ". Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you"; + } + warnOnce(msg); + return undefined; + } + }); + } + unexportedRuntimeSymbol(sym); + } + + function unexportedRuntimeSymbol(sym) { + if (!Object.getOwnPropertyDescriptor(Module, sym)) { + Object.defineProperty(Module, sym, { + configurable: true, + get() { + var msg = `'${sym}' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the Emscripten FAQ)`; + if (isExportedByForceFilesystem(sym)) { + msg += ". Alternatively, forcing filesystem support (-sFORCE_FILESYSTEM) can export this for you"; + } + abort(msg); + } + }); + } + } + + function __asyncjs__weavedrive_open(c_filename, mode) { + return Asyncify.handleAsync(async () => { + const filename = UTF8ToString(Number(c_filename)); + if (!Module.WeaveDrive) { + return Promise.resolve(null); + } + const drive = Module.WeaveDrive(Module, FS); + const driveResponse = await drive.open(filename); + if (typeof driveResponse === 'string') { + throw new Error('HALT: FILE NOT FOUND') + } + return driveResponse + }); + } + + function __asyncjs__weavedrive_read(fd, dst_ptr, length) { + return Asyncify.handleAsync(async () => { + const drive = Module.WeaveDrive(Module, FS); + return Promise.resolve(await drive.read(fd, dst_ptr, length)); + }); + } + function __asyncjs__weavedrive_close(fd) { + return Asyncify.handleAsync(async () => { + const drive = Module.WeaveDrive(Module, FS); + return drive.close(fd); + }); + } + function metering_gasUsed() { + return Module.gas.used; + } /** @constructor */ function ExitStatus(status) { this.name = "ExitStatus"; @@ -4916,6 +4916,7 @@ function metering_gasUsed() { } var previousAsync = Asyncify.currData; var ret = func(...cArgs); + function onDone(ret) { runtimeKeepalivePop(); if (stack !== 0) stackRestore(stack); @@ -4927,11 +4928,17 @@ function metering_gasUsed() { assert(!(previousAsync && Asyncify.currData), "We cannot start an async operation when one is already flight"); assert(!(previousAsync && !Asyncify.currData), "We cannot stop an async operation in flight"); assert(asyncMode, "The call to " + ident + " is running asynchronously. If this was intended, add the async option to the ccall/cwrap call."); - return Asyncify.whenDone().then(onDone); + return Asyncify.whenDone().then(onDone).then(res => { + // CHG: free ptr of returned result in HEAP Memory + _free(ret) + return res; + }); } - ret = onDone(ret); - if (asyncMode) return Promise.resolve(ret); - return ret; + let res = onDone(ret); + // CHG: free ptr of returned result in HEAP Memory + _free(ret); + if (asyncMode) return Promise.resolve(res); + return res; }; /** diff --git a/loader/src/index.cjs b/loader/src/index.cjs index 9707d440b..acb860bce 100644 --- a/loader/src/index.cjs +++ b/loader/src/index.cjs @@ -128,7 +128,7 @@ const applyMetering = (arrayBuffer, importObject) => { } // Override WebAssembly.compileStreaming to apply metering -WebAssembly.compileStreaming = async function (source, importObject = { }) { +WebAssembly.compileStreaming = async function (source, importObject = {}) { if (!shouldApplyMetering(importObject)) return originalCompileStreaming(source) const arrayBuffer = await source.arrayBuffer() @@ -136,7 +136,7 @@ WebAssembly.compileStreaming = async function (source, importObject = { }) { } // Override WebAssembly.compile to apply metering -WebAssembly.compile = async function (source, importObject = { }) { +WebAssembly.compile = async function (source, importObject = {}) { if (!shouldApplyMetering(importObject)) return originalCompile(source) return applyMetering(source, importObject) @@ -197,9 +197,9 @@ module.exports = async function (binary, options) { instance.cleanupListeners() } - if (options.format !== "wasm64-unknown-emscripten-draft_2024_02_15" && - options.format !== "wasm32-unknown-emscripten4" && - options.format !== "wasm32-unknown-emscripten-metering" && + if (options.format !== "wasm64-unknown-emscripten-draft_2024_02_15" && + options.format !== "wasm32-unknown-emscripten4" && + options.format !== "wasm32-unknown-emscripten-metering" && options.format !== "wasm64-unknown-emscripten-draft_2024_10_16-metering") { doHandle = instance.cwrap('handle', 'string', ['string', 'string']) }