From 313a32874b7848fc86de6e28ba8de65550b9324b Mon Sep 17 00:00:00 2001 From: Mike Barry Date: Sun, 3 Dec 2023 06:56:16 -0500 Subject: [PATCH] fix UTF-8 script loading --- .../experimental/lua/LuaEnvironment.java | 10 +++++++++- .../experimental/lua/LuaEnvironmentTests.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/planetiler-experimental/src/main/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironment.java b/planetiler-experimental/src/main/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironment.java index 20c50dec4a..98c535017b 100644 --- a/planetiler-experimental/src/main/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironment.java +++ b/planetiler-experimental/src/main/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironment.java @@ -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; @@ -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"); diff --git a/planetiler-experimental/src/test/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironmentTests.java b/planetiler-experimental/src/test/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironmentTests.java index fa305a7cb2..2b38bc93ad 100644 --- a/planetiler-experimental/src/test/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironmentTests.java +++ b/planetiler-experimental/src/test/java/com/onthegomap/planetiler/experimental/lua/LuaEnvironmentTests.java @@ -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 void assertConvertsTo(T java, LuaValue lua) { assertConvertsTo(java.getClass(), java, lua); }