1.1 --- a/emul/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java Mon Dec 24 08:04:45 2012 +0100
1.2 +++ b/emul/src/main/java/org/apidesign/bck2brwsr/emul/AnnotationImpl.java Mon Dec 24 08:35:36 2012 +0100
1.3 @@ -1,6 +1,19 @@
1.4 -/*
1.5 - * To change this template, choose Tools | Templates
1.6 - * and open the template in the editor.
1.7 +/**
1.8 + * Back 2 Browser Bytecode Translator
1.9 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.10 + *
1.11 + * This program is free software: you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation, version 2 of the License.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful,
1.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.18 + * GNU General Public License for more details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.23 */
1.24 package org.apidesign.bck2brwsr.emul;
1.25
2.1 --- a/javap/src/main/java/org/apidesign/javap/Hashtable.java Mon Dec 24 08:04:45 2012 +0100
2.2 +++ b/javap/src/main/java/org/apidesign/javap/Hashtable.java Mon Dec 24 08:35:36 2012 +0100
2.3 @@ -1,6 +1,19 @@
2.4 -/*
2.5 - * To change this template, choose Tools | Templates
2.6 - * and open the template in the editor.
2.7 +/**
2.8 + * Back 2 Browser Bytecode Translator
2.9 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.10 + *
2.11 + * This program is free software: you can redistribute it and/or modify
2.12 + * it under the terms of the GNU General Public License as published by
2.13 + * the Free Software Foundation, version 2 of the License.
2.14 + *
2.15 + * This program is distributed in the hope that it will be useful,
2.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.18 + * GNU General Public License for more details.
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License
2.21 + * along with this program. Look for COPYING file in the top folder.
2.22 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.23 */
2.24 package org.apidesign.javap;
2.25
2.26 @@ -62,7 +75,7 @@
2.27 }
2.28
2.29 @JavaScriptBody(args = {"self", "key" }, body =
2.30 - "return self[key];"
2.31 + "var r = self[key]; return r ? r : null;"
2.32 )
2.33 Object get(Object key) {
2.34 return get(key, null);
3.1 --- a/javap/src/main/java/org/apidesign/javap/MethodData.java Mon Dec 24 08:04:45 2012 +0100
3.2 +++ b/javap/src/main/java/org/apidesign/javap/MethodData.java Mon Dec 24 08:35:36 2012 +0100
3.3 @@ -357,10 +357,10 @@
3.4 /**
3.5 * Return exception table in code attributre.
3.6 */
3.7 - public Vector getexception_table(){
3.8 - return exception_table;
3.9 + public TrapDataIterator getTrapDataIterator(){
3.10 + return new TrapDataIterator(exception_table);
3.11 }
3.12 -
3.13 +
3.14
3.15 /**
3.16 * Return method attributes.
4.1 --- a/javap/src/main/java/org/apidesign/javap/TrapData.java Mon Dec 24 08:04:45 2012 +0100
4.2 +++ b/javap/src/main/java/org/apidesign/javap/TrapData.java Mon Dec 24 08:35:36 2012 +0100
4.3 @@ -26,7 +26,6 @@
4.4
4.5 package org.apidesign.javap;
4.6
4.7 -import java.util.*;
4.8 import java.io.*;
4.9
4.10 /**
4.11 @@ -34,15 +33,18 @@
4.12 *
4.13 * @author Sucheta Dambalkar (Adopted code from jdis)
4.14 */
4.15 -class TrapData {
4.16 - short start_pc, end_pc, handler_pc, catch_cpx;
4.17 - int num;
4.18 +public final class TrapData {
4.19 + public final short start_pc;
4.20 + public final short end_pc;
4.21 + public final short handler_pc;
4.22 + public final short catch_cpx;
4.23 + final int num;
4.24
4.25
4.26 /**
4.27 * Read and store exception table data in code attribute.
4.28 */
4.29 - public TrapData(DataInputStream in, int num) throws IOException {
4.30 + TrapData(DataInputStream in, int num) throws IOException {
4.31 this.num=num;
4.32 start_pc = in.readShort();
4.33 end_pc=in.readShort();
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java Mon Dec 24 08:35:36 2012 +0100
5.3 @@ -0,0 +1,83 @@
5.4 +/**
5.5 + * Back 2 Browser Bytecode Translator
5.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5.7 + *
5.8 + * This program is free software: you can redistribute it and/or modify
5.9 + * it under the terms of the GNU General Public License as published by
5.10 + * the Free Software Foundation, version 2 of the License.
5.11 + *
5.12 + * This program is distributed in the hope that it will be useful,
5.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5.15 + * GNU General Public License for more details.
5.16 + *
5.17 + * You should have received a copy of the GNU General Public License
5.18 + * along with this program. Look for COPYING file in the top folder.
5.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
5.20 + */
5.21 +package org.apidesign.javap;
5.22 +
5.23 +/**
5.24 + *
5.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
5.26 + */
5.27 +public final class TrapDataIterator {
5.28 + private final Hashtable exStart = new Hashtable();
5.29 + private final Hashtable exStop = new Hashtable();
5.30 + private TrapData[] current = new TrapData[10];
5.31 + private int currentCount;
5.32 +
5.33 + TrapDataIterator(Vector exceptionTable) {
5.34 + for (int i=0 ; i < exceptionTable.size(); i++) {
5.35 + final TrapData td = (TrapData)exceptionTable.elementAt(i);
5.36 + exStart.put(td.start_pc, td);
5.37 + exStop.put(td.end_pc, td);
5.38 + }
5.39 + }
5.40 +
5.41 + public void advanceTo(int i) {
5.42 + Short s = Short.valueOf((short)i);
5.43 + TrapData e = (TrapData) exStart.get(s);
5.44 + if (e != null) {
5.45 + add(e);
5.46 + }
5.47 + e = (TrapData) exStop.get(s);
5.48 + if (e != null) {
5.49 + remove(e);
5.50 + }
5.51 + }
5.52 +
5.53 + public boolean useTry() {
5.54 + return currentCount > 0;
5.55 + }
5.56 +
5.57 + public TrapData[] current() {
5.58 + return current;
5.59 + }
5.60 +
5.61 + private void add(TrapData e) {
5.62 + if (currentCount == current.length) {
5.63 + TrapData[] data = new TrapData[currentCount * 2];
5.64 + for (int i = 0; i < currentCount; i++) {
5.65 + data[i] = current[i];
5.66 + }
5.67 + current = data;
5.68 + }
5.69 + current[currentCount++] = e;
5.70 + }
5.71 +
5.72 + private void remove(TrapData e) {
5.73 + int from = 0;
5.74 + while (from < currentCount) {
5.75 + if (e == current[from++]) {
5.76 + break;
5.77 + }
5.78 + }
5.79 + while (from < currentCount) {
5.80 + current[from - 1] = current[from];
5.81 + current[from] = null;
5.82 + from++;
5.83 + }
5.84 + currentCount--;
5.85 + }
5.86 +}
6.1 --- a/javap/src/main/java/org/apidesign/javap/Vector.java Mon Dec 24 08:04:45 2012 +0100
6.2 +++ b/javap/src/main/java/org/apidesign/javap/Vector.java Mon Dec 24 08:35:36 2012 +0100
6.3 @@ -1,6 +1,19 @@
6.4 -/*
6.5 - * To change this template, choose Tools | Templates
6.6 - * and open the template in the editor.
6.7 +/**
6.8 + * Back 2 Browser Bytecode Translator
6.9 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
6.10 + *
6.11 + * This program is free software: you can redistribute it and/or modify
6.12 + * it under the terms of the GNU General Public License as published by
6.13 + * the Free Software Foundation, version 2 of the License.
6.14 + *
6.15 + * This program is distributed in the hope that it will be useful,
6.16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.18 + * GNU General Public License for more details.
6.19 + *
6.20 + * You should have received a copy of the GNU General Public License
6.21 + * along with this program. Look for COPYING file in the top folder.
6.22 + * If not, see http://opensource.org/licenses/GPL-2.0.
6.23 */
6.24 package org.apidesign.javap;
6.25
7.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Dec 24 08:04:45 2012 +0100
7.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Dec 24 08:35:36 2012 +0100
7.3 @@ -25,6 +25,8 @@
7.4 import org.apidesign.javap.MethodData;
7.5 import org.apidesign.javap.StackMapIterator;
7.6 import static org.apidesign.javap.RuntimeConstants.*;
7.7 +import org.apidesign.javap.TrapData;
7.8 +import org.apidesign.javap.TrapDataIterator;
7.9
7.10 /** Translator of the code inside class files to JavaScript.
7.11 *
7.12 @@ -222,6 +224,7 @@
7.13 private void generateMethod(String prefix, String name, MethodData m)
7.14 throws IOException {
7.15 final StackMapIterator stackMapIterator = m.createStackMapIterator();
7.16 + TrapDataIterator trap = m.getTrapDataIterator();
7.17 final LocalsMapper lmapper =
7.18 new LocalsMapper(stackMapIterator.getArguments());
7.19
7.20 @@ -273,14 +276,18 @@
7.21 for (int i = 0; i < byteCodes.length; i++) {
7.22 int prev = i;
7.23 stackMapIterator.advanceTo(i);
7.24 + trap.advanceTo(i);
7.25 if (lastStackFrame != stackMapIterator.getFrameIndex()) {
7.26 lastStackFrame = stackMapIterator.getFrameIndex();
7.27 lmapper.syncWithFrameLocals(stackMapIterator.getFrameLocals());
7.28 smapper.syncWithFrameStack(stackMapIterator.getFrameStack());
7.29 - out.append(" case " + i).append(": ");
7.30 + out.append(" case " + i).append(": ");
7.31 } else {
7.32 out.append(" /* " + i).append(" */ ");
7.33 }
7.34 + if (trap.useTry()) {
7.35 + out.append("try {");
7.36 + }
7.37 final int c = readByte(byteCodes, i);
7.38 switch (c) {
7.39 case opc_aload_0:
7.40 @@ -1104,13 +1111,32 @@
7.41 Integer.toString(c));
7.42 }
7.43 }
7.44 + if (trap.useTry()) {
7.45 + out.append("} catch (e) {");
7.46 + for (TrapData e : trap.current()) {
7.47 + if (e == null) {
7.48 + break;
7.49 + }
7.50 + if (e.catch_cpx != 0) { //not finally
7.51 + final String classInternalName = jc.getClassName(e.catch_cpx);
7.52 + addReference(classInternalName);
7.53 + out.append("if (e.$instOf_"+classInternalName.replace('/', '_')+") {");
7.54 + out.append("gt="+e.handler_pc+"; continue;");
7.55 + out.append("} ");
7.56 + } else {
7.57 + //finally - todo
7.58 + }
7.59 + }
7.60 + out.append("throw e;");
7.61 + out.append("}");
7.62 + }
7.63 out.append(" //");
7.64 for (int j = prev; j <= i; j++) {
7.65 out.append(" ");
7.66 final int cc = readByte(byteCodes, j);
7.67 out.append(Integer.toString(cc));
7.68 }
7.69 - out.append("\n");
7.70 + out.append("\n");
7.71 }
7.72 out.append(" }\n");
7.73 out.append("};");
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
8.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Exceptions.java Mon Dec 24 08:35:36 2012 +0100
8.3 @@ -0,0 +1,49 @@
8.4 +/**
8.5 + * Back 2 Browser Bytecode Translator
8.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
8.7 + *
8.8 + * This program is free software: you can redistribute it and/or modify
8.9 + * it under the terms of the GNU General Public License as published by
8.10 + * the Free Software Foundation, version 2 of the License.
8.11 + *
8.12 + * This program is distributed in the hope that it will be useful,
8.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8.15 + * GNU General Public License for more details.
8.16 + *
8.17 + * You should have received a copy of the GNU General Public License
8.18 + * along with this program. Look for COPYING file in the top folder.
8.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
8.20 + */
8.21 +package org.apidesign.vm4brwsr;
8.22 +
8.23 +/**
8.24 + *
8.25 + * @author tom
8.26 + */
8.27 +public class Exceptions {
8.28 +
8.29 + public static int methodWithTryCatchNoThrow() {
8.30 + int res = 0;
8.31 + try {
8.32 + res = 1;
8.33 + } catch (IllegalArgumentException e) {
8.34 + res = 2;
8.35 + }
8.36 + //join point
8.37 + return res;
8.38 + }
8.39 +
8.40 + public static int methodWithTryCatchThrow() {
8.41 + int res = 0;
8.42 + try {
8.43 + res = 1;
8.44 + throw new IllegalArgumentException();
8.45 + } catch (IllegalArgumentException e) {
8.46 + res = 2;
8.47 + }
8.48 + //join point
8.49 + return res;
8.50 + }
8.51 +
8.52 +}
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/ExceptionsTest.java Mon Dec 24 08:35:36 2012 +0100
9.3 @@ -0,0 +1,63 @@
9.4 +/**
9.5 + * Back 2 Browser Bytecode Translator
9.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
9.7 + *
9.8 + * This program is free software: you can redistribute it and/or modify
9.9 + * it under the terms of the GNU General Public License as published by
9.10 + * the Free Software Foundation, version 2 of the License.
9.11 + *
9.12 + * This program is distributed in the hope that it will be useful,
9.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9.15 + * GNU General Public License for more details.
9.16 + *
9.17 + * You should have received a copy of the GNU General Public License
9.18 + * along with this program. Look for COPYING file in the top folder.
9.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
9.20 + */
9.21 +package org.apidesign.vm4brwsr;
9.22 +
9.23 +import javax.script.Invocable;
9.24 +import javax.script.ScriptException;
9.25 +import static org.testng.Assert.*;
9.26 +import org.testng.annotations.BeforeClass;
9.27 +import org.testng.annotations.Test;
9.28 +
9.29 +/**
9.30 + *
9.31 + * @author Tomas Zezula <tzezula@netbeans.org>
9.32 + */
9.33 +public class ExceptionsTest {
9.34 + @Test
9.35 + public void verifyMethodWithTryCatchNoThrow() throws Exception {
9.36 + assertExec(
9.37 + "No throw",
9.38 + Exceptions.class,
9.39 + "methodWithTryCatchNoThrow__I",
9.40 + new Double(1.0));
9.41 + }
9.42 +
9.43 + @Test
9.44 + public void verifyMethodWithTryCatchThrow() throws Exception {
9.45 + assertExec(
9.46 + "Throw",
9.47 + Exceptions.class,
9.48 + "methodWithTryCatchThrow__I",
9.49 + new Double(2.0));
9.50 + }
9.51 +
9.52 + private static CharSequence codeSeq;
9.53 + private static Invocable code;
9.54 +
9.55 + @BeforeClass
9.56 + public void compileTheCode() throws Exception {
9.57 + StringBuilder sb = new StringBuilder();
9.58 + code = StaticMethodTest.compileClass(sb,
9.59 + "org/apidesign/vm4brwsr/Exceptions"
9.60 + );
9.61 + codeSeq = sb;
9.62 + }
9.63 + private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
9.64 + StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
9.65 + }
9.66 +}