@@ -223,6 +223,151 @@ describe("Weak collections of Java objects", function () {
223223 expect ( map instanceof WeakMap ) . toBe ( true ) ;
224224 } ) ;
225225
226+ it ( "keys the entries given to the WeakMap and WeakSet constructors by the Java object" , function ( ) {
227+ var map ;
228+ var set ;
229+ ( function ( ) {
230+ var runnable = heldRunnable ( ) ;
231+ map = new WeakMap ( [ [ runnable , "constructed" ] ] ) ;
232+ set = new WeakSet ( [ runnable ] ) ;
233+ } ) ( ) ;
234+ collectBoth ( ) ;
235+
236+ expect ( map . get ( Holder . held ) ) . toBe ( "constructed" ) ;
237+ expect ( set . has ( Holder . held ) ) . toBe ( true ) ;
238+ } ) ;
239+
240+ it ( "keys a Java object in a subclass of WeakMap" , function ( ) {
241+ class TaggedMap extends WeakMap {
242+ set ( key , value ) {
243+ return super . set ( key , "tagged:" + value ) ;
244+ }
245+ }
246+ var map = new TaggedMap ( ) ;
247+ ( function ( ) {
248+ map . set ( heldRunnable ( ) , "value" ) ;
249+ } ) ( ) ;
250+ collectBoth ( ) ;
251+
252+ expect ( map . get ( Holder . held ) ) . toBe ( "tagged:value" ) ;
253+ expect ( map instanceof WeakMap ) . toBe ( true ) ;
254+ } ) ;
255+
256+ it ( "does not run a FinalizationRegistry callback while Java holds the object" , function ( done ) {
257+ var cleaned = false ;
258+ var registry = new FinalizationRegistry ( function ( ) {
259+ cleaned = true ;
260+ } ) ;
261+ ( function ( ) {
262+ registry . register ( heldRunnable ( ) , "held" ) ;
263+ } ) ( ) ;
264+ var polls = 0 ;
265+ ( function poll ( ) {
266+ collectBoth ( ) ;
267+ if ( cleaned ) {
268+ done . fail ( "the cleanup callback ran while Java held the object" ) ;
269+ return ;
270+ }
271+ if ( ++ polls < 5 ) {
272+ setTimeout ( poll , 50 ) ;
273+ return ;
274+ }
275+ expect ( Holder . held ) . not . toBe ( null ) ;
276+ done ( ) ;
277+ } ) ( ) ;
278+ } , 20000 ) ;
279+
280+ // Released, the wrapper is a plain JS object: what was keyed on it lives exactly as long
281+ // as it does. Only booleans reach expect(), which could otherwise keep the wrapper.
282+ it ( "keeps what was keyed on a Java object after its link is released, while the wrapper lives" , function ( done ) {
283+ var map = new WeakMap ( ) ;
284+ var set = new WeakSet ( ) ;
285+ var cleaned = false ;
286+ var registry = new FinalizationRegistry ( function ( held ) {
287+ cleaned = held === "released" ;
288+ } ) ;
289+ var runnable = new java . lang . Runnable ( { run : function ( ) { } } ) ;
290+ map . set ( runnable , "value" ) ;
291+ set . add ( runnable ) ;
292+ var ref = new WeakRef ( runnable ) ;
293+ registry . register ( runnable , "released" ) ;
294+ __releaseNativeCounterpart ( runnable ) ;
295+
296+ expect ( map . get ( runnable ) ) . toBe ( "value" ) ;
297+ expect ( set . has ( runnable ) ) . toBe ( true ) ;
298+ expect ( ref . deref ( ) === runnable ) . toBe ( true ) ;
299+ setTimeout ( function ( ) {
300+ collectBoth ( ) ;
301+ expect ( map . get ( runnable ) ) . toBe ( "value" ) ;
302+ expect ( set . has ( runnable ) ) . toBe ( true ) ;
303+ expect ( ref . deref ( ) === runnable ) . toBe ( true ) ;
304+ expect ( new WeakRef ( runnable ) . deref ( ) === runnable ) . toBe ( true ) ;
305+ map . set ( runnable , "again" ) ;
306+ expect ( map . get ( runnable ) ) . toBe ( "again" ) ;
307+ expect ( cleaned ) . toBe ( false ) ;
308+ runnable = null ;
309+ setTimeout ( function ( ) {
310+ eventually ( function ( ) {
311+ // A collected registry runs no callbacks.
312+ return cleaned && registry !== null ;
313+ } , done , "the cleanup callback did not run once the wrapper was collected" ) ;
314+ } , 0 ) ;
315+ } , 0 ) ;
316+ } , 20000 ) ;
317+
318+ it ( "makes no key for a call the standard collection rejects" , function ( ) {
319+ var runnable = new java . lang . Runnable ( { run : function ( ) { } } ) ;
320+ var registry = new FinalizationRegistry ( function ( ) { } ) ;
321+ var map = new WeakMap ( ) ;
322+ var states = runtime . getWrapperLifetimeStats ( ) . canaryStates ;
323+ expect ( function ( ) {
324+ WeakMap . prototype . set . call ( { } , runnable , 1 ) ;
325+ } ) . toThrowError ( TypeError ) ;
326+ expect ( function ( ) {
327+ WeakSet . prototype . add . call ( { } , runnable ) ;
328+ } ) . toThrowError ( TypeError ) ;
329+ expect ( function ( ) {
330+ registry . register ( runnable , "held" , 1 ) ;
331+ } ) . toThrowError ( TypeError ) ;
332+ expect ( function ( ) {
333+ FinalizationRegistry . prototype . register . call ( { } , runnable , "held" ) ;
334+ } ) . toThrowError ( TypeError ) ;
335+ if ( typeof map . getOrInsertComputed === "function" ) {
336+ expect ( function ( ) {
337+ WeakMap . prototype . getOrInsert . call ( { } , runnable , 1 ) ;
338+ } ) . toThrowError ( TypeError ) ;
339+ expect ( function ( ) {
340+ map . getOrInsertComputed ( runnable , function ( ) {
341+ throw new Error ( "callback" ) ;
342+ } ) ;
343+ } ) . toThrowError ( "callback" ) ;
344+ }
345+ expect ( runtime . getWrapperLifetimeStats ( ) . canaryStates ) . toBe ( states ) ;
346+ } ) ;
347+
348+ it ( "hands getOrInsertComputed's callback the key it was given" , function ( ) {
349+ var map = new WeakMap ( ) ;
350+ if ( typeof map . getOrInsertComputed !== "function" ) {
351+ return ;
352+ }
353+ var runnable = new java . lang . Runnable ( { run : function ( ) { } } ) ;
354+ var seen ;
355+ expect ( map . getOrInsertComputed ( runnable , function ( key ) {
356+ seen = key ;
357+ return "computed" ;
358+ } ) ) . toBe ( "computed" ) ;
359+ expect ( seen === runnable ) . toBe ( true ) ;
360+ expect ( map . get ( runnable ) ) . toBe ( "computed" ) ;
361+ } ) ;
362+
363+ it ( "matches the standard FinalizationRegistry.prototype.register signature and errors" , function ( ) {
364+ var registry = new FinalizationRegistry ( function ( ) { } ) ;
365+ expect ( FinalizationRegistry . prototype . register . length ) . toBe ( 2 ) ;
366+ expect ( function ( ) {
367+ registry . register ( 1 , 1 ) ;
368+ } ) . toThrowError ( TypeError , / i n v a l i d t a r g e t / ) ;
369+ } ) ;
370+
226371 it ( "installs the canary collections only in canary mode" , function ( ) {
227372 var native = WeakMap . toString ( ) . indexOf ( "[native code]" ) !== - 1 ;
228373 expect ( native ) . toBe ( ! canary ) ;
0 commit comments