Skip to content

Commit

Permalink
anyref --> externref, add kind to ref.null and ref.is_null, WebAssemb…
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed May 30, 2020
1 parent 56c38bb commit cfbadfa
Show file tree
Hide file tree
Showing 23 changed files with 69 additions and 65 deletions.
12 changes: 6 additions & 6 deletions src/de/inetsoftware/jwebassembly/binary/BinaryModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ private void writeTableSection() throws IOException {

// string constants table
if( count >= 2 ) {
stream.writeValueType( ValueType.anyref ); // the type of elements
stream.writeValueType( ValueType.externref ); // the type of elements
stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available
stream.writeVaruint32( stringCount ); // initial length
}

// table with classes
if( count >= 3 ) {
stream.writeValueType( ValueType.anyref ); // the type of elements
stream.writeValueType( ValueType.externref ); // the type of elements
stream.writeVaruint32( 0 ); // flags; 1-maximum is available, 0-no maximum value available
stream.writeVaruint32( typeCount ); // initial length
}
Expand Down Expand Up @@ -497,7 +497,7 @@ protected int writeStructType( StructType type ) throws IOException {
type.writeToStream( dataStream, (funcName) -> getFunction( funcName ).id );

if( !options.useGC() ) {
return ValueType.anyref.getCode();
return ValueType.externref.getCode();
}

int typeId = functionTypes.size();
Expand All @@ -512,7 +512,7 @@ protected int writeStructType( StructType type ) throws IOException {
protected void writeException() throws IOException {
if( exceptionSignatureIndex <= 0 ) {
FunctionTypeEntry type = new FunctionTypeEntry();
type.params.add( ValueType.anyref );
type.params.add( ValueType.externref );
exceptionSignatureIndex = functionTypes.indexOf( type );
if( exceptionSignatureIndex < 0 ) {
exceptionSignatureIndex = functionTypes.size();
Expand All @@ -522,7 +522,7 @@ protected void writeException() throws IOException {
// result type of catch block for unboxing
type = new FunctionTypeEntry();
type.params.add( ValueType.exnref );
type.results.add( ValueType.anyref );
type.results.add( ValueType.externref );
options.setCatchType( functionTypes.size() );
functionTypes.add( type );
}
Expand Down Expand Up @@ -1394,7 +1394,7 @@ protected void writeStructOperator( StructOperator op, AnyType type, NamedStorag
break;
case NULL:
opCode = REF_NULL;
type = null;
type = ValueType.externref;
break;
default:
throw new Error( "Unknown operator: " + op );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ interface InstructionOpcodes {

static final int REF_NULL = 0xD0;

static final int REF_ISNULL = 0xD1;
static final int REF_ISNULL = 0xD16F; // "ref.is_null extern"

/** converts a nullable reference to a non-nullable one or traps if null */
static final int REF_AS_NON_NULL = 0xD3;
Expand Down
6 changes: 4 additions & 2 deletions src/de/inetsoftware/jwebassembly/binary/WasmOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void writeRefValueType( AnyType type ) throws IOException {
if( options.useGC() ) {
writeValueType( ValueType.ref_type );
} else {
type = ValueType.anyref;
type = ValueType.externref;
}
}
writeValueType( type );
Expand Down Expand Up @@ -128,14 +128,16 @@ public void writeDefaultValue( AnyType type ) throws IOException {
case i16:
writeConst( 0, ValueType.i32 );
break;
case anyref:
case externref:
writeOpCode( InstructionOpcodes.REF_NULL );
writeValueType( ValueType.externref );
break;
default:
throw new WasmException( "Not supported storage type: " + type, -1 );
}
} else {
writeOpCode( InstructionOpcodes.REF_NULL );
writeValueType( ValueType.externref );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static ValueType getElementType( ArrayType type ) {
type = (ArrayType)arrayType;
continue;
}
return arrayType.getClass() == ValueType.class ? (ValueType)arrayType : ValueType.anyref;
return arrayType.getClass() == ValueType.class ? (ValueType)arrayType : ValueType.externref;
} while( true );
}

Expand Down
8 changes: 4 additions & 4 deletions src/de/inetsoftware/jwebassembly/javascript/NonGC.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class NonGC {
native static double[] array_new_f64( int length );

@Import( js = "(l) => Object.seal(new Array(l).fill(null))" )
native static Object[] array_new_anyref( int length );
native static Object[] array_new_externref( int length );

@Import( js = "(a) => a.length" )
native static int array_len_i8( Object array );
Expand All @@ -65,7 +65,7 @@ public abstract class NonGC {
native static int array_len_f64( Object array );

@Import( js = "(a) => a.length" )
native static int array_len_anyref( Object array );
native static int array_len_externref( Object array );

@Import( js = "(a,i,v) => a[i]=v" )
native static void array_set_i8( byte[] array, int idx, byte value );
Expand All @@ -86,7 +86,7 @@ public abstract class NonGC {
native static void array_set_f64( double[] array, int idx, double value );

@Import( js = "(a,i,v) => a[i]=v" )
native static void array_set_anyref( Object[] array, int idx, Object value );
native static void array_set_externref( Object[] array, int idx, Object value );

@Import( js = "(a,i) => a[i]" )
native static byte array_get_i8( byte[] array, int idx );
Expand All @@ -107,7 +107,7 @@ public abstract class NonGC {
native static double array_get_f64( double[] array, int idx );

@Import( js = "(a,i) => a[i]" )
native static Object array_get_anyref( Object[] array, int idx );
native static Object array_get_externref( Object[] array, int idx );

@Import( js = "(a,b) => a === b" )
native static int ref_eq( Object a, Object b );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
addLoadStoreInstruction( ValueType.f64, true, byteCode.readUnsignedIndex( wide ), codePos, lineNumber );
break;
case 25: // aload
addLoadStoreInstruction( ValueType.anyref, true, byteCode.readUnsignedIndex( wide ), codePos, lineNumber );
addLoadStoreInstruction( ValueType.externref, true, byteCode.readUnsignedIndex( wide ), codePos, lineNumber );
break;
case 26: // iload_0
case 27: // iload_1
Expand Down Expand Up @@ -188,7 +188,7 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
case 43: //aload_1
case 44: //aload_2
case 45: //aload_3
addLoadStoreInstruction( ValueType.anyref, true, op - 42, codePos, lineNumber );
addLoadStoreInstruction( ValueType.externref, true, op - 42, codePos, lineNumber );
break;
case 46: // iaload
addArrayInstruction( ArrayOperator.GET, ValueType.i32, codePos, lineNumber );
Expand Down Expand Up @@ -229,8 +229,8 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
break;
case 58: // astore
if( branchManager.isCatch( codePos ) ) {
addJumpPlaceholder( codePos, 0, ValueType.anyref, codePos, lineNumber );
storeType = ValueType.anyref; // for the catch there are no previous instructions
addJumpPlaceholder( codePos, 0, ValueType.externref, codePos, lineNumber );
storeType = ValueType.externref; // for the catch there are no previous instructions
} else {
storeType = findValueTypeFromStack( 1, codePos );
}
Expand Down Expand Up @@ -265,8 +265,8 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
case 77: // astore_2
case 78: // astore_3
if( branchManager.isCatch( codePos ) ) {
addJumpPlaceholder( codePos, 0, ValueType.anyref, codePos, lineNumber );
storeType = ValueType.anyref; // for the catch there are no previous instructions
addJumpPlaceholder( codePos, 0, ValueType.externref, codePos, lineNumber );
storeType = ValueType.externref; // for the catch there are no previous instructions
} else {
storeType = findValueTypeFromStack( 1, codePos );
}
Expand Down Expand Up @@ -580,7 +580,7 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
type = ValueType.f64;
break;
case 176: // areturn
type = ValueType.anyref;
type = ValueType.externref;
break;
}
addBlockInstruction( WasmBlockOperator.RETURN, type, codePos, lineNumber );
Expand Down Expand Up @@ -671,7 +671,7 @@ private void writeCode( CodeInputStream byteCode, ConstantPool constantPool, Cla
break;
case 189: // anewarray
name = ((ConstantClass)constantPool.get( byteCode.readUnsignedShort() )).getName();
type = ValueType.anyref; //TODO we need to use the right type from name; getTypeManager().valueOf( name );
type = ValueType.externref; //TODO we need to use the right type from name; getTypeManager().valueOf( name );
addArrayInstruction( ArrayOperator.NEW, type, codePos, lineNumber );
break;
case 190: // arraylength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void reset( LocalVariableTable variableTable, MethodInfo method, Iterator<AnyTyp
if( (maxLocals > 0 || variableTable == null) && size == 0 && (method != null || signature != null )) {
Iterator<AnyType> parser = signature == null ? new ValueTypeParser( method.getType(), types ) : signature;
if( method != null && !method.isStatic() ) {
resetAddVar( ValueType.anyref, size );
resetAddVar( ValueType.externref, size );
}
while( true ) {
AnyType type = parser.next();
Expand Down
2 changes: 1 addition & 1 deletion src/de/inetsoftware/jwebassembly/module/TypeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ public boolean isRefType() {
@Override
public boolean isSubTypeOf( AnyType type ) {
//TODO if type is StructType (class or interface)
return type == this || type == ValueType.anyref;
return type == this || type == ValueType.externref;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ AnyType getPushValueType() {
case NEW:
return types.arrayType( type );
case GET:
return type instanceof ValueType ? (ValueType)type : ValueType.anyref;
return type instanceof ValueType ? (ValueType)type : ValueType.externref;
case SET:
return null;
case LEN:
Expand All @@ -98,9 +98,9 @@ AnyType getPushValueType() {
@Override
int getPopCount() {
switch( op ) {
case NEW:
case GET:
return 2;
case NEW:
case LEN:
return 1;
case SET:
Expand Down
4 changes: 2 additions & 2 deletions src/de/inetsoftware/jwebassembly/module/WasmCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,8 @@ protected void addArrayInstruction( ArrayOperator op, AnyType type, int javaCode
if( options.useGC() ) {
instructions.add( new WasmArrayInstruction( op, type, types, javaCodePos, lineNumber ) );
} else {
if( type.getCode() >= 0 || type.getCode() == ValueType.anyref.getCode() ) {
type = ValueType.anyref; // handle all not native types as anyref
if( type.getCode() >= 0 || type.getCode() == ValueType.externref.getCode() ) {
type = ValueType.externref; // handle all not native types as anyref
}
String api = "array_" + op.toString().toLowerCase() + "_" + type;
FunctionName name = getNonGC( api, lineNumber );
Expand Down
2 changes: 1 addition & 1 deletion src/de/inetsoftware/jwebassembly/module/WasmOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ void registerGet_i32() {
}
if( get_i32 == null ) {
SyntheticFunctionName name;
get_i32 = name = new JavaScriptSyntheticFunctionName( "NonGC", "get_i32", () -> "(a,i) => a[i]", ValueType.anyref, ValueType.i32, null, ValueType.i32 );
get_i32 = name = new JavaScriptSyntheticFunctionName( "NonGC", "get_i32", () -> "(a,i) => a[i]", ValueType.externref, ValueType.i32, null, ValueType.i32 );
functions.markAsNeeded( name );
functions.markAsImport( name, name.getAnnotation() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ SyntheticFunctionName createNonGcFunction() {
js.append( type.getVTable() );
} else {
AnyType fieldType = storageType.getType();
if( fieldType instanceof ValueType && fieldType != ValueType.anyref ) {
if( fieldType instanceof ValueType && fieldType != ValueType.externref ) {
js.append( '0' );
} else {
js.append( "null" );
Expand All @@ -108,11 +108,11 @@ SyntheticFunctionName createNonGcFunction() {
break;
case SET:
AnyType fieldType = fieldName.getType();
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "set_" + validJsName( fieldType ), () -> "(a,v,i) => a[i]=v", ValueType.anyref, fieldType, ValueType.i32, null, null );
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "set_" + validJsName( fieldType ), () -> "(a,v,i) => a[i]=v", ValueType.externref, fieldType, ValueType.i32, null, null );
break;
case GET:
fieldType = fieldName.getType();
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "get_" + validJsName( fieldType ), () -> "(a,i) => a[i]", ValueType.anyref, ValueType.i32, null, fieldType );
functionName = new JavaScriptSyntheticFunctionName( "NonGC", "get_" + validJsName( fieldType ), () -> "(a,i) => a[i]", ValueType.externref, ValueType.i32, null, fieldType );
break;
default:
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
AnyType getPushValueType() {
switch( op ) {
case NULL:
return ValueType.anyref;
return ValueType.externref;
case NEW:
case NEW_DEFAULT:
case CAST:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void writeTo( @Nonnull ModuleWriter writer ) throws IOException {
*/
@Override
AnyType getPushValueType() {
return load ? ValueType.anyref : null;
return load ? ValueType.externref : null;
}

/**
Expand Down
26 changes: 13 additions & 13 deletions src/de/inetsoftware/jwebassembly/text/TextModuleWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ public void close() throws IOException {
textOutput.append( "(table $functions 0 funcref)" );
}
newline( textOutput );
textOutput.append( "(table $strings " ).append( Integer.toString( stringCount ) ).append( " anyref)" );
textOutput.append( "(table $strings " ).append( Integer.toString( stringCount ) ).append( " externref)" );
}

// table with classes
int typeCount = options.types.size();
if( typeCount > 0 ) {
newline( textOutput );
textOutput.append( "(table $classes " ).append( Integer.toString( typeCount ) ).append( " anyref)" );
textOutput.append( "(table $classes " ).append( Integer.toString( typeCount ) ).append( " externref)" );
}

int dataSize = dataStream.size();
Expand Down Expand Up @@ -194,7 +194,7 @@ protected int writeStructType( StructType type ) throws IOException {
type.writeToStream( dataStream, (funcName) -> getFunction( funcName ).id );

if( !options.useGC() ) {
return ValueType.anyref.getCode();
return ValueType.externref.getCode();
}

int oldInset = inset;
Expand Down Expand Up @@ -230,7 +230,7 @@ protected void writeException() throws IOException {
int oldInset = inset;
inset = 1;
newline( output );
output.append( "(event (param anyref))" );
output.append( "(event (param externref))" );
inset = oldInset;

options.setCatchType( types.size() );
Expand Down Expand Up @@ -318,7 +318,7 @@ private void writeTypeName( Appendable output, AnyType type ) throws IOException
} else if( options.useGC() ) {
output.append( "(optref " ).append( normalizeName( type.toString() ) ).append( ')' );
} else {
output.append( ValueType.anyref.toString() );
output.append( ValueType.externref.toString() );
}
}

Expand Down Expand Up @@ -538,14 +538,14 @@ private static void writeDefaultValue( Appendable output, AnyType type ) throws
case i16:
writeDefaultValue( output, ValueType.i32 );
break;
case anyref:
output.append( "ref.null" );
case externref:
output.append( "ref.null extern" );
break;
default:
throw new WasmException( "Not supported storage type: " + type, -1 );
}
} else {
output.append( "ref.null" );
output.append( "ref.null extern" );
}
}

Expand All @@ -569,11 +569,11 @@ protected void writeNumericOperator( NumericOperator numOp, @Nullable ValueType
op += "_s";
break;
case ifnonnull:
op = "ref.is_null";
op = "ref.is_null extern";
negate = true;
break;
case ifnull:
op = "ref.is_null";
op = "ref.is_null extern";
break;
case ref_ne:
op = options.useGC() ? "ref.eq" : null;
Expand Down Expand Up @@ -766,13 +766,13 @@ protected void writeBlockCode( @Nonnull WasmBlockOperator op, @Nullable Object d
insetAfter++;
break;
case THROW:
name = options.useEH() ? "throw 0" : "unreachable"; // currently there is only one event/exception with anyref
name = options.useEH() ? "throw 0" : "unreachable"; // currently there is only one event/exception with externref
break;
case RETHROW:
name = "rethrow";
break;
case BR_ON_EXN:
name = options.useEH() ? "br_on_exn " + data + " 0" : "unreachable"; // br_on_exn, break depth, event; // currently there is only one event/exception with anyref
name = options.useEH() ? "br_on_exn " + data + " 0" : "unreachable"; // br_on_exn, break depth, event; // currently there is only one event/exception with externref
break;
case MONITOR_ENTER:
case MONITOR_EXIT:
Expand Down Expand Up @@ -858,7 +858,7 @@ protected void writeStructOperator( StructOperator op, AnyType type, NamedStorag
operation = "struct.set";
break;
case NULL:
operation = "ref.null";
operation = "ref.null extern";
type = null;
break;
default:
Expand Down
Loading

0 comments on commit cfbadfa

Please sign in to comment.