-
Notifications
You must be signed in to change notification settings - Fork 64
/
JWebAssembly.java
380 lines (346 loc) · 12.4 KB
/
JWebAssembly.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright 2017 - 2022 Volker Berlin (i-net software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.inetsoftware.jwebassembly;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import javax.annotation.Nonnull;
import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
import de.inetsoftware.jwebassembly.module.ModuleGenerator;
import de.inetsoftware.jwebassembly.module.ModuleWriter;
import de.inetsoftware.jwebassembly.module.WasmOptions;
import de.inetsoftware.jwebassembly.module.WasmTarget;
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
/**
* The main class of the compiler.
*
* @author Volker Berlin
*/
public class JWebAssembly {
private final List<URL> classFiles = new ArrayList<>();
private final HashMap<String, String> properties = new HashMap<>();
private final List<URL> libraries = new ArrayList<>();
/**
* Property for adding debug names to the output if true.
*/
public static final String DEBUG_NAMES = "DebugNames";
/**
* Property for relative path between the final wasm file location and the source files location for the source map.
* If not empty it should end with a slash like "../../src/main/java/".
*/
public static final String SOURCE_MAP_BASE = "SourceMapBase";
/**
* The name of the annotation for import functions.
*/
public static final String IMPORT_ANNOTATION = "de.inetsoftware.jwebassembly.api.annotation.Import";
/**
* The name of the annotation for export functions.
*/
public static final String EXPORT_ANNOTATION = "de.inetsoftware.jwebassembly.api.annotation.Export";
/**
* The name of the annotation for native WASM code in text format.
*/
public static final String TEXTCODE_ANNOTATION = "de.inetsoftware.jwebassembly.api.annotation.WasmTextCode";
/**
* The name of the annotation for replacing a single method of the Java runtime.
*/
public static final String REPLACE_ANNOTATION = "de.inetsoftware.jwebassembly.api.annotation.Replace";
/**
* The name of the annotation for partial class another class of the Java runtime.
*/
public static final String PARTIAL_ANNOTATION = "de.inetsoftware.jwebassembly.api.annotation.Partial";
/**
* If the GC feature of WASM should be use or the GC of the JavaScript host. If true use the GC instructions of WASM.
*/
public static final String WASM_USE_GC = "wasm.use_gc";
/**
* If the exception handling feature of WASM should be use or an unreachable instruction. If true use the exception instructions of WASM.
*/
public static final String WASM_USE_EH = "wasm.use_eh";
/**
* Compiler property to ignore all referenced native methods without declared replacement in a library and replace them with a stub that throws an exception at runtime.
*/
public static final String IGNORE_NATIVE = "IgnoreNative";
/**
* The logger instance
*/
public static final Logger LOGGER = Logger.getAnonymousLogger( null );
static {
LOGGER.setUseParentHandlers( false );
Formatter formatter = new Formatter() {
@Override
public String format( LogRecord record ) {
String msg = record.getMessage() + '\n';
if( record.getThrown() != null ) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter( sw );
record.getThrown().printStackTrace( pw );
pw.close();
msg += sw.toString();
}
return msg;
}
};
StreamHandler handler = new StreamHandler( System.out, formatter ) {
@Override
public void publish(LogRecord record) {
super.publish(record);
flush();
}
};
handler.setLevel( Level.ALL );
LOGGER.addHandler( handler );
//LOGGER.setLevel( Level.FINE );
}
/**
* Create a instance.
*/
public JWebAssembly() {
ProtectionDomain protectionDomain = getClass().getProtectionDomain();
if( protectionDomain != null ) {
libraries.add( protectionDomain.getCodeSource().getLocation() ); // add the compiler self to the library path
}
}
/**
* Add a classFile to compile
*
* @param classFile
* the file
*/
public void addFile( @Nonnull File classFile ) {
try {
classFiles.add( classFile.toURI().toURL() );
} catch( MalformedURLException ex ) {
throw new IllegalArgumentException( ex );
}
}
/**
* Add a classFile to compile
*
* @param classFile
* the file
*/
public void addFile( @Nonnull URL classFile ) {
classFiles.add( classFile );
}
/**
* Set property to control the behavior of the compiler
*
* @param key
* the key
* @param value
* the new value
*/
public void setProperty( String key, String value ) {
properties.put( key, value );
}
/**
* Get the value of a property.
*
* @param key
* the key
* @return the current value
*/
public String getProperty( String key ) {
return properties.get( key );
}
/**
* Add a jar or zip file as library to the compiler. Methods from the library will be add to the wasm only when used.
*
* @param library
* a archive file
*/
public void addLibrary( @Nonnull File library ) {
try {
addLibrary( library.toURI().toURL() );
} catch( MalformedURLException ex ) {
throw new IllegalArgumentException( ex );
}
}
/**
* Add a jar or zip file as library to the compiler. Methods from the library will be add to the wasm only when used.
*
* @param library
* a archive file
*/
public void addLibrary( @Nonnull URL library ) {
libraries.add( library );
}
/**
* Convert the added files to a WebAssembly module in text representation.
*
* @return the module as string
* @throws WasmException
* if any conversion error occurs
*/
public String compileToText() throws WasmException {
StringBuilder output = new StringBuilder();
try {
compileToText( output );
return output.toString();
} catch( Exception ex ) {
System.err.println( output );
throw ex;
}
}
/**
* Convert the added files to a WebAssembly module in text representation.
*
* @param file
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
public void compileToText( File file ) throws WasmException {
try (WasmTarget target = new WasmTarget( file )) {
compileToText( target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module in text representation.
*
* @param output
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
public void compileToText( Appendable output ) throws WasmException {
try (WasmTarget target = new WasmTarget( output )) {
compileToText( target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module in text representation.
*
* @param target
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
private void compileToText( WasmTarget target ) throws WasmException {
try (TextModuleWriter writer = new TextModuleWriter( target, new WasmOptions( properties ) )) {
compile( writer, target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module in binary representation.
*
* @return the module as string
* @throws WasmException
* if any conversion error occurs
*/
public byte[] compileToBinary() throws WasmException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
compileToBinary( output );
return output.toByteArray();
}
/**
* Convert the added files to a WebAssembly module in binary representation.
*
* @param file
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
public void compileToBinary( File file ) throws WasmException {
try (WasmTarget target = new WasmTarget( file ) ) {
compileToBinary( target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module in binary representation.
*
* @param output
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
public void compileToBinary( OutputStream output ) throws WasmException {
try (WasmTarget target = new WasmTarget( output )) {
compileToBinary( target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module in binary representation.
*
* @param target
* the target for the module data
* @throws WasmException
* if any conversion error occurs
*/
private void compileToBinary( WasmTarget target ) throws WasmException {
try (BinaryModuleWriter writer = new BinaryModuleWriter( target, new WasmOptions( properties ) )) {
compile( writer, target );
} catch( Exception ex ) {
throw WasmException.create( ex );
}
}
/**
* Convert the added files to a WebAssembly module.
*
* @param writer
* the formatter
* @param target
* the target for the module data
* @throws IOException
* if any I/O error occur
* @throws WasmException
* if any conversion error occurs
*/
private void compile( ModuleWriter writer, WasmTarget target ) throws IOException, WasmException {
ModuleGenerator generator = new ModuleGenerator( writer, target, libraries );
for( URL url : classFiles ) {
try {
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
generator.prepare( classFile );
} catch( IOException ex ) {
LOGGER.fine( url + " " + ex );
if( url.getFile().endsWith( ".class" ) ) {
throw WasmException.create( "Parsing of file " + url + " failed.", ex );
}
// does not throw an exception on non *.class files like resource files or *.kotlin_module
}
}
generator.prepareFinish();
generator.finish();
}
}