diff --git a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java index edaa3cc..245aa06 100644 --- a/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java +++ b/src/de/inetsoftware/jwebassembly/module/ModuleGenerator.java @@ -42,6 +42,7 @@ import de.inetsoftware.jwebassembly.WasmException; import de.inetsoftware.jwebassembly.module.TypeManager.StructType; import de.inetsoftware.jwebassembly.wasm.AnyType; import de.inetsoftware.jwebassembly.wasm.NamedStorageType; +import de.inetsoftware.jwebassembly.wasm.StructOperator; import de.inetsoftware.jwebassembly.wasm.ValueType; import de.inetsoftware.jwebassembly.wasm.ValueTypeParser; import de.inetsoftware.jwebassembly.watparser.WatParser; @@ -239,7 +240,7 @@ public class ModuleGenerator { list.add( new NamedStorageType( fieldtype, field.getName() ) ); } int id = writer.writeStruct( className, list ); - types.useType( type, id ); + types.useType( type, id, list ); for( NamedStorageType namedType : list ) { if( namedType.type.getCode() == Integer.MAX_VALUE ) { writeStructType( (StructType)namedType.type ); @@ -365,6 +366,13 @@ public class ModuleGenerator { int lastJavaSourceLine = -1; for( WasmInstruction instruction : instructions ) { try { + // add source-map information + int javaSourceLine = instruction.getLineNumber(); + if( javaSourceLine >= 0 && javaSourceLine != lastJavaSourceLine ) { + writer.markSourceLine( javaSourceLine ); + lastJavaSourceLine = javaSourceLine; + } + switch( instruction.getType() ) { case Block: switch( ((WasmBlockInstruction)instruction).getOperation() ) { @@ -379,15 +387,36 @@ public class ModuleGenerator { functions.functionCall( ((WasmCallInstruction)instruction).getFunctionName() ); break; case Struct: - setStructType( (WasmStructInstruction)instruction ); + WasmStructInstruction instr = (WasmStructInstruction)instruction; + setStructType( instr ); + if( instr.getOperator() == StructOperator.NEW_DEFAULT ) { + List list = instr.getStructType().getFields(); + for( NamedStorageType storageType : list ) { + if( storageType.type.getCode() < 0 ) { + ValueType type = (ValueType)storageType.type; + switch( type ) { + case i32: + case i64: + case f32: + case f64: + writer.writeConst( 0, type ); + break; + case i8: + case i16: + writer.writeConst( 0, ValueType.i32 ); + break; + default: + throw new WasmException( "Not supported storage type: " + type, instruction.getLineNumber() ); + } + } else { + writer.writeStructOperator( StructOperator.NULL, null, null ); + } + } + } break; default: } - int javaSourceLine = instruction.getLineNumber(); - if( javaSourceLine >= 0 && javaSourceLine != lastJavaSourceLine ) { - writer.markSourceLine( javaSourceLine ); - lastJavaSourceLine = javaSourceLine; - } + instruction.writeTo( writer ); } catch( Throwable th ) { throw WasmException.create( th, instruction.getLineNumber() ); diff --git a/src/de/inetsoftware/jwebassembly/module/TypeManager.java b/src/de/inetsoftware/jwebassembly/module/TypeManager.java index 1acf370..f26a63d 100644 --- a/src/de/inetsoftware/jwebassembly/module/TypeManager.java +++ b/src/de/inetsoftware/jwebassembly/module/TypeManager.java @@ -18,11 +18,13 @@ package de.inetsoftware.jwebassembly.module; import java.util.Collection; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import javax.annotation.Nonnull; import de.inetsoftware.jwebassembly.wasm.AnyType; +import de.inetsoftware.jwebassembly.wasm.NamedStorageType; /** * Manage the written and to write types (classes) @@ -40,9 +42,12 @@ public class TypeManager { * the reference to a type * @param id * the id in the type section of the wasm + * @param fields + * the fields of the type */ - void useType( StructType type, int id ) { + void useType( StructType type, int id, List fields ) { type.code = id; + type.fields = fields; } /** @@ -78,9 +83,11 @@ public class TypeManager { */ static class StructType implements AnyType { - private final String name; + private final String name; - private int code = Integer.MAX_VALUE; + private int code = Integer.MAX_VALUE; + + private List fields; /** * Create a reference to type @@ -108,6 +115,14 @@ public class TypeManager { return name; } + /** + * Get the fields of this struct + * @return the fields + */ + public List getFields() { + return fields; + } + /** * {@inheritDoc} */ diff --git a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java index 8439c50..3bff572 100644 --- a/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java +++ b/src/de/inetsoftware/jwebassembly/module/WasmStructInstruction.java @@ -62,6 +62,15 @@ class WasmStructInstruction extends WasmInstruction { this.fieldName = fieldName; } + /** + * Get the StructOperator + * + * @return the operator + */ + StructOperator getOperator() { + return op; + } + /** * Get the struct type of this instruction. * diff --git a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java index d8c3c78..bbd7d82 100644 --- a/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java +++ b/src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java @@ -549,10 +549,8 @@ public class TextModuleWriter extends ModuleWriter { String operation; switch( op ) { case NEW: - operation = "struct.new"; - break; case NEW_DEFAULT: - operation = "struct.new_default"; + operation = "struct.new"; break; case GET: operation = "struct.get";