Skip to content
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 Backend: Long_fromNumber does not gracefully support NaN #1000

Open
McMurat opened this issue Jan 20, 2025 · 4 comments
Open

JS Backend: Long_fromNumber does not gracefully support NaN #1000

McMurat opened this issue Jan 20, 2025 · 4 comments

Comments

@McMurat
Copy link

McMurat commented Jan 20, 2025

When converting a double to a long the conversion may fail when the double is a NaN:

RangeError: The number NaN cannot be converted to a BigInt because it is not an integer at BigInt (<anonymous>) at Long_fromNumber

Expected behaviour: Return 0L instead.

@konsoletyper
Copy link
Owner

Note that fixing this may affect performance. Since such situation is quite rare, I doubt if it worth fixing. Also, did you try WasmGC backend?

@McMurat
Copy link
Author

McMurat commented Jan 20, 2025

I think this case is not uncommon and can also happen rather "randomly" if some computation gets numerical instable. I really like that TeaVM is respecting the Java Language Specification to a high level, so it would be nice if this would be no exception.

If you are scared of the extra isNaN check, then it would also be possible to wrap the BigInt constructor in a try { ... } catch { return BigInt(0) }.

Unfortunately, I am running libgdx and cannot use WasmGC backend yet as I am also relying on the JS multithread feature.

@konsoletyper
Copy link
Owner

I think this case is not uncommon and can also happen rather "randomly" if some computation gets numerical instable

I have another opinion regarding this. Also, it's quite easy to fix on source code level: you can insert additional if (isNaN) in your Java/Kotlin code.

I really like that TeaVM is respecting the Java Language Specification to a high level

No, TeaVM has a lots of places where it violates this. For example, TeaVM does not produce fair NPE, CCE or AAIOBE by default. Also, there are lots of edge cases like integer zero division, etc. GWT, j2cl, Kotlin/JS - all behave the same way, sacrificing some rare edge cases for performance.

If you are scared of the extra isNaN check, then it would also be possible to wrap the BigInt constructor in a try { ... } catch { return BigInt(0) }.

No, this would kill performance even further.

@McMurat
Copy link
Author

McMurat commented Jan 20, 2025

It's just complicated when these things occur in third party dependencies where you cannot add these extra checks.

I know that integer zero division, NPE, CCE, ... are not handled correctly. But these are all cases which would throw in a real JVM and are widely considered as programmer's faults. It's very rare and very bad practice to rely on these exceptions for control flow. But here it's different. TeaVM throws although a JVM would not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants