@@ -197,28 +197,39 @@ try test("Closure Lifetime") {
197
197
return arguments [ 0 ]
198
198
}
199
199
try expectEqual ( evalClosure ( c1, JSValue . number ( 1.0 ) ) , . number( 1.0 ) )
200
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
200
201
c1. release ( )
202
+ #endif
201
203
}
202
204
203
205
do {
204
- let c1 = JSClosure { arguments in
205
- return arguments [ 0 ]
206
- }
206
+ let c1 = JSClosure { _ in . undefined }
207
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
207
208
c1. release ( )
208
- // Call a released closure
209
- try expectCrashByCall ( ofClosure: c1)
209
+ #endif
210
210
}
211
211
212
212
do {
213
- let c1 = JSClosure { _ in
214
- // JSClosure will be deallocated before `release()`
215
- _ = JSClosure { _ in . undefined }
216
- return . undefined
217
- }
218
- try expectCrashByCall ( ofClosure: c1)
213
+ let array = JSObject . global. Array. function!. new ( )
214
+ let c1 = JSClosure { _ in . number( 3 ) }
215
+ _ = array. push!( c1)
216
+ try expectEqual ( array [ 0 ] . function!( ) . number, 3.0 )
217
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
219
218
c1. release ( )
219
+ #endif
220
220
}
221
221
222
+ // do {
223
+ // let weakRef = { () -> JSObject in
224
+ // let c1 = JSClosure { _ in .undefined }
225
+ // return JSObject.global.WeakRef.function!.new(c1)
226
+ // }()
227
+ //
228
+ // // unsure if this will actually work since GC may not run immediately
229
+ // try expectEqual(weakRef.deref!(), .undefined)
230
+ // }
231
+
232
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
222
233
do {
223
234
let c1 = JSOneshotClosure { _ in
224
235
return . boolean( true )
@@ -228,6 +239,7 @@ try test("Closure Lifetime") {
228
239
try expectCrashByCall ( ofClosure: c1)
229
240
// OneshotClosure won't call fatalError even if it's deallocated before `release`
230
241
}
242
+ #endif
231
243
}
232
244
233
245
try test ( " Host Function Registration " ) {
@@ -259,7 +271,9 @@ try test("Host Function Registration") {
259
271
try expectEqual ( call_host_1Func ( ) , . number( 1 ) )
260
272
try expectEqual ( isHostFunc1Called, true )
261
273
274
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
262
275
hostFunc1. release ( )
276
+ #endif
263
277
264
278
let hostFunc2 = JSClosure { ( arguments) -> JSValue in
265
279
do {
@@ -272,7 +286,10 @@ try test("Host Function Registration") {
272
286
273
287
try expectEqual ( evalClosure ( hostFunc2, 3 ) , . number( 6 ) )
274
288
_ = try expectString ( evalClosure ( hostFunc2, true ) )
289
+
290
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
275
291
hostFunc2. release ( )
292
+ #endif
276
293
}
277
294
278
295
try test ( " New Object Construction " ) {
@@ -386,9 +403,13 @@ try test("ObjectRef Lifetime") {
386
403
let ref2 = evalClosure ( identity, ref1) . object!
387
404
try expectEqual ( ref1. prop_2, . number( 2 ) )
388
405
try expectEqual ( ref2. prop_2, . number( 2 ) )
406
+
407
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
389
408
identity. release ( )
409
+ #endif
390
410
}
391
411
412
+ #if JAVASCRIPTKIT_WITHOUT_WEAKREFS
392
413
func closureScope( ) -> ObjectIdentifier {
393
414
let closure = JSClosure { _ in . undefined }
394
415
let result = ObjectIdentifier ( closure)
@@ -401,6 +422,7 @@ try test("Closure Identifiers") {
401
422
let oid2 = closureScope ( )
402
423
try expectEqual ( oid1, oid2)
403
424
}
425
+ #endif
404
426
405
427
func checkArray< T> ( _ array: [ T ] ) throws where T: TypedArrayElement {
406
428
try expectEqual ( toString ( JSTypedArray ( array) . jsValue ( ) . object!) , jsStringify ( array) )
@@ -519,7 +541,7 @@ try test("Timer") {
519
541
interval = JSTimer ( millisecondsDelay: 5 , isRepeating: true ) {
520
542
// ensure that JSTimer is living
521
543
try ! expectNotNil ( interval)
522
- // verify that at least `timeoutMilliseconds * count` passed since the `timeout`
544
+ // verify that at least `timeoutMilliseconds * count` passed since the `timeout`
523
545
// timer started
524
546
try ! expectEqual ( start + timeoutMilliseconds * count <= JSDate ( ) . valueOf ( ) , true )
525
547
@@ -555,7 +577,8 @@ try test("Promise") {
555
577
exp1. fulfill ( )
556
578
return JSValue . undefined
557
579
}
558
- . catch { _ -> JSValue in
580
+ . catch { err -> JSValue in
581
+ print ( err. object!. stack. string!)
559
582
fatalError ( " Not fired due to no throw " )
560
583
}
561
584
. finally { exp1. fulfill ( ) }
0 commit comments