diff --git a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java index dafd958a4..ed5d3222b 100644 --- a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java +++ b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/ReferenceQueueGenerator.java @@ -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("(") diff --git a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java index 68200e5ca..10474c4b1 100644 --- a/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java +++ b/core/src/main/java/org/teavm/backend/javascript/intrinsics/ref/WeakReferenceGenerator.java @@ -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(); } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ref/WeakReferenceTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ref/WeakReferenceTest.java index cba7e5225..d4ff531bd 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/ref/WeakReferenceTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ref/WeakReferenceTest.java @@ -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;