wasm gc: fix broken module when Class.getName is never called

This commit is contained in:
Alexey Andreev 2024-09-19 19:49:25 +02:00
parent 64d6b1e570
commit 21cc3b048b

View File

@ -1012,15 +1012,15 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit
} }
private WasmExpression fillPrimitiveClass(WasmGlobal global, String name, int kind) { private WasmExpression fillPrimitiveClass(WasmGlobal global, String name, int kind) {
var str = name != null var call = new WasmCall(getCreatePrimitiveClassFunction());
call.getArguments().add(new WasmGetGlobal(global));
if (metadataRequirements.hasName()) {
call.getArguments().add(name != null
? new WasmGetGlobal(strings.getStringConstant(name).global) ? new WasmGetGlobal(strings.getStringConstant(name).global)
: new WasmNullConstant(standardClasses.stringClass().getType()); : new WasmNullConstant(standardClasses.stringClass().getType()));
return new WasmCall( }
getCreatePrimitiveClassFunction(), call.getArguments().add(new WasmInt32Constant(kind));
new WasmGetGlobal(global), return call;
str,
new WasmInt32Constant(kind)
);
} }
@Override @Override
@ -1261,21 +1261,27 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit
} }
private WasmFunction createCreatePrimitiveClassFunction() { private WasmFunction createCreatePrimitiveClassFunction() {
var functionType = functionTypes.of( var params = new ArrayList<WasmType>();
null, params.add(standardClasses.classClass().getType());
standardClasses.classClass().getType(), if (metadataRequirements.hasName()) {
standardClasses.stringClass().getType(), params.add(standardClasses.stringClass().getType());
WasmType.INT32 }
); params.add(WasmType.INT32);
var functionType = functionTypes.of(null, params.toArray(new WasmType[0]));
var function = new WasmFunction(functionType); var function = new WasmFunction(functionType);
function.setName(names.topLevel("teavm@fill_primitive_class")); function.setName(names.topLevel("teavm@fillPrimitiveClass"));
module.functions.add(function); module.functions.add(function);
var targetVar = new WasmLocal(standardClasses.classClass().getType(), "target"); var targetVar = new WasmLocal(standardClasses.classClass().getType(), "target");
var nameVar = new WasmLocal(standardClasses.stringClass().getType(), "name");
var kindVar = new WasmLocal(WasmType.INT32, "kind");
function.add(targetVar); function.add(targetVar);
WasmLocal nameVar;
if (metadataRequirements.hasName()) {
nameVar = new WasmLocal(standardClasses.stringClass().getType(), "name");
function.add(nameVar); function.add(nameVar);
} else {
nameVar = null;
}
var kindVar = new WasmLocal(WasmType.INT32, "kind");
function.add(kindVar); function.add(kindVar);
standardClasses.classClass().getStructure().init(); standardClasses.classClass().getStructure().init();
@ -1303,12 +1309,14 @@ public class WasmGCClassGenerator implements WasmGCClassInfoProvider, WasmGCInit
classFlagsOffset, classFlagsOffset,
flagsExpr flagsExpr
)); ));
if (nameVar != null) {
function.getBody().add(new WasmStructSet( function.getBody().add(new WasmStructSet(
standardClasses.classClass().getStructure(), standardClasses.classClass().getStructure(),
new WasmGetLocal(targetVar), new WasmGetLocal(targetVar),
classNameOffset, classNameOffset,
new WasmGetLocal(nameVar) new WasmGetLocal(nameVar)
)); ));
}
function.getBody().add(new WasmStructSet( function.getBody().add(new WasmStructSet(
standardClasses.classClass().getStructure(), standardClasses.classClass().getStructure(),
new WasmGetLocal(targetVar), new WasmGetLocal(targetVar),