mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-25 15:37:52 +01:00
Generalize the usage of WasmTarget
This commit is contained in:
parent
ab39801cf4
commit
3709e9cc55
@ -16,16 +16,12 @@
|
|||||||
package de.inetsoftware.jwebassembly;
|
package de.inetsoftware.jwebassembly;
|
||||||
|
|
||||||
import java.io.BufferedInputStream;
|
import java.io.BufferedInputStream;
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -181,8 +177,8 @@ public class JWebAssembly {
|
|||||||
* if any conversion error occurs
|
* if any conversion error occurs
|
||||||
*/
|
*/
|
||||||
public void compileToText( File file ) throws WasmException {
|
public void compileToText( File file ) throws WasmException {
|
||||||
try (OutputStreamWriter output = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( file ) ), StandardCharsets.UTF_8 )) {
|
try (WasmTarget target = new WasmTarget( file )) {
|
||||||
compileToText( output );
|
compileToText( target );
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throw WasmException.create( ex );
|
throw WasmException.create( ex );
|
||||||
}
|
}
|
||||||
@ -197,7 +193,23 @@ public class JWebAssembly {
|
|||||||
* if any conversion error occurs
|
* if any conversion error occurs
|
||||||
*/
|
*/
|
||||||
public void compileToText( Appendable output ) throws WasmException {
|
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 );
|
compile( writer );
|
||||||
} catch( Exception ex ) {
|
} catch( Exception ex ) {
|
||||||
throw WasmException.create( ex );
|
throw WasmException.create( ex );
|
||||||
|
@ -36,8 +36,13 @@ public class WasmTarget implements Closeable {
|
|||||||
|
|
||||||
private OutputStream output;
|
private OutputStream output;
|
||||||
|
|
||||||
|
private Appendable textOutput;
|
||||||
|
|
||||||
private Writer sourceMap;
|
private Writer sourceMap;
|
||||||
|
|
||||||
|
private Writer javaScript;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a target with a file.
|
* Create a target with a file.
|
||||||
*
|
*
|
||||||
@ -58,6 +63,16 @@ public class WasmTarget implements Closeable {
|
|||||||
this.output = output;
|
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
|
* Get the wasm OutputStream
|
||||||
*
|
*
|
||||||
@ -73,6 +88,20 @@ public class WasmTarget implements Closeable {
|
|||||||
return output;
|
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.
|
* 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;
|
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
|
* Close all streams
|
||||||
*
|
*
|
||||||
@ -112,8 +155,14 @@ public class WasmTarget implements Closeable {
|
|||||||
if( output != null ) {
|
if( output != null ) {
|
||||||
output.close();
|
output.close();
|
||||||
}
|
}
|
||||||
|
if( textOutput instanceof Closeable ) {
|
||||||
|
((Closeable)textOutput).close();
|
||||||
|
}
|
||||||
if( sourceMap != null ) {
|
if( sourceMap != null ) {
|
||||||
sourceMap.close();
|
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.ModuleWriter;
|
||||||
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
|
||||||
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
import de.inetsoftware.jwebassembly.module.ValueTypeConvertion;
|
||||||
|
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
||||||
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||||
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
|
||||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||||
@ -91,8 +92,8 @@ public class TextModuleWriter extends ModuleWriter {
|
|||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if any I/O error occur
|
* if any I/O error occur
|
||||||
*/
|
*/
|
||||||
public TextModuleWriter( Appendable output, HashMap<String, String> properties ) throws IOException {
|
public TextModuleWriter( WasmTarget target, HashMap<String, String> properties ) throws IOException {
|
||||||
this.output = output;
|
this.output = target.getTextOutput();
|
||||||
debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
||||||
output.append( "(module" );
|
output.append( "(module" );
|
||||||
inset++;
|
inset++;
|
||||||
|
@ -39,7 +39,7 @@ public class WatParserTest {
|
|||||||
parser.parse( wat, 100 );
|
parser.parse( wat, 100 );
|
||||||
WasmCodeBuilder codeBuilder = parser;
|
WasmCodeBuilder codeBuilder = parser;
|
||||||
StringBuilder builder = new StringBuilder();
|
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 );
|
writer.writeMethodStart( new FunctionName( "A.a()V" ), null );
|
||||||
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
||||||
instruction.writeTo( writer );
|
instruction.writeTo( writer );
|
||||||
|
Loading…
x
Reference in New Issue
Block a user