vm/src/main/java/org/apidesign/vm4brwsr/VM.java
branchlazyvm
changeset 298 885acca2fa0b
parent 277 e4b9eee9be83
child 300 39695cc9a54f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/VM.java	Tue Dec 11 09:36:44 2012 +0100
     1.3 @@ -0,0 +1,207 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +
    1.26 +/** Generator of JavaScript from bytecode of classes on classpath of the VM.
    1.27 + *
    1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.29 + */
    1.30 +class VM extends ByteCodeToJavaScript {
    1.31 +    public VM(Appendable out) {
    1.32 +        super(out);
    1.33 +    }
    1.34 +    
    1.35 +    static {
    1.36 +        // uses VMLazy to load dynamic classes
    1.37 +        VMLazy.init();
    1.38 +    }
    1.39 +    
    1.40 +    static void compile(Bck2Brwsr.Resources l, Appendable out, StringArray names) throws IOException {
    1.41 +        new VM(out).doCompile(l, names);
    1.42 +    }
    1.43 +    protected void doCompile(Bck2Brwsr.Resources l, StringArray names) throws IOException {
    1.44 +        out.append("(function VM(global) {");
    1.45 +        out.append("\n  var vm = {};");
    1.46 +        StringArray processed = new StringArray();
    1.47 +        StringArray initCode = new StringArray();
    1.48 +        for (String baseClass : names.toArray()) {
    1.49 +            references.add(baseClass);
    1.50 +            for (;;) {
    1.51 +                String name = null;
    1.52 +                for (String n : references.toArray()) {
    1.53 +                    if (processed.contains(n)) {
    1.54 +                        continue;
    1.55 +                    }
    1.56 +                    name = n;
    1.57 +                }
    1.58 +                if (name == null) {
    1.59 +                    break;
    1.60 +                }
    1.61 +                InputStream is = loadClass(l, name);
    1.62 +                if (is == null) {
    1.63 +                    throw new IOException("Can't find class " + name); 
    1.64 +                }
    1.65 +                try {
    1.66 +                    String ic = compile(is);
    1.67 +                    processed.add(name);
    1.68 +                    initCode.add(ic == null ? "" : ic);
    1.69 +                } catch (RuntimeException ex) {
    1.70 +                    if (out instanceof CharSequence) {
    1.71 +                        CharSequence seq = (CharSequence)out;
    1.72 +                        int lastBlock = seq.length();
    1.73 +                        while (lastBlock-- > 0) {
    1.74 +                            if (seq.charAt(lastBlock) == '{') {
    1.75 +                                break;
    1.76 +                            }
    1.77 +                        }
    1.78 +                        throw new IOException("Error while compiling " + name + "\n" 
    1.79 +                            + seq.subSequence(lastBlock + 1, seq.length()), ex
    1.80 +                        );
    1.81 +                    } else {
    1.82 +                        throw new IOException("Error while compiling " + name + "\n" 
    1.83 +                            + out, ex
    1.84 +                        );
    1.85 +                    }
    1.86 +                }
    1.87 +            }
    1.88 +
    1.89 +            for (String resource : scripts.toArray()) {
    1.90 +                while (resource.startsWith("/")) {
    1.91 +                    resource = resource.substring(1);
    1.92 +                }
    1.93 +                InputStream emul = l.get(resource);
    1.94 +                if (emul == null) {
    1.95 +                    throw new IOException("Can't find " + resource);
    1.96 +                }
    1.97 +                readResource(emul, out);
    1.98 +            }
    1.99 +            scripts = new StringArray();
   1.100 +            
   1.101 +            StringArray toInit = StringArray.asList(references.toArray());
   1.102 +            toInit.reverse();
   1.103 +
   1.104 +            for (String ic : toInit.toArray()) {
   1.105 +                int indx = processed.indexOf(ic);
   1.106 +                if (indx >= 0) {
   1.107 +                    out.append(initCode.toArray()[indx]).append("\n");
   1.108 +                    initCode.toArray()[indx] = "";
   1.109 +                }
   1.110 +            }
   1.111 +        }
   1.112 +        out.append(
   1.113 +              "  global.bck2brwsr = function() {\n"
   1.114 +            + "    var args = arguments;\n"
   1.115 +            + "    var loader = {};\n"
   1.116 +            + "    loader.vm = vm;\n"
   1.117 +            + "    loader.loadClass = function(name) {\n"
   1.118 +            + "      var attr = name.replace__Ljava_lang_String_2CC(name, '.','_');\n"
   1.119 +            + "      var fn = vm[attr];\n"
   1.120 +            + "      if (fn) return fn(false);\n"
   1.121 +            + "      return vm.org_apidesign_vm4brwsr_VMLazy(false).\n"
   1.122 +            + "        load___3Ljava_lang_Object_2Ljava_lang_Object_2Ljava_lang_String_2_3Ljava_lang_Object_2(loader, name, args);\n"
   1.123 +            + "    }\n"
   1.124 +            + "    return loader;\n"
   1.125 +            + "  };\n");
   1.126 +        out.append("}(this));");
   1.127 +    }
   1.128 +    private static void readResource(InputStream emul, Appendable out) throws IOException {
   1.129 +        try {
   1.130 +            int state = 0;
   1.131 +            for (;;) {
   1.132 +                int ch = emul.read();
   1.133 +                if (ch == -1) {
   1.134 +                    break;
   1.135 +                }
   1.136 +                if (ch < 0 || ch > 255) {
   1.137 +                    throw new IOException("Invalid char in emulation " + ch);
   1.138 +                }
   1.139 +                switch (state) {
   1.140 +                    case 0: 
   1.141 +                        if (ch == '/') {
   1.142 +                            state = 1;
   1.143 +                        } else {
   1.144 +                            out.append((char)ch);
   1.145 +                        }
   1.146 +                        break;
   1.147 +                    case 1:
   1.148 +                        if (ch == '*') {
   1.149 +                            state = 2;
   1.150 +                        } else {
   1.151 +                            out.append('/').append((char)ch);
   1.152 +                            state = 0;
   1.153 +                        }
   1.154 +                        break;
   1.155 +                    case 2:
   1.156 +                        if (ch == '*') {
   1.157 +                            state = 3;
   1.158 +                        }
   1.159 +                        break;
   1.160 +                    case 3:
   1.161 +                        if (ch == '/') {
   1.162 +                            state = 0;
   1.163 +                        } else {
   1.164 +                            state = 2;
   1.165 +                        }
   1.166 +                        break;
   1.167 +                }
   1.168 +            }
   1.169 +        } finally {
   1.170 +            emul.close();
   1.171 +        }
   1.172 +    }
   1.173 +
   1.174 +    private static InputStream loadClass(Bck2Brwsr.Resources l, String name) throws IOException {
   1.175 +        return l.get(name + ".class"); // NOI18N
   1.176 +    }
   1.177 +
   1.178 +    static String toString(String name) throws IOException {
   1.179 +        StringBuilder sb = new StringBuilder();
   1.180 +//        compile(sb, name);
   1.181 +        return sb.toString().toString();
   1.182 +    }
   1.183 +
   1.184 +    private StringArray scripts = new StringArray();
   1.185 +    private StringArray references = new StringArray();
   1.186 +    
   1.187 +    @Override
   1.188 +    protected boolean requireReference(String cn) {
   1.189 +        if (references.contains(cn)) {
   1.190 +            return false;
   1.191 +        }
   1.192 +        references.add(cn);
   1.193 +        return true;
   1.194 +    }
   1.195 +
   1.196 +    @Override
   1.197 +    protected void requireScript(String resourcePath) {
   1.198 +        scripts.add(resourcePath);
   1.199 +    }
   1.200 +
   1.201 +    @Override
   1.202 +    String assignClass(String className) {
   1.203 +        return "vm." + className + " = ";
   1.204 +    }
   1.205 +    
   1.206 +    @Override
   1.207 +    String accessClass(String className) {
   1.208 +        return "vm." + className;
   1.209 +    }
   1.210 +}