-
Notifications
You must be signed in to change notification settings - Fork 146
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
Transpiled autoboxing of byte to Byte does not work with unsigned byte values in JavaScript anymore #260
Comments
A byte value in Java is signed and could be only between values '-128' and '127'. You can never get a value out of this range in JDK. And only reason if you would get a value out of this range in J2CL would be incorrect JS boundary code that declares You mentioned the value is coming from FileReader; given J2CL doesn't provide that class, I recommended checking the implementation of the class and make sure it conforms to boundaries of |
@gkdn thank you very much for the explanation! I am aware that Java uses signed bytes. In JavaScript, it is possible to work with unsigned or signed bytes (although the type "byte" does not exist). In our case, our JavaScript code that is using the transpiled Java library was working with unsigned arrays in the form of Since the code was working fine until now, I was under the assumption, that the transpiled Java code is designed to both function when using both signed or unsigned JavaScript bytes. However, it looks like the transpiled Java code should only be called with signed bytes in JavaScript, or else there will be runtime errors? Since this change of the transpiled My workaround for now, without changing too much of our code, is the following: Instead of the Java code |
Yes, you need to adhere to the rules of Java otherwise you would be leaking incorrect values into type system (e.g. if type is byte, the value is assumed to be correct). The way the error will manifest itself could be in surprising; might be runtime error (if you are lucky), might just some math error or number printing error etc.
As I mentioned these are errors distant to the actual source of the problem; the changes there are not intended to be no-op and should be no-op can expose such typing errors (it might have showed up in any change - that's why I said incorrect values has undefined behavior). The way to address the issue is; when you expose such API from JavaScript, you should use a Java type at the JS boundary that actually correctly encapsulates the JS value. For example, if the value is 0-255, you can use Another example of that would be; let's say the JS value can be I noticed that we never open-sourced the documentation around type representations. I just did that which might help a bit here. |
Just note that if you simply change the view to Int8Array, this will have correct ranges and values could be treated as byte. |
Describe the bug
I am not sure if this is a bug or something that was intentionally changed.
The transpiled version of
Byte.valueOf(byte)
accepts now only signed byte values (-128 to 127). If a value > 127 or a value < -128 is given,undefined
is returned.In previous versions of j2cl, the transpiled value
Byte.valueOf(byte)
always returned a Byte object, regardless of the value (values > 255 also worked). I am not sure, when this change was introduced.This change breaks our application. We have a Java library that parses PDF files. Reading the PDF file is done via an interface. In Java, we use e.g.
RandomAccessFile
, and in the transpiled JavaScript version we useFileReader
(returning unsigned byte values 0-255, therefore the error). In Java, the code works fine. In JavaScript, the code also worked, but because of this change not anymore. (at least, whereByte.valueOf(byte)
is used in the transpiled version; most other parts seem to work as before).More specifcally, in Java, I have the following code:
Transpiled version.
I recently upgraded bazel (and it seems j2cl too), because it seems there was a problem with the
new String(byte[])
constructor, which, however, seems to fixed now. This is how I stumbled on this error.Bazel version
7.4.0 (I was previously on 5.4.1)
Expected behavior
The transpiled JavaScript version of
Byte.valueOf(byte)
should handle unsigned bytes correctly.The text was updated successfully, but these errors were encountered: