improve array handling with GC

This commit is contained in:
Volker Berlin 2020-08-09 19:19:58 +02:00
parent 28b03b05c2
commit be56c02b15
5 changed files with 18 additions and 20 deletions

View File

@ -40,6 +40,7 @@ import de.inetsoftware.jwebassembly.sourcemap.SourceMapWriter;
import de.inetsoftware.jwebassembly.sourcemap.SourceMapping;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.ArrayType;
import de.inetsoftware.jwebassembly.wasm.FunctionType;
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
@ -1353,7 +1354,7 @@ public class BinaryModuleWriter extends ModuleWriter implements InstructionOpcod
* {@inheritDoc}
*/
@Override
protected void writeArrayOperator( @Nonnull ArrayOperator op, AnyType type ) throws IOException {
protected void writeArrayOperator( @Nonnull ArrayOperator op, ArrayType type ) throws IOException {
int opCode;
switch(op) {
case NEW:

View File

@ -26,6 +26,7 @@ import javax.annotation.Nullable;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.ArrayType;
import de.inetsoftware.jwebassembly.wasm.FunctionType;
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
@ -304,11 +305,11 @@ public abstract class ModuleWriter implements Closeable {
* @param op
* the operation
* @param type
* the resulting type
* the type of the array
* @throws IOException
* if any I/O error occur
*/
protected abstract void writeArrayOperator( @Nonnull ArrayOperator op, AnyType type ) throws IOException;
protected abstract void writeArrayOperator( @Nonnull ArrayOperator op, ArrayType type ) throws IOException;
/**
* Write a struct operation

View File

@ -36,11 +36,13 @@ import de.inetsoftware.jwebassembly.wasm.ValueType;
*/
class WasmArrayInstruction extends WasmInstruction {
private final ArrayOperator op;
private final ArrayOperator op;
private final AnyType type;
private final AnyType type;
private final TypeManager types;
private final ArrayType arrayType;
private final TypeManager types;
private SyntheticFunctionName functionName;
@ -51,6 +53,8 @@ class WasmArrayInstruction extends WasmInstruction {
* the array operation
* @param type
* the type of the parameters
* @param types
* the type manager
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
@ -61,6 +65,7 @@ class WasmArrayInstruction extends WasmInstruction {
this.op = op;
this.type = type;
this.types = types;
this.arrayType = types.arrayType( type );
}
/**
@ -100,7 +105,6 @@ class WasmArrayInstruction extends WasmInstruction {
cmd = "Object.seal(new Array(l).fill(null))";
}
}
ArrayType arrayType = types.arrayType( type );
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "array_new_" + validJsName( type ), () -> {
// create the default values of a new type
return new StringBuilder( "(l)=>Object.seal({0:" ) // fix count of elements
@ -150,7 +154,7 @@ class WasmArrayInstruction extends WasmInstruction {
if( functionName != null ) { // nonGC
writer.writeFunctionCall( functionName, null );
} else {
writer.writeArrayOperator( op, type );
writer.writeArrayOperator( op, arrayType );
}
}
@ -160,7 +164,7 @@ class WasmArrayInstruction extends WasmInstruction {
AnyType getPushValueType() {
switch( op ) {
case NEW:
return types.arrayType( type );
return arrayType;
case GET:
return type instanceof ValueType ? (ValueType)type : ValueType.externref;
case SET:

View File

@ -37,6 +37,7 @@ import de.inetsoftware.jwebassembly.module.WasmOptions;
import de.inetsoftware.jwebassembly.module.WasmTarget;
import de.inetsoftware.jwebassembly.wasm.AnyType;
import de.inetsoftware.jwebassembly.wasm.ArrayOperator;
import de.inetsoftware.jwebassembly.wasm.ArrayType;
import de.inetsoftware.jwebassembly.wasm.FunctionType;
import de.inetsoftware.jwebassembly.wasm.MemoryOperator;
import de.inetsoftware.jwebassembly.wasm.NamedStorageType;
@ -820,7 +821,7 @@ public class TextModuleWriter extends ModuleWriter {
* {@inheritDoc}
*/
@Override
protected void writeArrayOperator( @Nonnull ArrayOperator op, AnyType type ) throws IOException {
protected void writeArrayOperator( @Nonnull ArrayOperator op, ArrayType type ) throws IOException {
String operation;
switch( op ) {
case NEW:
@ -839,7 +840,7 @@ public class TextModuleWriter extends ModuleWriter {
throw new Error( "Unknown operator: " + op );
}
newline( methodOutput );
methodOutput.append( "array." ).append( operation ).append( ' ' ).append( type );
methodOutput.append( "array." ).append( operation ).append( ' ' ).append( normalizeName( type.toString() ) );
}
/**

View File

@ -121,13 +121,4 @@ public class ArrayType extends StructType {
public boolean isSubTypeOf( AnyType type ) {
return type == this || type == ValueType.externref;
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
// until there is a real type definition we will define write it as externref
return ValueType.externref.toString();
}
}