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}
*/
@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;
switch(op) {
case NEW:
@ -1043,8 +1043,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
codeStream.writeValueType( type );
}
if( fieldName != null ) {
StructTypeEntry entry = (StructTypeEntry)functionTypes.get( type.getCode() );
codeStream.writeVaruint32( entry.getFieldIdx( fieldName ) );
codeStream.writeVaruint32( idx );
}
}
}

View File

@ -18,9 +18,6 @@ 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;
@ -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}
*/

View File

@ -427,13 +427,13 @@ public class ModuleGenerator {
writer.writeConst( 0, ValueType.i32 );
break;
case anyref:
writer.writeStructOperator( StructOperator.NULL, null, null );
writer.writeStructOperator( StructOperator.NULL, null, null, -1 );
break;
default:
throw new WasmException( "Not supported storage type: " + type, instruction.getLineNumber() );
}
} 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
* @param type
* 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
* 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}
*/
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;
inset = 1;
newline( output );
output.append( "(type $" ).append( normalizeName( typeName ) ).append( " (struct" );
typeName = normalizeName( typeName );
output.append( "(type $" ).append( typeName ).append( " (struct" );
inset++;
for( NamedStorageType field : fields ) {
newline( output );
output.append( "(field" );
if( debugNames && field.getUniqueName() != null ) {
output.append( " $" ).append( normalizeName( field.getUniqueName() ) );
if( debugNames && field.getName() != null ) {
output.append( " $" ).append( typeName ).append( '.' ).append( field.getName() );
}
output.append( " (mut " );
AnyType type = field.getType();
@ -545,7 +546,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@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;
switch( op ) {
case NEW:
@ -571,7 +572,7 @@ public class TextModuleWriter extends ModuleWriter {
methodOutput.append( ' ' ).append( normalizeName( type.toString() ) );
}
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 String className;
private final String name;
/**
@ -38,7 +40,7 @@ public class NamedStorageType {
* the FieldInfo
*/
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
*/
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
* the type
* @param className
* the class name
* @param name
* the name
*/
private NamedStorageType( String type, String name ) {
private NamedStorageType( String type, String className, String name ) {
this.type = new ValueTypeParser( type ).next();
this.className = className;
this.name = name;
}
@ -79,7 +84,7 @@ public class NamedStorageType {
*
* @return the name
*/
public String getUniqueName() {
public String getName() {
return name;
}
@ -106,6 +111,6 @@ public class NamedStorageType {
return false;
}
NamedStorageType other = (NamedStorageType)obj;
return name.equals( other.name );
return name.equals( other.name ) && className.equals( other.className );
}
}