classlib: fix crash in WeakReference constructor

It occurred when ReferenceQueue.remove method was never used

Fix #756
This commit is contained in:
Alexey Andreev 2023-09-24 13:47:15 +02:00
parent e02f91b0fd
commit fa2e003dd6
3 changed files with 14 additions and 5 deletions

View File

@ -47,6 +47,12 @@ public class ReferenceQueueGenerator implements Generator {
writer.append(context.getParameterName(0)).append(".").appendField(INNER_FIELD).ws().append("=")
.ws().append("[];").softNewLine();
if (context.getDependency().getMethod(REPORT_METHOD) != null) {
generateFinalizationRegistry(context, writer);
}
}
private void generateFinalizationRegistry(GeneratorContext context, SourceWriter writer) throws IOException {
writer.append(context.getParameterName(0)).append(".").appendField(REGISTRY_FIELD).ws().append("=")
.ws().append("new $rt_globals.FinalizationRegistry(ref").ws().append("=>").appendBlockStart();
writer.appendIf().append("!").appendMethodBody(REPORT_METHOD).append("(")

View File

@ -55,10 +55,15 @@ public class WeakReferenceGenerator implements Generator {
writer.appendIf().append(context.getParameterName(2)).ws().append("!==").ws().append("null")
.ws().append("&&").ws().append("supported)")
.appendBlockStart();
writer.append(context.getParameterName(2)).append(".")
.appendField(new FieldReference(ReferenceQueue.class.getName(), "registry"))
.append(".").append("register(").append(context.getParameterName(1))
writer.append("var registry").ws().append("=").ws()
.append(context.getParameterName(2)).append(".")
.appendField(new FieldReference(ReferenceQueue.class.getName(), "registry")).append(";")
.softNewLine();
writer.appendIf().append("registry").ws().append("!==").ws().append("null)").ws();
writer.append("registry.register(").append(context.getParameterName(1))
.append(",").ws().append(context.getParameterName(0)).append(");").softNewLine();
writer.appendBlockEnd();
}

View File

@ -27,10 +27,8 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.teavm.junit.SkipJVM;
import org.teavm.junit.TeaVMTestRunner;
import org.teavm.junit.WholeClassCompilation;
@RunWith(TeaVMTestRunner.class)
@WholeClassCompilation
@SkipJVM
public class WeakReferenceTest {
private Node lastNode;