diff --git a/teavm-core/src/main/java/org/teavm/model/util/ModelUtils.java b/teavm-core/src/main/java/org/teavm/model/util/ModelUtils.java index f728d144b..9fb039bc7 100644 --- a/teavm-core/src/main/java/org/teavm/model/util/ModelUtils.java +++ b/teavm-core/src/main/java/org/teavm/model/util/ModelUtils.java @@ -65,7 +65,7 @@ public final class ModelUtils { return copy; } - private static void copyAnnotations(AnnotationContainerReader src, AnnotationContainer dst) { + public static void copyAnnotations(AnnotationContainerReader src, AnnotationContainer dst) { for (AnnotationReader annot : src.all()) { dst.add(copyAnnotation(annot)); } diff --git a/teavm-jso/src/main/java/org/teavm/jso/JS.java b/teavm-jso/src/main/java/org/teavm/jso/JS.java index bd99b5f6f..a0dc42cde 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/JS.java +++ b/teavm-jso/src/main/java/org/teavm/jso/JS.java @@ -479,7 +479,7 @@ public final class JS { }; } - public static Iterable iterate(final JSStringArrayReader array) { + public static Iterable iterate(final JSStringArrayReader array) { return new Iterable() { @Override public Iterator iterator() { return new Iterator() { diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSBodyGenerator.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSBodyGenerator.java index 40c3525a2..d7abe2da4 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/JSBodyGenerator.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JSBodyGenerator.java @@ -35,7 +35,7 @@ public class JSBodyGenerator implements Generator { boolean isStatic = annot.getValue("isStatic").getBoolean(); List paramNames = annot.getValue("params").getList(); - int bodyParamCount = method.parameterCount(); + int bodyParamCount = isStatic ? method.parameterCount() : method.parameterCount() - 1; int offset = isStatic ? 1 : 0; writer.append("if (!").appendMethodBody(methodRef).append(".$native)").ws().append('{').indent().newLine(); diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java index e55d0a2b9..76e04f012 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/JavascriptNativeProcessor.java @@ -23,6 +23,7 @@ import org.teavm.jso.*; import org.teavm.model.*; import org.teavm.model.instructions.*; import org.teavm.model.util.InstructionVariableMapper; +import org.teavm.model.util.ModelUtils; import org.teavm.model.util.ProgramUtils; /** @@ -80,7 +81,11 @@ class JavascriptNativeProcessor { public void processFinalMethods(ClassHolder cls) { // TODO: don't allow final methods to override anything for (MethodHolder method : cls.getMethods().toArray(new MethodHolder[0])) { - if (method.hasModifier(ElementModifier.FINAL) && method.getProgram() != null) { + if (method.hasModifier(ElementModifier.STATIC)) { + continue; + } + if (method.hasModifier(ElementModifier.FINAL) && method.getProgram() != null && + method.getProgram().basicBlockCount() > 0) { ValueType[] staticSignature = getStaticSignature(method.getReference()); MethodHolder callerMethod = new MethodHolder(new MethodDescriptor(method.getName() + "$static", staticSignature)); @@ -115,6 +120,7 @@ class JavascriptNativeProcessor { } } callerMethod.setProgram(program); + ModelUtils.copyAnnotations(method.getAnnotations(), callerMethod.getAnnotations()); cls.addMethod(callerMethod); } } @@ -188,11 +194,13 @@ class JavascriptNativeProcessor { continue; } if (method.hasModifier(ElementModifier.FINAL)) { - invoke.setMethod(new MethodReference(method.getOwnerName(), method.getName() + "$static", - getStaticSignature(method.getReference()))); + if (method.getProgram() != null && method.getProgram().basicBlockCount() > 0) { + invoke.setMethod(new MethodReference(method.getOwnerName(), method.getName() + "$static", + getStaticSignature(method.getReference()))); + invoke.getArguments().add(0, invoke.getInstance()); + invoke.setInstance(null); + } invoke.setType(InvocationType.SPECIAL); - invoke.getArguments().add(0, invoke.getInstance()); - invoke.setInstance(null); continue; } CallLocation callLocation = new CallLocation(methodToProcess.getReference(), insn.getLocation()); @@ -390,6 +398,9 @@ class JavascriptNativeProcessor { for (int i = 0; i < paramCount; ++i) { params.add(program.createVariable()); } + if (isStatic) { + program.createVariable(); + } methodToProcess.setProgram(program); // Generate invoke instruction @@ -398,7 +409,7 @@ class JavascriptNativeProcessor { invoke.setType(InvocationType.SPECIAL); invoke.setMethod(proxyMethod.getReference()); for (int i = 0; i < paramCount; ++i) { - Variable var = program.createVariable(); + Variable var = program.variableAt(isStatic ? i + 1 : i); invoke.getArguments().add(wrapArgument(location, var, paramTypes[i])); } block.getInstructions().addAll(replacement); @@ -580,7 +591,7 @@ class JavascriptNativeProcessor { } Variable result = program.createVariable(); InvokeInstruction insn = new InvokeInstruction(); - insn.setMethod(new MethodReference(JS.class.getName(), "wrap", type, getWrapperType(type))); + insn.setMethod(new MethodReference(JS.class.getName(), "wrap", getWrappedType(type), getWrapperType(type))); insn.getArguments().add(var); insn.setReceiver(result); insn.setType(InvocationType.SPECIAL); @@ -589,6 +600,21 @@ class JavascriptNativeProcessor { return result; } + private ValueType getWrappedType(ValueType type) { + if (type instanceof ValueType.Array) { + ValueType itemType = ((ValueType.Array)type).getItemType(); + return ValueType.arrayOf(getWrappedType(itemType)); + } else if (type instanceof ValueType.Object) { + if (type.isObject("java.lang.String")) { + return type; + } else { + return ValueType.parse(JSObject.class); + } + } else { + return type; + } + } + private ValueType getWrapperType(ValueType type) { if (type instanceof ValueType.Array) { ValueType itemType = ((ValueType.Array)type).getItemType(); diff --git a/teavm-jso/src/main/java/org/teavm/jso/plugin/NativeJavascriptClassRepository.java b/teavm-jso/src/main/java/org/teavm/jso/plugin/NativeJavascriptClassRepository.java index 4b2483a1b..b7429789c 100644 --- a/teavm-jso/src/main/java/org/teavm/jso/plugin/NativeJavascriptClassRepository.java +++ b/teavm-jso/src/main/java/org/teavm/jso/plugin/NativeJavascriptClassRepository.java @@ -64,6 +64,11 @@ class NativeJavascriptClassRepository { return true; } } + if (cls.getParent() != null && !cls.getParent().equals(cls.getName())) { + if (isJavaScriptClass(cls.getParent())) { + return true; + } + } return false; }