Skip to content

Commit 5285a95

Browse files
committed
Merge pull request #2661 from bitjammer/remote-mirror-closure-tests-arity
Add more Remote Mirror closure tests with different arity/capture count
2 parents d102209 + 8f86719 commit 5285a95

File tree

2 files changed

+395
-11
lines changed

2 files changed

+395
-11
lines changed

stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -391,28 +391,89 @@ public func reflect<T: ErrorProtocol>(error: T) {
391391
reflect(instanceAddress: errorPointerValue, kind: .ErrorExistential)
392392
}
393393

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+
394419
/// Reflect a closure context. The given function must be a Swift-native
395420
/// @convention(thick) function value.
396421
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))
400425

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+
}
405463

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))
409470

410471
let parts = unsafeBitCast(fn, to: UnsafePointer<ThickFunctionParts>.self)
411472
let contextPointer = unsafeBitCast(parts.pointee.context, to: UInt.self)
412473

413474
reflect(instanceAddress: contextPointer, kind: .Object)
414475

415-
fn.deallocateCapacity(sizeof(ThickFunction.self))
476+
fn.deallocateCapacity(sizeof(ThickFunction3.self))
416477
}
417478

418479
/// Call this function to indicate to the parent that there are

0 commit comments

Comments
 (0)