# HG changeset patch # User Jaroslav Tulach # Date 1353050815 -3600 # Node ID 6f2aef4cf160b8d5a1385bbbc4ad462682ede16b # Parent 1d4bf362c3a4e03183a3eb43119b940e821bf84b Removing the printing part from javap. The bck2brwsr VM can now be compiled into JavaScript diff -r 1d4bf362c3a4 -r 6f2aef4cf160 emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js Fri Nov 16 08:12:01 2012 +0100 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_String.js Fri Nov 16 08:26:55 2012 +0100 @@ -2228,29 +2228,10 @@ function java_lang_String_toStringLjava_lang_String(arg0) { return arg0.toString(); } +function java_lang_String_toCharArrayAC(arg0) { + return arg0.toString().split(''); +} /* -function java_lang_String_toCharArrayAC(arg0) { - var arg1; - var arg2; -; - var stack = new Array(5); - var gt = 0; - for(;;) switch(gt) { - case 0: stack.push(arg0); // 42 - case 1: stack.push(stack.pop().count); // 180 1 97 - case 4: stack.push(new Array(stack.pop())); // 188 5 - case 6: arg1 = stack.pop(); // 76 - case 7: stack.push(arg0); // 42 - case 8: stack.push(0); // 3 - case 9: stack.push(arg0); // 42 - case 10: stack.push(stack.pop().count); // 180 1 97 - case 13: stack.push(arg1); // 43 - case 14: stack.push(0); // 3 - case 15: { var v3 = stack.pop(); var v2 = stack.pop(); var v1 = stack.pop(); var v0 = stack.pop(); var self = stack.pop(); self.getCharsVIIACAI(self, v0, v1, v2, v3); } // 182 1 138 - case 18: stack.push(arg1); // 43 - case 19: return stack.pop(); // 176 - } -} function java_lang_String_formatLjava_lang_StringLjava_lang_StringLjava_lang_Object(arg0,arg1) { var stack = new Array(); var gt = 0; @@ -2475,6 +2456,7 @@ String.prototype.substringLjava_lang_StringI = java_lang_String_substringLjava_lang_StringI; String.prototype.substringLjava_lang_StringII = java_lang_String_substringLjava_lang_StringII; String.prototype.equalsZLjava_lang_Object = java_lang_String_equalsZLjava_lang_Object; +String.prototype.toCharArrayAC = java_lang_String_toCharArrayAC; String.prototype.$instOf_java_lang_String = true; String.prototype.$instOf_java_io_Serializable = true; String.prototype.$instOf_java_lang_Comparable = true; @@ -2532,7 +2514,6 @@ this.toUpperCaseLjava_lang_String = java_lang_String_toUpperCaseLjava_lang_String; this.trimLjava_lang_String = java_lang_String_trimLjava_lang_String; this.toStringLjava_lang_String = java_lang_String_toStringLjava_lang_String; - this.toCharArrayAC = java_lang_String_toCharArrayAC; this.internLjava_lang_String = java_lang_String_internLjava_lang_String; this.compareToILjava_lang_Object = java_lang_String_compareToILjava_lang_Object; */ diff -r 1d4bf362c3a4 -r 6f2aef4cf160 javap/src/main/java/org/apidesign/javap/ClassData.java --- a/javap/src/main/java/org/apidesign/javap/ClassData.java Fri Nov 16 08:12:01 2012 +0100 +++ b/javap/src/main/java/org/apidesign/javap/ClassData.java Fri Nov 16 08:26:55 2012 +0100 @@ -481,12 +481,6 @@ return this_class; } - public String TagString (int tag) { - String res=Tables.tagName(tag); - if (res==null) return "BOGUS_TAG:"+tag; - return res; - } - /** * Returns string at that index. */ @@ -654,19 +648,6 @@ return cpool_count; } - public String StringTag(int cpx) { - byte tag=0; - String str=null; - try { - if (cpx==0) throw new IndexOutOfBoundsException(); - tag=tags[cpx]; - return TagString(tag); - } catch (IndexOutOfBoundsException e) { - str="Incorrect CP index:"+cpx; - } - return str; - } - /** * Returns minor version of class file. */ diff -r 1d4bf362c3a4 -r 6f2aef4cf160 javap/src/main/java/org/apidesign/javap/Tables.java --- a/javap/src/main/java/org/apidesign/javap/Tables.java Fri Nov 16 08:12:01 2012 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,373 +0,0 @@ -/* - * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - - -package org.apidesign.javap; - - - -public class Tables implements Constants { - /** - * Define mnemocodes table. - */ - static Hashtable mnemocodes = new Hashtable(301, 0.5f); - static String opcExtNamesTab[]=new String[128]; - static String opcPrivExtNamesTab[]=new String[128]; - static void defineNonPriv(int opc, String mnem) { - mnemocodes.put(opcExtNamesTab[opc]=mnem, new Integer(opc_nonpriv*256+opc)); - } - static void definePriv(int opc, String mnem) { - mnemocodes.put(opcPrivExtNamesTab[opc]="priv_"+mnem, new Integer(opc_priv*256+opc)); - } - static void defineExt(int opc, String mnem) { - defineNonPriv(opc, mnem); - definePriv(opc, mnem); - } - static { int k; - for (k=0; k>8) { - case 0: - return opcLengthsTab[opc]; - case opc_wide: - switch (opc&0xFF) { - case opc_aload: case opc_astore: - case opc_fload: case opc_fstore: - case opc_iload: case opc_istore: - case opc_lload: case opc_lstore: - case opc_dload: case opc_dstore: - case opc_ret: - return 4; - case opc_iinc: - return 6; - default: - throw new ArrayIndexOutOfBoundsException(); - } - case opc_nonpriv: - case opc_priv: - return 2; - default: - throw new ArrayIndexOutOfBoundsException(); - } - } - - public static String opcName(int opc) { - try { - switch (opc>>8) { - case 0: - return opcNamesTab[opc]; - case opc_wide: { - String mnem=opcNamesTab[opc&0xFF]+"_w"; - if (mnemocodes.get(mnem) == null) - return null; // non-existent opcode - return mnem; - } - case opc_nonpriv: - return opcExtNamesTab[opc&0xFF]; - case opc_priv: - return opcPrivExtNamesTab[opc&0xFF]; - default: - return null; - } - } catch (ArrayIndexOutOfBoundsException e) { - switch (opc) { - case opc_nonpriv: - return "nonpriv"; - case opc_priv: - return "priv"; - default: - return null; - } - } - } - - public static int opcode(String mnem) { - Integer Val=(Integer)(mnemocodes.get(mnem)); - if (Val == null) return -1; - return Val.intValue(); - } - - /** - * Initialized keyword and token Hashtables - */ - static Vector keywordNames = new Vector(40); - private static void defineKeywordName(String id, int token) { - - if (token>=keywordNames.size()) { - keywordNames.setSize(token+1); - } - keywordNames.setElementAt(id, token); - } - public static String keywordName(int token) { - if (token==-1) return "EOF"; - if (token>=keywordNames.size()) return null; - return (String)keywordNames.elementAt(token); - } - static { - defineKeywordName("ident", IDENT); - defineKeywordName("STRINGVAL", STRINGVAL); - defineKeywordName("intVal", INTVAL); - defineKeywordName("longVal", LONGVAL); - defineKeywordName("floatVal", FLOATVAL); - defineKeywordName("doubleVal", DOUBLEVAL); - defineKeywordName("SEMICOLON", SEMICOLON); - defineKeywordName("COLON", COLON); - defineKeywordName("LBRACE", LBRACE); - defineKeywordName("RBRACE", RBRACE); - } - - static Hashtable keywords = new Hashtable(40); - public static int keyword(String idValue) { - Integer Val=(Integer)(keywords.get(idValue)); - if (Val == null) return IDENT; - return Val.intValue(); - } - - private static void defineKeyword(String id, int token) { - keywords.put(id, new Integer(token)); - defineKeywordName(id, token); - } - static { - // Modifier keywords - defineKeyword("private", PRIVATE); - defineKeyword("public", PUBLIC); - defineKeyword("protected", PROTECTED); - defineKeyword("static", STATIC); - defineKeyword("transient", TRANSIENT); - defineKeyword("synchronized", SYNCHRONIZED); - defineKeyword("super", SUPER); - defineKeyword("native", NATIVE); - defineKeyword("abstract", ABSTRACT); - defineKeyword("volatile", VOLATILE); - defineKeyword("final", FINAL); - defineKeyword("interface",INTERFACE); - defineKeyword("synthetic",SYNTHETIC); - defineKeyword("strict",STRICT); - - // Declaration keywords - defineKeyword("package",PACKAGE); - defineKeyword("class",CLASS); - defineKeyword("extends",EXTENDS); - defineKeyword("implements",IMPLEMENTS); - defineKeyword("const", CONST); - defineKeyword("throws",THROWS); - defineKeyword("interface",INTERFACE); - defineKeyword("Method",METHODREF); - defineKeyword("Field",FIELDREF); - defineKeyword("stack",STACK); - defineKeyword("locals",LOCAL); - - // used in switchtables - defineKeyword("default", DEFAULT); - - // used in inner class declarations - defineKeyword("InnerClass", INNERCLASS); - defineKeyword("of", OF); - - // misc - defineKeyword("bits",BITS); - defineKeyword("Infinity",INF); - defineKeyword("Inf",INF); - defineKeyword("NaN",NAN); - } - - /** - * Define tag table. - */ - private static Vector tagNames = new Vector(10); - private static Hashtable Tags = new Hashtable(10); - static { - defineTag("Asciz",CONSTANT_UTF8); - defineTag("int",CONSTANT_INTEGER); - defineTag("float",CONSTANT_FLOAT); - defineTag("long",CONSTANT_LONG); - defineTag("double",CONSTANT_DOUBLE); - defineTag("class",CONSTANT_CLASS); - defineTag("String",CONSTANT_STRING); - defineTag("Field",CONSTANT_FIELD); - defineTag("Method",CONSTANT_METHOD); - defineTag("InterfaceMethod",CONSTANT_INTERFACEMETHOD); - defineTag("NameAndType",CONSTANT_NAMEANDTYPE); - } - private static void defineTag(String id, int val) { - Tags.put(id, new Integer(val)); - if (val>=tagNames.size()) { - tagNames.setSize(val+1); - } - tagNames.setElementAt(id, val); - } - public static String tagName(int tag) { - if (tag>=tagNames.size()) return null; - return (String)tagNames.elementAt(tag); - } - public static int tagValue(String idValue) { - Integer Val=(Integer)(Tags.get(idValue)); - if (Val == null) return 0; - return Val.intValue(); - } - - /** - * Define type table. These types used in "newarray" instruction only. - */ - private static Vector typeNames = new Vector(10); - private static Hashtable Types = new Hashtable(10); - static { - defineType("int",T_INT); - defineType("long",T_LONG); - defineType("float",T_FLOAT); - defineType("double",T_DOUBLE); - defineType("class",T_CLASS); - defineType("boolean",T_BOOLEAN); - defineType("char",T_CHAR); - defineType("byte",T_BYTE); - defineType("short",T_SHORT); - } - private static void defineType(String id, int val) { - Types.put(id, new Integer(val)); - if (val>=typeNames.size()) { - typeNames.setSize(val+1); - } - typeNames.setElementAt(id, val); - } - public static int typeValue(String idValue) { - Integer Val=(Integer)(Types.get(idValue)); - if (Val == null) return -1; - return Val.intValue(); - } - public static String typeName(int type) { - if (type>=typeNames.size()) return null; - return (String)typeNames.elementAt(type); - } - - /** - * Define MapTypes table. - * These constants used in stackmap tables only. - */ - private static Vector mapTypeNames = new Vector(10); - private static Hashtable MapTypes = new Hashtable(10); - static { - defineMapType("bogus", ITEM_Bogus); - defineMapType("int", ITEM_Integer); - defineMapType("float", ITEM_Float); - defineMapType("double", ITEM_Double); - defineMapType("long", ITEM_Long); - defineMapType("null", ITEM_Null); - defineMapType("this", ITEM_InitObject); - defineMapType("CP", ITEM_Object); - defineMapType("uninitialized", ITEM_NewObject); - } - private static void defineMapType(String id, int val) { - MapTypes.put(id, new Integer(val)); - if (val>=mapTypeNames.size()) { - mapTypeNames.setSize(val+1); - } - mapTypeNames.setElementAt(id, val); - } - public static int mapTypeValue(String idValue) { - Integer Val=(Integer)(MapTypes.get(idValue)); - if (Val == null) return -1; - return Val.intValue(); - } - public static String mapTypeName(int type) { - if (type>=mapTypeNames.size()) return null; - return (String)mapTypeNames.elementAt(type); - } - -} diff -r 1d4bf362c3a4 -r 6f2aef4cf160 vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/VMinVMTest.java Fri Nov 16 08:26:55 2012 +0100 @@ -0,0 +1,55 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.vm4brwsr; + +import static org.testng.Assert.*; +import javax.script.Invocable; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class VMinVMTest { + + private static CharSequence codeSeq; + private static Invocable code; + + @Test public void compareTheGeneratedCode() throws Exception { + StringBuilder hotspot = new StringBuilder(); + GenJS.compile(hotspot, "org/apidesign/vm4brwsr/Array"); + + Object ret = code.invokeFunction( + "org_apidesign_vm4brwsr_GenJS_toStringLjava_lang_StringLjava_lang_String", + "org/apidesign/vm4brwsr/Array" + ); + assertTrue(ret instanceof String, "It is string: " + ret); + + assertEquals((String)ret, hotspot.toString(), "The code is the same"); + } + + @BeforeClass + public void compileTheCode() throws Exception { + StringBuilder sb = new StringBuilder(); + code = StaticMethodTest.compileClass(sb, + "org/apidesign/vm4brwsr/GenJS" + ); + codeSeq = sb; + } +}