rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
branchflow
changeset 1816 6c1fa412c72d
parent 1814 ea9fd59c8b62
child 1842 dd4dabfead82
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Thu Mar 12 11:16:15 2015 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Fri Mar 13 11:41:04 2015 +0100
     1.3 @@ -466,9 +466,13 @@
     1.4              didBranches = false;
     1.5              lastStackFrame = 0;
     1.6          } else {
     1.7 -            didBranches = true;
     1.8              lastStackFrame = -1;
     1.9 -            append("\n  var gt = 0;\n");
    1.10 +            if (flow == null) {
    1.11 +                didBranches = true;
    1.12 +                append("\n  var gt = 0;\n");
    1.13 +            } else {
    1.14 +                didBranches = false;
    1.15 +            }
    1.16          }
    1.17          
    1.18          int openBraces = 0;
    1.19 @@ -486,7 +490,7 @@
    1.20              }
    1.21              if (lastStackFrame != stackMapIterator.getFrameIndex()) {
    1.22                  smapper.flush(this);
    1.23 -                if (i != 0) {
    1.24 +                if (flow == null && i != 0) {
    1.25                      append("    }\n");
    1.26                  }
    1.27                  if (openBraces > 64) {
    1.28 @@ -500,8 +504,16 @@
    1.29                  lastStackFrame = stackMapIterator.getFrameIndex();
    1.30                  lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
    1.31                  smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
    1.32 -                append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
    1.33 -                openBraces++;
    1.34 +                if (flow == null) {
    1.35 +                    append("    X_" + i).append(": for (;;) { IF: if (gt <= " + i + ") {\n");
    1.36 +                    openBraces++;
    1.37 +                } else {
    1.38 +                    if (!flow.isFlow(i)) {
    1.39 +                        if (i != 0) {
    1.40 +                            throw new IOException("Expecting flow suggestion at " + i);
    1.41 +                        }
    1.42 +                    }
    1.43 +                }
    1.44                  changeInCatch = true;
    1.45              } else {
    1.46                  debug("    /* " + i + " */ ");
    1.47 @@ -510,6 +522,20 @@
    1.48                  append("try {");
    1.49                  previousTrap = trap.current();
    1.50              }
    1.51 +            if (flow != null) {
    1.52 +                if (flow.isLoop(i)) {
    1.53 +                    append("    for (;;) {\n");
    1.54 +                } 
    1.55 +                if (flow.isBreak(i)) {
    1.56 +                    append("\n    break;");
    1.57 +                }
    1.58 +                for (int j = 0; j < flow.isEnd(i); j++) {
    1.59 +                    append("\n    }");
    1.60 +                }
    1.61 +                if (flow.isElse(i)) {
    1.62 +                    append("    else {\n");
    1.63 +                }
    1.64 +            }
    1.65              final int c = readUByte(byteCodes, i);
    1.66              switch (c) {
    1.67                  case opc_aload_0:
    1.68 @@ -1091,8 +1117,10 @@
    1.69                      break;
    1.70                  case opc_goto: {
    1.71                      smapper.flush(this);
    1.72 -                    int indx = i + readShortArg(byteCodes, i);
    1.73 -                    goTo(this, flow, i, indx, topMostLabel);
    1.74 +                    if (flow == null) {
    1.75 +                        int indx = i + readShortArg(byteCodes, i);
    1.76 +                        goTo(this, flow, i, indx, topMostLabel);
    1.77 +                    }
    1.78                      i += 2;
    1.79                      break;
    1.80                  }
    1.81 @@ -2275,6 +2303,13 @@
    1.82      }
    1.83  
    1.84      private static void goTo(Appendable out, Flow flow, int current, int to, int canBack) throws IOException {
    1.85 +        if (flow != null) {
    1.86 +            if (flow.isIf(current)) {
    1.87 +                out.append("{");
    1.88 +                return;
    1.89 +            }
    1.90 +            throw new IllegalStateException("Opn: ");
    1.91 +        }
    1.92          if (to < current) {
    1.93              if (canBack < to) {
    1.94                  out.append("{ gt = 0; continue X_" + to + "; }");