Fill object arrays with null, otherwise use 0 arithmetic
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 14 Jan 2013 14:20:49 +0100
brancharithmetic
changeset 4465b3d5ed03014
parent 445 9e4f01dd6acb
child 447 aa48132c8a80
Fill object arrays with null, otherwise use 0
emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vm/src/test/java/org/apidesign/vm4brwsr/Array.java
vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
     1.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Mon Jan 14 13:21:40 2013 +0100
     1.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js	Mon Jan 14 14:20:49 2013 +0100
     1.3 @@ -2,8 +2,8 @@
     1.4  vm.java_lang_String(false);
     1.5  
     1.6  // we need initialized arrays
     1.7 -Array.prototype.fillNulls = function() {
     1.8 -  for(var i = 0; i < this.length; i++) this[i] = null;
     1.9 +Array.prototype.fillWith = function(value) {
    1.10 +  for(var i = 0; i < this.length; i++) this[i] = value;
    1.11    return this;
    1.12  };
    1.13  Array.prototype.clone__Ljava_lang_Object_2 = function() {
     2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 14 13:21:40 2013 +0100
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 14 14:20:49 2013 +0100
     2.3 @@ -910,19 +910,19 @@
     2.4                  }
     2.5                  case opc_newarray:
     2.6                      ++i; // skip type of array
     2.7 -                    emit(out, "@2 = new Array(@1).fillNulls();",
     2.8 +                    emit(out, "@2 = new Array(@1).fillWith(0);",
     2.9                           smapper.popI(), smapper.pushA());
    2.10                      break;
    2.11                  case opc_anewarray:
    2.12                      i += 2; // skip type of array
    2.13 -                    emit(out, "@2 = new Array(@1).fillNulls();",
    2.14 +                    emit(out, "@2 = new Array(@1).fillWith(null);",
    2.15                           smapper.popI(), smapper.pushA());
    2.16                      break;
    2.17                  case opc_multianewarray: {
    2.18                      i += 2;
    2.19                      int dim = readByte(byteCodes, ++i);
    2.20                      out.append("{ var a0 = new Array(").append(smapper.popI())
    2.21 -                       .append(").fillNulls();");
    2.22 +                       .append(").fillWith(null);");
    2.23                      for (int d = 1; d < dim; d++) {
    2.24                          out.append("\n  var l" + d).append(" = ")
    2.25                             .append(smapper.popI()).append(';');
    2.26 @@ -930,7 +930,7 @@
    2.27                              append(" < a" + (d - 1)).
    2.28                              append(".length; i" + d).append("++) {");
    2.29                          out.append("\n    var a" + d).
    2.30 -                            append (" = new Array(l" + d).append(").fillNulls();");
    2.31 +                            append (" = new Array(l" + d).append(").fillWith(null);");
    2.32                          out.append("\n    a" + (d - 1)).append("[i" + d).append("] = a" + d).
    2.33                              append(";");
    2.34                      }
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Mon Jan 14 13:21:40 2013 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Array.java	Mon Jan 14 14:20:49 2013 +0100
     3.3 @@ -98,6 +98,11 @@
     3.4          return sum;
     3.5      }
     3.6      
     3.7 +    public static int sum(int size) {
     3.8 +        int[] arr = new int[size];
     3.9 +        return arr[0] + arr[1];
    3.10 +    }
    3.11 +    
    3.12      static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
    3.13          while (count-- > 0) {
    3.14              dst[dstBegin++] = value[srcBegin++];
     4.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Mon Jan 14 13:21:40 2013 +0100
     4.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java	Mon Jan 14 14:20:49 2013 +0100
     4.3 @@ -27,6 +27,11 @@
     4.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     4.5   */
     4.6  public class ArrayTest {
     4.7 +    @Test public void intArrayShouldBeFilledWithZeroes() throws Exception {
     4.8 +            assertExec("0 + 0", Array.class, "sum__II", 
     4.9 +            Double.valueOf(0), 2
    4.10 +        );
    4.11 +    }
    4.12      @Test public void verifySimpleIntOperation() throws Exception {
    4.13              assertExec("CheckTheSum", Array.class, "simple__IZ", 
    4.14              Double.valueOf(15), false