Write the default/initial values before struct.new

This commit is contained in:
Volker Berlin 2019-04-20 21:41:46 +02:00
parent 09b817117a
commit 2be6f1a3be
4 changed files with 64 additions and 13 deletions

View File

@ -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<NamedStorageType> 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() );

View File

@ -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<NamedStorageType> 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<NamedStorageType> 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<NamedStorageType> getFields() {
return fields;
}
/**
* {@inheritDoc}
*/

View File

@ -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.
*

View File

@ -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";