Same basic operations with Number subclasses now work
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 18:04:11 +0200
changeset 114a0505844750a
parent 113 1b6410322d6e
child 115 3d5597011af0
child 285 c8be2d837788
Same basic operations with Number subclasses now work
emul/src/main/java/java/lang/Double.java
emul/src/main/java/java/lang/Integer.java
vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
     1.1 --- a/emul/src/main/java/java/lang/Double.java	Tue Oct 16 17:40:51 2012 +0200
     1.2 +++ b/emul/src/main/java/java/lang/Double.java	Tue Oct 16 18:04:11 2012 +0200
     1.3 @@ -25,6 +25,8 @@
     1.4  
     1.5  package java.lang;
     1.6  
     1.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     1.8 +
     1.9  /**
    1.10   * The {@code Double} class wraps a value of the primitive type
    1.11   * {@code double} in an object. An object of type
    1.12 @@ -188,6 +190,7 @@
    1.13       * @param   d   the {@code double} to be converted.
    1.14       * @return a string representation of the argument.
    1.15       */
    1.16 +    @JavaScriptBody(args="d", body="return d.toString();")
    1.17      public static String toString(double d) {
    1.18          throw new UnsupportedOperationException();
    1.19      }
    1.20 @@ -497,6 +500,7 @@
    1.21       * @throws     NumberFormatException  if the string does not contain a
    1.22       *             parsable number.
    1.23       */
    1.24 +    @JavaScriptBody(args="s", body="return parseFloat(s);")
    1.25      public static Double valueOf(String s) throws NumberFormatException {
    1.26          throw new UnsupportedOperationException();
    1.27  //        return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
    1.28 @@ -534,6 +538,7 @@
    1.29       * @see    java.lang.Double#valueOf(String)
    1.30       * @since 1.2
    1.31       */
    1.32 +    @JavaScriptBody(args="s", body="return parseFloat(s);")
    1.33      public static double parseDouble(String s) throws NumberFormatException {
    1.34          throw new UnsupportedOperationException();
    1.35  //        return FloatingDecimal.readJavaFormatString(s).doubleValue();
     2.1 --- a/emul/src/main/java/java/lang/Integer.java	Tue Oct 16 17:40:51 2012 +0200
     2.2 +++ b/emul/src/main/java/java/lang/Integer.java	Tue Oct 16 18:04:11 2012 +0200
     2.3 @@ -25,6 +25,8 @@
     2.4  
     2.5  package java.lang;
     2.6  
     2.7 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
     2.8 +
     2.9  /**
    2.10   * The {@code Integer} class wraps a value of the primitive type
    2.11   * {@code int} in an object. An object of type {@code Integer}
    2.12 @@ -439,6 +441,7 @@
    2.13       * @exception  NumberFormatException if the {@code String}
    2.14       *             does not contain a parsable {@code int}.
    2.15       */
    2.16 +    @JavaScriptBody(args={"s", "radix"}, body="return parseInt(s,radix);")
    2.17      public static int parseInt(String s, int radix)
    2.18                  throws NumberFormatException
    2.19      {
     3.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Tue Oct 16 17:40:51 2012 +0200
     3.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java	Tue Oct 16 18:04:11 2012 +0200
     3.3 @@ -80,6 +80,8 @@
     3.4                      && !name.equals("java/lang/Class")
     3.5                      && !name.equals("java/lang/Math")
     3.6                      && !name.equals("java/lang/Number")
     3.7 +                    && !name.equals("java/lang/NumberFormatException")
     3.8 +                    && !name.equals("java/lang/IllegalArgumentException")
     3.9                      && !name.equals("java/lang/Integer")
    3.10                      && !name.equals("java/lang/Float")
    3.11                      && !name.equals("java/lang/Double")
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Tue Oct 16 18:04:11 2012 +0200
     4.3 @@ -0,0 +1,82 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.vm4brwsr;
    4.22 +
    4.23 +import javax.script.Invocable;
    4.24 +import javax.script.ScriptException;
    4.25 +import org.testng.annotations.BeforeClass;
    4.26 +import static org.testng.Assert.*;
    4.27 +import org.testng.annotations.Test;
    4.28 +
    4.29 +/**
    4.30 + *
    4.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.32 + */
    4.33 +public class NumberTest {
    4.34 +    @Test public void integerFromString() throws Exception {
    4.35 +        assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
    4.36 +            Double.valueOf(333), "333"
    4.37 +        );
    4.38 +    }
    4.39 +
    4.40 +    @Test public void doubleFromString() throws Exception {
    4.41 +        assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
    4.42 +            Double.valueOf(33.3), "33.3"
    4.43 +        );
    4.44 +    }
    4.45 +
    4.46 +    @Test public void autoboxDouble() throws Exception {
    4.47 +        assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
    4.48 +            "3.3"
    4.49 +        );
    4.50 +    }
    4.51 +
    4.52 +    
    4.53 +    private static CharSequence codeSeq;
    4.54 +    private static Invocable code;
    4.55 +
    4.56 +    @BeforeClass
    4.57 +    public void compileTheCode() throws Exception {
    4.58 +        if (codeSeq == null) {
    4.59 +            StringBuilder sb = new StringBuilder();
    4.60 +            code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
    4.61 +            codeSeq = sb;
    4.62 +        }
    4.63 +    }
    4.64 +
    4.65 +    private static void assertExec(
    4.66 +        String msg, String methodName, Object expRes, Object... args) throws Exception {
    4.67 +
    4.68 +        Object ret = null;
    4.69 +        try {
    4.70 +            ret = code.invokeFunction(methodName, args);
    4.71 +        } catch (ScriptException ex) {
    4.72 +            fail("Execution failed in\n" + codeSeq, ex);
    4.73 +        } catch (NoSuchMethodException ex) {
    4.74 +            fail("Cannot find method in\n" + codeSeq, ex);
    4.75 +        }
    4.76 +        if (ret == null && expRes == null) {
    4.77 +            return;
    4.78 +        }
    4.79 +        if (expRes.equals(ret)) {
    4.80 +            return;
    4.81 +        }
    4.82 +        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    4.83 +    }
    4.84 +    
    4.85 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Tue Oct 16 18:04:11 2012 +0200
     5.3 @@ -0,0 +1,31 @@
     5.4 +/**
     5.5 + * Back 2 Browser Bytecode Translator
     5.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     5.7 + *
     5.8 + * This program is free software: you can redistribute it and/or modify
     5.9 + * it under the terms of the GNU General Public License as published by
    5.10 + * the Free Software Foundation, version 2 of the License.
    5.11 + *
    5.12 + * This program is distributed in the hope that it will be useful,
    5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    5.15 + * GNU General Public License for more details.
    5.16 + *
    5.17 + * You should have received a copy of the GNU General Public License
    5.18 + * along with this program. Look for COPYING file in the top folder.
    5.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    5.20 + */
    5.21 +package org.apidesign.vm4brwsr;
    5.22 +
    5.23 +/**
    5.24 + *
    5.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    5.26 + */
    5.27 +public class Numbers {
    5.28 +    private static Double autoboxDbl() {
    5.29 +        return 3.3;
    5.30 +    }
    5.31 +    public static String autoboxDblToString() {
    5.32 +        return autoboxDbl().toString().toString();
    5.33 +    }
    5.34 +}