From bfc6cbb3f09181ad130f556190d4ab6751e05ca9 Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 19 Dec 2018 20:10:26 +0100 Subject: [PATCH] add TypeManager --- .../module/JavaMethodWasmCodeBuilder.java | 3 +- .../jwebassembly/module/ModuleGenerator.java | 4 + .../jwebassembly/module/TypeManager.java | 80 +++++++++++++++++++ .../module/WasmStructInstruction.java | 11 ++- 4 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 src/de/inetsoftware/jwebassembly/module/TypeManager.java diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index a02bf00..a136e1b 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -26,6 +26,7 @@ import de.inetsoftware.classparser.ConstantClass; import de.inetsoftware.classparser.ConstantPool; import de.inetsoftware.classparser.ConstantRef; import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.NumericOperator; import de.inetsoftware.jwebassembly.wasm.StorageType; @@ -545,7 +546,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { //TODO case 186: // invokedynamic case 187: // new String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName(); - StorageType storageType = ValueType.anyref; + StorageType storageType = new StructType(name); addStructInstruction( StructOperator.NEW_DEFAULT, storageType, codePos ); break; case 188: // newarray diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 9f06acf..fb4c81c 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -58,6 +58,8 @@ public class ModuleGenerator { private FunctionManager functions = new FunctionManager(); + private TypeManager types = new TypeManager(); + /** * Create a new generator. * @@ -237,6 +239,8 @@ public class ModuleGenerator { case Call: functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() ); break; + case Struct: + types.useType( ((WasmStructInstruction)instruction).getStorageType() ); default: } instruction.writeTo( writer ); diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java new file mode 100644 index 0000000..476918f --- /dev/null +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -0,0 +1,80 @@ +/* + Copyright 2018 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.module; + +import java.util.HashMap; + +import de.inetsoftware.jwebassembly.wasm.StorageType; + +/** + * Manage the written and to write types (classes) + * + * @author Volker Berlin + */ +class TypeManager { + + private final HashMap map = new HashMap<>(); + + /** + * Use the type in the output. + * + * @param storageType + * the reference to a type + */ + void useType( StorageType storageType ) { + if( storageType instanceof StructType ) { + StructType type = (StructType)storageType; + StructType existingType = map.get( type.name ); + if( existingType == null ) { + type.code = map.size(); + map.put( type.name, type ); + } else { + type.code = existingType.code; + } + } + } + + /** + * A reference to a type. + * + * @author Volker Berlin + */ + static class StructType implements StorageType { + + private final String name; + + private int code; + + /** + * Create a reference to type + * + * @param name + * the Java class name + */ + StructType( String name ) { + this.name = name; + } + + /** + * {@inheritDoc} + */ + @Override + public int getCode() { + return code; + } + } +} diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 8b5f93c..e234b48 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -22,8 +22,6 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; import de.inetsoftware.jwebassembly.WasmException; -import de.inetsoftware.jwebassembly.module.WasmInstruction.Type; -import de.inetsoftware.jwebassembly.wasm.ArrayOperator; import de.inetsoftware.jwebassembly.wasm.StorageType; import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -56,6 +54,15 @@ class WasmStructInstruction extends WasmInstruction { this.type = type; } + /** + * Get the storage type of this instruction. + * + * @return the type + */ + StorageType getStorageType() { + return type; + } + /** * {@inheritDoc} */