forked from maxence-charriere/go-objcruntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.go
261 lines (191 loc) · 6.59 KB
/
class.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package objc
// #include <stdlib.h>
// #include <objc/runtime.h>
import "C"
import "unsafe"
type Class C.Class
type Imp C.IMP
func Class_getName(cls Class) string {
cname := C.class_getName(cls)
return C.GoString(cname)
}
func Class_getSuperclass(cls Class) Class {
return Class(C.class_getSuperclass(cls))
}
func Class_isMetaClass(cls Class) bool {
return C.class_isMetaClass(cls) != 0
}
func Class_getInstanceSize(cls Class) uint {
return uint(C.class_getInstanceSize(cls))
}
func Class_getInstanceVariable(cls Class, name string) Ivar {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return Ivar(C.class_getInstanceVariable(cls, cname))
}
func Class_getClassVariable(cls Class, name string) Ivar {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return Ivar(C.class_getClassVariable(cls, cname))
}
func Class_addIvar(cls Class, name string, size uint, alignment uint8, types string) bool {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ctypes := C.CString(types)
defer C.free(unsafe.Pointer(ctypes))
return C.class_addIvar(cls, cname, C.size_t(size), C.uint8_t(alignment), ctypes) != 0
}
func Class_copyIvarList(cls Class) (ivars []Ivar) {
var coutCount C.uint
ivarList := C.class_copyIvarList(cls, &coutCount)
defer C.free(unsafe.Pointer(ivarList))
if outCount := uint(coutCount); outCount > 0 {
ivars = make([]Ivar, outCount)
for i, elem := uint(0), ivarList; i < outCount; i++ {
ivars[i] = Ivar(*elem)
elem = nextIvar(elem)
}
}
return
}
func Class_getProperty(cls Class, name string) Property {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
return Property(C.class_getProperty(cls, cname))
}
func Class_copyPropertyList(cls Class) (properties []Property) {
var coutCount C.uint
propertyList := C.class_copyPropertyList(cls, &coutCount)
defer C.free(unsafe.Pointer(propertyList))
if outCount := uint(coutCount); outCount > 0 {
properties = make([]Property, outCount)
for i, elem := uint(0), propertyList; i < outCount; i++ {
properties[i] = Property(*elem)
elem = nextProperty(elem)
}
}
return
}
func Class_addMethod(cls Class, name Sel, imp Imp, types string) bool {
ctype := C.CString(types)
defer C.free(unsafe.Pointer(ctype))
return C.class_addMethod(cls, name, imp, ctype) != 0
}
func Class_getInstanceMethod(aClass Class, aSelector Sel) Method {
return Method(C.class_getInstanceMethod(aClass, aSelector))
}
func Class_getClassMethod(aClass Class, aSelector Sel) Method {
return Method(C.class_getClassMethod(aClass, aSelector))
}
func Class_copyMethodList(cls Class) (methods []Method) {
var coutCount C.uint
methodList := C.class_copyMethodList(cls, &coutCount)
defer C.free(unsafe.Pointer(methodList))
if outCount := uint(coutCount); outCount > 0 {
methods = make([]Method, outCount)
for i, elem := uint(0), methodList; i < outCount; i++ {
methods[i] = Method(*elem)
elem = nextMethod(elem)
}
}
return
}
func Class_replaceMethod(cls Class, name Sel, imp Imp, types string) Imp {
ctype := C.CString(types)
defer C.free(unsafe.Pointer(ctype))
return Imp(C.class_replaceMethod(cls, name, imp, ctype))
}
func Class_getMethodImplementation(cls Class, name Sel) Imp {
return Imp(C.class_getMethodImplementation(cls, name))
}
func Class_getMethodImplementation_stret(cls Class, name Sel) Imp {
return Imp(C.class_getMethodImplementation_stret(cls, name))
}
func Class_respondsToSelector(cls Class, sel Sel) bool {
return C.class_respondsToSelector(cls, sel) != 0
}
func Class_addProtocol(cls Class, protocol Protocol) bool {
return C.class_addProtocol(cls, protocol) != 0
}
func Class_addProperty(cls Class, name string, attributes []PropertyAttribute) bool {
var cattributes *C.objc_property_attribute_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
attrSize := unsafe.Sizeof(*cattributes)
attributeCount := len(attributes)
if len(attributes) != 0 {
cattributes = (*C.objc_property_attribute_t)(calloc(uint(attributeCount), attrSize))
defer func(cattributes *C.objc_property_attribute_t, attributeCount int) {
for i, elem := 0, cattributes; i < attributeCount; i++ {
C.free(unsafe.Pointer(elem.name))
C.free(unsafe.Pointer(elem.value))
elem = nextPropertyAttr(elem)
}
C.free(unsafe.Pointer(cattributes))
}(cattributes, attributeCount)
for i, elem := 0, cattributes; i < attributeCount; i++ {
attr := attributes[i]
elem.name = C.CString(attr.Name)
elem.value = C.CString(attr.Value)
elem = nextPropertyAttr(elem)
}
}
return C.class_addProperty(cls, cname, cattributes, C.uint(attributeCount)) != 0
}
func Class_replaceProperty(cls Class, name string, attributes []PropertyAttribute) {
var cattributes *C.objc_property_attribute_t
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
attrSize := unsafe.Sizeof(*cattributes)
attributeCount := len(attributes)
if len(attributes) != 0 {
cattributes = (*C.objc_property_attribute_t)(calloc(uint(attributeCount), attrSize))
defer func(cattributes *C.objc_property_attribute_t, attributeCount int) {
for i, elem := 0, cattributes; i < attributeCount; i++ {
C.free(unsafe.Pointer(elem.name))
C.free(unsafe.Pointer(elem.value))
elem = nextPropertyAttr(elem)
}
C.free(unsafe.Pointer(cattributes))
}(cattributes, attributeCount)
for i, elem := 0, cattributes; i < attributeCount; i++ {
attr := attributes[i]
elem.name = C.CString(attr.Name)
elem.value = C.CString(attr.Value)
elem = nextPropertyAttr(elem)
}
}
C.class_replaceProperty(cls, cname, cattributes, C.uint(attributeCount))
}
func Class_conformsToProtocol(cls Class, protocol Protocol) bool {
return C.class_conformsToProtocol(cls, protocol) != 0
}
func Class_copyProtocolList(cls Class) (protocols []Protocol) {
var coutCount C.uint
protocolList := C.class_copyProtocolList(cls, &coutCount)
C.free(unsafe.Pointer(protocolList))
if outCount := uint(coutCount); outCount > 0 {
protocols = make([]Protocol, outCount)
for i, elem := uint(0), protocolList; i < outCount; i++ {
protocols[i] = Protocol(*elem)
elem = nextProtocol(elem)
}
}
return
}
func Class_getVersion(theClass Class) int {
return int(C.class_getVersion(theClass))
}
func Class_setVersion(theClass Class, version int) {
C.class_setVersion(theClass, C.int(version))
}
func Class_createInstance(cls Class, extraBytes uint) Id {
return Id(C.class_createInstance(cls, C.size_t(extraBytes)))
}
func Class_getImageName(cls Class) string {
return C.GoString(C.class_getImageName(cls))
}
func nextClass(list *C.Class) *C.Class {
ptr := uintptr(unsafe.Pointer(list)) + unsafe.Sizeof(*list)
return (*C.Class)(unsafe.Pointer(ptr))
}