mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
Generalize the usage of WasmTarget
This commit is contained in:
parent
ab39801cf4
commit
3709e9cc55
@ -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 );
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<String, String> properties ) throws IOException {
|
||||
this.output = output;
|
||||
public TextModuleWriter( WasmTarget target, HashMap<String, String> properties ) throws IOException {
|
||||
this.output = target.getTextOutput();
|
||||
debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
||||
output.append( "(module" );
|
||||
inset++;
|
||||
|
@ -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 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user