add support for Class constants

This commit is contained in:
Volker Berlin 2020-03-15 15:49:52 +01:00
parent eba564fe67
commit 775496640c
5 changed files with 21 additions and 14 deletions

View File

@ -81,7 +81,7 @@ class ReplacementForClass {
* @param classIdx
* the id/index of the Class.
* @return the string
* @see #CLASS_CONSTANT_FUNCTION
* @see TypeManager#getClassConstantFunction()
*/
private static ReplacementForClass classConstant( int classIdx ) {
ReplacementForClass clazz = getClassFromTable( classIdx );

View File

@ -79,6 +79,8 @@ public class TypeManager {
*/
private static final int VTABLE_FIRST_FUNCTION_INDEX = 3;
private static final FunctionName CLASS_CONSTANT_FUNCTION = new FunctionName( "java/lang/Class.classConstant(I)Lde/inetsoftware/jwebassembly/module/ReplacementForClass;" );
private Map<String, StructType> structTypes = new LinkedHashMap<>();
private Map<AnyType, ArrayType> arrayTypes = new LinkedHashMap<>();
@ -151,6 +153,10 @@ public class TypeManager {
return offsetFunction;
}
FunctionName getClassConstantFunction() {
return CLASS_CONSTANT_FUNCTION;
}
/**
* Get the StructType. If needed an instance is created.
*

View File

@ -26,6 +26,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.classparser.ConstantClass;
import de.inetsoftware.classparser.LocalVariableTable;
import de.inetsoftware.classparser.Member;
import de.inetsoftware.classparser.MethodInfo;
@ -400,6 +401,12 @@ public abstract class WasmCodeBuilder {
addCallInstruction( name, javaCodePos, lineNumber );
} else if( value instanceof Number ) {
instructions.add( new WasmConstInstruction( (Number)value, javaCodePos, lineNumber ) );
} else if( value instanceof ConstantClass ) {
String className = ((ConstantClass)value).getName();
Integer id = types.valueOf( className ).getClassIndex();
FunctionName name = types.getClassConstantFunction();
instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) );
addCallInstruction( name, javaCodePos, lineNumber );
} else {
//TODO There can be ConstantClass, MethodType and MethodHandle
throw new WasmException( "Class constants are not supported. : " + value, lineNumber );

View File

@ -125,17 +125,4 @@ public class RuntimeErrors {
return list.size();
}
}
@Test
public void classConatnt() throws IOException {
compileErrorTest( "Class constants are not supported.", ClassConstant.class );
}
static class ClassConstant {
@Export
static Object runnable() {
Class elemtentType = Integer.class;
return java.lang.reflect.Array.newInstance(elemtentType, 42);
}
}
}

View File

@ -59,6 +59,7 @@ public class StructsNonGC extends AbstractBaseTest {
addParam( list, script, "objectClassName" );
addParam( list, script, "integerClassName" );
addParam( list, script, "classClassName" );
addParam( list, script, "classConst" );
}
rule.setTestParameters( list );
return list;
@ -195,6 +196,12 @@ public class StructsNonGC extends AbstractBaseTest {
Class clazz = obj.getClass().getClass();
return JSObject.domString( clazz.getName() );
}
@Export
static String classConst() {
Class clazz = Float.class;
return JSObject.domString( clazz.getName() );
}
}
interface TestDefault {