You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
unint64_t is passed through to JS as a signed value instead #13902
While trying to pass std::numeric_limits<uint64_t>::max() through to JS in bigint mode, I hit an issue where I get a -1 on the JS side. I would expect the value to come through correctly as bigint can represent a number that large.
I can reproduce this with a cpp file with a JS library as follows:
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
The issue I believe is that at the boundary of JS and Wasm we only have i32 and i64 and interpreting those types as either signed or unsigned is up to the embedder (in this case JS).
In this example the JS library function doesn't actually know the signature of the native function, and neither the wasm module since the type was lowered to i64 during compilation.
I'm not even sure how to go about reinterpreting the BigInt here as an unisigned i64. Perhaps one could write it do a DataView via .setBigInt64() and then read it back via .getBigUint64() but that seems like overkill.
I hit an issue where I get a -1 on the JS side. I would expect the value to come through correctly as bigint can represent a number that large.
When JS side code receives values from Wasm, they will all come out as signed Numbers, or now as BigInts when supported and enabled. Wasm does not really have any machinery to know the intended interpretation of the signed vs unsigned aspect, so there will always be a need for the JS side to "fix up" the received numbers it gets.
#15433 aims to resolve some issues around this. Also, there exists the library src/library_int53.js today, which can help non-BigInt uses.
However neither of those will help the 64-bit unsigned integer aspect. To fix that, off the top of my head I can't think of anything better except to convert via arithmetic. One can do
consti64ToU64Offset=BigInt(2)**BigInt(64);// In the future can write this as "2n**64n" when Emscripten's terser is updated with BigInt literal supportfunctionconvertBigI64ToBigU64(signedBigInt){returnsignedBigInt<0 ? i64ToU64Offset-signedBigInt : signedBigInt;}
Then in the JS functions that receive a BigInt that should be an unsigned 64-bit thing, you can call convertBigI64ToBigU64(arg) on each to convert to 64-bit unsigned form.
I also see this with embind and value_object,
A struct with an std::uint64_t data member will appear in JS as negative if
over std::numeric_limits<int64_t>::max(), is this the same issue?
Should I open a separate ticket for that?
Small bug in that snippet, i64ToU64Offset - signedBigInt should be i64ToU64Offset + signedBigInt as we know signedBigInt is negative.
consti64ToU64Offset=BigInt(2)**BigInt(64);// In the future can write this as "2n**64n" when Emscripten's terser is updated with BigInt literal supportfunctionconvertBigI64ToBigU64(signedBigInt){returnsignedBigInt<0 ? i64ToU64Offset+signedBigInt : signedBigInt;}
Activity
stale commentedon Apr 16, 2022
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.
mmarczell-graphisoft commentedon Apr 19, 2022
@juj @kripken @sbc100 I would be interested in working on this, but I have no idea where to start. Any ideas?
sbc100 commentedon Apr 19, 2022
The issue I believe is that at the boundary of JS and Wasm we only have i32 and i64 and interpreting those types as either signed or unsigned is up to the embedder (in this case JS).
In this example the JS library function doesn't actually know the signature of the native function, and neither the wasm module since the type was lowered to i64 during compilation.
I'm not even sure how to go about reinterpreting the BigInt here as an unisigned i64. Perhaps one could write it do a DataView via
.setBigInt64()and then read it back via.getBigUint64()but that seems like overkill.juj commentedon Apr 19, 2022
When JS side code receives values from Wasm, they will all come out as signed Numbers, or now as BigInts when supported and enabled. Wasm does not really have any machinery to know the intended interpretation of the signed vs unsigned aspect, so there will always be a need for the JS side to "fix up" the received numbers it gets.
#15433 aims to resolve some issues around this. Also, there exists the library
src/library_int53.jstoday, which can help non-BigInt uses.However neither of those will help the 64-bit unsigned integer aspect. To fix that, off the top of my head I can't think of anything better except to convert via arithmetic. One can do
Then in the JS functions that receive a BigInt that should be an unsigned 64-bit thing, you can call
convertBigI64ToBigU64(arg)on each to convert to 64-bit unsigned form.logankaser commentedon Jun 20, 2023
I also see this with embind and
value_object,A struct with an
std::uint64_tdata member will appear in JS as negative ifover
std::numeric_limits<int64_t>::max(), is this the same issue?Should I open a separate ticket for that?
logankaser commentedon Jun 20, 2023
Small bug in that snippet,
i64ToU64Offset - signedBigIntshould bei64ToU64Offset + signedBigIntas we know signedBigInt is negative.Fix handling of uint64_t in Embind (emscripten-core#24285)