Skip to content

Commit

Permalink
feature: add void return type
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Jan 31, 2024
1 parent 42fb64e commit 05f13e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 12 deletions.
42 changes: 36 additions & 6 deletions Source/AppleObjectiveCReflection/NSObjectClassMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension NSObjectClass {
@discardableResult
public func call<Return>(
_ classMethod: NSObjectClassMethod,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -52,6 +52,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector)
return () as? Return
}
}

Expand All @@ -60,7 +65,7 @@ extension NSObjectClass {
public func call<Return>(
_ classMethod: NSObjectClassMethod,
with argument0: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -77,6 +82,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector, argument0)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector, argument0)
return () as? Return
}
}

Expand All @@ -86,7 +96,7 @@ extension NSObjectClass {
_ classMethod: NSObjectClassMethod,
with argument0: Any?,
with argument1: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -103,6 +113,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector, argument0, argument1)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector, argument0, argument1)
return () as? Return
}
}

Expand All @@ -113,7 +128,7 @@ extension NSObjectClass {
with argument0: Any?,
with argument1: Any?,
with argument2: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -130,6 +145,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector, argument0, argument1, argument2)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector, argument0, argument1, argument2)
return () as? Return
}
}

Expand All @@ -141,7 +161,7 @@ extension NSObjectClass {
with argument1: Any?,
with argument2: Any?,
with argument3: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -158,6 +178,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector, argument0, argument1, argument2, argument3)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector, Any?, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector, argument0, argument1, argument2, argument3)
return () as? Return
}
}

Expand All @@ -170,7 +195,7 @@ extension NSObjectClass {
with argument2: Any?,
with argument3: Any?,
with argument4: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard anyClass == classMethod.anyClass else { return nil }

Expand All @@ -187,6 +212,11 @@ extension NSObjectClass {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(anyClass, classMethod.selector, argument0, argument1, argument2, argument3, argument4)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(AnyClass, Selector, Any?, Any?, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(anyClass, classMethod.selector, argument0, argument1, argument2, argument3, argument4)
return () as? Return
}
}
}
Expand Down
42 changes: 36 additions & 6 deletions Source/AppleObjectiveCReflection/NSObjectInstanceMethod.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension NSObjectInstance {
@discardableResult
public func call<Return>(
_ instanceMethod: NSObjectInstanceMethod,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -52,6 +52,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector)
return () as? Return
}
}

Expand All @@ -60,7 +65,7 @@ extension NSObjectInstance {
public func call<Return>(
_ instanceMethod: NSObjectInstanceMethod,
with argument0: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -77,6 +82,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector, argument0)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector, argument0)
return () as? Return
}
}

Expand All @@ -86,7 +96,7 @@ extension NSObjectInstance {
_ instanceMethod: NSObjectInstanceMethod,
with argument0: Any?,
with argument1: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -103,6 +113,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector, argument0, argument1)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector, argument0, argument1)
return () as? Return
}
}

Expand All @@ -113,7 +128,7 @@ extension NSObjectInstance {
with argument0: Any?,
with argument1: Any?,
with argument2: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -130,6 +145,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector, argument0, argument1, argument2)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector, argument0, argument1, argument2)
return () as? Return
}
}

Expand All @@ -141,7 +161,7 @@ extension NSObjectInstance {
with argument1: Any?,
with argument2: Any?,
with argument3: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -158,6 +178,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector, argument0, argument1, argument2, argument3)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector, Any?, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector, argument0, argument1, argument2, argument3)
return () as? Return
}
}

Expand All @@ -170,7 +195,7 @@ extension NSObjectInstance {
with argument2: Any?,
with argument3: Any?,
with argument4: Any?,
return: NSObjectReturn<Return>
return: NSObjectReturn<Return> = .void
) -> Return? {
guard object == instanceMethod.object else { return nil }

Expand All @@ -187,6 +212,11 @@ extension NSObjectInstance {
let function = unsafeBitCast(implementation, to: Function.self)
let result = function(object, instanceMethod.selector, argument0, argument1, argument2, argument3, argument4)
return cast(result, to: type)
case .void:
typealias Function = @convention(c)(NSObject, Selector, Any?, Any?, Any?, Any?, Any?)->Void
let function = unsafeBitCast(implementation, to: Function.self)
function(object, instanceMethod.selector, argument0, argument1, argument2, argument3, argument4)
return () as? Return
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Source/AppleObjectiveCReflection/NSObjectReturn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import Foundation
public enum NSObjectReturn<Actual> {
case reference(Actual.Type)
case value(Actual.Type)
case void(Actual.Type)
}

extension NSObjectReturn where Actual == Any {
public static let reference = NSObjectReturn.reference(Any.self)
}

extension NSObjectReturn where Actual == Void {
public static let void = NSObjectReturn.void(Void.self)
}

0 comments on commit 05f13e2

Please sign in to comment.