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 }}
This repository was archived by the owner on Apr 25, 2025. It is now read-only.
Suppose a JavaScript number in 31bit int range flows into Wasm as an anyref type.
Then in Wasm, we can perform a dynamic type-check to see if it has type i31ref.
As far as I see, the spec wants this check to fail. But the problem is that no matter how this is specified, it causes some issues in the implementation.
Option 1: If the check is specified to fail, this means that i31ref and JavaScript numbers need a different encoding, but there is no space in a 32bit word to encode two different kinds of 31bit integers in addition to pointers. For V8 (32bit or pointer compressed builds), this would mean that either Smis have to be boxed when they flow to Wasm or i31ref would have to be boxed, which makes i31ref pointless.
Option 2: If the check is specified to succeed, then even engines that don't have Smi's would have to convert in-range JS numbers to i31ref. Even in V8, this would require normalizing HeapNumber to Smi at the boundary when possible, because otherwise we would leak the unobservable difference between Smi and a HeapNumber storing a Smi-range value. This conversion only has to happen when we cast or check for i31ref.
Option 3: The only alternative would be to leave this unspecified, leading to implementation-defined behavior, which I guess we don't want.
I think a better alternative to i31ref would be to just expose JavaScript numbers as an anyref subtype in Wasm. Since all major engines implement JavaScript numbers in a way that they are unboxed for at least the 31bit range, using this type in Wasm would still result in the desired unboxed representation. i31ref as a subtype of JavaScript numbers could also work, this corresponds to Option 2.