@@ -46,21 +46,25 @@ describe("GC finalizer callbacks", function () {
4646 }
4747 } , { name : "TNSApiDeallocThread" } ) ;
4848
49- // Owned only through its wrapper, so its finalizer releases it.
50- ( function ( ) {
51- DeallocThread . alloc ( ) . init ( ) ;
52- } ) ( ) ;
49+ // Owned only through its wrapper, so its finalizer releases it. Held
50+ // until the background block, so no collection on the main thread
51+ // gets to it first.
52+ var keep = DeallocThread . alloc ( ) . init ( ) ;
5353
54+ var deallocsInBlock = - 1 ;
5455 var queue = dispatch_get_global_queue ( qos_class_t . QOS_CLASS_DEFAULT , 0 ) ;
5556 dispatch_async ( queue , function ( ) {
57+ keep = null ;
5658 scrubStack ( ) ;
5759 __collect ( ) ;
5860 __collect ( ) ;
61+ deallocsInBlock = threads . length ;
5962 } ) ;
6063
6164 var attempts = 200 ;
6265 ( function poll ( ) {
6366 if ( threads . length > 0 || -- attempts === 0 ) {
67+ expect ( deallocsInBlock ) . toBe ( 0 ) ;
6468 expect ( threads ) . toEqual ( [ true ] ) ;
6569 done ( ) ;
6670 return ;
@@ -100,6 +104,89 @@ describe("GC finalizer callbacks", function () {
100104 } ) ( ) ;
101105 } ) ;
102106
107+ // A wrapper built for an object whose release still waits for the
108+ // runtime's thread adopts that release as its own retain. The block below
109+ // holds the Locker throughout, so the release cannot run in between.
110+ [
111+ {
112+ suffix : " (extend)" ,
113+ // No retain/release swizzle: natively held instances are not kept.
114+ keepsWrapper : false ,
115+ define : function ( onDealloc ) {
116+ return TNSApi . extend ( { methodCalledInDealloc : onDealloc } , { name : "TNSApiReleaseTakeover" } ) ;
117+ }
118+ } ,
119+ {
120+ suffix : " (__extends)" ,
121+ keepsWrapper : true ,
122+ define : function ( onDealloc ) {
123+ return ( function ( _super ) {
124+ __extends ( TNSApiReleaseTakeoverTS , _super ) ;
125+ function TNSApiReleaseTakeoverTS ( ) {
126+ return ( _super !== null && _super . apply ( this , arguments ) ) || this ;
127+ }
128+ TNSApiReleaseTakeoverTS . prototype . methodCalledInDealloc = onDealloc ;
129+ return TNSApiReleaseTakeoverTS ;
130+ } ) ( TNSApi ) ;
131+ }
132+ }
133+ ] . forEach ( function ( variant ) {
134+ it ( "hands a waiting release to the wrapper that replaces it" + variant . suffix , function ( done ) {
135+ var threads = [ ] ;
136+ var Klass = variant . define ( function ( ) {
137+ threads . push ( NSThread . isMainThread ) ;
138+ } ) ;
139+
140+ var holder = NSMutableArray . alloc ( ) . init ( ) ;
141+ ( function ( ) {
142+ holder . addObject ( Klass . alloc ( ) . init ( ) ) ;
143+ } ) ( ) ;
144+
145+ var retainCount = - 1 ;
146+ var queue = dispatch_get_global_queue ( qos_class_t . QOS_CLASS_DEFAULT , 0 ) ;
147+ dispatch_async ( queue , function ( ) {
148+ __releaseNativeCounterpart ( holder . objectAtIndex ( 0 ) ) ;
149+ var again = holder . objectAtIndex ( 0 ) ;
150+ again . marker = "kept" ;
151+ // The holder's reference and the adopted one.
152+ retainCount = again . retainCount ( ) ;
153+ } ) ;
154+
155+ function collectAndWait ( rounds , callback ) {
156+ scrubStack ( ) ;
157+ __collect ( ) ;
158+ __collect ( ) ;
159+ setTimeout ( function ( ) {
160+ if ( rounds > 1 ) {
161+ collectAndWait ( rounds - 1 , callback ) ;
162+ } else {
163+ callback ( ) ;
164+ }
165+ } , 10 ) ;
166+ }
167+
168+ var attempts = 200 ;
169+ ( function poll ( ) {
170+ if ( retainCount === - 1 && -- attempts > 0 ) {
171+ setTimeout ( poll , 10 ) ;
172+ return ;
173+ }
174+ expect ( retainCount ) . toBe ( 2 ) ;
175+ collectAndWait ( 2 , function ( ) {
176+ expect ( threads ) . toEqual ( [ ] ) ;
177+ if ( variant . keepsWrapper ) {
178+ expect ( holder . objectAtIndex ( 0 ) . marker ) . toBe ( "kept" ) ;
179+ }
180+ holder . removeAllObjects ( ) ;
181+ collectAndWait ( 3 , function ( ) {
182+ expect ( threads ) . toEqual ( [ true ] ) ;
183+ done ( ) ;
184+ } ) ;
185+ } ) ;
186+ } ) ( ) ;
187+ } ) ;
188+ } ) ;
189+
103190 // A GcProtected (natively retained) instance is resurrected by its
104191 // finalizer, but anything reachable only through it is queued in the same
105192 // GC and disposed — the resurrected parent then holds "husks" whose
0 commit comments