From 2c792f4a4f55e53c8813b1bc12435b4f279861d7 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 21 Nov 2018 19:44:05 +0100 Subject: [PATCH] add method addLibrary --- .../jwebassembly/JWebAssembly.java | 28 ++++++++++++++++++- .../jwebassembly/module/ModuleGenerator.java | 9 +++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index 7158cb9..6a19050 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -48,6 +48,8 @@ public class JWebAssembly { private final HashMap properties = new HashMap<>(); + private final List libraries = new ArrayList<>(); + /** * 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. * * @return the module as string @@ -222,7 +248,7 @@ public class JWebAssembly { * if any conversion error occurs */ private void compile( ModuleWriter writer ) throws IOException, WasmException { - ModuleGenerator generator = new ModuleGenerator( writer ); + ModuleGenerator generator = new ModuleGenerator( writer, libraries ); for( URL url : classFiles ) { ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) ); generator.prepare( classFile ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index ef05d1e..a0d32d8 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -16,6 +16,8 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.List; import java.util.Map; import java.util.function.Consumer; @@ -41,6 +43,8 @@ public class ModuleGenerator { private final ModuleWriter writer; + private final URLClassLoader libraries; + private final JavaMethodWasmCodeBuilder javaCodeBuilder = new JavaMethodWasmCodeBuilder(); private final WatParser watParser = new WatParser(); @@ -54,9 +58,12 @@ public class ModuleGenerator { * * @param writer * the target writer + * @param libraries + * libraries */ - public ModuleGenerator( @Nonnull ModuleWriter writer ) { + public ModuleGenerator( @Nonnull ModuleWriter writer, List libraries ) { this.writer = writer; + this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ); } /**