Add support for primitive classes

This commit is contained in:
Volker Berlin 2020-04-04 16:09:58 +02:00
parent 9bbc82bb6d
commit 026f962677
2 changed files with 57 additions and 1 deletions

View File

@ -158,4 +158,44 @@ class ReplacementForClass {
"i32.load offset=0 align=4 " + //
"return" )
private static native int getIntFromMemory( int pos );
/**
* Replacement of the native Java methods
*
* @param name
* the class name
* @return the class
* @see TypeManager#PRIMITIVE_CLASSES
*/
static ReplacementForClass getPrimitiveClass( String name ) {
switch( name ) {
case "boolean":
return classConstant( 0 );
case "byte":
return classConstant( 1 );
case "char":
return classConstant( 2 );
case "double":
return classConstant( 3 );
case "float":
return classConstant( 4 );
case "int":
return classConstant( 5 );
case "long":
return classConstant( 6 );
case "short":
return classConstant( 7 );
case "void":
return classConstant( 8 );
}
return null;
}
/**
* Replacement of the native Java methods.
*/
public boolean desiredAssertionStatus() {
return false;
}
}

View File

@ -81,6 +81,13 @@ public class TypeManager {
private static final FunctionName CLASS_CONSTANT_FUNCTION = new FunctionName( "java/lang/Class.classConstant(I)Ljava/lang/Class;" );
/**
* the list of primitive types. The order is important and must correlate with getPrimitiveClass.
*
* @see ReplacementForClass#getPrimitiveClass(String)
*/
private static final String[] PRIMITIVE_CLASSES = { "boolean", "byte", "char", "double", "float", "int", "long", "short", "void" };
private Map<String, StructType> structTypes = new LinkedHashMap<>();
private Map<AnyType, ArrayType> arrayTypes = new LinkedHashMap<>();
@ -188,6 +195,13 @@ public class TypeManager {
if( isFinish ) {
throw new WasmException( "Register needed type after scanning: " + name, -1 );
}
if( structTypes.size() == 0 ) {
for( String primitiveTypeName : PRIMITIVE_CLASSES ) {
structTypes.put( primitiveTypeName, new StructType( primitiveTypeName, structTypes.size() ) );
}
}
type = new StructType( name, structTypes.size() );
structTypes.put( name, type );
}
@ -363,7 +377,9 @@ public class TypeManager {
instanceOFs = new LinkedHashSet<>(); // remembers the order from bottom to top class.
instanceOFs.add( this );
HashSet<String> allNeededFields = new HashSet<>();
listStructFields( name, functions, types, classFileLoader, allNeededFields );
if( classIndex >= PRIMITIVE_CLASSES.length ) {
listStructFields( name, functions, types, classFileLoader, allNeededFields );
}
}
/**