From 752ec040b0487f0422592b8d83c6d0b0db5ef211 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 4 Nov 2022 14:48:53 +0100 Subject: [PATCH] Wasm: fix resource files getting into garbage collection metadata --- .../org/teavm/backend/wasm/WasmTarget.java | 2 +- .../wasm/generate/WasmClassGenerator.java | 36 ++++++++----------- .../lowlevel/LowLevelNullCheckFilter.java | 2 +- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java index 64b9e60a3..d15343252 100644 --- a/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java +++ b/core/src/main/java/org/teavm/backend/wasm/WasmTarget.java @@ -427,7 +427,7 @@ public class WasmTarget implements TeaVMTarget, TeaVMWasmHost { ClassMetadataRequirements metadataRequirements = new ClassMetadataRequirements(controller.getDependencyInfo()); WasmClassGenerator classGenerator = new WasmClassGenerator(classes, controller.getUnprocessedClassSource(), vtableProvider, tagRegistry, binaryWriter, names, metadataRequirements, - controller.getClassInitializerInfo()); + controller.getClassInitializerInfo(), characteristics); Decompiler decompiler = new Decompiler(classes, new HashSet<>(), false); WasmStringPool stringPool = classGenerator.getStringPool(); diff --git a/core/src/main/java/org/teavm/backend/wasm/generate/WasmClassGenerator.java b/core/src/main/java/org/teavm/backend/wasm/generate/WasmClassGenerator.java index 799a7646f..c20aee5d1 100644 --- a/core/src/main/java/org/teavm/backend/wasm/generate/WasmClassGenerator.java +++ b/core/src/main/java/org/teavm/backend/wasm/generate/WasmClassGenerator.java @@ -49,12 +49,14 @@ import org.teavm.model.classes.TagRegistry; import org.teavm.model.classes.VirtualTable; import org.teavm.model.classes.VirtualTableEntry; import org.teavm.model.classes.VirtualTableProvider; +import org.teavm.model.lowlevel.Characteristics; import org.teavm.runtime.RuntimeClass; import org.teavm.runtime.RuntimeObject; public class WasmClassGenerator { private ClassReaderSource processedClassSource; private ClassReaderSource classSource; + private Characteristics characteristics; public final NameProvider names; private Map binaryDataMap = new LinkedHashMap<>(); private BinaryWriter binaryWriter; @@ -115,7 +117,7 @@ public class WasmClassGenerator { public WasmClassGenerator(ClassReaderSource processedClassSource, ClassReaderSource classSource, VirtualTableProvider vtableProvider, TagRegistry tagRegistry, BinaryWriter binaryWriter, NameProvider names, ClassMetadataRequirements metadataRequirements, - ClassInitializerInfo classInitializerInfo) { + ClassInitializerInfo classInitializerInfo, Characteristics characteristics) { this.processedClassSource = processedClassSource; this.classSource = classSource; this.vtableProvider = vtableProvider; @@ -125,6 +127,7 @@ public class WasmClassGenerator { this.names = names; this.metadataRequirements = metadataRequirements; this.classInitializerInfo = classInitializerInfo; + this.characteristics = characteristics; } public WasmStringPool getStringPool() { @@ -161,10 +164,12 @@ public class WasmClassGenerator { break; } - binaryData.data = createPrimitiveClassData(size, type); + binaryData.data = classStructure.createValue(); + createPrimitiveClassData(binaryData.data, size, type); binaryData.start = binaryWriter.append(binaryData.data); } else if (type == ValueType.VOID) { - binaryData.data = createPrimitiveClassData(0, type); + binaryData.data = classStructure.createValue(); + createPrimitiveClassData(binaryData.data, 0, type); binaryData.start = binaryWriter.append(binaryData.data); } else if (type instanceof ValueType.Object) { String className = ((ValueType.Object) type).getClassName(); @@ -207,8 +212,7 @@ public class WasmClassGenerator { } } - private DataValue createPrimitiveClassData(int size, ValueType type) { - DataValue value = classStructure.createValue(); + private DataValue createPrimitiveClassData(DataValue value, int size, ValueType type) { value.setInt(CLASS_SIZE, size); value.setInt(CLASS_FLAGS, RuntimeClass.PRIMITIVE); value.setInt(CLASS_IS_INSTANCE, functionTable.size()); @@ -395,23 +399,11 @@ public class WasmClassGenerator { if (type instanceof ValueType.Primitive) { return false; } else if (type instanceof ValueType.Object) { - ClassReader cls = classSource.get(((ValueType.Object) type).getClassName()); - if (cls == null) { - return true; - } - if (cls.getName().equals(Address.class.getName())) { - return false; - } - while (cls != null) { - if (cls.getName().equals(Structure.class.getName()) || cls.getName().equals(Function.class.getName())) { - return false; - } - if (cls.getParent() == null) { - return true; - } - cls = classSource.get(cls.getParent()); - } - return true; + String className = ((ValueType.Object) type).getClassName(); + return !characteristics.isStructure(className) + && !characteristics.isFunction(className) + && !characteristics.isResource(className) + && !className.equals(Address.class.getName()); } else { return true; } diff --git a/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java b/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java index 9c9ef6130..4b4927661 100644 --- a/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java +++ b/core/src/main/java/org/teavm/model/lowlevel/LowLevelNullCheckFilter.java @@ -29,7 +29,7 @@ public class LowLevelNullCheckFilter implements NullCheckFilter { @Override public boolean apply(FieldReference field) { return !characteristics.isStructure(field.getClassName()) - && !characteristics.isStructure(field.getClassName()); + && !characteristics.isResource(field.getClassName()); } @Override