Update Kotlin hacks

This commit is contained in:
Alexey Andreev 2021-03-12 18:39:23 +03:00
parent 2765a6344f
commit 4f9346e4e3

View File

@ -17,15 +17,22 @@ package org.teavm.classlib.impl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.teavm.model.AccessLevel;
import org.teavm.model.BasicBlock;
import org.teavm.model.ClassHolder; import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer; import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext; import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.ElementModifier;
import org.teavm.model.MethodHolder; import org.teavm.model.MethodHolder;
import org.teavm.model.Program;
import org.teavm.model.instructions.ExitInstruction;
import org.teavm.model.instructions.StringConstantInstruction;
public class KotlinHacks implements ClassHolderTransformer { public class KotlinHacks implements ClassHolderTransformer {
@Override @Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) { public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
if (cls.getName().equals("kotlin.jvm.internal.Lambda")) { if (cls.getName().equals("kotlin.jvm.internal.Lambda")
|| cls.getName().equals("kotlin.coroutines.jvm.internal.BaseContinuationImpl")) {
List<MethodHolder> methodsToRemove = new ArrayList<>(); List<MethodHolder> methodsToRemove = new ArrayList<>();
for (MethodHolder method : cls.getMethods()) { for (MethodHolder method : cls.getMethods()) {
if (method.getName().equals("toString")) { if (method.getName().equals("toString")) {
@ -35,6 +42,26 @@ public class KotlinHacks implements ClassHolderTransformer {
for (MethodHolder method : methodsToRemove) { for (MethodHolder method : methodsToRemove) {
cls.removeMethod(method); cls.removeMethod(method);
} }
} else if (cls.getName().equals("kotlin.jvm.internal.Reflection")) {
for (MethodHolder method : cls.getMethods()) {
if (method.getLevel() == AccessLevel.PUBLIC && method.hasModifier(ElementModifier.STATIC)
&& method.getName().equals("renderLambdaToString")
&& method.getResultType().isObject(String.class)) {
Program program = new Program();
BasicBlock block = program.createBasicBlock();
StringConstantInstruction stringConstant = new StringConstantInstruction();
stringConstant.setReceiver(program.createVariable());
stringConstant.setConstant("lambda");
block.add(stringConstant);
ExitInstruction exit = new ExitInstruction();
exit.setValueToReturn(stringConstant.getReceiver());
block.add(exit);
method.setProgram(program);
}
}
} }
} }
} }