From 2f2c39a07a867fbce42c837014098ff06b10914d Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Mon, 24 Feb 2020 21:08:29 +0100 Subject: [PATCH] simplify the initializing --- .../jwebassembly/module/CodeOptimizer.java | 6 +++--- .../jwebassembly/module/ModuleGenerator.java | 18 ++++++++++-------- .../jwebassembly/module/StringManager.java | 13 +++++++------ .../jwebassembly/module/TypeManager.java | 4 ++-- .../jwebassembly/module/WasmCodeBuilder.java | 18 +++++++----------- .../jwebassembly/wasm/WasmOptions.java | 12 ++++++++++++ .../jwebassembly/module/WatParserTest.java | 4 ++-- 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java b/src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java index ddd97c5..3227361 100644 --- a/src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java +++ b/src/de/inetsoftware/jwebassembly/module/CodeOptimizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 Volker Berlin (i-net software) + * Copyright 2019 - 2020 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. @@ -25,7 +25,7 @@ import de.inetsoftware.jwebassembly.wasm.VariableOperator; * * @author Volker Berlin */ -class CodeOptimizer { +public class CodeOptimizer { /** * Optimize the code before writing. @@ -33,7 +33,7 @@ class CodeOptimizer { * @param instructions * the list of instructions */ - public void optimze( List instructions ) { + void optimze( List instructions ) { for( int i = instructions.size()-1; i >= 0; i-- ) { WasmInstruction instr = instructions.get( i ); switch( instr.getType() ) { diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index ab6a8e9..b03611c 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -70,13 +70,13 @@ public class ModuleGenerator { private String className; - private FunctionManager functions = new FunctionManager(); + private final FunctionManager functions; - private TypeManager types = new TypeManager(); + private final TypeManager types; - private StringManager strings = new StringManager(); + private final StringManager strings; - private CodeOptimizer optimizer = new CodeOptimizer(); + private final CodeOptimizer optimizer; /** * Create a new generator. @@ -95,10 +95,12 @@ public class ModuleGenerator { this.javaScript = new JavaScriptWriter( target ); this.classFileLoader = new ClassFileLoader( new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ) ); WasmOptions options = writer.options; - types.init( options ); - strings.init( functions ); - javaCodeBuilder.init( types, functions, strings, options, classFileLoader ); - ((WasmCodeBuilder)watParser).init( types, functions, strings, options, classFileLoader ); + functions = options.functions; + types = options.types; + strings = options.strings; + optimizer = options.optimizer; + javaCodeBuilder.init( options, classFileLoader ); + ((WasmCodeBuilder)watParser).init( options, classFileLoader ); scanLibraries( libraries ); } diff --git a/src/de/inetsoftware/jwebassembly/module/StringManager.java b/src/de/inetsoftware/jwebassembly/module/StringManager.java index cf43c22..9011423 100644 --- a/src/de/inetsoftware/jwebassembly/module/StringManager.java +++ b/src/de/inetsoftware/jwebassembly/module/StringManager.java @@ -27,13 +27,14 @@ import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode; import de.inetsoftware.jwebassembly.wasm.ValueType; +import de.inetsoftware.jwebassembly.wasm.WasmOptions; /** * Handle all the constant strings. The constant strings will be write into the data section. * * @author Volker Berlin */ -class StringManager extends LinkedHashMap { +public class StringManager extends LinkedHashMap { /** * Signature of method stringConstant. @@ -48,13 +49,13 @@ class StringManager extends LinkedHashMap { private int stringMemoryOffset = -1; /** - * Initialize the string manager. + * Create a new instance. * - * @param functions - * the function manager + * @param options + * compiler properties and shared managers */ - void init( FunctionManager functions ) { - this.functions = functions; + public StringManager( WasmOptions options ) { + this.functions = options.functions; } /** diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index 9d8a7c0..1414f4e 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -52,7 +52,7 @@ public class TypeManager { private boolean isFinish; - private WasmOptions options; + private final WasmOptions options; /** * Initialize the type manager. @@ -60,7 +60,7 @@ public class TypeManager { * @param options * compiler properties */ - void init( WasmOptions options ) { + public TypeManager( WasmOptions options ) { this.options = options; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index b7cfbda..8a4940e 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -75,20 +75,16 @@ public abstract class WasmCodeBuilder { /** * Initialize the code builder; * - * @param types - * the type manager - * @param functions - * the function manager - * @param strings - * the string manager * @param options * compiler properties + * @param classFileLoader + * for loading the class files */ - void init( TypeManager types, FunctionManager functions, StringManager strings, WasmOptions options, ClassFileLoader classFileLoader ) { - this.localVariables.init( types ); - this.types = types; - this.functions = functions; - this.strings = strings; + void init( WasmOptions options, ClassFileLoader classFileLoader ) { + this.localVariables.init( options.types ); + this.types = options.types; + this.functions = options.functions; + this.strings = options.strings; this.options = options; this.classFileLoader = classFileLoader; } diff --git a/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java index 2b7e28d..b1fd089 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java +++ b/src/de/inetsoftware/jwebassembly/wasm/WasmOptions.java @@ -20,7 +20,11 @@ import java.util.HashMap; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.JWebAssembly; +import de.inetsoftware.jwebassembly.module.CodeOptimizer; +import de.inetsoftware.jwebassembly.module.FunctionManager; import de.inetsoftware.jwebassembly.module.FunctionName; +import de.inetsoftware.jwebassembly.module.StringManager; +import de.inetsoftware.jwebassembly.module.TypeManager; /** * The option/properties for the behavior of the compiler. @@ -29,6 +33,14 @@ import de.inetsoftware.jwebassembly.module.FunctionName; */ public class WasmOptions { + public final FunctionManager functions = new FunctionManager(); + + public final TypeManager types = new TypeManager( this ); + + public final StringManager strings = new StringManager( this ); + + public final CodeOptimizer optimizer = new CodeOptimizer(); + private final boolean debugNames; private final boolean useGC; diff --git a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java index ee3ca05..8005f8b 100644 --- a/test/de/inetsoftware/jwebassembly/module/WatParserTest.java +++ b/test/de/inetsoftware/jwebassembly/module/WatParserTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2018 - 2019 Volker Berlin (i-net software) + * Copyright 2018 - 2020 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. @@ -40,7 +40,7 @@ public class WatParserTest { WasmOptions options = new WasmOptions( new HashMap<>() ); WatParser parser = new WatParser(); WasmCodeBuilder codeBuilder = parser; - codeBuilder.init( null, null, null, options, null ); + codeBuilder.init( options, null ); parser.parse( wat, null, 100 ); StringBuilder builder = new StringBuilder(); ModuleWriter writer = new TextModuleWriter( new WasmTarget( builder ), options );