File tree 4 files changed +19
-3
lines changed
4 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -495,14 +495,16 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
495
495
#include "URIUtils.js"
496
496
497
497
#if ASSERTIONS
498
- function createExportWrapper ( name ) {
498
+ function createExportWrapper ( name , nargs ) {
499
499
return ( ...args ) => {
500
500
assert ( runtimeInitialized , `native function \`${ name } \` called before runtime initialization` ) ;
501
501
#if EXIT_RUNTIME
502
502
assert ( ! runtimeExited , `native function \`${ name } \` called after runtime exit (use NO_EXIT_RUNTIME to keep it alive after main() exits)` ) ;
503
503
#endif
504
504
var f = wasmExports [ name ] ;
505
505
assert ( f , `exported native function \`${ name } \` not found` ) ;
506
+ // Only assert for too many arguments. Too few can be valid since the missing arguments will be zero filled.
507
+ assert ( args . length <= nargs , `native function \`${ name } \` called with ${ args . length } args but expects ${ nargs } ` ) ;
506
508
return f ( ...args ) ;
507
509
} ;
508
510
}
Original file line number Diff line number Diff line change @@ -12035,6 +12035,20 @@ def test_native_call_after_exit(self):
12035
12035
out = self.run_js('foo.js', assert_returncode=NON_ZERO)
12036
12036
self.assertContained('native function `main` called after runtime exit', out)
12037
12037
12038
+ def test_native_call_nargs(self):
12039
+ self.set_setting('ASSERTIONS')
12040
+ self.set_setting('EXPORTED_FUNCTIONS', ['_main', '_foo'])
12041
+ create_file('foo.c', r'''
12042
+ #include <emscripten.h>
12043
+ void foo(int arg) {}
12044
+ int main() {
12045
+ EM_ASM(_foo(99, 100));
12046
+ }
12047
+ ''')
12048
+ self.build('foo.c')
12049
+ out = self.run_js('foo.js', assert_returncode=NON_ZERO)
12050
+ self.assertContained('native function `foo` called with 2 args but expects 1', out)
12051
+
12038
12052
def test_metadce_wasm2js_i64(self):
12039
12053
# handling i64 unsigned remainder brings in some i64 support code. metadce
12040
12054
# must not remove it.
Original file line number Diff line number Diff line change @@ -885,7 +885,7 @@ def install_wrapper(sym):
885
885
if settings .ASSERTIONS and install_wrapper (name ):
886
886
# With assertions enabled we create a wrapper that are calls get routed through, for
887
887
# the lifetime of the program.
888
- wrapper += "createExportWrapper('%s' );" % name
888
+ wrapper += f "createExportWrapper('{ name } ', { nargs } );"
889
889
elif settings .WASM_ASYNC_COMPILATION :
890
890
# With WASM_ASYNC_COMPILATION wrapper will replace the global var and Module var on
891
891
# first use.
Original file line number Diff line number Diff line change @@ -536,7 +536,7 @@ def is_ptr_arg(i):
536
536
elif arg_type == 'Double' :
537
537
body += " if (typeof {0} == 'object') {{ {0} = ensureFloat64({0}); }}\n " .format (js_arg )
538
538
539
- call_args = pre_arg
539
+ call_args = pre_arg . copy ()
540
540
541
541
for i , arg in enumerate (args ):
542
542
if options .wasm64 and is_ptr_arg (i ):
You can’t perform that action at this time.
0 commit comments