vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
changeset 128 8db5cf267d74
parent 127 5134b17d623c
child 129 15df78d24302
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 30 20:29:55 2012 +0100
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 30 22:22:18 2012 +0100
     1.3 @@ -236,7 +236,7 @@
     1.4          for (int i = 0; i < byteCodes.length; i++) {
     1.5              int prev = i;
     1.6              out.append("    case " + i).append(": ");
     1.7 -            final int c = (byteCodes[i] + 256) % 256;
     1.8 +            final int c = readByte(byteCodes, i);
     1.9              switch (c) {
    1.10                  case bc_aload_0:
    1.11                  case bc_iload_0:
    1.12 @@ -271,7 +271,7 @@
    1.13                  case bc_fload:
    1.14                  case bc_dload:
    1.15                  case bc_aload: {
    1.16 -                    final int indx = (byteCodes[++i] + 256) % 256;
    1.17 +                    final int indx = readByte(byteCodes, ++i);
    1.18                      out.append("stack.push(arg").append(indx + ");");
    1.19                      break;
    1.20                  }
    1.21 @@ -280,7 +280,7 @@
    1.22                  case bc_fstore:
    1.23                  case bc_dstore:
    1.24                  case bc_astore: {
    1.25 -                    final int indx = (byteCodes[++i] + 256) % 256;
    1.26 +                    final int indx = readByte(byteCodes, ++i);
    1.27                      out.append("arg" + indx).append(" = stack.pop()");
    1.28                      break;
    1.29                  }
    1.30 @@ -369,7 +369,7 @@
    1.31                      out.append("{ var v = stack.pop(); stack.push(stack.pop() >>> v); }");
    1.32                      break;
    1.33                  case bc_iinc: {
    1.34 -                    final int varIndx = (byteCodes[++i] + 256) % 256;
    1.35 +                    final int varIndx = readByte(byteCodes, ++i);
    1.36                      final int incrBy = byteCodes[++i];
    1.37                      if (incrBy == 1) {
    1.38                          out.append("arg" + varIndx).append("++;");
    1.39 @@ -442,7 +442,7 @@
    1.40                      out.append("stack.push(5);");
    1.41                      break;
    1.42                  case bc_ldc: {
    1.43 -                    int indx = byteCodes[++i];
    1.44 +                    int indx = readByte(byteCodes, ++i);
    1.45                      CPEntry entry = jc.getConstantPool().get(indx);
    1.46                      String v = encodeConstant(entry);
    1.47                      out.append("stack.push(").append(v).append(");");
    1.48 @@ -553,7 +553,7 @@
    1.49                      break;
    1.50                  }
    1.51                  case bc_lookupswitch: {
    1.52 -                    int table = (i - 1) / 4 * 4 + 4;
    1.53 +                    int table = i / 4 * 4 + 4;
    1.54                      int dflt = i + readInt4(byteCodes, table);
    1.55                      table += 4;
    1.56                      int n = readInt4(byteCodes, table);
    1.57 @@ -571,7 +571,7 @@
    1.58                      break;
    1.59                  }
    1.60                  case bc_tableswitch: {
    1.61 -                    int table = (i - 1) / 4 * 4 + 4;
    1.62 +                    int table = i / 4 * 4 + 4;
    1.63                      int dflt = i + readInt4(byteCodes, table);
    1.64                      table += 4;
    1.65                      int low = readInt4(byteCodes, table);
    1.66 @@ -622,6 +622,26 @@
    1.67                      out.append("stack.push(new Array(stack.pop()));");
    1.68                      break;
    1.69                  }
    1.70 +                case bc_multianewarray: {
    1.71 +                    i += 2;
    1.72 +                    int dim = readByte(byteCodes, ++i);
    1.73 +                    out.append("{ var a0 = new Array(stack.pop());");
    1.74 +                    for (int d = 1; d < dim; d++) {
    1.75 +                        out.append("\n  var l" + d).append(" = stack.pop();");
    1.76 +                        out.append("\n  for (var i" + d).append (" = 0; i" + d).
    1.77 +                            append(" < a" + (d - 1)).
    1.78 +                            append(".length; i" + d).append("++) {");
    1.79 +                        out.append("\n    var a" + d).
    1.80 +                            append (" = new Array(l" + d).append(");");
    1.81 +                        out.append("\n    a" + (d - 1)).append("[i" + d).append("] = a" + d).
    1.82 +                            append(";");
    1.83 +                    }
    1.84 +                    for (int d = 1; d < dim; d++) {
    1.85 +                        out.append("\n  }");
    1.86 +                    }
    1.87 +                    out.append("\nstack.push(a0); }");
    1.88 +                    break;
    1.89 +                }
    1.90                  case bc_arraylength:
    1.91                      out.append("stack.push(stack.pop().length);");
    1.92                      break;
    1.93 @@ -731,7 +751,7 @@
    1.94              out.append(" //");
    1.95              for (int j = prev; j <= i; j++) {
    1.96                  out.append(" ");
    1.97 -                final int cc = (byteCodes[j] + 256) % 256;
    1.98 +                final int cc = readByte(byteCodes, j);
    1.99                  out.append(Integer.toString(cc));
   1.100              }
   1.101              out.append("\n");
   1.102 @@ -758,6 +778,9 @@
   1.103          final int a = byteCodes[offsetInstruction + 3];
   1.104          return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
   1.105      }
   1.106 +    private int readByte(byte[] byteCodes, int offsetInstruction) {
   1.107 +        return (byteCodes[offsetInstruction] + 256) % 256;
   1.108 +    }
   1.109      
   1.110      private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
   1.111          int cnt = 0;