set initial values of non GC struct values

This commit is contained in:
Volker Berlin 2019-09-15 15:42:50 +02:00
parent f19423d53a
commit 53319942ae
3 changed files with 26 additions and 3 deletions

View File

@ -22,7 +22,6 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -500,6 +499,9 @@ public class ModuleGenerator {
((WasmCallInstruction)instruction).markAsNeeded( functions, false );
break;
case Struct:
if( !writer.options.useGC() ) {
break;
}
WasmStructInstruction instr = (WasmStructInstruction)instruction;
if( instr.getOperator() == StructOperator.NEW_DEFAULT ) {
StructType structType = instr.getStructType();

View File

@ -82,7 +82,28 @@ class WasmStructInstruction extends WasmInstruction {
case NEW:
case NEW_DEFAULT:
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "new_" + type.getName().replace( '/', '_' ), () -> {
return "() => {return {0:0}}"; //TODO default values of fields and then use Object.seal()
// create the default values of a new type
StringBuilder js = new StringBuilder("() => Object.seal({");
List<NamedStorageType> list = type.getFields();
for( int i = 0; i < list.size(); i++ ) {
if( i > 0 ) {
js.append( ',' );
}
js.append( i ).append( ':' );
NamedStorageType storageType = list.get( i );
if( TypeManager.VTABLE == storageType.getName() ) {
js.append( type.getVTable() );
} else {
AnyType fieldType = storageType.getType();
if( fieldType instanceof ValueType && fieldType != ValueType.anyref ) {
js.append( '0' );
} else {
js.append( "null" );
}
}
}
js.append( "})" );
return js.toString();
}, null, type );
break;
case SET:

View File

@ -50,7 +50,7 @@ public class StructsNonGC extends AbstractBaseTest {
addParam( list, script, "isNotSame" );
addParam( list, script, "simple" );
addParam( list, script, "callSuperMethod" );
//TODO addParam( list, script, "callVirtualMethod" );
addParam( list, script, "callVirtualMethod" );
addParam( list, script, "useGlobalObject" );
addParam( list, script, "multipleAssign" );
}