Write the "this" parameter type of a method as ref type instead of anyref.

This commit is contained in:
Volker Berlin 2019-04-24 21:41:33 +02:00
parent e8a4613d74
commit b08a986945
5 changed files with 30 additions and 17 deletions

View File

@ -467,10 +467,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
localsStream.writeVaruint32( locals.size() );
for( AnyType valueType : locals ) {
localsStream.writeVaruint32( 1 ); // TODO optimize, write the count of same types.
if( valueType.getCode() >= 0 ) {
localsStream.writeValueType( ValueType.ref_type );
}
localsStream.writeValueType( valueType );
localsStream.writeRefValueType( valueType );
}
WasmOutputStream functionsStream = function.functionsStream = new WasmOutputStream();
functionsStream.writeVaruint32( localsStream.size() + codeStream.size() + 1 );

View File

@ -48,11 +48,11 @@ class FunctionTypeEntry extends TypeEntry {
void writeSectionEntryDetails( WasmOutputStream stream ) throws IOException {
stream.writeVaruint32( this.params.size() );
for( AnyType valueType : this.params ) {
stream.writeValueType( valueType );
stream.writeRefValueType( valueType );
}
stream.writeVaruint32( this.results.size() );
for( AnyType valueType : this.results ) {
stream.writeValueType( valueType );
stream.writeRefValueType( valueType );
}
}

View File

@ -56,10 +56,7 @@ class StructTypeEntry extends TypeEntry {
stream.writeVaruint32( this.fields.size() );
for( NamedStorageType field : this.fields ) {
stream.writeVarint( 1 ); // 0 - immutable; 1 - mutable
if( field.getType().getCode() > 0 ) {
stream.writeValueType( ValueType.ref_type );
}
stream.writeValueType( field.getType() );
stream.writeRefValueType( field.getType() );
}
}

View File

@ -96,6 +96,21 @@ class WasmOutputStream extends FilterOutputStream {
writeVarint( type.getCode() );
}
/**
* Write the value type. If it is a struct type then as reference type.
*
* @param type
* a type constant
* @throws IOException
* if an I/O error occurs.
*/
public void writeRefValueType( AnyType type ) throws IOException {
if( type.getCode() >= 0 ) {
writeValueType( ValueType.ref_type );
}
writeValueType( type );
}
/**
* Write a integer little endian (ever 4 bytes)
*

View File

@ -210,9 +210,7 @@ public class ModuleGenerator {
*/
private void setStructType( WasmStructInstruction instruction ) throws IOException {
StructType type = instruction.getStructType();
if( type != null && type.getCode() == Integer.MAX_VALUE ) {
writeStructType( type );
}
writeStructType( type );
}
/**
@ -224,6 +222,9 @@ public class ModuleGenerator {
* if any I/O error occur
*/
private void writeStructType( StructType type ) throws IOException {
if( type == null || type.getCode() != Integer.MAX_VALUE ) {
return;
}
String className = type.getName();
List<NamedStorageType> list = new ArrayList<>();
listStructFields( className, list );
@ -231,14 +232,15 @@ public class ModuleGenerator {
int id = writer.writeStruct( className, list );
types.useType( type, id, list );
for( NamedStorageType namedType : list ) {
if( namedType.getType().getCode() == Integer.MAX_VALUE ) {
writeStructType( (StructType)namedType.getType() );
AnyType fieldType = namedType.getType();
if( fieldType.getCode() == Integer.MAX_VALUE ) {
writeStructType( (StructType)fieldType );
}
}
}
/**
* List the non static fields of the class and its superclasses.
* List the non static fields of the class and its super classes.
*
* @param className
* the className
@ -488,7 +490,9 @@ public class ModuleGenerator {
private void writeMethodSignature( FunctionName name, boolean isStatic, WasmCodeBuilder codeBuilder ) throws IOException, WasmException {
int paramCount = 0;
if( !isStatic ) {
writer.writeMethodParam( "param", ValueType.anyref, "this" );
StructType instanceType = types.valueOf( name.className );
writeStructType( instanceType );
writer.writeMethodParam( "param", instanceType, "this" );
}
Iterator<AnyType> parser = name.getSignature();
AnyType type;