vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 29 Nov 2012 10:47:49 +0100
changeset 219 f395bd3e477c
parent 172 9eb74b221cff
child 250 42c2ceb1e160
permissions -rw-r--r--
No need to embed String definition 82 times. Once is enough.
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@29
    18
package org.apidesign.vm4brwsr;
jaroslav@29
    19
jaroslav@29
    20
import java.io.IOException;
jaroslav@29
    21
import java.io.InputStream;
jaroslav@93
    22
import java.net.URL;
jaroslav@93
    23
import java.util.Enumeration;
jaroslav@29
    24
jaroslav@29
    25
/** Generator of JavaScript from bytecode of classes on classpath of the VM.
jaroslav@29
    26
 *
jaroslav@29
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@29
    28
 */
jtulach@162
    29
final class GenJS extends ByteCodeToJavaScript {
jtulach@162
    30
    public GenJS(Appendable out) {
jtulach@162
    31
        super(out);
jtulach@162
    32
    }
jaroslav@29
    33
    
jaroslav@29
    34
    static void compile(Appendable out, String... names) throws IOException {
jtulach@162
    35
        compile(out, StringArray.asList(names));
jaroslav@29
    36
    }
jtulach@168
    37
    static void compile(ClassLoader l, Appendable out, String... names) throws IOException {
jtulach@168
    38
        compile(l, out, StringArray.asList(names));
jtulach@168
    39
    }
jtulach@162
    40
    static void compile(Appendable out, StringArray names) throws IOException {
jaroslav@109
    41
        compile(GenJS.class.getClassLoader(), out, names);
jaroslav@109
    42
    }
jtulach@162
    43
    static void compile(ClassLoader l, Appendable out, StringArray names) throws IOException {
jaroslav@172
    44
        out.append("Array.prototype.fillNulls = function() {\n" +
jaroslav@172
    45
             "  for(var i = 0; i < this.length; i++) {\n" +
jaroslav@172
    46
             "    this[i] = null;\n" +
jaroslav@172
    47
             "  }\n" +
jaroslav@172
    48
             "  return this;\n" +
jaroslav@172
    49
             "};");
jaroslav@172
    50
        
jaroslav@172
    51
        
jtulach@162
    52
        StringArray processed = new StringArray();
jtulach@162
    53
        StringArray initCode = new StringArray();
jtulach@162
    54
        for (String baseClass : names.toArray()) {
jtulach@162
    55
            GenJS js = new GenJS(out);
jtulach@162
    56
            js.references.add(baseClass);
jaroslav@97
    57
            for (;;) {
jaroslav@97
    58
                String name = null;
jtulach@162
    59
                for (String n : js.references.toArray()) {
jtulach@162
    60
                    if (processed.contains(n)) {
jaroslav@97
    61
                        continue;
jaroslav@97
    62
                    }
jaroslav@97
    63
                    name = n;
jaroslav@97
    64
                }
jaroslav@97
    65
                if (name == null) {
jaroslav@97
    66
                    break;
jaroslav@97
    67
                }
jaroslav@109
    68
                InputStream is = loadClass(l, name);
jaroslav@97
    69
                if (is == null) {
jaroslav@97
    70
                    throw new IOException("Can't find class " + name); 
jaroslav@97
    71
                }
jaroslav@97
    72
                try {
jtulach@162
    73
                    String ic = js.compile(is);
jtulach@162
    74
                    processed.add(name);
jtulach@162
    75
                    initCode.add(ic == null ? "" : ic);
jaroslav@97
    76
                } catch (RuntimeException ex) {
jaroslav@97
    77
                    if (out instanceof CharSequence) {
jaroslav@97
    78
                        CharSequence seq = (CharSequence)out;
jaroslav@97
    79
                        int lastBlock = seq.length();
jaroslav@151
    80
                        while (lastBlock-- > 0) {
jaroslav@97
    81
                            if (seq.charAt(lastBlock) == '{') {
jaroslav@97
    82
                                break;
jaroslav@97
    83
                            }
jaroslav@29
    84
                        }
jaroslav@97
    85
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    86
                            + seq.subSequence(lastBlock + 1, seq.length()), ex
jaroslav@97
    87
                        );
jaroslav@97
    88
                    } else {
jaroslav@97
    89
                        throw new IOException("Error while compiling " + name + "\n" 
jaroslav@97
    90
                            + out, ex
jaroslav@97
    91
                        );
jaroslav@29
    92
                    }
jaroslav@97
    93
                }
jtulach@162
    94
                for (String resource : js.scripts.toArray()) {
jaroslav@109
    95
                    while (resource.startsWith("/")) {
jaroslav@109
    96
                        resource = resource.substring(1);
jaroslav@109
    97
                    }
jaroslav@109
    98
                    InputStream emul = l.getResourceAsStream(resource);
jaroslav@97
    99
                    if (emul == null) {
jaroslav@97
   100
                        throw new IOException("Can't find " + resource);
jaroslav@97
   101
                    }
jaroslav@97
   102
                    readResource(emul, out);
jaroslav@29
   103
                }
jaroslav@219
   104
                js.scripts = new StringArray();
jaroslav@29
   105
            }
jaroslav@97
   106
jtulach@162
   107
            StringArray toInit = StringArray.asList(js.references.toArray());
jtulach@162
   108
            toInit.reverse();
jtulach@129
   109
jtulach@162
   110
            for (String ic : toInit.toArray()) {
jtulach@162
   111
                int indx = processed.indexOf(ic);
jtulach@162
   112
                if (indx >= 0) {
jtulach@162
   113
                    out.append(initCode.toArray()[indx]).append("\n");
jtulach@162
   114
                    initCode.toArray()[indx] = "";
jaroslav@91
   115
                }
jaroslav@91
   116
            }
jaroslav@133
   117
jaroslav@29
   118
        }
jaroslav@29
   119
    }
