diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 9425527..bc86fe6 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -17,6 +17,7 @@ package de.inetsoftware.jwebassembly.module; import java.io.IOException; +import java.util.List; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -93,7 +94,22 @@ class WasmStructInstruction extends WasmInstruction { * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - int idx = type != null && fieldName != null ? type.getFields().indexOf( fieldName ) : -1; + int idx = -1; + if( type != null && fieldName != null ) { + // The fieldName of the struct operation does not contain the class name in which the field was declared. It contains the class name of the variable. This can be the class or a subclass. + List fields = type.getFields(); + boolean classNameMatched = false; + for( int i = fields.size()-1; i >= 0; i-- ) { + NamedStorageType field = fields.get( i ); + if( !classNameMatched && field.geClassName().equals( fieldName.geClassName() ) ) { + classNameMatched = true; + } + if( classNameMatched && field.getName().equals( fieldName.getName() ) ) { + idx = i; + break; + } + } + } writer.writeStructOperator( op, type, fieldName, idx ); } diff --git a/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java b/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java index 9fe9938..b7f2c6c 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java +++ b/src/de/inetsoftware/jwebassembly/wasm/NamedStorageType.java @@ -78,6 +78,15 @@ public class NamedStorageType { return type; } + /** + * Get the class name in which the filed is define. + * + * @return the field + */ + public String geClassName() { + return className; + } + /** * Get the global unique name of the field. See * https://github.com/lars-t-hansen/moz-gc-experiments/blob/master/version2.md#struct-and-ref-types