diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 9b979e8..6b2fba0 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -96,6 +96,10 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod private FunctionName startFunction; + private StructType stringType; + + private StructType classType; + /** * Create new instance. * @@ -195,14 +199,14 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod // string constants table if( count >= 2 ) { - stream.writeValueType( ValueType.externref ); // the type of elements + stream.writeValueType( stringType != null ? stringType : ValueType.externref ); // the type of elements stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available stream.writeVaruint32( stringCount ); // initial length } // table with classes if( count >= 3 ) { - stream.writeValueType( ValueType.externref ); // the type of elements + stream.writeValueType( classType != null ? classType : ValueType.externref ); // the type of elements stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available stream.writeVaruint32( typeCount ); // initial length } @@ -506,6 +510,15 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod return ValueType.externref.getCode(); } + switch( type.getName() ) { + case "java/lang/String": + stringType = type; + break; + case "java/lang/Class": + classType = type; + break; + } + int typeId = functionTypes.size(); List fields = type.getFields(); functionTypes.add( type.getKind() == StructTypeKind.array_native ? new ArrayTypeEntry( fields ) : new StructTypeEntry( fields ) ); diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index d3fccd9..95b1485 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -86,6 +86,10 @@ public class TextModuleWriter extends ModuleWriter { private boolean callIndirect; + private boolean useTypeString; + + private boolean useTypeClass; + /** * Create a new instance. * @@ -154,7 +158,7 @@ public class TextModuleWriter extends ModuleWriter { textOutput.append( "(table $functions 0 funcref)" ); } newline( textOutput ); - String tableTypeName = options.useGC() && types.contains( "java/lang/String" ) ? "$java/lang/String" : "externref"; + String tableTypeName = options.useGC() && useTypeString ? "(ref null $java/lang/String)" : "externref"; textOutput.append( "(table $strings " ).append( Integer.toString( stringCount ) ).append( ' ' ).append( tableTypeName ).append( ')' ); } @@ -162,7 +166,7 @@ public class TextModuleWriter extends ModuleWriter { int typeCount = options.types.size(); if( typeCount > 0 ) { newline( textOutput ); - String tableTypeName = options.useGC() && types.contains( "java/lang/Class" ) ? "$java/lang/Class" : "externref"; + String tableTypeName = options.useGC() && useTypeClass ? "(ref null $java/lang/Class)" : "externref"; textOutput.append( "(table $classes " ).append( Integer.toString( typeCount ) ).append( ' ' ).append( tableTypeName ).append( ')' ); } @@ -209,6 +213,14 @@ public class TextModuleWriter extends ModuleWriter { inset = 1; newline( output ); String typeName = normalizeName( type.getName() ); + switch( typeName ) { + case "java/lang/String": + useTypeString = true; + break; + case "java/lang/Class": + useTypeClass = true; + break; + } String kind = type.getKind() == StructTypeKind.array_native ? "array" : "struct"; output.append( "(type $" ).append( typeName ).append( " (" ).append( kind ); inset++;