use constants for primitive type IDs

This commit is contained in:
Volker Berlin 2020-09-20 13:59:13 +02:00
parent 0290a5f8e4
commit 622fd5d63c
2 changed files with 60 additions and 13 deletions

View File

@ -19,6 +19,8 @@ package de.inetsoftware.jwebassembly.module;
import de.inetsoftware.jwebassembly.api.annotation.Replace;
import de.inetsoftware.jwebassembly.api.annotation.WasmTextCode;
import static de.inetsoftware.jwebassembly.module.TypeManager.*;
/**
* Replacement for java.lang.Class
*
@ -45,7 +47,7 @@ class ReplacementForClass {
* @return the name
*/
String getName() {
return StringManager.stringConstant( getIntFromMemory( vtable + TypeManager.TYPE_DESCRIPTION_TYPE_NAME ) );
return StringManager.stringConstant( getIntFromMemory( vtable + TYPE_DESCRIPTION_TYPE_NAME ) );
}
/**
@ -59,7 +61,7 @@ class ReplacementForClass {
@WasmTextCode( "local.get 0 " // THIS
+ "struct.get java/lang/Object .vtable " // vtable is on index 0
+ "local.tee 1 " // save the vtable location
+ "i32.const " + TypeManager.TYPE_DESCRIPTION_INSTANCEOF_OFFSET + " " // vtable is on index 0
+ "i32.const " + TYPE_DESCRIPTION_INSTANCEOF_OFFSET + " " // vtable is on index 0
+ "i32.add " //
+ "call $java/lang/Class.getIntFromMemory(I)I " //
+ "local.get 1 " // get the vtable location
@ -177,7 +179,7 @@ class ReplacementForClass {
* Replacement of the native Java methods getComponentType()
*/
ReplacementForClass getComponentType() {
int classIdx = getIntFromMemory( vtable + TypeManager.TYPE_DESCRIPTION_ARRAY_TYPE );
int classIdx = getIntFromMemory( vtable + TYPE_DESCRIPTION_ARRAY_TYPE );
return classIdx >= 0 ? classConstant( classIdx ) : null;
}
@ -192,23 +194,23 @@ class ReplacementForClass {
static ReplacementForClass getPrimitiveClass( String name ) {
switch( name ) {
case "boolean":
return classConstant( 0 );
return classConstant( BOOLEAN );
case "byte":
return classConstant( 1 );
return classConstant( BYTE );
case "char":
return classConstant( 2 );
return classConstant( CHAR );
case "double":
return classConstant( 3 );
return classConstant( DOUBLE );
case "float":
return classConstant( 4 );
return classConstant( FLOAT );
case "int":
return classConstant( 5 );
return classConstant( INT );
case "long":
return classConstant( 6 );
return classConstant( LONG );
case "short":
return classConstant( 7 );
return classConstant( SHORT );
case "void":
return classConstant( 8 );
return classConstant( VOID );
}
return null;
}

View File

@ -82,7 +82,7 @@ public class TypeManager {
/**
* Byte position in the type description that contains the type of the array (component type). Length 4 bytes.
*/
static final int TYPE_DESCRIPTION_ARRAY_TYPE = 12;
public static final int TYPE_DESCRIPTION_ARRAY_TYPE = 12;
/**
* The reserved position on start of the vtable:
@ -94,6 +94,51 @@ public class TypeManager {
private static final FunctionName CLASS_CONSTANT_FUNCTION = new FunctionName( "java/lang/Class.classConstant(I)Ljava/lang/Class;" );
/**
* Type id of primitive class
*/
public static final int BOOLEAN = 0;
/**
* Type id of primitive class
*/
public static final int BYTE = 1;
/**
* Type id of primitive class
*/
public static final int CHAR = 2;
/**
* Type id of primitive class
*/
public static final int DOUBLE = 3;
/**
* Type id of primitive class
*/
public static final int FLOAT = 4;
/**
* Type id of primitive class
*/
public static final int INT = 5;
/**
* Type id of primitive class
*/
public static final int LONG = 6;
/**
* Type id of primitive class
*/
public static final int SHORT = 7;
/**
* Type id of primitive class
*/
public static final int VOID = 8;
/**
* the list of primitive types. The order is important and must correlate with getPrimitiveClass.
*