From 3709e9cc55524df9ce32011d50f84fbf5a7988f8 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Sun, 30 Jun 2019 14:43:45 +0200 Subject: [PATCH] Generalize the usage of WasmTarget --- .../jwebassembly/JWebAssembly.java | 26 +++++++--- .../jwebassembly/module/WasmTarget.java | 49 +++++++++++++++++++ .../jwebassembly/text/TextModuleWriter.java | 5 +- .../jwebassembly/module/WatParserTest.java | 2 +- 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index 427c429..4fb0699 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -16,16 +16,12 @@ package de.inetsoftware.jwebassembly; import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -181,8 +177,8 @@ public class JWebAssembly { * if any conversion error occurs */ public void compileToText( File file ) throws WasmException { - try (OutputStreamWriter output = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( file ) ), StandardCharsets.UTF_8 )) { - compileToText( output ); + try (WasmTarget target = new WasmTarget( file )) { + compileToText( target ); } catch( Exception ex ) { throw WasmException.create( ex ); } @@ -197,7 +193,23 @@ public class JWebAssembly { * if any conversion error occurs */ public void compileToText( Appendable output ) throws WasmException { - try (TextModuleWriter writer = new TextModuleWriter( output, properties )) { + 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 output + * 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, properties )) { compile( writer ); } catch( Exception ex ) { throw WasmException.create( ex ); diff --git a/src/de/inetsoftware/jwebassembly/module/WasmTarget.java b/src/de/inetsoftware/jwebassembly/module/WasmTarget.java index 1e2a092..5ac8c03 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmTarget.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmTarget.java @@ -36,8 +36,13 @@ public class WasmTarget implements Closeable { private OutputStream output; + private Appendable textOutput; + private Writer sourceMap; + private Writer javaScript; + + /** * Create a target with a file. * @@ -58,6 +63,16 @@ public class WasmTarget implements Closeable { this.output = output; } + /** + * Create a target with an Appendable for text export + * + * @param output + * the stream for the wasm file + */ + public WasmTarget( Appendable output ) { + this.textOutput = output; + } + /** * Get the wasm OutputStream * @@ -73,6 +88,20 @@ public class WasmTarget implements Closeable { return output; } + /** + * Get the output for the text format. + * + * @return the target + * @throws IOException + * if any I/O error occur + */ + public Appendable getTextOutput() throws IOException { + if( textOutput == null ) { + textOutput = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( file ) ), StandardCharsets.UTF_8 ); + } + return textOutput; + } + /** * Get the URL for the source mapping that should be write into the assembly. * @@ -101,6 +130,20 @@ public class WasmTarget implements Closeable { return sourceMap; } + /** + * Get the output for the JavaScript glue code. + * + * @return the script output. + * @throws IOException + * if any I/O error occur + */ + public Writer getJavaScriptOutput() throws IOException { + if( javaScript == null && file != null ) { + javaScript = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( file + ".js" ) ), StandardCharsets.UTF_8 ); + } + return javaScript; + } + /** * Close all streams * @@ -112,8 +155,14 @@ public class WasmTarget implements Closeable { if( output != null ) { output.close(); } + if( textOutput instanceof Closeable ) { + ((Closeable)textOutput).close(); + } if( sourceMap != null ) { sourceMap.close(); } + if( javaScript != null ) { + javaScript.close(); + } } } diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 474ef69..b7438af 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -32,6 +32,7 @@ import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.module.ValueTypeConvertion; +import de.inetsoftware.jwebassembly.module.WasmTarget; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; @@ -91,8 +92,8 @@ public class TextModuleWriter extends ModuleWriter { * @throws IOException * if any I/O error occur */ - public TextModuleWriter( Appendable output, HashMap properties ) throws IOException { - this.output = output; + public TextModuleWriter( WasmTarget target, HashMap properties ) throws IOException { + this.output = target.getTextOutput(); debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) ); output.append( "(module" ); inset++; diff --git a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java index 952b530..a25affb 100644 --- a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java +++ b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java @@ -39,7 +39,7 @@ public class WatParserTest { parser.parse( wat, 100 ); WasmCodeBuilder codeBuilder = parser; StringBuilder builder = new StringBuilder(); - ModuleWriter writer = new TextModuleWriter( builder, new HashMap<>() ); + ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), new HashMap<>() ); writer.writeMethodStart( new FunctionName( "A.a()V" ), null ); for( WasmInstruction instruction : codeBuilder.getInstructions() ) { instruction.writeTo( writer );