Adds JS int number to java.lang.Integer conversion

This commit is contained in:
konsoletyper 2014-02-17 17:13:48 +04:00
parent fa1f8f1c6e
commit 119de7ab82
5 changed files with 25 additions and 13 deletions

View File

@ -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

View File

@ -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);

View File

@ -21,13 +21,13 @@ import org.teavm.javascript.ni.GeneratedBy;
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
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);
}

View File

@ -25,10 +25,12 @@ import org.teavm.model.*;
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
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();
}

View File

@ -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);
}