-
-
Notifications
You must be signed in to change notification settings - Fork 656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] Undefined values when iterating over IntMap when keys are larger than the max signed 32 bit integer #10316
Comments
I think this is ok. We can only guarantee |
The name That said, the issue is slight different. On all other platforms, the ints either overflow or not. The map will handle the resulting values consistently. The discrepancy observed on JS is caused directly by the implementation's
|
Wow that was fast, thanks! It does look like
Should I open another ticket for that? Or should that be fixed as part of this ticket? |
That's a different matter. While the signature of You can propose to move the Haxe implementation to If you need runtime conversion from float to int beyond the supported range, you can use |
I'm actually not sure where the real issue is here, but the symptom is that if a IntMap contains a key that is larger in value than 2147483647, when iterating over that map, the value will be undefined.
Code
Compile with:
haxe Main.hx -js main.js --main Main
and open index.html in a browser. The console output will be:What appears to be going on is: the key/value iteration iterates over the keys in a JavaScript object that is used to hold the IntMap data, and the generated code uses
key | 0
to convert the object key to a number. The problem is that bitwise operators like | will convert the value to a 32 bit integer (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_OR), which in this case is different than the key stored in the map. This causes a subsequent call to map.get to fail, which results in the undefined value.In the code above, I had to add
2147483647 + 1
to show this error. If I just declaredfinal largeKey = 2147483648
the Haxe compiler would complain thatFloat should be Int
, but it let me add numbers to create larger integers (as done here). So it's also possible that the issue here is not the use of| 0
to convert object keys to numbers, but rather that all of that was designed assuming that a positive integer key would never exceed 2147483647. This is why I said above that I'm not sure where the real issue lies. The Haxe library docs are not very clear on what the precision of an Int should be (only that it is platform specific).For what it's worth, Javascript's number type represents a 64 bit double value which does not hold a full 64 bit integer, so I think it would be reasonable to restrict integer precision to 32 bits but if that's the case the Haxe language/compiler should probably overflow in a consistent way (e.g.
2147483647 + 1
should result in -2147483648, or emit the same compiler error I mentioned above). Or, it would be reasonable to support "native precision" integers (which is ~ 2^53) but then the compiler error and the use of| 0
are probably not right.The text was updated successfully, but these errors were encountered: