From c1041325b75dfedeef92ac60648690b6f43f50b2 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Mon, 9 Sep 2019 21:07:45 +0200 Subject: [PATCH] add WasmOptions to reduce boilerplate code --- .../jwebassembly/JWebAssembly.java | 3 +- .../jwebassembly/module/ModuleGenerator.java | 11 +++-- .../jwebassembly/module/TypeManager.java | 11 +++-- .../jwebassembly/module/WasmCodeBuilder.java | 17 +++---- .../jwebassembly/wasm/WasmOptions.java | 49 +++++++++++++++++++ 5 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java diff --git a/src/de/inetsoftware/jwebassembly/JWebAssembly.java b/src/de/inetsoftware/jwebassembly/JWebAssembly.java index b532f47..9f2f5e6 100644 --- a/src/de/inetsoftware/jwebassembly/JWebAssembly.java +++ b/src/de/inetsoftware/jwebassembly/JWebAssembly.java @@ -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 ); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 61e7996..aed5961 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -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 libraries, HashMap properties ) { + public ModuleGenerator( @Nonnull ModuleWriter writer, WasmTarget target, @Nonnull List 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 ); } diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index ebee427..6bdb50c 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -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 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 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(); } /** diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 85f1773..45e837d 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -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 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 ); diff --git a/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java new file mode 100644 index 0000000..26a0743 --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java @@ -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 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; + } +}