implements StructType.isSubTypeOf(x) for Arrays and Lambdas

This commit is contained in:
Volker Berlin 2021-03-28 20:32:52 +02:00
parent 1f167d99b9
commit 6d4379b2e8
3 changed files with 35 additions and 1 deletions

View File

@ -1206,6 +1206,17 @@ public class TypeManager {
String getInterfaceMethodName() {
return interfaceMethodName;
}
/**
* {@inheritDoc}
*/
@Override
public boolean isSubTypeOf( AnyType type ) {
if( type == this || type == ValueType.externref || type == ValueType.anyref || type == ValueType.eqref ) {
return true;
}
return type == interfaceType;
}
}
/**

View File

@ -167,6 +167,19 @@ public class ArrayType extends StructType {
*/
@Override
public boolean isSubTypeOf( AnyType type ) {
return type == this || type == ValueType.externref;
if( type == this || type == ValueType.externref ) {
return true;
}
if( !(type instanceof StructType) ) {
return false;
}
StructType structType = (StructType)type;
switch( structType.getKind() ) {
case normal:
return "java/lang/Object".equals( structType.getName() );
case array:
return arrayType.isSubTypeOf( ((ArrayType)structType).arrayType );
}
return false;
}
}

View File

@ -19,12 +19,14 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashMap;
import org.junit.Before;
import org.junit.Test;
import de.inetsoftware.jwebassembly.JWebAssembly;
import de.inetsoftware.jwebassembly.module.TypeManager.LambdaType;
import de.inetsoftware.jwebassembly.module.TypeManager.StructType;
import de.inetsoftware.jwebassembly.module.TypeManager.StructTypeKind;
@ -99,4 +101,12 @@ public class StructTypeTest {
assertFalse( typeObjArray.isSubTypeOf( typeIntArray ) );
}
@Test
public void isSubTypeOf_Lambda() throws Throwable {
StructType typeRunnable = manager.valueOf( "java/lang/Runnable" );
LambdaType lambda = manager.lambdaType( "typeName", new ArrayList(), typeRunnable, new FunctionName( "", "", "" ), "run" );
assertTrue( lambda.isSubTypeOf( typeRunnable ) );
assertFalse( typeRunnable.isSubTypeOf( lambda ) );
}
}