mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
use WasmOptions on more places for consistency
This commit is contained in:
parent
c1041325b7
commit
b4d2f6caa2
@ -25,7 +25,6 @@ import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.logging.ConsoleHandler;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
@ -245,7 +244,7 @@ public class JWebAssembly {
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
private void compileToText( WasmTarget target ) throws WasmException {
|
||||
try (TextModuleWriter writer = new TextModuleWriter( target, properties )) {
|
||||
try (TextModuleWriter writer = new TextModuleWriter( target, new WasmOptions( properties ) )) {
|
||||
compile( writer, target );
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex );
|
||||
@ -306,7 +305,7 @@ public class JWebAssembly {
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
private void compileToBinary( WasmTarget target ) throws WasmException {
|
||||
try (BinaryModuleWriter writer = new BinaryModuleWriter( target, properties )) {
|
||||
try (BinaryModuleWriter writer = new BinaryModuleWriter( target, new WasmOptions( properties ) )) {
|
||||
compile( writer, target );
|
||||
} catch( Exception ex ) {
|
||||
throw WasmException.create( ex );
|
||||
@ -326,7 +325,7 @@ public class JWebAssembly {
|
||||
* if any conversion error occurs
|
||||
*/
|
||||
private void compile( ModuleWriter writer, WasmTarget target ) throws IOException, WasmException {
|
||||
ModuleGenerator generator = new ModuleGenerator( writer, target, libraries, new WasmOptions( properties ) );
|
||||
ModuleGenerator generator = new ModuleGenerator( writer, target, libraries );
|
||||
for( URL url : classFiles ) {
|
||||
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
|
||||
generator.prepare( classFile );
|
||||
|
@ -45,6 +45,7 @@ import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* Module Writer for binary format. http://webassembly.org/docs/binary-encoding/
|
||||
@ -61,8 +62,6 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
|
||||
private WasmOutputStream wasm;
|
||||
|
||||
private final boolean debugNames;
|
||||
|
||||
private final boolean createSourceMap;
|
||||
|
||||
private WasmOutputStream dataStream = new WasmOutputStream();
|
||||
@ -96,15 +95,16 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
*
|
||||
* @param target
|
||||
* the target for the module data.
|
||||
* @param properties
|
||||
* @param options
|
||||
* compiler properties
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
public BinaryModuleWriter( WasmTarget target, HashMap<String, String> properties ) throws IOException {
|
||||
public BinaryModuleWriter( WasmTarget target, WasmOptions options ) throws IOException {
|
||||
super( options );
|
||||
this.target = target;
|
||||
// for now we build the source map together with debug names
|
||||
debugNames = createSourceMap = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
||||
createSourceMap = options.debugNames();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -351,7 +351,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
* if any I/O error occur
|
||||
*/
|
||||
private void writeDebugNames() throws IOException {
|
||||
if( !debugNames ) {
|
||||
if( !options.debugNames() ) {
|
||||
return;
|
||||
}
|
||||
WasmOutputStream stream = new WasmOutputStream();
|
||||
@ -541,7 +541,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
|
||||
locals.add( valueType );
|
||||
break;
|
||||
}
|
||||
if( debugNames && name != null ) {
|
||||
if( options.debugNames() && name != null ) {
|
||||
if( function.paramNames == null ) {
|
||||
function.paramNames = new ArrayList<>();
|
||||
}
|
||||
|
@ -85,15 +85,14 @@ public class ModuleGenerator {
|
||||
* the target for the module data
|
||||
* @param libraries
|
||||
* libraries
|
||||
* @param options
|
||||
* compiler properties
|
||||
*/
|
||||
public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> libraries, WasmOptions options ) {
|
||||
public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> libraries ) {
|
||||
this.javaCodeBuilder = new JavaMethodWasmCodeBuilder();
|
||||
this.watParser = new WatParser();
|
||||
this.writer = writer;
|
||||
this.javaScript = new JavaScriptWriter( target );
|
||||
this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) );
|
||||
WasmOptions options = writer.options;
|
||||
types.init( options );
|
||||
javaCodeBuilder.init( types, functions, options );
|
||||
((WasmCodeBuilder)watParser).init( types, functions, options );
|
||||
|
@ -30,6 +30,7 @@ import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* Module Writer base class.
|
||||
@ -38,6 +39,21 @@ import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||
*/
|
||||
public abstract class ModuleWriter implements Closeable {
|
||||
|
||||
/**
|
||||
* The compiler options.
|
||||
*/
|
||||
protected final WasmOptions options;
|
||||
|
||||
/**
|
||||
* Create a instance with its options.
|
||||
*
|
||||
* @param options
|
||||
* the compiler options
|
||||
*/
|
||||
protected ModuleWriter( WasmOptions options ) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish the prepare after all classes/methods are prepare. This must be call before we can start with write the
|
||||
* first method.
|
||||
|
@ -28,7 +28,6 @@ import java.util.Set;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import de.inetsoftware.jwebassembly.JWebAssembly;
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.module.FunctionName;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||
@ -43,6 +42,7 @@ import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.VariableOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* Module Writer for text format with S-expressions.
|
||||
@ -56,8 +56,6 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
|
||||
private Appendable output;
|
||||
|
||||
private final boolean debugNames;
|
||||
|
||||
private final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
|
||||
|
||||
private final ArrayList<String> methodParamNames = new ArrayList<>();
|
||||
@ -86,25 +84,25 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
|
||||
private boolean callIndirect;
|
||||
|
||||
private boolean useGC;
|
||||
private WasmOptions options;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param target
|
||||
* target for the result
|
||||
* @param properties
|
||||
* @param options
|
||||
* compiler properties
|
||||
* @throws IOException
|
||||
* if any I/O error occur
|
||||
*/
|
||||
public TextModuleWriter( WasmTarget target, HashMap<String, String> properties ) throws IOException {
|
||||
public TextModuleWriter( WasmTarget target, WasmOptions options ) throws IOException {
|
||||
super( options );
|
||||
this.output = target.getTextOutput();
|
||||
debugNames = Boolean.parseBoolean( properties.get( JWebAssembly.DEBUG_NAMES ) );
|
||||
output.append( "(module" );
|
||||
inset++;
|
||||
useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) );
|
||||
if( spiderMonkey && useGC ) {
|
||||
this.options = options;
|
||||
if( spiderMonkey && options.useGC() ) {
|
||||
output.append( " (gc_feature_opt_in 3)" ); // enable GcFeatureOptIn for SpiderMonkey https://github.com/lars-t-hansen/moz-gc-experiments/blob/master/version2.md
|
||||
}
|
||||
}
|
||||
@ -191,7 +189,7 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
for( NamedStorageType field : type.getFields() ) {
|
||||
newline( output );
|
||||
output.append( "(field" );
|
||||
if( debugNames && field.getName() != null ) {
|
||||
if( options.debugNames() && field.getName() != null ) {
|
||||
output.append( " $" ).append( typeName ).append( '.' ).append( field.getName() );
|
||||
}
|
||||
output.append( " (mut " );
|
||||
@ -300,7 +298,7 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
private void writeTypeName( Appendable output, AnyType type ) throws IOException {
|
||||
if( type instanceof ValueType ) {
|
||||
output.append( type.toString() );
|
||||
} else if( useGC ) {
|
||||
} else if( options.useGC() ) {
|
||||
output.append( "(ref " ).append( normalizeName( type.toString() ) ).append( ')' );
|
||||
} else {
|
||||
output.append( ValueType.anyref.toString() );
|
||||
@ -330,7 +328,7 @@ public class TextModuleWriter extends ModuleWriter {
|
||||
return;
|
||||
}
|
||||
methodOutput.append( '(' ).append( kind );
|
||||
if( debugNames ) {
|
||||
if( options.debugNames() ) {
|
||||
if( name != null ) {
|
||||
methodOutput.append( " $" ).append( name );
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import org.junit.Test;
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.binary.BinaryModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
import de.inetsoftware.jwebassembly.watparser.WatParser;
|
||||
|
||||
/**
|
||||
@ -36,11 +37,13 @@ import de.inetsoftware.jwebassembly.watparser.WatParser;
|
||||
public class WatParserTest {
|
||||
|
||||
private void test( String wat ) throws IOException {
|
||||
WasmOptions options = new WasmOptions( new HashMap<>() );
|
||||
WatParser parser = new WatParser();
|
||||
parser.parse( wat, 100 );
|
||||
WasmCodeBuilder codeBuilder = parser;
|
||||
codeBuilder.init( null, null, options );
|
||||
parser.parse( wat, 100 );
|
||||
StringBuilder builder = new StringBuilder();
|
||||
ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), new HashMap<>() );
|
||||
ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), options );
|
||||
writer.writeMethodStart( new FunctionName( "A.a()V" ), null );
|
||||
for( WasmInstruction instruction : codeBuilder.getInstructions() ) {
|
||||
instruction.writeTo( writer );
|
||||
@ -52,7 +55,7 @@ public class WatParserTest {
|
||||
assertEquals( expected, actual );
|
||||
|
||||
// smoke test of the binary writer
|
||||
writer = new BinaryModuleWriter( new WasmTarget( builder ), new HashMap<>() );
|
||||
writer = new BinaryModuleWriter( new WasmTarget( builder ), new WasmOptions( 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