Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(loader): added _free ptr to ccall #1100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 78 additions & 71 deletions loader/src/formats/wasm64-emscripten.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -4916,6 +4916,7 @@ function metering_gasUsed() {
}
var previousAsync = Asyncify.currData;
var ret = func(...cArgs);

function onDone(ret) {
runtimeKeepalivePop();
if (stack !== 0) stackRestore(stack);
Expand All @@ -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;
};

/**
Expand Down
10 changes: 5 additions & 5 deletions loader/src/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ 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()
return applyMetering(arrayBuffer, 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)
Expand Down Expand Up @@ -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'])
}
Expand Down
Loading