write the class name of all classes in the wasm file for later use

This commit is contained in:
Volker Berlin 2020-03-07 18:35:52 +01:00
parent 62b16ac07e
commit 5eed2c2e79
3 changed files with 20 additions and 5 deletions

View File

@ -57,6 +57,21 @@ class StringManager extends LinkedHashMap<String, Integer> {
this.functions = options.functions; this.functions = options.functions;
} }
/**
* Get the positive id for the string.
*
* @param str
* the string
* @return the id
*/
public Integer get( @Nonnull Object str ) {
Integer id = super.get( str );
if( id == null ) {
put( (String)str, id = size() );
}
return id;
}
/** /**
* Get the function name object for the {@link #stringConstant(int)}. * Get the function name object for the {@link #stringConstant(int)}.
* *

View File

@ -57,7 +57,7 @@ public class TypeManager {
* <li>offset of interface call table (itable) * <li>offset of interface call table (itable)
* <li>offset of instanceof list * <li>offset of instanceof list
*/ */
private static final int VTABLE_FIRST_FUNCTION_INDEX = 2; private static final int VTABLE_FIRST_FUNCTION_INDEX = 3;
private Map<String, StructType> structTypes = new LinkedHashMap<>(); private Map<String, StructType> structTypes = new LinkedHashMap<>();
@ -220,7 +220,7 @@ public class TypeManager {
* *
* @author Volker Berlin * @author Volker Berlin
*/ */
public static class StructType implements AnyType { public class StructType implements AnyType {
private final String name; private final String name;
@ -446,6 +446,9 @@ public class TypeManager {
data.writeInt32( type.getClassIndex() ); data.writeInt32( type.getClassIndex() );
} }
int classNameIdx = options.strings.get( getName().replace( '/', '.' ) );
header.writeInt32( classNameIdx ); // string id of the className
data.writeTo( dataStream ); data.writeTo( dataStream );
} }

View File

@ -395,9 +395,6 @@ public abstract class WasmCodeBuilder {
protected void addConstInstruction( Object value, int javaCodePos, int lineNumber ) { protected void addConstInstruction( Object value, int javaCodePos, int lineNumber ) {
if( value.getClass() == String.class ) { if( value.getClass() == String.class ) {
Integer id = strings.get( value ); Integer id = strings.get( value );
if( id == null ) {
strings.put( (String)value, id = strings.size() );
}
FunctionName name = strings.getStringConstantFunction(); FunctionName name = strings.getStringConstantFunction();
instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) ); instructions.add( new WasmConstInstruction( id, ValueType.i32, javaCodePos, lineNumber ) );
addCallInstruction( name, javaCodePos, lineNumber ); addCallInstruction( name, javaCodePos, lineNumber );