main_x64.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //go:build (darwin || freebsd || linux || windows) && (amd64 || arm64 || ppc64 || ppc64le || s390x || mips64 || mips64le)
  2. package yu_sys
  3. import (
  4. "unsafe"
  5. )
  6. func (t Address) Call(args ...any) (r1, r2, err uintptr) {
  7. if len(args) == 0 {
  8. return SyscallN(t)
  9. }
  10. j_args := make([]uintptr, 0, len(args))
  11. j_bufs := make([][]byte, 0)
  12. for _, j_arg := range args {
  13. if j_arg == nil {
  14. j_args = append(j_args, 0)
  15. continue
  16. }
  17. switch j_val := j_arg.(type) {
  18. case bool:
  19. if j_val {
  20. j_args = append(j_args, 1)
  21. } else {
  22. j_args = append(j_args, 0)
  23. }
  24. case byte:
  25. j_args = append(j_args, uintptr(j_val))
  26. case int8:
  27. j_args = append(j_args, uintptr(j_val))
  28. case int16:
  29. j_args = append(j_args, uintptr(j_val))
  30. case uint16:
  31. j_args = append(j_args, uintptr(j_val))
  32. case int32:
  33. j_args = append(j_args, uintptr(j_val))
  34. case uint32:
  35. j_args = append(j_args, uintptr(j_val))
  36. case int64:
  37. j_args = append(j_args, uintptr(j_val))
  38. case uint64:
  39. j_args = append(j_args, uintptr(j_val))
  40. case int:
  41. j_args = append(j_args, uintptr(j_val))
  42. case uint:
  43. j_args = append(j_args, uintptr(j_val))
  44. case uintptr:
  45. j_args = append(j_args, j_val)
  46. case unsafe.Pointer:
  47. j_args = append(j_args, uintptr(j_val))
  48. case *bool:
  49. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  50. case *byte:
  51. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  52. case *int8:
  53. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  54. case *int16:
  55. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  56. case *uint16:
  57. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  58. case *int32:
  59. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  60. case *uint32:
  61. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  62. case *int64:
  63. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  64. case *uint64:
  65. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  66. case *int:
  67. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  68. case *uint:
  69. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  70. case *uintptr:
  71. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  72. case *unsafe.Pointer:
  73. j_args = append(j_args, uintptr(unsafe.Pointer(j_val)))
  74. case []bool:
  75. if len(j_val) == 0 {
  76. j_args = append(j_args, 0)
  77. continue
  78. }
  79. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  80. case []byte:
  81. if len(j_val) == 0 {
  82. j_args = append(j_args, 0)
  83. continue
  84. }
  85. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  86. case []int8:
  87. if len(j_val) == 0 {
  88. j_args = append(j_args, 0)
  89. continue
  90. }
  91. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  92. case []int16:
  93. if len(j_val) == 0 {
  94. j_args = append(j_args, 0)
  95. continue
  96. }
  97. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  98. case []uint16:
  99. if len(j_val) == 0 {
  100. j_args = append(j_args, 0)
  101. continue
  102. }
  103. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  104. case []int32:
  105. if len(j_val) == 0 {
  106. j_args = append(j_args, 0)
  107. continue
  108. }
  109. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  110. case []uint32:
  111. if len(j_val) == 0 {
  112. j_args = append(j_args, 0)
  113. continue
  114. }
  115. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  116. case []int64:
  117. if len(j_val) == 0 {
  118. j_args = append(j_args, 0)
  119. continue
  120. }
  121. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  122. case []uint64:
  123. if len(j_val) == 0 {
  124. j_args = append(j_args, 0)
  125. continue
  126. }
  127. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  128. case []int:
  129. if len(j_val) == 0 {
  130. j_args = append(j_args, 0)
  131. continue
  132. }
  133. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  134. case []uint:
  135. if len(j_val) == 0 {
  136. j_args = append(j_args, 0)
  137. continue
  138. }
  139. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  140. case []uintptr:
  141. if len(j_val) == 0 {
  142. j_args = append(j_args, 0)
  143. continue
  144. }
  145. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  146. case []unsafe.Pointer:
  147. if len(j_val) == 0 {
  148. j_args = append(j_args, 0)
  149. continue
  150. }
  151. j_args = append(j_args, uintptr(unsafe.Pointer(&j_val[0])))
  152. case string:
  153. if j_val == "" {
  154. j_args = append(j_args, 0)
  155. continue
  156. }
  157. j_args = append(j_args, (*[2]uintptr)(unsafe.Pointer(&j_val))[0])
  158. case S:
  159. if j_val.Utf8 == "" {
  160. j_args = append(j_args, 0)
  161. continue
  162. }
  163. switch j_val.Code {
  164. case Gbk:
  165. j_unicode_len := len(j_val.Utf8) * 2
  166. j_unicode := make([]byte, j_unicode_len+2) // 结尾 {0,0}
  167. j_unicode_len = MultiByteToWideChar.CallInt(65001, 0, j_val.Utf8, len(j_val.Utf8), j_unicode, j_unicode_len)
  168. j_gbk_len := j_unicode_len * 2
  169. j_bufs = append(j_bufs, make([]byte, j_gbk_len+2)) // 结尾 {0,0}
  170. j_i := len(j_bufs) - 1
  171. WideCharToMultiByte.Call(936, 0, j_unicode, j_unicode_len, j_bufs[j_i], j_gbk_len, 0, 0)
  172. j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
  173. case Unicode:
  174. j_utf8_len := len(j_val.Utf8)
  175. j_unicode_len := j_utf8_len * 2
  176. j_bufs = append(j_bufs, make([]byte, j_unicode_len+2)) // 结尾 {0,0}
  177. j_i := len(j_bufs) - 1
  178. MultiByteToWideChar.Call(65001, 0, j_val.Utf8, j_utf8_len, j_bufs[j_i], j_unicode_len)
  179. j_args = append(j_args, uintptr(unsafe.Pointer(&j_bufs[j_i][0])))
  180. }
  181. default:
  182. panic("不支持的数据类型")
  183. }
  184. }
  185. return SyscallN(t, j_args...)
  186. }
  187. func (t Address) CallInt64(args ...any) int64 {
  188. j_i, _, _ := t.Call(args...)
  189. return int64(j_i)
  190. }
  191. func (t Address) CallUint64(args ...any) uint64 {
  192. j_i, _, _ := t.Call(args...)
  193. return uint64(j_i)
  194. }