diff options
-rw-r--r-- | src/wasm-js.cpp | 21 | ||||
-rw-r--r-- | test/calls.cpp | 7 | ||||
-rw-r--r-- | test/calls.post.js | 7 | ||||
-rw-r--r-- | test/calls.txt | 6 |
4 files changed, 24 insertions, 17 deletions
diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index c966e07cf..6b5c014c2 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -178,25 +178,16 @@ extern "C" double EMSCRIPTEN_KEEPALIVE call_from_js(const char *target) { IString name(target); assert(instance->functions.find(name) != instance->functions.end()); Function *function = instance->functions[name]; - size_t num = EM_ASM_INT_V({ return Module['tempArguments'].length }); - if (num != function->params.size()) { - std::cerr << "warning: bad # of arguments to " << target << ": got " << num << ", but expect " << function->params.size() << '\n'; - abort(); // TODO: fake missing/extra args? -#if 0 - if (num > function->params.size()) { - num = function->params.size(); // ignore the rest - } else { - .. fake missing .. - } -#endif - } + size_t seen = EM_ASM_INT_V({ return Module['tempArguments'].length }); + size_t actual = function->params.size(); ModuleInstance::LiteralList arguments; - for (size_t i = 0; i < num; i++) { + for (size_t i = 0; i < actual; i++) { WasmType type = function->params[i].type; + // add the parameter, with a zero value if JS did not provide it. if (type == i32) { - arguments.push_back(Literal(EM_ASM_INT({ return Module['tempArguments'][$0] }, i))); + arguments.push_back(Literal(i < seen ? EM_ASM_INT({ return Module['tempArguments'][$0] }, i) : (int32_t)0)); } else if (type == f64) { - arguments.push_back(Literal(EM_ASM_DOUBLE({ return Module['tempArguments'][$0] }, i))); + arguments.push_back(Literal(i < seen ? EM_ASM_DOUBLE({ return Module['tempArguments'][$0] }, i) : (double)0.0)); } else { abort(); } diff --git a/test/calls.cpp b/test/calls.cpp index b73dff3f4..0afb0eb69 100644 --- a/test/calls.cpp +++ b/test/calls.cpp @@ -1,5 +1,3 @@ -#include <cmath> -#include <algorithm> #include <emscripten.h> extern "C" { @@ -17,5 +15,10 @@ int EMSCRIPTEN_KEEPALIVE fibo(int x) { return fibo(x-1) + fibo(x-2); } +int EMSCRIPTEN_KEEPALIVE run_script() { + emscripten_run_script("Module.print('hello from called script')"); + return emscripten_run_script_int("1+2+3+4-1"); +} + } diff --git a/test/calls.post.js b/test/calls.post.js index f2b422ed2..bec4d498b 100644 --- a/test/calls.post.js +++ b/test/calls.post.js @@ -14,3 +14,10 @@ function test(name) { test('simple'); test('fibo'); +Module.print('run_script'); +Module.print(Module['_run_script']()); + +Module.print('too many/few arguments'); +Module.print(Module['_simple']()); +Module.print(Module['_simple'](10, 20)); + diff --git a/test/calls.txt b/test/calls.txt index 7d1b8ee47..6a175ed04 100644 --- a/test/calls.txt +++ b/test/calls.txt @@ -10,3 +10,9 @@ fibo 3 ==> 3 4 ==> 5 7 ==> 21 +run_script +hello from called script +9 +too many/few arguments +2 +102 |