From 119de7ab82a5053d4cca4fdbd9f3c22df040e678 Mon Sep 17 00:00:00 2001 From: konsoletyper Date: Mon, 17 Feb 2014 17:13:48 +0400 Subject: [PATCH] Adds JS int number to java.lang.Integer conversion --- .../org/teavm/html4j/JavaScriptBodyDependency.java | 7 ++++--- .../org/teavm/html4j/JavaScriptBodyGenerator.java | 4 ++-- ...vaScriptBodyConverter.java => JavaScriptConv.java} | 8 ++++---- ...terGenerator.java => JavaScriptConvGenerator.java} | 11 +++++++---- .../html4j/test/JavaScriptBodyConversionTests.java | 8 ++++++++ 5 files changed, 25 insertions(+), 13 deletions(-) rename teavm-html4j/src/main/java/org/teavm/html4j/{JavaScriptBodyConverter.java => JavaScriptConv.java} (81%) rename teavm-html4j/src/main/java/org/teavm/html4j/{JavaScriptBodyConverterGenerator.java => JavaScriptConvGenerator.java} (89%) diff --git a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyDependency.java b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyDependency.java index ded276f45..02bad2296 100644 --- a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyDependency.java +++ b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyDependency.java @@ -76,9 +76,10 @@ public class JavaScriptBodyDependency implements DependencyListener { } private void includeDefaultDependencies(DependencyChecker dependencyChecker) { - dependencyChecker.attachMethodGraph(JavaScriptBodyConverterGenerator.fromJsMethod); - dependencyChecker.attachMethodGraph(JavaScriptBodyConverterGenerator.toJsMethod); - dependencyChecker.attachMethodGraph(JavaScriptBodyConverterGenerator.intValueMethod); + dependencyChecker.attachMethodGraph(JavaScriptConvGenerator.fromJsMethod); + dependencyChecker.attachMethodGraph(JavaScriptConvGenerator.toJsMethod); + dependencyChecker.attachMethodGraph(JavaScriptConvGenerator.intValueMethod); + dependencyChecker.attachMethodGraph(JavaScriptConvGenerator.valueOfIntMethod); } @Override diff --git a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java index 88c7677b2..f89c13033 100644 --- a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java +++ b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyGenerator.java @@ -63,7 +63,7 @@ public class JavaScriptBodyGenerator implements Generator { private void wrapParameter(SourceWriter writer, ValueType type, String param) throws IOException { if (type.isObject("java.lang.Object")) { - writer.appendMethodBody(JavaScriptBodyConverterGenerator.toJsMethod); + writer.appendMethodBody(JavaScriptConvGenerator.toJsMethod); writer.append("(").append(param).append(")"); } else { writer.append(param); @@ -72,7 +72,7 @@ public class JavaScriptBodyGenerator implements Generator { private void unwrapValue(SourceWriter writer, ValueType type, String param) throws IOException { if (type.isObject("java.lang.Object")) { - writer.appendMethodBody(JavaScriptBodyConverterGenerator.fromJsMethod); + writer.appendMethodBody(JavaScriptConvGenerator.fromJsMethod); writer.append("(").append(param).append(")"); } else { writer.append(param); diff --git a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverter.java b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConv.java similarity index 81% rename from teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverter.java rename to teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConv.java index 1dc0d0fdd..4139f076c 100644 --- a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverter.java +++ b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConv.java @@ -21,13 +21,13 @@ import org.teavm.javascript.ni.GeneratedBy; * * @author Alexey Andreev */ -public final class JavaScriptBodyConverter { - private JavaScriptBodyConverter() { +public final class JavaScriptConv { + private JavaScriptConv() { } - @GeneratedBy(JavaScriptBodyConverterGenerator.class) + @GeneratedBy(JavaScriptConvGenerator.class) public static native Object toJavaScript(Object obj); - @GeneratedBy(JavaScriptBodyConverterGenerator.class) + @GeneratedBy(JavaScriptConvGenerator.class) public static native Object fromJavaScript(Object obj); } diff --git a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverterGenerator.java b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java similarity index 89% rename from teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverterGenerator.java rename to teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java index 930695608..e9ba59792 100644 --- a/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptBodyConverterGenerator.java +++ b/teavm-html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java @@ -25,10 +25,12 @@ import org.teavm.model.*; * * @author Alexey Andreev */ -public class JavaScriptBodyConverterGenerator implements Generator { - private static final String convCls = JavaScriptBodyConverter.class.getName(); +public class JavaScriptConvGenerator implements Generator { + private static final String convCls = JavaScriptConv.class.getName(); static final MethodReference intValueMethod = new MethodReference("java.lang.Integer", new MethodDescriptor("intValue", ValueType.INTEGER)); + static final MethodReference valueOfIntMethod = new MethodReference("java.lang.Integer", + new MethodDescriptor("valueOf", ValueType.INTEGER, ValueType.object("java.lang.Integer"))); private static final ValueType objType = ValueType.object("java.lang.Object"); static final MethodReference toJsMethod = new MethodReference(convCls, new MethodDescriptor( "toJavaScript", objType, objType)); @@ -86,8 +88,9 @@ public class JavaScriptBodyConverterGenerator implements Generator { writer.append(" else if (" + obj + ".constructor === ").appendClass("java.lang.String") .append(") {").indent().softNewLine(); writer.append("return $rt_str(" + obj + ");").softNewLine(); - writer.outdent().append("}"); - writer.ws().append("else").ws().append("{").indent().softNewLine(); + writer.outdent().append("} else if (" + obj + " | 0 === " + obj + ") {").indent().softNewLine(); + writer.append("return ").appendMethodBody(valueOfIntMethod).append("(" + obj + ");").softNewLine(); + writer.outdent().append("} else {").indent().softNewLine(); writer.append("return ").append(obj).append(";").softNewLine(); writer.outdent().append("}").softNewLine(); } diff --git a/teavm-html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyConversionTests.java b/teavm-html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyConversionTests.java index b64d34f37..96167b4c8 100644 --- a/teavm-html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyConversionTests.java +++ b/teavm-html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyConversionTests.java @@ -29,6 +29,14 @@ public class JavaScriptBodyConversionTests { assertEquals(23, returnAsInt(23)); } + @Test + public void convertsIntegerResult() { + assertEquals(23, returnAsObject(23)); + } + @JavaScriptBody(args = { "value" }, body = "return value;") private native int returnAsInt(Object value); + + @JavaScriptBody(args = { "value" }, body = "return value;") + private native Object returnAsObject(int value); }