diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index b2f2b4b..31e112b 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -838,7 +838,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod * {@inheritDoc} */ @Override - protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException { + protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException { int opCode; switch(op) { case NEW: @@ -862,5 +862,9 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod if( type != null ) { codeStream.writeValueType( type ); } + if( fieldName != null ) { + StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() ); + codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) ); + } } } diff --git a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java index 3964d7d..c69b6d9 100644 --- a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java +++ b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java @@ -18,6 +18,9 @@ package de.inetsoftware.jwebassembly.binary; import java.io.IOException; import java.util.List; +import javax.annotation.Nonnull; + +import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.ValueType; @@ -60,6 +63,23 @@ class StructTypeEntry extends TypeEntry { } } + /** + * Get the index of the field in this structure + * + * @param fieldName + * the name of the field + * @return the index + */ + int getFieldIdx( @Nonnull String fieldName ) { + for( int i = 0; i < fields.size(); i++ ) { + NamedStorageType field = fields.get( i ); + if( field.name.equals( fieldName ) ) { + return i; + } + } + throw new WasmException( fieldName + " not found", -1 ); + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java index 62593be..7b9d20e 100644 --- a/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/JavaMethodWasmCodeBuilder.java @@ -90,7 +90,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { case 0: // nop break; case 1: // aconst_null - addStructInstruction( StructOperator.NULL, null, codePos ); + addStructInstruction( StructOperator.NULL, null, null, codePos ); break; case 2: // iconst_m1 case 3: // iconst_0 @@ -532,8 +532,14 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() ); addGlobalInstruction( false, ref, codePos ); break; - //TODO case 180: // getfield - //TODO case 181: // putfield + case 180: // getfield + ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() ); + addStructInstruction( StructOperator.GET, ref.getClassName(), ref.getName(), codePos ); + break; + case 181: // putfield + ref = (ConstantRef)constantPool.get( byteCode.readUnsignedShort() ); + addStructInstruction( StructOperator.SET, ref.getClassName(), ref.getName(), codePos ); + break; //TODO case 182: // invokevirtual case 183: // invokespecial, invoke a constructor case 184: // invokestatic @@ -545,7 +551,7 @@ class JavaMethodWasmCodeBuilder extends WasmCodeBuilder { //TODO case 186: // invokedynamic case 187: // new String name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName(); - addStructInstruction( StructOperator.NEW_DEFAULT, name, codePos ); + addStructInstruction( StructOperator.NEW_DEFAULT, name, null, codePos ); break; case 188: // newarray int typeValue = byteCode.readByte(); diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index 4b9a950..ba6a2f3 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -173,7 +173,7 @@ public class ModuleGenerator { throw new WasmException( "Missing class: " + className, -1 ); } ClassFile classFile = new ClassFile( stream ); - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); FieldInfo[] fields = classFile.getFields(); for( FieldInfo field : fields ) { if( field.isStatic() ) { diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java index 2ca6db8..ac3325d 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleWriter.java @@ -236,8 +236,9 @@ public abstract class ModuleWriter implements Closeable { * the operation * @param type * the type of the struct + * @param fieldName TODO * @throws IOException * if any I/O error occur */ - protected abstract void writeStructOperator( StructOperator op, StorageType type ) throws IOException; + protected abstract void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException; } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java index a3968a7..8a84840 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java @@ -233,10 +233,12 @@ public abstract class WasmCodeBuilder { * the operation * @param typeName * the type name + * @param fieldName + * the name of field if needed for the operation * @param javaCodePos * the code position/offset in the Java method */ - protected void addStructInstruction( StructOperator op, String typeName, int javaCodePos ) { - instructions.add( new WasmStructInstruction( op, typeName, javaCodePos ) ); + protected void addStructInstruction( StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) { + instructions.add( new WasmStructInstruction( op, typeName, fieldName, javaCodePos ) ); } } diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 4ec93c9..2036651 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -1,5 +1,5 @@ /* - Copyright 2018 Volker Berlin (i-net software) + Copyright 2018 - 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. @@ -41,6 +41,8 @@ class WasmStructInstruction extends WasmInstruction { private StorageType type; + private final String fieldName; + /** * Create an instance of numeric operation. * @@ -48,13 +50,16 @@ class WasmStructInstruction extends WasmInstruction { * the struct operation * @param typeName * the type name 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, int javaCodePos ) { + WasmStructInstruction( @Nullable StructOperator op, @Nullable String typeName, @Nullable String fieldName, int javaCodePos ) { super( javaCodePos ); this.op = op; this.typeName = typeName; + this.fieldName = fieldName; } /** @@ -88,7 +93,7 @@ class WasmStructInstruction extends WasmInstruction { * {@inheritDoc} */ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { - writer.writeStructOperator( op, type ); + writer.writeStructOperator( op, type, fieldName ); } /** diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 8d49640..7bdaecc 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -257,8 +257,10 @@ public class TextModuleWriter extends ModuleWriter { case ref_eq: methodOutput.append( "ref.eq" ); return; + default: } break; + default: } methodOutput.append( valueType ).append( '.' ).append( op ); } @@ -320,8 +322,10 @@ public class TextModuleWriter extends ModuleWriter { } /** - * Add a newline the insets. + * Add a newline with the insets. * + * @param output + * the target * @throws IOException * if any I/O error occur */ @@ -445,7 +449,7 @@ public class TextModuleWriter extends ModuleWriter { * {@inheritDoc} */ @Override - protected void writeStructOperator( StructOperator op, StorageType type ) throws IOException { + protected void writeStructOperator( StructOperator op, StorageType type, String fieldName ) throws IOException { String operation; switch( op ) { case NEW: @@ -470,13 +474,10 @@ public class TextModuleWriter extends ModuleWriter { newline( methodOutput ); methodOutput.append( operation ); if( type != null ) { - int typeCode = type.getCode(); - methodOutput.append( ' ' ); - if( typeCode < 0 ) { - methodOutput.append( type ); - } else { - methodOutput.append( typeCode ); - } + methodOutput.append( ' ' ).append( type ); + } + if( fieldName != null ) { + methodOutput.append( " $" ).append( fieldName ); } } }