Skip to content

Commit 9c07c68

Browse files
committed
Improve FinalizationRegistry ECMAScript compliance and refactor validation logic
1 parent 5d25301 commit 9c07c68

File tree

2 files changed

+140
-81
lines changed

2 files changed

+140
-81
lines changed

rhino/src/main/java/org/mozilla/javascript/FinalizationQueue.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
/**
1414
* Shared finalization queue infrastructure for FinalizationRegistry.
1515
*
16-
* <p>Provides shared ReferenceQueue for JVM efficiency and GC detection polling.
16+
* <p>Provides shared ReferenceQueue for JVM efficiency and GC detection polling. Uses JSCode
17+
* architecture for Context-safe cleanup execution.
1718
*/
1819
public class FinalizationQueue {
1920

@@ -30,10 +31,11 @@ public static ReferenceQueue<Object> getSharedQueue() {
3031
}
3132

3233
/**
33-
* Poll for finalized objects and schedule cleanups.
34+
* Poll for finalized objects and schedule cleanups using JSCode architecture.
3435
*
3536
* <p>Called from Context during JavaScript execution to process recently finalized objects.
36-
* Processes at most maxCleanups items to bound execution time.
37+
* Processes at most maxCleanups items to bound execution time. Uses JSCode execution for
38+
* Context safety per aardvark179's architecture patterns.
3739
*
3840
* @param cx the JavaScript execution context
3941
* @param maxCleanups maximum number of cleanup tasks to process
@@ -47,18 +49,20 @@ public static void pollAndScheduleCleanups(Context cx, int maxCleanups) {
4749
while (processed < maxCleanups && (ref = SHARED_QUEUE.poll()) != null) {
4850
if (ref instanceof TrackedPhantomReference) {
4951
TrackedPhantomReference trackedRef = (TrackedPhantomReference) ref;
50-
trackedRef.scheduleCleanup(cx);
52+
// Use JSCode execution instead of direct Context capture
53+
trackedRef.scheduleJSCodeCleanup(cx);
5154
processed++;
5255
}
5356
ref.clear();
5457
}
5558
}
5659

5760
/**
58-
* PhantomReference that knows how to schedule its own cleanup.
61+
* PhantomReference that knows how to schedule its own cleanup using JSCode architecture.
5962
*
6063
* <p>Base class for references that need to perform cleanup when their target is garbage
61-
* collected. Automatically registers with the shared queue.
64+
* collected. Automatically registers with the shared queue. Uses JSCode execution for
65+
* Context-safe cleanup per aardvark179's architecture patterns.
6266
*/
6367
public abstract static class TrackedPhantomReference extends PhantomReference<Object> {
6468

@@ -67,13 +71,13 @@ protected TrackedPhantomReference(Object target) {
6771
}
6872

6973
/**
70-
* Schedule cleanup in the given Context.
74+
* Schedule cleanup using JSCode execution (Context-safe).
7175
*
72-
* <p>Called when the referenced object has been garbage collected. Implementations should
73-
* schedule appropriate cleanup actions.
76+
* <p>Called when the referenced object has been garbage collected. Uses JSCode architecture
77+
* to avoid Context capture issues.
7478
*
75-
* @param cx the JavaScript execution context
79+
* @param cx the JavaScript execution context (fresh, never stored)
7680
*/
77-
protected abstract void scheduleCleanup(Context cx);
81+
protected abstract void scheduleJSCodeCleanup(Context cx);
7882
}
7983
}

0 commit comments

Comments
 (0)