1515/**
1616 * Implementation of ECMAScript 2021 FinalizationRegistry.
1717 *
18- * <p>FinalizationRegistry allows JavaScript code to register cleanup callbacks that are called when
19- * objects are garbage collected. This implementation provides:
20- *
21- * <ul>
22- * <li>Self-contained design - each registry manages its own registrations
23- * <li>Shared ReferenceQueue for JVM efficiency (per aardvark179's recommendation)
24- * <li>Integration with Rhino's Context finalization infrastructure
25- * <li>Thread-safe registration/unregistration using concurrent data structures
26- * <li>Bounded cleanup processing to prevent performance issues
27- * </ul>
28- *
29- * <p>Key methods:
30- *
31- * <ul>
32- * <li>{@code register(target, heldValue, unregisterToken)} - register object for cleanup
33- * <li>{@code unregister(token)} - remove registrations by token
34- * <li>{@code cleanupSome(callback)} - process pending cleanups synchronously
35- * </ul>
18+ * <p>Allows JavaScript code to register cleanup callbacks for garbage collected objects. Uses
19+ * shared ReferenceQueue for efficiency and integrates with Rhino's Context system.
3620 *
3721 * @see <a href="https://tc39.es/ecma262/#sec-finalization-registry-objects">ECMAScript
3822 * FinalizationRegistry</a>
@@ -59,14 +43,7 @@ public class NativeFinalizationRegistry extends ScriptableObject {
5943 /** Temporary callback override for cleanupSome() method */
6044 private volatile Function cleanupSomeCallback = null ;
6145
62- /**
63- * Initialize the FinalizationRegistry constructor and prototype in the given scope.
64- *
65- * @param cx the JavaScript context
66- * @param scope the scope to install FinalizationRegistry in
67- * @param sealed whether to seal the constructor and prototype
68- * @return the FinalizationRegistry constructor function
69- */
46+ /** Initialize FinalizationRegistry constructor and prototype. */
7047 public static Object init (Context cx , Scriptable scope , boolean sealed ) {
7148 LambdaConstructor constructor =
7249 new LambdaConstructor (
@@ -120,24 +97,12 @@ public static Object init(Context cx, Scriptable scope, boolean sealed) {
12097 return constructor ;
12198 }
12299
123- /**
124- * Private constructor for FinalizationRegistry instances.
125- *
126- * @param cleanupCallback the function to call when objects are finalized
127- */
100+ /** Private constructor for FinalizationRegistry instances. */
128101 private NativeFinalizationRegistry (Function cleanupCallback ) {
129102 this .cleanupCallback = cleanupCallback ;
130103 }
131104
132- /**
133- * JavaScript constructor implementation.
134- *
135- * @param cx the JavaScript context
136- * @param scope the constructor scope
137- * @param args constructor arguments (first must be a function)
138- * @return new FinalizationRegistry instance
139- * @throws TypeError if callback is not a function
140- */
105+ /** JavaScript constructor implementation. */
141106 private static NativeFinalizationRegistry jsConstructor (
142107 Context cx , Scriptable scope , Object [] args ) {
143108 if (args .length < 1 || !(args [0 ] instanceof Function )) {
@@ -153,16 +118,7 @@ private static NativeFinalizationRegistry jsConstructor(
153118 return registry ;
154119 }
155120
156- /**
157- * JavaScript register() method implementation.
158- *
159- * @param cx the JavaScript context (unused but required by Rhino)
160- * @param scope the method scope (unused but required by Rhino)
161- * @param thisObj the 'this' object (must be FinalizationRegistry)
162- * @param args method arguments: target, heldValue, [unregisterToken]
163- * @return undefined
164- * @throws TypeError if arguments are invalid
165- */
121+ /** JavaScript register() method implementation. */
166122 private static Object register (
167123 Context cx , Scriptable scope , Scriptable thisObj , Object [] args ) {
168124 NativeFinalizationRegistry registry = realThis (thisObj , "register" );
@@ -198,31 +154,14 @@ private static Object register(
198154 return Undefined .instance ;
199155 }
200156
201- /**
202- * JavaScript unregister() method implementation.
203- *
204- * @param cx the JavaScript context (unused but required by Rhino)
205- * @param scope the method scope (unused but required by Rhino)
206- * @param thisObj the 'this' object (must be FinalizationRegistry)
207- * @param args method arguments: unregisterToken
208- * @return boolean indicating whether any registrations were removed
209- */
157+ /** JavaScript unregister() method implementation. */
210158 private static Object unregister (
211159 Context cx , Scriptable scope , Scriptable thisObj , Object [] args ) {
212160 NativeFinalizationRegistry registry = realThis (thisObj , "unregister" );
213161 return registry .unregisterToken (args );
214162 }
215163
216- /**
217- * JavaScript cleanupSome() method implementation.
218- *
219- * @param cx the JavaScript context
220- * @param scope the method scope (unused but required by Rhino)
221- * @param thisObj the 'this' object (must be FinalizationRegistry)
222- * @param args method arguments: [callback]
223- * @return undefined
224- * @throws TypeError if callback is provided but not a function
225- */
164+ /** JavaScript cleanupSome() method implementation. */
226165 private static Object cleanupSome (
227166 Context cx , Scriptable scope , Scriptable thisObj , Object [] args ) {
228167 NativeFinalizationRegistry registry = realThis (thisObj , "cleanupSome" );
@@ -411,17 +350,8 @@ private void processCleanups(Context cx, int maxCleanups) {
411350 /**
412351 * Clean up when this registry is being GC'd.
413352 *
414- * <p>Note: finalize() is deprecated in Java 9+ but is still the most appropriate mechanism for
415- * this use case. The newer Cleaner API is not suitable because:
416- *
417- * <ul>
418- * <li>FinalizationRegistry needs dynamic registration/unregistration
419- * <li>Cleaner requires static cleanup actions defined at object creation
420- * <li>We need to clear references when the registry itself is collected
421- * </ul>
422- *
423- * <p>This is intentionally using finalize() as recommended by aardvark179 for this specific use
424- * case where we need to clean up when the registry itself is GC'd.
353+ * <p>Uses finalize() over Cleaner API due to dynamic registration requirements. Recommended by
354+ * aardvark179 for this specific GC cleanup use case.
425355 */
426356 @ Override
427357 @ SuppressWarnings ({"deprecation" , "finalize" , "Finalize" })
0 commit comments