vm/src/main/java/org/apidesign/vm4brwsr/GenJS.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 17 Nov 2012 17:43:15 +0100
branchjavap
changeset 172 9eb74b221cff
parent 168 1d4bf362c3a4
child 219 f395bd3e477c
permissions -rw-r--r--
Pre-fill arrays with nulls
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@29
   104
            }
jaroslav@97
   105
jtulach@162
   106
            StringArray toInit = StringArray.asList(js.references.toArray());
jtulach@162
   107
            toInit.reverse();
jtulach@129
   108
jtulach@162
   109
            for (String ic : toInit.toArray()) {
jtulach@162
   110
                int indx = processed.indexOf(ic);
jtulach@162
   111
                if (indx >= 0) {
jtulach@162
   112
                    out.append(initCode.toArray()[indx]).append("\n");
jtulach@162
   113
                    initCode.toArray()[indx] = "";
jaroslav@91
   114
                }
jaroslav@91
   115
            }
jaroslav@133
   116
jaroslav@29
   117
        }
jaroslav@29
   118
    }
jaroslav@90
   119
    private static void readResource(InputStream emul, Appendable out) throws IOException {
jaroslav@90
   120
        try {
jaroslav@90
   121
            int state = 0;
jaroslav@90
   122
            for (;;) {
jaroslav@90
   123
                int ch = emul.read();
jaroslav@90
   124
                if (ch == -1) {
jaroslav@90
   125
                    break;
jaroslav@90
   126
                }
jaroslav@90
   127
                if (ch < 0 || ch > 255) {
jaroslav@90
   128
                    throw new IOException("Invalid char in emulation " + ch);
jaroslav@90
   129
                }
jaroslav@90
   130
                switch (state) {
jaroslav@90
   131
                    case 0: 
jaroslav@90
   132
                        if (ch == '/') {
jaroslav@90
   133
                            state = 1;
jaroslav@90
   134
                        } else {
jaroslav@90
   135
                            out.append((char)ch);
jaroslav@90
   136
                        }
jaroslav@90
   137
                        break;
jaroslav@90
   138
                    case 1:
jaroslav@90
   139
                        if (ch == '*') {
jaroslav@90
   140
                            state = 2;
jaroslav@90
   141
                        } else {
jaroslav@90
   142
                            out.append('/').append((char)ch);
jaroslav@90
   143
                            state = 0;
jaroslav@90
   144
                        }
jaroslav@90
   145
                        break;
jaroslav@90
   146
                    case 2:
jaroslav@90
   147
                        if (ch == '*') {
jaroslav@90
   148
                            state = 3;
jaroslav@90
   149
                        }
jaroslav@90
   150
                        break;
jaroslav@90
   151
                    case 3:
jaroslav@90
   152
                        if (ch == '/') {
jaroslav@90
   153
                            state = 0;
jaroslav@90
   154
                        } else {
jaroslav@90
   155
                            state = 2;
jaroslav@90
   156
                        }
jaroslav@90
   157
                        break;
jaroslav@90
   158
                }
jaroslav@90
   159
            }
jaroslav@90
   160
        } finally {
jaroslav@90
   161
            emul.close();
jaroslav@90
   162
        }
jaroslav@90
   163
    }
jaroslav@93
   164
jaroslav@109
   165
    private static InputStream loadClass(ClassLoader l, String name) throws IOException {
jaroslav@109
   166
        Enumeration<URL> en = l.getResources(name + ".class");
jaroslav@93
   167
        URL u = null;
jaroslav@93
   168
        while (en.hasMoreElements()) {
jaroslav@93
   169
            u = en.nextElement();
jaroslav@93
   170
        }
jaroslav@93
   171
        if (u == null) {
jaroslav@93
   172
            throw new IOException("Can't find " + name);
jaroslav@93
   173
        }
jtulach@162
   174
        if (u.toExternalForm().contains("rt.jar!")) {
jtulach@162
   175
            throw new IOException("No emulation for " + u);
jtulach@162
   176
        }
jaroslav@93
   177
        return u.openStream();
jaroslav@93
   178
    }
jaroslav@136
   179
jaroslav@136
   180
    static String toString(String name) throws IOException {
jaroslav@136
   181
        StringBuilder sb = new StringBuilder();
jaroslav@136
   182
        compile(sb, name);
jaroslav@136
   183
        return sb.toString().toString();
jaroslav@136
   184
    }
jtulach@162
   185
jtulach@162
   186
    private StringArray scripts = new StringArray();
jtulach@162
   187
    private StringArray references = new StringArray();
jtulach@162
   188
    
jtulach@162
   189
    @Override
jtulach@162
   190
    protected boolean requireReference(String cn) {
jtulach@162
   191
        if (references.contains(cn)) {
jtulach@162
   192
            return false;
jtulach@162
   193
        }
jtulach@162
   194
        references.add(cn);
jtulach@162
   195
        return true;
jtulach@162
   196
    }
jtulach@162
   197
jtulach@162
   198
    @Override
jtulach@162
   199
    protected void requireScript(String resourcePath) {
jtulach@162
   200
        scripts.add(resourcePath);
jtulach@162
   201
    }
jaroslav@29
   202
}