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());
? new WasmGetGlobal(strings.getStringConstant(name).global) call.getArguments().add(new WasmGetGlobal(global));
: new WasmNullConstant(standardClasses.stringClass().getType()); if (metadataRequirements.hasName()) {
return new WasmCall( call.getArguments().add(name != null
getCreatePrimitiveClassFunction(), ? new WasmGetGlobal(strings.getStringConstant(name).global)
new WasmGetGlobal(global), : new WasmNullConstant(standardClasses.stringClass().getType()));
str, }
new WasmInt32Constant(kind) call.getArguments().add(new WasmInt32Constant(kind));
); return call;
} }
@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);
function.add(nameVar); WasmLocal nameVar;
if (metadataRequirements.hasName()) {
nameVar = new WasmLocal(standardClasses.stringClass().getType(), "name");
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
)); ));
function.getBody().add(new WasmStructSet( if (nameVar != null) {
standardClasses.classClass().getStructure(), function.getBody().add(new WasmStructSet(
new WasmGetLocal(targetVar), standardClasses.classClass().getStructure(),
classNameOffset, new WasmGetLocal(targetVar),
new WasmGetLocal(nameVar) classNameOffset,
)); 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),