# HG changeset patch # User Jaroslav Tulach # Date 1360240916 -3600 # Node ID fa42b3d8cbbcf1d5a9344cfe551836e017d549f2 # Parent bfb3f72249de65b73eb3e338148c1605608d67f2 Recognizing Main-Class attribute, so initializing the VM is just about passing in correct classpath diff -r bfb3f72249de -r fa42b3d8cbbc javaquery/demo-calculator/pom.xml --- a/javaquery/demo-calculator/pom.xml Thu Feb 07 13:15:41 2013 +0100 +++ b/javaquery/demo-calculator/pom.xml Thu Feb 07 13:41:56 2013 +0100 @@ -50,6 +50,7 @@ true lib/ + org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc diff -r bfb3f72249de -r fa42b3d8cbbc javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Thu Feb 07 13:15:41 2013 +0100 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Thu Feb 07 13:41:56 2013 +0100 @@ -79,9 +79,9 @@
diff -r bfb3f72249de -r fa42b3d8cbbc vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ParseCPAttr.java Thu Feb 07 13:15:41 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ -/** - * 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 java.io.IOException; -import java.io.InputStream; -import org.apidesign.bck2brwsr.emul.lang.ManifestInputStream; - -/** - * - * @author Jaroslav Tulach - */ -final class ParseCPAttr extends ManifestInputStream { - private String cp; - - public ParseCPAttr(InputStream is) throws IOException { - super(is); - readAttributes(new byte[512]); - } - - @Override - protected String putValue(String key, String value) { - if ("Class-Path".equals(key)) { - cp = value; - } - return null; - } - - @Override - public String toString() { - return cp; - } - -} diff -r bfb3f72249de -r fa42b3d8cbbc vm/src/main/java/org/apidesign/vm4brwsr/ParseMan.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ParseMan.java Thu Feb 07 13:41:56 2013 +0100 @@ -0,0 +1,57 @@ +/** + * 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 java.io.IOException; +import java.io.InputStream; +import org.apidesign.bck2brwsr.emul.lang.ManifestInputStream; + +/** + * + * @author Jaroslav Tulach + */ +final class ParseMan extends ManifestInputStream { + private String cp; + private String mc; + + public ParseMan(InputStream is) throws IOException { + super(is); + readAttributes(new byte[512]); + } + + @Override + protected String putValue(String key, String value) { + if ("Class-Path".equals(key)) { + cp = value; + } + if ("Main-Class".equals(key)) { + mc = value; + } + return null; + } + + String getMainClass() { + return mc; + } + + @Override + public String toString() { + return cp; + } + +} diff -r bfb3f72249de -r fa42b3d8cbbc vm/src/main/java/org/apidesign/vm4brwsr/VM.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu Feb 07 13:15:41 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VM.java Thu Feb 07 13:41:56 2013 +0100 @@ -120,10 +120,11 @@ " global.bck2brwsr = function() {\n" + " var args = arguments;\n" + " var loader = {};\n" + + " var init = null;\n" + " loader.vm = vm;\n" + " if (args.length == 1 && typeof args[0] !== 'function') {;\n" + " var classpath = args[0];\n" - + " args[0] = function(name) {\n" + + " init = args[0] = function(name) {\n" + " return vm.org_apidesign_vm4brwsr_Zips(false).loadFromCp___3B_3Ljava_lang_Object_2Ljava_lang_String_2(classpath, name);\n" + " };\n" + " };\n" @@ -142,6 +143,7 @@ + " return args[0](name);\n" + " }\n" + " }\n" + + " if (init) init(null);\n" + " return loader;\n" + " };\n"); out.append("}(this));"); diff -r bfb3f72249de -r fa42b3d8cbbc vm/src/main/java/org/apidesign/vm4brwsr/Zips.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Thu Feb 07 13:15:41 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/Zips.java Thu Feb 07 13:41:56 2013 +0100 @@ -19,7 +19,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -36,7 +35,7 @@ public static void init() { } - public static byte[] loadFromCp(Object[] classpath, String res) { + public static byte[] loadFromCp(Object[] classpath, String res) throws Exception { for (int i = 0; i < classpath.length; i++) { Object c = classpath[i]; if (c instanceof String) { @@ -46,13 +45,17 @@ c = classpath[i] = z; final byte[] man = z.findRes("META-INF/MANIFEST.MF"); if (man != null) { - processClassPathAttr(man, url, classpath); + String mainClass = processClassPathAttr(man, url, classpath); + if (mainClass != null) { + Class.forName(mainClass); + } } - } catch (IOException ex) { + } catch (Exception ex) { classpath[i] = ex; + throw ex; } } - if (c instanceof Zips) { + if (res != null && c instanceof Zips) { Object checkRes = ((Zips)c).findRes(res); if (checkRes instanceof byte[]) { return (byte[])checkRes; @@ -95,30 +98,26 @@ return z; } - private static void processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException { - try (InputStream is = initIS(new ByteArrayInputStream(man))) { + private static String processClassPathAttr(final byte[] man, String url, Object[] classpath) throws IOException { + try (ParseMan is = new ParseMan(new ByteArrayInputStream(man))) { String cp = is.toString(); - if (cp == null) { - return; + if (cp != null) { + cp = cp.trim(); + for (int p = 0; p < cp.length();) { + int n = cp.indexOf(' ', p); + if (n == -1) { + n = cp.length(); + } + String el = cp.substring(p, n); + URL u = new URL(new URL(url), el); + classpath = addToArray(classpath, u.toString()); + p = n + 1; + } } - cp = cp.trim(); - for (int p = 0; p < cp.length();) { - int n = cp.indexOf(' ', p); - if (n == -1) { - n = cp.length(); - } - String el = cp.substring(p, n); - URL u = new URL(new URL(url), el); - classpath = addToArray(classpath, u.toString()); - p = n + 1; - } + return is.getMainClass(); } } - private static InputStream initIS(InputStream is) throws IOException { - return new ParseCPAttr(is); - } - private static Object[] addToArray(Object[] arr, String value) { final int last = arr.length; Object[] ret = enlargeArray(arr, last + 1);