vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
changeset 115 3d5597011af0
parent 106 346633cd13d6
child 127 5134b17d623c
     1.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Oct 16 12:42:00 2012 +0200
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Wed Oct 17 15:59:04 2012 +0200
     1.3 @@ -569,6 +569,43 @@
     1.4                      i += 2;
     1.5                      break;
     1.6                  }
     1.7 +                case bc_lookupswitch: {
     1.8 +                    int table = (i - 1) / 4 * 4 + 4;
     1.9 +                    int dflt = i + readInt4(byteCodes, table);
    1.10 +                    table += 4;
    1.11 +                    int n = readInt4(byteCodes, table);
    1.12 +                    table += 4;
    1.13 +                    out.append("switch (stack.pop()) {\n");
    1.14 +                    while (n-- > 0) {
    1.15 +                        int cnstnt = readInt4(byteCodes, table);
    1.16 +                        table += 4;
    1.17 +                        int offset = i + readInt4(byteCodes, table);
    1.18 +                        table += 4;
    1.19 +                        out.append("  case " + cnstnt).append(": gt = " + offset).append("; continue;\n");
    1.20 +                    }
    1.21 +                    out.append("  default: gt = " + dflt).append("; continue;\n}");
    1.22 +                    i = table - 1;
    1.23 +                    break;
    1.24 +                }
    1.25 +                case bc_tableswitch: {
    1.26 +                    int table = (i - 1) / 4 * 4 + 4;
    1.27 +                    int dflt = i + readInt4(byteCodes, table);
    1.28 +                    table += 4;
    1.29 +                    int low = readInt4(byteCodes, table);
    1.30 +                    table += 4;
    1.31 +                    int high = readInt4(byteCodes, table);
    1.32 +                    table += 4;
    1.33 +                    out.append("switch (stack.pop()) {\n");
    1.34 +                    while (low <= high) {
    1.35 +                        int offset = i + readInt4(byteCodes, table);
    1.36 +                        table += 4;
    1.37 +                        out.append("  case " + low).append(": gt = " + offset).append("; continue;\n");
    1.38 +                        low++;
    1.39 +                    }
    1.40 +                    out.append("  default: gt = " + dflt).append("; continue;\n}");
    1.41 +                    i = table - 1;
    1.42 +                    break;
    1.43 +                }
    1.44                  case bc_invokeinterface: {
    1.45                      i = invokeVirtualMethod(byteCodes, i) + 2;
    1.46                      break;
    1.47 @@ -731,6 +768,13 @@
    1.48          final int indxLo = byteCodes[offsetInstruction + 2];
    1.49          return (indxHi & 0xffffff00) | (indxLo & 0xff);
    1.50      }
    1.51 +    private int readInt4(byte[] byteCodes, int offsetInstruction) {
    1.52 +        final int d = byteCodes[offsetInstruction + 0] << 24;
    1.53 +        final int c = byteCodes[offsetInstruction + 1] << 16;
    1.54 +        final int b = byteCodes[offsetInstruction + 2] << 8;
    1.55 +        final int a = byteCodes[offsetInstruction + 3];
    1.56 +        return (d & 0xff000000) | (c & 0xff0000) | (b & 0xff00) | (a & 0xff);
    1.57 +    }
    1.58      
    1.59      private static int countArgs(String descriptor, boolean[] hasReturnType, StringBuilder sig) {
    1.60          int cnt = 0;