From ada2bf9c589621eb864c46fa70c1c5ac76ca83eb Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Tue, 31 Aug 2021 12:35:56 +0300 Subject: [PATCH] Fix bug in support for resources --- .../teavm/platform/plugin/StringAmplifier.java | 4 ++++ .../plugin/StringAmplifierDependencyPlugin.java | 7 ++++++- .../plugin/StringAmplifierTransformer.java | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifier.java b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifier.java index 51caab845..cd1f17c96 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifier.java +++ b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifier.java @@ -25,4 +25,8 @@ final class StringAmplifier { @PluggableDependency(StringAmplifierDependencyPlugin.class) @Unmanaged static native String amplify(String string); + + @PluggableDependency(StringAmplifierDependencyPlugin.class) + @Unmanaged + static native String[] amplifyArray(String[] strings); } diff --git a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierDependencyPlugin.java b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierDependencyPlugin.java index 3ebb90942..632a619fd 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierDependencyPlugin.java +++ b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierDependencyPlugin.java @@ -22,6 +22,11 @@ import org.teavm.dependency.MethodDependency; public class StringAmplifierDependencyPlugin implements DependencyPlugin { @Override public void methodReached(DependencyAgent agent, MethodDependency method) { - method.getResult().propagate(agent.getType("java.lang.String")); + if (method.getMethod().getName().equals("amplify")) { + method.getResult().propagate(agent.getType("java.lang.String")); + } else { + method.getResult().propagate(agent.getType("[Ljava/lang/String;")); + method.getResult().getArrayItem().propagate(agent.getType("java.lang.String")); + } } } diff --git a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierTransformer.java b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierTransformer.java index 09db7726a..262ebf195 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierTransformer.java +++ b/platform/src/main/java/org/teavm/platform/plugin/StringAmplifierTransformer.java @@ -28,6 +28,7 @@ import org.teavm.model.Variable; import org.teavm.model.instructions.InvocationType; import org.teavm.model.instructions.InvokeInstruction; import org.teavm.platform.metadata.Resource; +import org.teavm.platform.metadata.ResourceMap; public class StringAmplifierTransformer implements ClassHolderTransformer { @Override @@ -54,7 +55,21 @@ public class StringAmplifierTransformer implements ClassHolderTransformer { MethodReference method = invoke.getMethod(); String owningClass = method.getClassName(); if (hierarchy.isSuperType(Resource.class.getName(), owningClass, false)) { - if (method.getReturnType().isObject(String.class)) { + if (method.getClassName().equals(ResourceMap.class.getName())) { + if (method.getName().equals("keys")) { + Variable var = program.createVariable(); + InvokeInstruction amplifyInstruction = new InvokeInstruction(); + amplifyInstruction.setMethod(new MethodReference(StringAmplifier.class, "amplifyArray", + String[].class, String[].class)); + amplifyInstruction.setType(InvocationType.SPECIAL); + amplifyInstruction.setArguments(var); + amplifyInstruction.setReceiver(invoke.getReceiver()); + amplifyInstruction.setLocation(invoke.getLocation()); + + invoke.setReceiver(var); + invoke.insertNext(amplifyInstruction); + } + } else if (method.getReturnType().isObject(String.class)) { Variable var = program.createVariable(); InvokeInstruction amplifyInstruction = new InvokeInstruction(); amplifyInstruction.setMethod(new MethodReference(StringAmplifier.class, "amplify",