XCTLSwiftInvocation.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // XCTLSwiftInvocation.swift
  3. // XCTreeLang
  4. //
  5. // Created by 邢铖 on 2023/6/3.
  6. //
  7. import Foundation
  8. import XCTLRuntimeTypeInstanceModule
  9. public class XCTLSwiftInvocation {
  10. private let invocation: XCTLInvocation
  11. public init(target: NSObject, selector: Selector) {
  12. self.invocation = XCTLInvocation(object: target, for: selector)
  13. }
  14. public func invokeMemberFunc(params: [Any?]) throws -> Any {
  15. var id = 2
  16. if self.invocation.numberOfArguments() != params.count + 2 {
  17. throw XCTLRuntimeError.paragraphArgsNotEnough(needCount: self.invocation.numberOfArguments() - 2, butGot: params.count)
  18. }
  19. for it in params {
  20. var type = self.invocation.typeEncodingForArgument(at: id)
  21. if type.count == 2, type.last == "?" {
  22. type.removeLast()
  23. }
  24. switch type {
  25. case "c":
  26. guard let ch = (it as? String)?.utf8.first else {
  27. throw XCTLRuntimeError.callingTypeEncodingError
  28. }
  29. self.invocation.setArgument_c(CChar(ch), at: id)
  30. break
  31. case "s":
  32. guard let double = it as? Double else {
  33. throw XCTLRuntimeError.callingTypeEncodingError
  34. }
  35. self.invocation.setArgument_s(Int16(double), at: id)
  36. break
  37. case "i":
  38. guard let double = it as? Double else {
  39. throw XCTLRuntimeError.callingTypeEncodingError
  40. }
  41. self.invocation.setArgument_i(Int32(double), at: id)
  42. break
  43. case "q":
  44. guard let double = it as? Double else {
  45. throw XCTLRuntimeError.callingTypeEncodingError
  46. }
  47. self.invocation.setArgument_q(Int64(double), at: id)
  48. break
  49. case "C":
  50. guard let ch = (it as? String)?.utf8.first else {
  51. throw XCTLRuntimeError.callingTypeEncodingError
  52. }
  53. self.invocation.setArgument_C(UInt8(ch), at: id)
  54. break
  55. case "S":
  56. guard let double = it as? Double else {
  57. throw XCTLRuntimeError.callingTypeEncodingError
  58. }
  59. self.invocation.setArgument_S(UInt16(double), at: id)
  60. break
  61. case "I":
  62. guard let double = it as? Double else {
  63. throw XCTLRuntimeError.callingTypeEncodingError
  64. }
  65. self.invocation.setArgument_I(UInt32(double), at: id)
  66. break
  67. case "Q":
  68. guard let double = it as? Double else {
  69. throw XCTLRuntimeError.callingTypeEncodingError
  70. }
  71. self.invocation.setArgument_Q(UInt64(double), at: id)
  72. break
  73. case "L":
  74. guard let double = it as? Double else {
  75. throw XCTLRuntimeError.callingTypeEncodingError
  76. }
  77. self.invocation.setArgument_L(UInt(double), at: id)
  78. break
  79. case "f":
  80. guard let double = it as? Double else {
  81. throw XCTLRuntimeError.callingTypeEncodingError
  82. }
  83. self.invocation.setArgument_f(Float(double), at: id)
  84. break
  85. case "d":
  86. guard let double = it as? Double else {
  87. throw XCTLRuntimeError.callingTypeEncodingError
  88. }
  89. self.invocation.setArgument_d(double, at: id)
  90. break
  91. case "B":
  92. guard let bool = it as? Bool else {
  93. throw XCTLRuntimeError.callingTypeEncodingError
  94. }
  95. self.invocation.setArgument_B(bool, at: id)
  96. break
  97. case "*":
  98. guard let string = it as? String else {
  99. throw XCTLRuntimeError.callingTypeEncodingError
  100. }
  101. self.invocation.setArgument_star(string, at: id)
  102. break
  103. case "@":
  104. guard let object = it as? NSObject else {
  105. throw XCTLRuntimeError.callingTypeEncodingError
  106. }
  107. self.invocation.setArgument_at(object, at: id)
  108. break
  109. default:
  110. throw XCTLRuntimeError.unknownTypeEncoding(name: type)
  111. }
  112. id += 1
  113. }
  114. self.invocation.invoke()
  115. switch self.invocation.methodReturnType() {
  116. case "v":
  117. return NSNull()
  118. case "c":
  119. return self.invocation.getReturnValue_c()
  120. case "s":
  121. return self.invocation.getReturnValue_s()
  122. case "i":
  123. return self.invocation.getReturnValue_i()
  124. case "q":
  125. return self.invocation.getReturnValue_q()
  126. case "C":
  127. return self.invocation.getReturnValue_C()
  128. case "S":
  129. return self.invocation.getReturnValue_S()
  130. case "I":
  131. return self.invocation.getReturnValue_I()
  132. case "Q":
  133. return self.invocation.getReturnValue_Q()
  134. case "L":
  135. return self.invocation.getReturnValue_L()
  136. case "f":
  137. return self.invocation.getReturnValue_F()
  138. case "d":
  139. return self.invocation.getReturnValue_D()
  140. case "B":
  141. return self.invocation.getReturnValue_B()
  142. case "*":
  143. return self.invocation.getReturnValue_star()
  144. case "@":
  145. return self.invocation.getReturnValue_at()
  146. default:
  147. throw XCTLRuntimeError.unknownTypeEncoding(name: self.invocation.methodReturnType())
  148. }
  149. }
  150. }