add support for global.get/set for the WatParser

This commit is contained in:
Volker Berlin 2022-04-16 17:31:48 +02:00
parent 0dfb7c4a34
commit 128906e4ef
2 changed files with 34 additions and 3 deletions

View File

@ -29,6 +29,7 @@ import javax.annotation.Nullable;
import de.inetsoftware.classparser.BootstrapMethod;
import de.inetsoftware.classparser.ClassFile;
import de.inetsoftware.classparser.ConstantClass;
import de.inetsoftware.classparser.FieldInfo;
import de.inetsoftware.classparser.LocalVariableTable;
import de.inetsoftware.classparser.Member;
import de.inetsoftware.classparser.MethodInfo;
@ -536,6 +537,33 @@ public abstract class WasmCodeBuilder {
functions.markClassAsUsed( name.className );
}
/**
* Add a global field access instruction
*
* @param load
* true: if load (get)
* @param fieldName
* the fieldName like java/lang/System.err
* @param javaCodePos
* the code position/offset in the Java method
* @param lineNumber
* the line number in the Java source code
*/
protected void addGlobalInstruction( boolean load, String fieldName, int javaCodePos, int lineNumber ) {
try {
if( fieldName.startsWith( "$" ) ) {
fieldName = fieldName.substring( 1 );
}
FunctionName name = new FunctionName( fieldName + "()V" );
ClassFile classFile = classFileLoader.get( name.className );
FieldInfo field = classFile.getField( name.methodName );
AnyType type = new ValueTypeParser( field.getType(), types ).next();
addGlobalInstruction( load, name, type, null, javaCodePos, lineNumber );
} catch( Exception ex ) {
throw WasmException.create( ex, lineNumber );
}
}
/**
* Add a WasmTableInstruction table.get/table.set.
*

View File

@ -77,9 +77,12 @@ public class WatParser extends WasmCodeBuilder {
case "local.tee":
addLocalInstruction( VariableOperator.tee, getInt( tokens, ++i), javaCodePos, lineNumber );
break;
// case "get_global":
// addGlobalInstruction( true, ref, javaCodePos );
// break;
case "global.get":
addGlobalInstruction( true, get( tokens, ++i), javaCodePos, lineNumber );
break;
case "global.set":
addGlobalInstruction( false, get( tokens, ++i), javaCodePos, lineNumber );
break;
case "i32.const":
addConstInstruction( getInt( tokens, ++i), ValueType.i32, javaCodePos, lineNumber );
break;