Skip to content

Commit

Permalink
fix UTF-8 script loading
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Dec 3, 2023
1 parent 4cf32a8 commit 313a328
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
import com.onthegomap.planetiler.util.SortKey;
import com.onthegomap.planetiler.util.Translations;
import com.onthegomap.planetiler.util.ZoomFunction;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -105,7 +108,12 @@ public static LuaEnvironment loadScript(Arguments arguments, String script, Stri
filesLoaded.add(path);
return oldFilder.findResource(path.toString());
};
globals.load(script, fileName).call();
// ensure source is treated as UTF-8
try (var in = new ByteArrayInputStream(script.getBytes(StandardCharsets.UTF_8))) {
globals.load(in, fileName, "t", globals).call();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
LuaProfile profile = new LuaProfile(env);
env.profile = new LuaProfile(env);
env.main = globals.get("main");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,20 @@ public int[] call() {
assertConvertsTo(2, env.main.call());
}

@Test
void testEmoji() {
var env = load("""
function main()
return '👍'
end
""", Map.of("obj", new Object() {
public int[] call() {
return new int[]{1};
}
}));
assertConvertsTo("👍", env.main.call());
}

private static <T> void assertConvertsTo(T java, LuaValue lua) {
assertConvertsTo(java.getClass(), java, lua);
}
Expand Down

0 comments on commit 313a328

Please sign in to comment.