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

fix(java): java engine now passes UTF-8 encoded strings correctly on Windows #202

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

sighphyre
Copy link
Member

This forces the Java engine to encode its strings as UTF-8 pointers rather than whatever the JVM feels like (thank you, Windows, very nice). To figure out what was going on here, there's some redesign to the way the pointers are materialized so that they bubble the Rust errors to the caller

@@ -69,14 +70,10 @@ public UnleashEngine(List<IStrategy> customStrategies, IStrategy fallbackStrateg
}
}

public void takeState(String toggles) throws YggdrasilInvalidInputException {
YggResponse<Void> response = read(yggdrasil.takeState(this.enginePtr, toggles),
public void takeState(String toggles) throws YggdrasilInvalidInputException, YggdrasilError {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the changes from here to line 148 are basically the same. Use read now directly returns the object requested rather than the Yggdrasil wrapping response. If the method passes a string to Yggdrasil, that's now encoded explicitly as UTF-8 before send

* Handle reading from a pointer into an YggdrasilResponse and unwrapping that
* to an object
*/
private <T> T read(Pointer pointer, TypeReference<YggResponse<T>> clazz) throws YggdrasilError {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just some convenience, this method now always bubbles the actual error response from Yggdrasil and also unwraps the response before returning it to the caller so that the caller doesn't have to check for errors, it can just expect an exception

/** Handle reading from a pointer into a String and mapping it to an object */
private <T> T read(Pointer pointer, TypeReference<T> clazz) {
private <T> T readRaw(Pointer pointer, TypeReference<T> clazz) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintained for cases where Yggdrasil returns static entities that are not encoded with the normal response formatting (JSON blob signalling if the call succeeded etc)

@@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.sun.jna.Memory;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just spotted this and I believe com.sun.jna is only part of Oracle's distribution of Java. It can be added as a dependency alongside Unleash but I'm not sure if we want to go this route.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It comes from this library: https://github.com/java-native-access/jna, which is LGPL licensed and we've been testing this on OpenJDK, I think it's fine. This is already an import, it's just new in this file

We may yeet JNA for different reasons though

Copy link
Contributor

@gastonfournier gastonfournier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

* chore(java): add some tests for camel case mapping
@sighphyre sighphyre merged commit 6d8b5af into main Jan 17, 2025
1 check passed
@sighphyre sighphyre deleted the fix/java-engine-now-passes-utf-strings-on-windows branch January 17, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants