Use index for fields of structs in text format until there are better naming rules.

This commit is contained in:
Volker Berlin 2019-04-22 15:56:11 +02:00
parent 02a2e9d8ff
commit b7323776d1
7 changed files with 27 additions and 36 deletions

View File

@ -1018,7 +1018,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException { protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException {
int opCode; int opCode;
switch(op) { switch(op) {
case NEW: case NEW:
@ -1043,8 +1043,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
codeStream.writeValueType( type ); codeStream.writeValueType( type );
} }
if( fieldName != null ) { if( fieldName != null ) {
StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() ); codeStream.writeVaruint32( idx );
codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) );
} }
} }
} }

View File

@ -18,9 +18,6 @@ package de.inetsoftware.jwebassembly.binary;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import de.inetsoftware.jwebassembly.WasmException;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueType;
@ -66,21 +63,6 @@ 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 NamedStorageType fieldName ) {
int idx = fields.indexOf( fieldName );
if( idx >= 0 ) {
return idx;
}
throw new WasmException( fieldName + " not found", -1 );
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View File

@ -427,13 +427,13 @@ public class ModuleGenerator {
writer.writeConst( 0, ValueType.i32 ); writer.writeConst( 0, ValueType.i32 );
break; break;
case anyref: case anyref:
writer.writeStructOperator( StructOperator.NULL, null, null ); writer.writeStructOperator( StructOperator.NULL, null, null, -1 );
break; break;
default: default:
throw new WasmException( "Not supported storage type: " + type, instruction.getLineNumber() ); throw new WasmException( "Not supported storage type: " + type, instruction.getLineNumber() );
} }
} else { } else {
writer.writeStructOperator( StructOperator.NULL, null, null ); writer.writeStructOperator( StructOperator.NULL, null, null, -1 );
} }
} }
} }

View File

@ -247,9 +247,12 @@ public abstract class ModuleWriter implements Closeable {
* the operation * the operation
* @param type * @param type
* the type of the struct * the type of the struct
* @param fieldName TODO * @param fieldName
* the fieldName if the operation is per field
* @param idx
* the index of the field if the operation is per field
* @throws IOException * @throws IOException
* if any I/O error occur * if any I/O error occur
*/ */
protected abstract void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException; protected abstract void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException;
} }

View File

@ -93,7 +93,8 @@ class WasmStructInstruction extends WasmInstruction {
* {@inheritDoc} * {@inheritDoc}
*/ */
public void writeTo( @Nonnull ModuleWriter writer ) throws IOException { public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
writer.writeStructOperator( op, type, fieldName ); int idx = type != null && fieldName != null ? type.getFields().indexOf( fieldName ) : -1;
writer.writeStructOperator( op, type, fieldName, idx );
} }
/** /**

View File

@ -103,13 +103,14 @@ public class TextModuleWriter extends ModuleWriter {
int oldInset = inset; int oldInset = inset;
inset = 1; inset = 1;
newline( output ); newline( output );
output.append( "(type $" ).append( normalizeName( typeName ) ).append( " (struct" ); typeName = normalizeName( typeName );
output.append( "(type $" ).append( typeName ).append( " (struct" );
inset++; inset++;
for( NamedStorageType field : fields ) { for( NamedStorageType field : fields ) {
newline( output ); newline( output );
output.append( "(field" ); output.append( "(field" );
if( debugNames && field.getUniqueName() != null ) { if( debugNames && field.getName() != null ) {
output.append( " $" ).append( normalizeName( field.getUniqueName() ) ); output.append( " $" ).append( typeName ).append( '.' ).append( field.getName() );
} }
output.append( " (mut " ); output.append( " (mut " );
AnyType type = field.getType(); AnyType type = field.getType();
@ -545,7 +546,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName ) throws IOException { protected void writeStructOperator( StructOperator op, AnyType type, NamedStorageType fieldName, int idx ) throws IOException {
String operation; String operation;
switch( op ) { switch( op ) {
case NEW: case NEW:
@ -571,7 +572,7 @@ public class TextModuleWriter extends ModuleWriter {
methodOutput.append( ' ' ).append( normalizeName( type.toString() ) ); methodOutput.append( ' ' ).append( normalizeName( type.toString() ) );
} }
if( fieldName != null ) { if( fieldName != null ) {
methodOutput.append( " $" ).append( normalizeName( fieldName.getUniqueName() ) ); methodOutput.append( ' ' ).append( idx ).append( " ;; $" ).append( normalizeName( fieldName.getName() ) );
} }
} }
} }

View File

@ -27,6 +27,8 @@ public class NamedStorageType {
private final AnyType type; private final AnyType type;
private final String className;
private final String name; private final String name;
/** /**
@ -38,7 +40,7 @@ public class NamedStorageType {
* the FieldInfo * the FieldInfo
*/ */
public NamedStorageType( String className, FieldInfo field ) { public NamedStorageType( String className, FieldInfo field ) {
this( field.getType(), className + '.' + field.getName() ); this( field.getType(), className, field.getName() );
} }
/** /**
@ -48,7 +50,7 @@ public class NamedStorageType {
* the reference * the reference
*/ */
public NamedStorageType( ConstantRef ref ) { public NamedStorageType( ConstantRef ref ) {
this( ref.getType(), ref.getClassName() + '.' + ref.getName() ); this( ref.getType(), ref.getClassName(), ref.getName() );
} }
/** /**
@ -56,11 +58,14 @@ public class NamedStorageType {
* *
* @param type * @param type
* the type * the type
* @param className
* the class name
* @param name * @param name
* the name * the name
*/ */
private NamedStorageType( String type, String name ) { private NamedStorageType( String type, String className, String name ) {
this.type = new ValueTypeParser( type ).next(); this.type = new ValueTypeParser( type ).next();
this.className = className;
this.name = name; this.name = name;
} }
@ -79,7 +84,7 @@ public class NamedStorageType {
* *
* @return the name * @return the name
*/ */
public String getUniqueName() { public String getName() {
return name; return name;
} }
@ -106,6 +111,6 @@ public class NamedStorageType {
return false; return false;
} }
NamedStorageType other = (NamedStorageType)obj; NamedStorageType other = (NamedStorageType)obj;
return name.equals( other.name ); return name.equals( other.name ) && className.equals( other.className );
} }
} }