jaroslav@90
   120
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   121
        try {
jaroslav@90
   122
            int state = 0;
jaroslav@90
   123
            for (;;) {
jaroslav@90
   124
                int ch = emul.read();
jaroslav@90
   125
                if (ch == -1) {
jaroslav@90
   126
                    break;
jaroslav@90
   127
                }
jaroslav@90
   128
                if (ch < 0 || ch > 255) {
jaroslav@90
   129
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   130
                }
jaroslav@90
   131
                switch (state) {
jaroslav@90
   132
                    case 0: 
jaroslav@90
   133
                        if (ch == '/') {
jaroslav@90
   134
                            state = 1;
jaroslav@90
   135
                        } else {
jaroslav@90
   136
                            out.append((char)ch);
jaroslav@90
   137
                        }
jaroslav@90
   138
                        break;
jaroslav@90
   139
                    case 1:
jaroslav@90
   140
                        if (ch == '*') {
jaroslav@90
   141
                            state = 2;
jaroslav@90
   142
                        } else {
jaroslav@90
   143
                            out.append('/').append((char)ch);
jaroslav@90
   144
                            state = 0;
jaroslav@90
   145
                        }
jaroslav@90
   146
                        break;
jaroslav@90
   147
                    case 2:
jaroslav@90
   148
                        if (ch == '*') {
jaroslav@90
   149
                            state = 3;
jaroslav@90
   150
                        }
jaroslav@90
   151
                        break;
jaroslav@90
   152
                    case 3:
jaroslav@90
   153
                        if (ch == '/') {
jaroslav@90
   154
                            state = 0;
jaroslav@90
   155
                        } else {
jaroslav@90
   156
                            state = 2;
jaroslav@90
   157
                        }
jaroslav@90
   158
                        break;
jaroslav@90
   159
                }
jaroslav@90
   160
            }
jaroslav@90
   161
        } finally {
jaroslav@90
   162
            emul.close();
jaroslav@90
   163
        }
jaroslav@90
   164
    }
jaroslav@93
   165
jaroslav@109
   166
    private static InputStream loadClass(ClassLoader l, String name) throws IOException {
jaroslav@109
   167
        Enumeration<URL> en = l.getResources(name + ".class");
jaroslav@93
   168
        URL u = null;
jaroslav@93
   169
        while (en.hasMoreElements()) {
jaroslav@93
   170
            u = en.nextElement();
jaroslav@93
   171
        }
jaroslav@93
   172
        if (u == null) {
jaroslav@93
   173
            throw new IOException("Can't find " + name);
jaroslav@93
   174
        }
jtulach@162
   175
        if (u.toExternalForm().contains("rt.jar!")) {
jtulach@162
   176
            throw new IOException("No emulation for " + u);
jtulach@162
   177
        }
jaroslav@93
   178
        return u.openStream();
jaroslav@93
   179
    }
jaroslav@136
   180
jaroslav@136
   181
    static String toString(String name) throws IOException {
jaroslav@136
   182
        StringBuilder sb = new StringBuilder();
jaroslav@136
   183
        compile(sb, name);
jaroslav@136
   184
        return sb.toString().toString();
jaroslav@136
   185
    }
jtulach@162
   186
jtulach@162
   187
    private StringArray scripts = new StringArray();
jtulach@162
   188
    private StringArray references = new StringArray();
jtulach@162
   189
    
jtulach@162
   190
    @Override
jtulach@162
   191
    protected boolean requireReference(String cn) {
jtulach@162
   192
        if (references.contains(cn)) {
jtulach@162
   193
            return false;
jtulach@162
   194
        }
jtulach@162
   195
        references.add(cn);
jtulach@162
   196
        return true;
jtulach@162
   197
    }
jtulach@162
   198
jtulach@162
   199
    @Override
jtulach@162
   200
    protected void requireScript(String resourcePath) {
jtulach@162
   201
        scripts.add(resourcePath);
jtulach@162
   202
    }
jaroslav@29
   203
}