diff --git a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java index 38e8a19..bb71820 100644 --- a/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java @@ -33,6 +33,7 @@ import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; +import de.inetsoftware.jwebassembly.module.TypeManager.StructTypeKind; import de.inetsoftware.jwebassembly.module.ValueTypeConvertion; import de.inetsoftware.jwebassembly.module.WasmOptions; import de.inetsoftware.jwebassembly.module.WasmTarget; @@ -497,6 +498,10 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod protected int writeStructType( StructType type ) throws IOException { type.writeToStream( dataStream, (funcName) -> getFunction( funcName ).id, options ); + if( type.getKind() == StructTypeKind.primitive ) { + return -9; // Should never use + } + if( !options.useGC() ) { return ValueType.externref.getCode(); } diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index e55e960..77a388b 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -148,6 +148,8 @@ public class TypeManager { private Map structTypes = new LinkedHashMap<>(); + private int typeIndexCounter; + private boolean isFinish; final WasmOptions options; @@ -260,7 +262,7 @@ public class TypeManager { if( structTypes.size() == 0 ) { for( String primitiveTypeName : PRIMITIVE_CLASSES ) { - structTypes.put( primitiveTypeName, new StructType( primitiveTypeName, structTypes.size() ) ); + structTypes.put( primitiveTypeName, new StructType( primitiveTypeName, StructTypeKind.primitive, this ) ); } } } @@ -279,7 +281,12 @@ public class TypeManager { return (StructType)new ValueTypeParser( name, options.types ).next(); } else { checkStructTypesState( name ); - type = new StructType( name, structTypes.size() ); + type = new StructType( name, StructTypeKind.normal, this ); + + if( "java/lang/String".equals( name ) ) { + // looks like strings are used, that register helper functions + options.strings.getStringConstantFunction(); + } } structTypes.put( name, type ); @@ -337,7 +344,7 @@ public class TypeManager { componentClassIndex = ((StructType)arrayType).classIndex; } - type = new ArrayType( arrayType, structTypes.size(), componentClassIndex ); + type = new ArrayType( arrayType, this, componentClassIndex ); structTypes.put( arrayType, type ); } return type; @@ -486,6 +493,15 @@ public class TypeManager { , valueOf( "java/lang/Object" ), ValueType.i32, null, valueOf( "java/lang/Object" ) ); } + /** + * The kind of type + * + * @author Volker Berlin + */ + public static enum StructTypeKind { + primitive, normal, array, array_native; + } + /** * A reference to a type. * @@ -495,6 +511,8 @@ public class TypeManager { private final String name; + private final StructTypeKind kind; + private final int classIndex; private int code = Integer.MAX_VALUE; @@ -519,12 +537,21 @@ public class TypeManager { * * @param name * the Java class name - * @param classIndex - * the running index of the class/type + * @param kind + * the type kind + * @param manager + * the manager which hold all StructTypes */ - protected StructType( String name, int classIndex ) { + protected StructType( @Nonnull String name, @Nonnull StructTypeKind kind, @Nonnull TypeManager manager ) { this.name = name; - this.classIndex = classIndex; + this.kind = kind; + switch( kind ) { + case array_native: + this.classIndex = -1; + break; + default: + this.classIndex = manager.typeIndexCounter++; + } } /** @@ -850,6 +877,14 @@ public class TypeManager { return type == this || type == ValueType.externref || type == ValueType.anyref || type == ValueType.eqref; } + /** + * Get kind of the StructType + * @return the type kind + */ + public StructTypeKind getKind() { + return kind; + } + /** * Get the name of the Java type * @return the name diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index 477df19..c7afbf7 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -32,6 +32,7 @@ import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.module.FunctionName; import de.inetsoftware.jwebassembly.module.ModuleWriter; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; +import de.inetsoftware.jwebassembly.module.TypeManager.StructTypeKind; import de.inetsoftware.jwebassembly.module.ValueTypeConvertion; import de.inetsoftware.jwebassembly.module.WasmOptions; import de.inetsoftware.jwebassembly.module.WasmTarget; @@ -194,6 +195,10 @@ public class TextModuleWriter extends ModuleWriter { protected int writeStructType( StructType type ) throws IOException { type.writeToStream( dataStream, (funcName) -> getFunction( funcName ).id, options ); + if( type.getKind() == StructTypeKind.primitive ) { + return -9; // Should never use + } + if( !options.useGC() ) { return ValueType.externref.getCode(); } diff --git a/src/de/inetsoftware/jwebassembly/wasm/ArrayType.java b/src/de/inetsoftware/jwebassembly/wasm/ArrayType.java index cdd6ef9..02af1d4 100644 --- a/src/de/inetsoftware/jwebassembly/wasm/ArrayType.java +++ b/src/de/inetsoftware/jwebassembly/wasm/ArrayType.java @@ -15,8 +15,12 @@ */ package de.inetsoftware.jwebassembly.wasm; +import javax.annotation.Nonnull; + import de.inetsoftware.jwebassembly.WasmException; +import de.inetsoftware.jwebassembly.module.TypeManager; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; +import de.inetsoftware.jwebassembly.module.TypeManager.StructTypeKind; /** * A reference to an array type @@ -35,13 +39,13 @@ public class ArrayType extends StructType { * * @param arrayType * the type of the array - * @param classIndex - * the running index of the main class/type + * @param manager + * the manager which hold all StructTypes * @param componentClassIndex * the running index of the component/array class/type */ - public ArrayType( AnyType arrayType, int classIndex, int componentClassIndex ) { - super( getJavaClassName( arrayType ), classIndex ); + public ArrayType( AnyType arrayType, @Nonnull TypeManager manager, int componentClassIndex ) { + super( getJavaClassName( arrayType ), StructTypeKind.array, manager ); this.arrayType = arrayType; this.componentClassIndex = componentClassIndex; }