mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 10:44:47 +01:00
add WasmOptions to reduce boilerplate code
This commit is contained in:
parent
713020ef60
commit
c1041325b7
@ -40,6 +40,7 @@ import de.inetsoftware.jwebassembly.module.ModuleGenerator;
|
||||
import de.inetsoftware.jwebassembly.module.ModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.module.WasmTarget;
|
||||
import de.inetsoftware.jwebassembly.text.TextModuleWriter;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* The main class of the compiler.
|
||||
@ -325,7 +326,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, properties );
|
||||
ModuleGenerator generator = new ModuleGenerator( writer, target, libraries, new WasmOptions( properties ) );
|
||||
for( URL url : classFiles ) {
|
||||
ClassFile classFile = new ClassFile( new BufferedInputStream( url.openStream() ) );
|
||||
generator.prepare( classFile );
|
||||
|
@ -46,6 +46,7 @@ import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||
import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
import de.inetsoftware.jwebassembly.watparser.WatParser;
|
||||
|
||||
/**
|
||||
@ -84,18 +85,18 @@ public class ModuleGenerator {
|
||||
* the target for the module data
|
||||
* @param libraries
|
||||
* libraries
|
||||
* @param properties
|
||||
* @param options
|
||||
* compiler properties
|
||||
*/
|
||||
public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> libraries, HashMap<String, String> properties ) {
|
||||
public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List<URL> libraries, WasmOptions options ) {
|
||||
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()] ) );
|
||||
types.init( properties );
|
||||
javaCodeBuilder.init( types, functions, properties );
|
||||
((WasmCodeBuilder)watParser).init( types, functions, properties );
|
||||
types.init( options );
|
||||
javaCodeBuilder.init( types, functions, options );
|
||||
((WasmCodeBuilder)watParser).init( types, functions, options );
|
||||
scanLibraries( libraries );
|
||||
}
|
||||
|
||||
|
@ -37,6 +37,7 @@ import de.inetsoftware.jwebassembly.wasm.AnyType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ArrayType;
|
||||
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* Manage the written and to write types (classes)
|
||||
@ -54,16 +55,16 @@ public class TypeManager {
|
||||
|
||||
private boolean isFinish;
|
||||
|
||||
private boolean useGC;
|
||||
private WasmOptions options;
|
||||
|
||||
/**
|
||||
* Initialize the type manager.
|
||||
*
|
||||
* @param properties
|
||||
* @param options
|
||||
* compiler properties
|
||||
*/
|
||||
void init( HashMap<String, String> properties ) {
|
||||
this.useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) );
|
||||
void init( WasmOptions options ) {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -206,7 +207,7 @@ public class TypeManager {
|
||||
methods = new ArrayList<>();
|
||||
HashSet<String> allNeededFields = new HashSet<>();
|
||||
listStructFields( name, functions, types, libraries, allNeededFields );
|
||||
code = types.useGC ? writer.writeStructType( this ) : ValueType.anyref.getCode();
|
||||
code = types.options.useGC() ? writer.writeStructType( this ) : ValueType.anyref.getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,7 +17,6 @@ package de.inetsoftware.jwebassembly.module;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nonnegative;
|
||||
@ -28,7 +27,6 @@ import de.inetsoftware.classparser.ClassFile;
|
||||
import de.inetsoftware.classparser.LocalVariableTable;
|
||||
import de.inetsoftware.classparser.Member;
|
||||
import de.inetsoftware.classparser.MethodInfo;
|
||||
import de.inetsoftware.jwebassembly.JWebAssembly;
|
||||
import de.inetsoftware.jwebassembly.WasmException;
|
||||
import de.inetsoftware.jwebassembly.javascript.NonGC;
|
||||
import de.inetsoftware.jwebassembly.module.WasmInstruction.Type;
|
||||
@ -40,6 +38,7 @@ import de.inetsoftware.jwebassembly.wasm.StructOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueType;
|
||||
import de.inetsoftware.jwebassembly.wasm.ValueTypeParser;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmBlockOperator;
|
||||
import de.inetsoftware.jwebassembly.wasm.WasmOptions;
|
||||
|
||||
/**
|
||||
* Base class for Code Building.
|
||||
@ -56,7 +55,7 @@ public abstract class WasmCodeBuilder {
|
||||
|
||||
private FunctionManager functions;
|
||||
|
||||
private boolean useGC;
|
||||
private WasmOptions options;
|
||||
|
||||
/**
|
||||
* Get the list of instructions
|
||||
@ -87,14 +86,14 @@ public abstract class WasmCodeBuilder {
|
||||
* the type manager
|
||||
* @param functions
|
||||
* the function manager
|
||||
* @param properties
|
||||
* @param options
|
||||
* compiler properties
|
||||
*/
|
||||
void init( TypeManager types, FunctionManager functions, HashMap<String, String> properties ) {
|
||||
void init( TypeManager types, FunctionManager functions, WasmOptions options ) {
|
||||
this.localVariables.init( types );
|
||||
this.types = types;
|
||||
this.functions = functions;
|
||||
this.useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) );
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -262,7 +261,7 @@ public abstract class WasmCodeBuilder {
|
||||
protected WasmNumericInstruction addNumericInstruction( @Nullable NumericOperator numOp, @Nullable ValueType valueType, int javaCodePos, int lineNumber ) {
|
||||
WasmNumericInstruction numeric = new WasmNumericInstruction( numOp, valueType, javaCodePos, lineNumber );
|
||||
instructions.add( numeric );
|
||||
if( !useGC && numOp == NumericOperator.ref_eq ) {
|
||||
if( !options.useGC() && numOp == NumericOperator.ref_eq ) {
|
||||
functions.markAsNeeded( getNonGC( "ref_eq", lineNumber ), true );
|
||||
}
|
||||
return numeric;
|
||||
@ -372,7 +371,7 @@ public abstract class WasmCodeBuilder {
|
||||
* the line number in the Java source code
|
||||
*/
|
||||
protected void addArrayInstruction( ArrayOperator op, AnyType type, int javaCodePos, int lineNumber ) {
|
||||
if( useGC ) {
|
||||
if( options.useGC() ) {
|
||||
instructions.add( new WasmArrayInstruction( op, type, types, javaCodePos, lineNumber ) );
|
||||
} else {
|
||||
if( type.getCode() >= 0 ) {
|
||||
@ -401,7 +400,7 @@ public abstract class WasmCodeBuilder {
|
||||
protected void addStructInstruction( StructOperator op, @Nonnull String typeName, @Nullable NamedStorageType fieldName, int javaCodePos, int lineNumber ) {
|
||||
WasmStructInstruction structInst = new WasmStructInstruction( op, typeName, fieldName, javaCodePos, lineNumber, types );
|
||||
instructions.add( structInst );
|
||||
if( !useGC ) {
|
||||
if( !options.useGC() ) {
|
||||
SyntheticFunctionName name = structInst.createNonGcFunction();
|
||||
if( name != null ) {
|
||||
functions.markAsNeeded( name, true );
|
||||
|
49
src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java
Normal file
49
src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java
Normal file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2017 - 2019 Volker Berlin (i-net software)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package de.inetsoftware.jwebassembly.wasm;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import de.inetsoftware.jwebassembly.JWebAssembly;
|
||||
|
||||
/**
|
||||
* The option/properties for the behavior of the compiler.
|
||||
*
|
||||
* @author Volker Berlin
|
||||
*/
|
||||
public class WasmOptions {
|
||||
|
||||
private final boolean useGC;
|
||||
|
||||
/**
|
||||
* Create a new instance of options
|
||||
*
|
||||
* @param properties
|
||||
* compiler properties
|
||||
*/
|
||||
public WasmOptions( HashMap<String, String> properties ) {
|
||||
useGC = Boolean.parseBoolean( properties.getOrDefault( JWebAssembly.WASM_USE_GC, "false" ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* If the GC feature of WASM should be use or the GC of the JavaScript host.
|
||||
*
|
||||
* @return true, use the GC instructions of WASM.
|
||||
*/
|
||||
public boolean useGC() {
|
||||
return useGC;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user