XCTLSwiftInvocation.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. let type = self.invocation.typeEncodingForArgument(at: id)
  21. switch type {
  22. case "c":
  23. guard let ch = (it as? String)?.utf8.first else {
  24. throw XCTLRuntimeError.callingTypeEncodingError
  25. }
  26. self.invocation.setArgument_c(CChar(ch), at: id)
  27. break
  28. case "s":
  29. guard let double = it as? Double else {
  30. throw XCTLRuntimeError.callingTypeEncodingError
  31. }
  32. self.invocation.setArgument_s(Int16(double), at: id)
  33. break
  34. case "i":
  35. guard let double = it as? Double else {
  36. throw XCTLRuntimeError.callingTypeEncodingError
  37. }
  38. self.invocation.setArgument_i(Int32(double), at: id)
  39. break
  40. case "q":
  41. guard let double = it as? Double else {
  42. throw XCTLRuntimeError.callingTypeEncodingError
  43. }
  44. self.invocation.setArgument_q(Int64(double), at: id)
  45. break
  46. case "C":
  47. guard let ch = (it as? String)?.utf8.first else {
  48. throw XCTLRuntimeError.callingTypeEncodingError
  49. }
  50. self.invocation.setArgument_C(UInt8(ch), at: id)
  51. break
  52. case "S":
  53. guard let double = it as? Double else {
  54. throw XCTLRuntimeError.callingTypeEncodingError
  55. }
  56. self.invocation.setArgument_S(UInt16(double), at: id)
  57. break
  58. case "I":
  59. guard let double = it as? Double else {
  60. throw XCTLRuntimeError.callingTypeEncodingError
  61. }
  62. self.invocation.setArgument_I(UInt32(double), at: id)
  63. break
  64. case "Q":
  65. guard let double = it as? Double else {
  66. throw XCTLRuntimeError.callingTypeEncodingError
  67. }
  68. self.invocation.setArgument_Q(UInt64(double), at: id)
  69. break
  70. case "L":
  71. guard let double = it as? Double else {
  72. throw XCTLRuntimeError.callingTypeEncodingError
  73. }
  74. self.invocation.setArgument_L(UInt(double), at: id)
  75. break
  76. case "f":
  77. guard let double = it as? Double else {
  78. throw XCTLRuntimeError.callingTypeEncodingError
  79. }
  80. self.invocation.setArgument_f(Float(double), at: id)
  81. break
  82. case "d":
  83. guard let double = it as? Double else {
  84. throw XCTLRuntimeError.callingTypeEncodingError
  85. }
  86. self.invocation.setArgument_d(double, at: id)
  87. break
  88. case "B":
  89. guard let bool = it as? Bool else {
  90. throw XCTLRuntimeError.callingTypeEncodingError
  91. }
  92. self.invocation.setArgument_B(bool, at: id)
  93. break
  94. case "*":
  95. guard let string = it as? String else {
  96. throw XCTLRuntimeError.callingTypeEncodingError
  97. }
  98. self.invocation.setArgument_star(string, at: id)
  99. break
  100. case "@":
  101. guard let object = it as? NSObject else {
  102. throw XCTLRuntimeError.callingTypeEncodingError
  103. }
  104. self.invocation.setArgument_at(object, at: id)
  105. break
  106. default:
  107. throw XCTLRuntimeError.unknownTypeEncoding(name: type)
  108. }
  109. id += 1
  110. }
  111. self.invocation.invoke()
  112. switch self.invocation.methodReturnType() {
  113. case "v":
  114. return NSNull()
  115. case "c":
  116. return self.invocation.getReturnValue_c()
  117. case "s":
  118. return self.invocation.getReturnValue_s()
  119. case "i":
  120. return self.invocation.getReturnValue_i()
  121. case "q":
  122. return self.invocation.getReturnValue_q()
  123. case "C":
  124. return self.invocation.getReturnValue_C()
  125. case "S":
  126. return self.invocation.getReturnValue_S()
  127. case "I":
  128. return self.invocation.getReturnValue_I()
  129. case "Q":
  130. return self.invocation.getReturnValue_Q()
  131. case "L":
  132. return self.invocation.getReturnValue_L()
  133. case "f":
  134. return self.invocation.getReturnValue_F()
  135. case "d":
  136. return self.invocation.getReturnValue_D()
  137. case "B":
  138. return self.invocation.getReturnValue_B()
  139. case "*":
  140. return self.invocation.getReturnValue_star()
  141. case "@":
  142. return self.invocation.getReturnValue_at()
  143. default:
  144. throw XCTLRuntimeError.unknownTypeEncoding(name: self.invocation.methodReturnType())
  145. }
  146. }
  147. }