Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,16 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
#include "URIUtils.js"

#if ASSERTIONS
function createExportWrapper(name) {
function createExportWrapper(name, nargs) {
return (...args) => {
assert(runtimeInitialized, `native function \`${name}\` called before runtime initialization`);
#if EXIT_RUNTIME
assert(!runtimeExited, `native function \`${name}\` called after runtime exit (use NO_EXIT_RUNTIME to keep it alive after main() exits)`);
#endif
var f = wasmExports[name];
assert(f, `exported native function \`${name}\` not found`);
// Only assert for too many arguments. Too few can be valid since the missing arguments will be zero filled.
assert(args.length <= nargs, `native function \`${name}\` called with ${args.length} args but expects ${nargs}`);
return f(...args);
};
}
Expand Down
14 changes: 14 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -12030,6 +12030,20 @@ def test_native_call_after_exit(self):
out = self.run_js('foo.js', assert_returncode=NON_ZERO)
self.assertContained('native function `main` called after runtime exit', out)

def test_native_call_nargs(self):
self.set_setting('ASSERTIONS')
self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_foo'])
create_file('foo.c', r'''
#include <emscripten.h>
void foo(int arg) {}
int main() {
EM_ASM(_foo(99, 100));
}
''')
self.build('foo.c')
out = self.run_js('foo.js', assert_returncode=NON_ZERO)
self.assertContained('native function `foo` called with 2 args but expects 1', out)

def test_metadce_wasm2js_i64(self):
# handling i64 unsigned remainder brings in some i64 support code. metadce
# must not remove it.
Expand Down
2 changes: 1 addition & 1 deletion tools/emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ def install_wrapper(sym):
if settings.ASSERTIONS and install_wrapper(name):
# With assertions enabled we create a wrapper that are calls get routed through, for
# the lifetime of the program.
wrapper += "createExportWrapper('%s');" % name
wrapper += f"createExportWrapper('{name}', {nargs});"
elif settings.WASM_ASYNC_COMPILATION:
# With WASM_ASYNC_COMPILATION wrapper will replace the global var and Module var on
# first use.
Expand Down
2 changes: 1 addition & 1 deletion tools/webidl_binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def is_ptr_arg(i):
elif arg_type == 'Double':
body += " if (typeof {0} == 'object') {{ {0} = ensureFloat64({0}); }}\n".format(js_arg)

call_args = pre_arg
call_args = pre_arg.copy()

for i, arg in enumerate(args):
if options.wasm64 and is_ptr_arg(i):
Expand Down