From b08a9869452806dc48f8eb47685ff24e350d20db Mon Sep 17 00:00:00 2001 From: Volker Berlin Date: Wed, 24 Apr 2019 21:41:33 +0200 Subject: [PATCH] Write the "this" parameter type of a method as ref type instead of anyref. --- .../binary/BinaryModuleWriter.java | 5 +---- .../jwebassembly/binary/FunctionTypeEntry.java | 4 ++-- .../jwebassembly/binary/StructTypeEntry.java | 5 +---- .../jwebassembly/binary/WasmOutputStream.java | 15 +++++++++++++++ .../jwebassembly/module/ModuleGenerator.java | 18 +++++++++++------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 7dfad49..df15c7e 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -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 ); diff --git a/src/de/inetsoftware/jwebassembly/binary/FunctionTypeEntry.java b/src/de/inetsoftware/jwebassembly/binary/FunctionTypeEntry.java index 68b5189..704b2e7 100644 --- a/src/de/inetsoftware/jwebassembly/binary/FunctionTypeEntry.java +++ b/src/de/inetsoftware/jwebassembly/binary/FunctionTypeEntry.java @@ -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 ); } } diff --git a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java index f55123f..c2c482b 100644 --- a/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java +++ b/src/de/inetsoftware/jwebassembly/binary/StructTypeEntry.java @@ -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() ); } } diff --git a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java index 237a275..39f5817 100644 --- a/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java +++ b/src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java @@ -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) * diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index eac08bb..4e87705 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -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 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 parser = name.getSignature(); AnyType type;