# HG changeset patch # User Jaroslav Tulach # Date 1350403451 -7200 # Node ID a0505844750a9b9168f0dec0a48bd5010871be9a # Parent 1b6410322d6ecb187245ec7de481867f73056186 Same basic operations with Number subclasses now work diff -r 1b6410322d6e -r a0505844750a emul/src/main/java/java/lang/Double.java --- a/emul/src/main/java/java/lang/Double.java Tue Oct 16 17:40:51 2012 +0200 +++ b/emul/src/main/java/java/lang/Double.java Tue Oct 16 18:04:11 2012 +0200 @@ -25,6 +25,8 @@ package java.lang; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + /** * The {@code Double} class wraps a value of the primitive type * {@code double} in an object. An object of type @@ -188,6 +190,7 @@ * @param d the {@code double} to be converted. * @return a string representation of the argument. */ + @JavaScriptBody(args="d", body="return d.toString();") public static String toString(double d) { throw new UnsupportedOperationException(); } @@ -497,6 +500,7 @@ * @throws NumberFormatException if the string does not contain a * parsable number. */ + @JavaScriptBody(args="s", body="return parseFloat(s);") public static Double valueOf(String s) throws NumberFormatException { throw new UnsupportedOperationException(); // return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue()); @@ -534,6 +538,7 @@ * @see java.lang.Double#valueOf(String) * @since 1.2 */ + @JavaScriptBody(args="s", body="return parseFloat(s);") public static double parseDouble(String s) throws NumberFormatException { throw new UnsupportedOperationException(); // return FloatingDecimal.readJavaFormatString(s).doubleValue(); diff -r 1b6410322d6e -r a0505844750a emul/src/main/java/java/lang/Integer.java --- a/emul/src/main/java/java/lang/Integer.java Tue Oct 16 17:40:51 2012 +0200 +++ b/emul/src/main/java/java/lang/Integer.java Tue Oct 16 18:04:11 2012 +0200 @@ -25,6 +25,8 @@ package java.lang; +import org.apidesign.bck2brwsr.core.JavaScriptBody; + /** * The {@code Integer} class wraps a value of the primitive type * {@code int} in an object. An object of type {@code Integer} @@ -439,6 +441,7 @@ * @exception NumberFormatException if the {@code String} * does not contain a parsable {@code int}. */ + @JavaScriptBody(args={"s", "radix"}, body="return parseInt(s,radix);") public static int parseInt(String s, int radix) throws NumberFormatException { diff -r 1b6410322d6e -r a0505844750a vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Tue Oct 16 17:40:51 2012 +0200 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java Tue Oct 16 18:04:11 2012 +0200 @@ -80,6 +80,8 @@ && !name.equals("java/lang/Class") && !name.equals("java/lang/Math") && !name.equals("java/lang/Number") + && !name.equals("java/lang/NumberFormatException") + && !name.equals("java/lang/IllegalArgumentException") && !name.equals("java/lang/Integer") && !name.equals("java/lang/Float") && !name.equals("java/lang/Double") diff -r 1b6410322d6e -r a0505844750a vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Tue Oct 16 18:04:11 2012 +0200 @@ -0,0 +1,82 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import javax.script.Invocable; +import javax.script.ScriptException; +import org.testng.annotations.BeforeClass; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class NumberTest { + @Test public void integerFromString() throws Exception { + assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String", + Double.valueOf(333), "333" + ); + } + + @Test public void doubleFromString() throws Exception { + assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String", + Double.valueOf(33.3), "33.3" + ); + } + + @Test public void autoboxDouble() throws Exception { + assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String", + "3.3" + ); + } + + + private static CharSequence codeSeq; + private static Invocable code; + + @BeforeClass + public void compileTheCode() throws Exception { + if (codeSeq == null) { + StringBuilder sb = new StringBuilder(); + code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers"); + codeSeq = sb; + } + } + + private static void assertExec( + String msg, String methodName, Object expRes, Object... args) throws Exception { + + Object ret = null; + try { + ret = code.invokeFunction(methodName, args); + } catch (ScriptException ex) { + fail("Execution failed in\n" + codeSeq, ex); + } catch (NoSuchMethodException ex) { + fail("Cannot find method in\n" + codeSeq, ex); + } + if (ret == null && expRes == null) { + return; + } + if (expRes.equals(ret)) { + return; + } + assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq); + } + +} diff -r 1b6410322d6e -r a0505844750a vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Tue Oct 16 18:04:11 2012 +0200 @@ -0,0 +1,31 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +/** + * + * @author Jaroslav Tulach + */ +public class Numbers { + private static Double autoboxDbl() { + return 3.3; + } + public static String autoboxDblToString() { + return autoboxDbl().toString().toString(); + } +}