add method addLibrary

This commit is contained in:
Volker Berlin 2018-11-21 19:44:05 +01:00
parent 5ca933f540
commit 2c792f4a4f
2 changed files with 35 additions and 2 deletions

View File

@ -48,6 +48,8 @@ public class JWebAssembly {
private final HashMap<String, String> properties = new HashMap<>(); 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. * Property for adding debug names to the output if true.
*/ */
@ -122,6 +124,30 @@ public class JWebAssembly {
} }
/** /**
* 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 {
libraries.add( 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. * Convert the added files to a WebAssembly module in text representation.
* *
* @return the module as string * @return the module as string
@ -222,7 +248,7 @@ public class JWebAssembly {
* if any conversion error occurs * if any conversion error occurs
*/ */
private void compile( ModuleWriter writer ) throws IOException, WasmException { private void compile( ModuleWriter writer ) throws IOException, WasmException {
ModuleGenerator generator = new ModuleGenerator( writer ); ModuleGenerator generator = new ModuleGenerator( writer, libraries );
for( URL url : classFiles ) { for( URL url : classFiles ) {
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) ); ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
generator.prepare( classFile ); generator.prepare( classFile );

View File

@ -16,6 +16,8 @@
package de.inetsoftware.jwebassembly.module; package de.inetsoftware.jwebassembly.module;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -41,6 +43,8 @@ public class ModuleGenerator {
private final ModuleWriter writer; private final ModuleWriter writer;
private final URLClassLoader libraries;
private final JavaMethodWasmCodeBuilder javaCodeBuilder = new JavaMethodWasmCodeBuilder(); private final JavaMethodWasmCodeBuilder javaCodeBuilder = new JavaMethodWasmCodeBuilder();
private final WatParser watParser = new WatParser(); private final WatParser watParser = new WatParser();
@ -54,9 +58,12 @@ public class ModuleGenerator {
* *
* @param writer * @param writer
* the target writer * the target writer
* @param libraries
* libraries
*/ */
public ModuleGenerator( @Nonnull ModuleWriter writer ) { public ModuleGenerator( @Nonnull ModuleWriter writer, List<URL> libraries ) {
this.writer = writer; this.writer = writer;
this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) );
} }
/** /**