diff --git a/src/parseTools.js b/src/parseTools.js index a4ce78812d9b0..d6c47b6f2edbd 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -213,6 +213,12 @@ function makeInlineCalculation(expression, value, tempVar) { // value, represented by a low and high i32 pair. // Will suffer from rounding and truncation. function splitI64(value) { + if (WASM_BIGINT) { + // Nothing to do: just make sure it is a BigInt (as it must be of that + // type, to be sent into wasm). + return `BigInt(${value})`; + } + // general idea: // // $1$0 = ~~$d >>> 0; @@ -229,7 +235,6 @@ function splitI64(value) { // For negatives, we need to ensure a -1 if the value is overall negative, // even if not significant negative component - assert(!WASM_BIGINT, 'splitI64 should not be used when WASM_BIGINT is enabled'); const low = value + '>>>0'; const high = makeInlineCalculation( asmCoercion('Math.abs(VALUE)', 'double') + ' >= ' + asmEnsureFloat('1', 'double') + ' ? ' + @@ -886,15 +891,10 @@ function receiveI64ParamAsI53(name, onError) { } function receiveI64ParamAsI53Unchecked(name) { - if (WASM_BIGINT) return `${name} = Number(${name});`; - return `var ${name} = convertI32PairToI53(${name}_low, ${name}_high);`; -} - -function sendI64Argument(low, high) { if (WASM_BIGINT) { - return 'BigInt(low) | (BigInt(high) << 32n)'; + return `${name} = Number(${name});`; } - return low + ', ' + high; + return `var ${name} = convertI32PairToI53(${name}_low, ${name}_high);`; } // Any function called from wasm64 may have bigint args, this function takes diff --git a/test/core/js_library_i64_params.c b/test/core/js_library_i64_params.c index 4f0cc33ad98a5..764fc20b0e490 100644 --- a/test/core/js_library_i64_params.c +++ b/test/core/js_library_i64_params.c @@ -1,4 +1,5 @@ #include +#include #include #define MAX_SAFE_INTEGER (1ll<<53) @@ -8,6 +9,11 @@ int64_t jscall(int64_t arg); +EMSCRIPTEN_KEEPALIVE +void called_from_js(uint64_t arg) { + printf("called_from_js with: %lld\n", arg); +} + void check_ok(int64_t val) { printf("checking: %lld\n", val); int64_t rtn = jscall(val); @@ -25,7 +31,11 @@ void check_invalid(int64_t val) { } int main() { + check_ok(0); + check_ok(1); + check_ok(-1); check_ok(42); + check_ok(-42); check_ok(MAX_SAFE_INTEGER/2); check_ok(MIN_SAFE_INTEGER/2); check_ok(MAX_SAFE_INTEGER); diff --git a/test/core/js_library_i64_params.js b/test/core/js_library_i64_params.js index 28a00cf1174df..6c9f886c81e78 100644 --- a/test/core/js_library_i64_params.js +++ b/test/core/js_library_i64_params.js @@ -4,6 +4,9 @@ TestLibrary = { jscall: function({{{ defineI64Param('foo') }}}) { {{{ receiveI64ParamAsI53('foo', `(err('overflow'), ${makeReturn64('42')})`) }}} err('js:got: ' + foo); + + _called_from_js({{{ splitI64("foo") }}}); + if (foo < 0) var rtn = Math.ceil(foo / 2); else diff --git a/test/core/js_library_i64_params.out b/test/core/js_library_i64_params.out index 8f1d9b62dcc6b..26db9827949b3 100644 --- a/test/core/js_library_i64_params.out +++ b/test/core/js_library_i64_params.out @@ -1,25 +1,54 @@ +checking: 0 +js:got: 0 +called_from_js with: 0 +js:returning: 0 +got: 0 +expected: 0 +checking: 1 +js:got: 1 +called_from_js with: 1 +js:returning: 0 +got: 0 +expected: 0 +checking: -1 +js:got: -1 +called_from_js with: -1 +js:returning: 0 +got: 0 +expected: 0 checking: 42 js:got: 42 +called_from_js with: 42 js:returning: 21 got: 21 expected: 21 +checking: -42 +js:got: -42 +called_from_js with: -42 +js:returning: -21 +got: -21 +expected: -21 checking: 4503599627370496 js:got: 4503599627370496 +called_from_js with: 4503599627370496 js:returning: 2251799813685248 got: 2251799813685248 expected: 2251799813685248 checking: -4503599627370496 js:got: -4503599627370496 +called_from_js with: -4503599627370496 js:returning: -2251799813685248 got: -2251799813685248 expected: -2251799813685248 checking: 9007199254740992 js:got: 9007199254740992 +called_from_js with: 9007199254740992 js:returning: 4503599627370496 got: 4503599627370496 expected: 4503599627370496 checking: -9007199254740992 js:got: -9007199254740992 +called_from_js with: -9007199254740992 js:returning: -4503599627370496 got: -4503599627370496 expected: -4503599627370496