@@ -391,28 +391,89 @@ public func reflect<T: ErrorProtocol>(error: T) {
391
391
reflect ( instanceAddress: errorPointerValue, kind: . ErrorExistential)
392
392
}
393
393
394
+ /// Wraps a thick function with arity 0.
395
+ struct ThickFunction0 {
396
+ var function : ( ) -> ( )
397
+ }
398
+
399
+ /// Wraps a thick function with arity 1.
400
+ struct ThickFunction1 {
401
+ var function : ( Int ) -> ( )
402
+ }
403
+
404
+ /// Wraps a thick function with arity 2.
405
+ struct ThickFunction2 {
406
+ var function : ( Int , String ) -> ( )
407
+ }
408
+
409
+ /// Wraps a thick function with arity 3.
410
+ struct ThickFunction3 {
411
+ var function : ( Int , String , AnyObject ? ) -> ( )
412
+ }
413
+
414
+ struct ThickFunctionParts {
415
+ var function : UnsafePointer < Void >
416
+ var context : Optional < UnsafePointer < Void > >
417
+ }
418
+
394
419
/// Reflect a closure context. The given function must be a Swift-native
395
420
/// @convention(thick) function value.
396
421
public func reflect( function: ( ) -> ( ) ) {
397
- struct ThickFunction {
398
- var function : ( ) -> ( )
399
- }
422
+ let fn = UnsafeMutablePointer < ThickFunction0 > (
423
+ allocatingCapacity : sizeof ( ThickFunction0 . self ) )
424
+ fn . initialize ( with : ThickFunction0 ( function : function ) )
400
425
401
- struct ThickFunctionParts {
402
- var function : UnsafePointer < Void >
403
- var context : Optional < UnsafePointer < Void > >
404
- }
426
+ let parts = unsafeBitCast ( fn, to: UnsafePointer< ThickFunctionParts> . self )
427
+ let contextPointer = unsafeBitCast ( parts. pointee. context, to: UInt . self)
428
+
429
+ reflect ( instanceAddress: contextPointer, kind: . Object)
430
+
431
+ fn. deallocateCapacity ( sizeof ( ThickFunction0 . self) )
432
+ }
433
+
434
+ /// Reflect a closure context. The given function must be a Swift-native
435
+ /// @convention(thick) function value.
436
+ public func reflect( function: ( Int ) -> ( ) ) {
437
+ let fn = UnsafeMutablePointer < ThickFunction1 > (
438
+ allocatingCapacity: sizeof ( ThickFunction1 . self) )
439
+ fn. initialize ( with: ThickFunction1 ( function: function) )
440
+
441
+ let parts = unsafeBitCast ( fn, to: UnsafePointer< ThickFunctionParts> . self )
442
+ let contextPointer = unsafeBitCast ( parts. pointee. context, to: UInt . self)
443
+
444
+ reflect ( instanceAddress: contextPointer, kind: . Object)
445
+
446
+ fn. deallocateCapacity ( sizeof ( ThickFunction1 . self) )
447
+ }
448
+
449
+ /// Reflect a closure context. The given function must be a Swift-native
450
+ /// @convention(thick) function value.
451
+ public func reflect( function: ( Int , String ) -> ( ) ) {
452
+ let fn = UnsafeMutablePointer < ThickFunction2 > (
453
+ allocatingCapacity: sizeof ( ThickFunction2 . self) )
454
+ fn. initialize ( with: ThickFunction2 ( function: function) )
455
+
456
+ let parts = unsafeBitCast ( fn, to: UnsafePointer< ThickFunctionParts> . self )
457
+ let contextPointer = unsafeBitCast ( parts. pointee. context, to: UInt . self)
458
+
459
+ reflect ( instanceAddress: contextPointer, kind: . Object)
460
+
461
+ fn. deallocateCapacity ( sizeof ( ThickFunction2 . self) )
462
+ }
405
463
406
- let fn = UnsafeMutablePointer < ThickFunction > (
407
- allocatingCapacity: sizeof ( ThickFunction . self) )
408
- fn. initialize ( with: ThickFunction ( function: function) )
464
+ /// Reflect a closure context. The given function must be a Swift-native
465
+ /// @convention(thick) function value.
466
+ public func reflect( function: ( Int , String , AnyObject ? ) -> ( ) ) {
467
+ let fn = UnsafeMutablePointer < ThickFunction3 > (
468
+ allocatingCapacity: sizeof ( ThickFunction3 . self) )
469
+ fn. initialize ( with: ThickFunction3 ( function: function) )
409
470
410
471
let parts = unsafeBitCast ( fn, to: UnsafePointer< ThickFunctionParts> . self )
411
472
let contextPointer = unsafeBitCast ( parts. pointee. context, to: UInt . self)
412
473
413
474
reflect ( instanceAddress: contextPointer, kind: . Object)
414
475
415
- fn. deallocateCapacity ( sizeof ( ThickFunction . self) )
476
+ fn. deallocateCapacity ( sizeof ( ThickFunction3 . self) )
416
477
}
417
478
418
479
/// Call this function to indicate to the parent that there are
0 commit comments