diff -r 2adac52f955e -r 945c799a6812 vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sat Dec 29 19:46:09 2012 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sat Dec 29 20:10:10 2012 +0100 @@ -280,22 +280,31 @@ } int lastStackFrame = -1; - + TrapData[] previousTrap = null; + out.append("\n var gt = 0;\n for(;;) switch(gt) {\n"); for (int i = 0; i < byteCodes.length; i++) { int prev = i; stackMapIterator.advanceTo(i); - trap.advanceTo(i); + boolean changeInCatch = trap.advanceTo(i); + if (changeInCatch || lastStackFrame != stackMapIterator.getFrameIndex()) { + if (previousTrap != null) { + generateCatch(previousTrap); + previousTrap = null; + } + } if (lastStackFrame != stackMapIterator.getFrameIndex()) { lastStackFrame = stackMapIterator.getFrameIndex(); lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals()); smapper.syncWithFrameStack(stackMapIterator.getFrameStack()); out.append(" case " + i).append(": "); + changeInCatch = true; } else { out.append(" /* " + i).append(" */ "); } - if (trap.useTry()) { + if (changeInCatch && trap.useTry()) { out.append("try {"); + previousTrap = trap.current(); } final int c = readByte(byteCodes, i); switch (c) { @@ -1120,25 +1129,6 @@ Integer.toString(c)); } } - if (trap.useTry()) { - out.append("} catch (e) {"); - for (TrapData e : trap.current()) { - if (e == null) { - break; - } - if (e.catch_cpx != 0) { //not finally - final String classInternalName = jc.getClassName(e.catch_cpx); - addReference(classInternalName); - out.append("if (e.$instOf_"+classInternalName.replace('/', '_')+") {"); - out.append("gt="+e.handler_pc+"; stA0 = e; continue;"); - out.append("} "); - } else { - //finally - todo - } - } - out.append("throw e;"); - out.append("}"); - } out.append(" //"); for (int j = prev; j <= i; j++) { out.append(" "); @@ -1147,6 +1137,9 @@ } out.append("\n"); } + if (previousTrap != null) { + generateCatch(previousTrap); + } out.append(" }\n"); out.append("};"); } @@ -1577,4 +1570,24 @@ out.append(format, processed, length); } + + private void generateCatch(TrapData[] traps) throws IOException { + out.append("} catch (e) {"); + for (TrapData e : traps) { + if (e == null) { + break; + } + if (e.catch_cpx != 0) { //not finally + final String classInternalName = jc.getClassName(e.catch_cpx); + addReference(classInternalName); + out.append("if (e.$instOf_" + classInternalName.replace('/', '_') + ") {"); + out.append("gt=" + e.handler_pc + "; stA0 = e; continue;"); + out.append("} "); + } else { + //finally - todo + } + } + out.append("throw e;"); + out.append("}"); + } }