From 0d0e606983dcfd9b34db52c539bb2875130fc1db Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 23 Jan 2019 20:27:57 +0100 Subject: [PATCH] Pass the StructType of WasmStructInstruction to the LocalevariableManager --- .../module/LocaleVariableManager.java | 10 ++++-- .../jwebassembly/module/ModuleGenerator.java | 12 +++---- .../module/WasmCallInstruction.java | 7 ++-- .../jwebassembly/module/WasmCodeBuilder.java | 14 +++++++- .../module/WasmStructInstruction.java | 32 ++++++------------- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java index fb3d0a8..836b0f4 100644 --- a/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java +++ b/src/de/inetsoftware/jwebassembly/module/LocaleVariableManager.java @@ -83,8 +83,14 @@ class LocaleVariableManager { size = Math.max( size, slot + 1 ); LocaleVariable var = variables[slot]; if( var.valueType != null && var.valueType != valueType ) { - throw new WasmException( "Redefine local variable type from " + var.valueType + " to " - + valueType, null, null, -1 ); + if( var.valueType.getCode() >= 0 && valueType == ValueType.anyref ) { + return; + } + if( valueType.getCode() >= 0 && var.valueType == ValueType.anyref ) { + // set the more specific type + } else { + throw new WasmException( "Redefine local variable type from " + var.valueType + " to " + valueType, null, null, -1 ); + } } var.valueType = valueType; } diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index c467d9c..8886faf 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -77,6 +77,8 @@ public class ModuleGenerator { public ModuleGenerator( @Nonnull ModuleWriter writer, List libraries ) { this.writer = writer; this.libraries = new URLClassLoader( libraries.toArray( new URL[libraries.size()] ) ); + javaCodeBuilder.init( types ); + ((WasmCodeBuilder)watParser).init( types ); } /** @@ -155,13 +157,9 @@ public class ModuleGenerator { * if any I/O error occur */ private void setStructType( WasmStructInstruction instruction ) throws IOException { - String name = instruction.getTypeName(); - if( name != null ) { - StructType type = types.valueOf( name ); - instruction.setType( type ); - if( type.getCode() == Integer.MAX_VALUE ) { - writeStructType( type ); - } + StructType type = instruction.getStructType(); + if( type != null && type.getCode() == Integer.MAX_VALUE ) { + writeStructType( type ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java index c228d8a..609b28b 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCallInstruction.java @@ -22,7 +22,6 @@ import java.util.Iterator; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.wasm.AnyType; -import de.inetsoftware.jwebassembly.wasm.ValueType; /** * WasmInstruction for a function call. @@ -32,7 +31,7 @@ import de.inetsoftware.jwebassembly.wasm.ValueType; */ class WasmCallInstruction extends WasmInstruction { - private ValueType valueType; + private AnyType valueType; private final FunctionName name; @@ -105,9 +104,9 @@ class WasmCallInstruction extends WasmInstruction { while( parser.next() != null ) { paramCount++; } - valueType = (ValueType)parser.next(); + valueType = parser.next(); while( parser.hasNext() ) { - valueType = (ValueType)parser.next(); + valueType = parser.next(); paramCount--; } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index 0072392..d03c6de 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -41,6 +41,8 @@ public abstract class WasmCodeBuilder { private final List instructions = new ArrayList<>(); + private TypeManager types; + /** * Get the list of instructions * @@ -50,6 +52,16 @@ public abstract class WasmCodeBuilder { return instructions; } + /** + * Initialize the code builder; + * + * @param types + * the type manager + */ + void init( TypeManager types ) { + this.types = types; + } + /** * Get the data types of the local variables. The value is only valid until the next call. * @@ -239,6 +251,6 @@ public abstract class WasmCodeBuilder { * the code position/offset in the Java method */ protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) { - instructions.add( new WasmStructInstruction( op, typeName, fieldName, javaCodePos ) ); + instructions.add( new WasmStructInstruction( op, typeName == null ? null : types.valueOf( typeName ), fieldName, javaCodePos ) ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 4b90f77..58e259a 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -37,9 +37,7 @@ class WasmStructInstruction extends WasmInstruction { private final StructOperator op; - private final String typeName; - - private AnyType type; + private final StructType type; private final String fieldName; @@ -48,37 +46,27 @@ class WasmStructInstruction extends WasmInstruction { * * @param op * the struct operation - * @param typeName - * the type name of the parameters + * @param type + * the type of the parameters * @param fieldName * the name of field if needed for the operation * @param javaCodePos * the code position/offset in the Java method */ - WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) { + WasmStructInstruction( @Nullable StructOperator op, @Nullable StructType type, @Nullable String fieldName, int javaCodePos ) { super( javaCodePos ); this.op = op; - this.typeName = typeName; + this.type = type; this.fieldName = fieldName; } /** - * Get the type name of this instruction. + * Get the struct type of this instruction. * * @return the type */ - String getTypeName() { - return typeName; - } - - /** - * Set the resolved StructType for the typeName - * - * @param type - * the type - */ - void setType( StructType type ) { - this.type = type; + StructType getStructType() { + return type; } /** @@ -101,10 +89,10 @@ class WasmStructInstruction extends WasmInstruction { */ AnyType getPushValueType() { switch( op ) { - case NEW: - case NEW_DEFAULT: case NULL: return ValueType.anyref; + case NEW: + case NEW_DEFAULT: case GET: return type; case SET: