Don't write Structure, Function and Address to GC roots

This commit is contained in:
Alexey Andreev 2016-09-08 19:17:06 +03:00
parent 3370898a54
commit d76598ab68
2 changed files with 40 additions and 2 deletions

View File

@ -36,6 +36,7 @@ public final class Example {
testArrayCopy();
testArrayIsObject();
testIsAssignableFrom();
testGC();
}
private static void testFibonacci() {
@ -142,6 +143,17 @@ public final class Example {
println("Base.isAssignableFrom(A) = " + Base.class.isAssignableFrom(A.class));
}
private static void testGC() {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100000; ++i) {
list.add(i);
if (list.size() == 1000) {
list = new ArrayList<>();
}
}
print("GC complete");
}
private static Base instance(int index) {
switch (index) {
case 0:

View File

@ -226,7 +226,7 @@ public class WasmClassGenerator {
private List<FieldReference> getReferenceFields(ClassReader cls) {
return cls.getFields().stream()
.filter(field -> !field.hasModifier(ElementModifier.STATIC))
.filter(field -> !(field.getType() instanceof ValueType.Primitive))
.filter(field -> isReferenceType(field.getType()))
.map(field -> field.getReference())
.collect(Collectors.toList());
}
@ -234,11 +234,37 @@ public class WasmClassGenerator {
private List<FieldReference> getStaticReferenceFields(ClassReader cls) {
return cls.getFields().stream()
.filter(field -> field.hasModifier(ElementModifier.STATIC))
.filter(field -> !(field.getType() instanceof ValueType.Primitive))
.filter(field -> isReferenceType(field.getType()))
.map(field -> field.getReference())
.collect(Collectors.toList());
}
private boolean isReferenceType(ValueType type) {
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 true;
}
while (cls != null) {
if (cls.getName().equals(Structure.class.getName()) || cls.getName().equals(Function.class.getName())) {
return false;
}
if (cls.getParent() == null || cls.getParent().equals(cls.getName())) {
return true;
}
cls = classSource.get(cls.getParent());
}
return true;
} else {
return true;
}
}
private void fillVirtualTable(VirtualTable vtable, DataValue array) {
int index = 0;
for (VirtualTableEntry vtableEntry : vtable.getEntries().values()) {