-
Notifications
You must be signed in to change notification settings - Fork 3
/
objc.nim
executable file
·660 lines (526 loc) · 23.5 KB
/
objc.nim
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
const OBJC_LIB_NAME* = "libobjc.A.dylib"
{.pragma: objcimport, cdecl, importc, dynlib: OBJC_LIB_NAME.}
{.pragma: objccallback, cdecl.}
when defined(cpu64):
type
CGFloat* = cdouble
NSInteger* = clong
NSUInteger* = culong
else:
type
CGFloat* = cfloat
NSInteger* = cint
NSUInteger* = cuint
type
Class* = distinct pointer
Method* = distinct pointer
Ivar* = distinct pointer
Category* = distinct pointer
Protocol* = distinct pointer
ID* = distinct pointer
SEL* = distinct pointer
STR* = ptr cchar
arith_t* = cint
uarith_t* = cuint
ptrdiff_t* = int
BOOL* = cchar
objc_method_description = object
name: SEL
types: cstring
MethodDescription* = object
name*: SEL
types*: string
Property* = distinct pointer
ObjcSuper* = object
receiver*: ID
superClass*: Class
objc_property_attribute_t = object
name: cstring
value: cstring
PropertyAttribute* = object
name*: string
value*: string
objc_exception_functions_t* = object
version: cint
throw_exc: proc(id: ID) {.objccallback.}
try_enter: proc(p: pointer) {.objccallback.}
try_exit: proc(p: pointer) {.objccallback.}
extract: proc(p: pointer): ID {.objccallback.}
match: proc(class: Class, id: ID): cint {.objccallback.}
IMP* = proc(id: ID, selector: SEL): ID {.cdecl, varargs.}
objc_AssociationPolicy* {.size: sizeof(cuint).} = enum
OBJC_ASSOCIATION_ASSIGN = 0
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1
OBJC_ASSOCIATION_COPY_NONATOMIC = 3
OBJC_ASSOCIATION_RETAIN = 01401
OBJC_ASSOCIATION_COPY = 01403
const
YES* = cchar(1)
NO* = cchar(0)
proc isNil*(a: Class): bool =
result = a.pointer == nil
proc isNil*(a: ID): bool =
result = a.pointer == nil
proc c_free(p: pointer) {.importc: "free", header: "<stdlib.h>".}
proc class_getName(cls: Class): cstring {.objcimport.}
proc getName*(cls: Class): string =
result = $class_getName(cls)
proc `$`*(cls: Class): string =
getName(cls)
proc class_getSuperclass(cls: Class): Class {.objcimport.}
template getSuperclass*(cls: Class): untyped =
class_getSuperClass(cls)
proc class_isMetaClass(cls: Class): BOOL {.objcimport.}
template isMetaClass*(cls: Class): untyped =
class_isMetaClass(cls)
proc class_getInstanceSize(cls: Class): csize {.objcimport.}
proc getInstanceSize*(cls: Class): int = class_getInstanceSize(cls)
proc class_getInstanceVariable(cls: Class; name: cstring): Ivar {.objcimport.}
template getIvar*(cls: Class, name: string): untyped =
class_getInstanceVariable(cls, name.cstring)
proc class_getClassVariable(cls: Class; name: cstring): Ivar {.objcimport.}
template getClassVariable*(cls: Class; name: string): untyped =
class_getClassVariable(cls, name.cstring)
proc class_addIvar(cls: Class; name: cstring; size: csize; alignment: uint8; types: cstring): BOOL {.objcimport.}
proc addIvar*(cls: Class; name: string; size: int; alignment: int; types: string): bool =
class_addIvar(cls, name.cstring, size.csize, alignment.uint8, types.cstring) == YES
proc class_copyIvarList(cls: Class; outCount: var cuint): ptr Ivar {.objcimport.}
proc ivarList*(cls: Class): seq[Ivar] =
var
count = 0.cuint
ivars = class_copyIvarList(cls, count)
if count == 0:
result = @[]
return result
result = newSeq[Ivar](count)
copyMem(result[0].addr, ivars, sizeof(Ivar) * count.int)
c_free(ivars)
proc class_getIvarLayout*(cls: Class): ptr uint8 {.objcimport.}
proc class_getWeakIvarLayout*(cls: Class): ptr uint8 {.objcimport.}
proc class_setIvarLayout*(cls: Class; layout: ptr uint8) {.objcimport.}
proc class_setWeakIvarLayout*(cls: Class; layout: ptr uint8) {.objcimport.}
proc class_getProperty(cls: Class; name: cstring): Property {.objcimport.}
template getProperty*(cls: Class; name: string): untyped =
class_getProperty(cls, name.cstring)
proc class_copyPropertyList*(cls: Class, outCount: var cuint): ptr Property {.objcimport.}
proc propertyList*(cls: Class): seq[Property] =
var
count = 0.cuint
props = class_copyPropertyList(cls, count)
if count == 0:
result = @[]
return result
result = newSeq[Property](count)
copyMem(result[0].addr, props, sizeof(Property) * count.int)
c_free(props)
proc class_addMethod(cls: Class; name: SEL; imp: IMP; types: cstring): BOOL {.objcimport.}
template addMethod*(cls: Class; name: SEL; imp: IMP; types: string): untyped =
class_addMethod(cls, name, imp, types.cstring)
proc class_getInstanceMethod(cls: Class; name: SEL): Method {.objcimport.}
template getInstanceMethod*(cls: Class; name: SEL): untyped =
class_getInstanceMethod(cls, name)
proc class_getClassMethod(cls: Class; name: SEL): Method {.objcimport.}
template getClassMethod*(cls: Class; name: SEL): untyped =
class_getClassMethod(cls, name)
proc class_copyMethodList(cls: Class; outCount: var cuint): ptr Method {.objcimport.}
proc methodList*(cls: Class): seq[Method] =
var
count = 0.cuint
procs = class_copyMethodList(cls, count)
if count == 0:
result = @[]
return result
result = newSeq[Method](count)
copyMem(result[0].addr, procs, sizeof(Method) * count.int)
c_free(procs)
proc class_replaceMethod(cls: Class; name: SEL; imp: IMP; types: cstring): IMP {.objcimport.}
template replaceMethod*(cls: Class; name: SEL; imp: IMP; types: string): untyped =
class_replaceMethod(cls, name, imp, types.cstring)
proc class_getMethodImplementation(cls: Class; name: SEL): IMP {.objcimport.}
template getMethodImplementation*(cls: Class; name: SEL): untyped =
class_getMethodImplementation(cls, name)
proc class_getMethodImplementation_stret*(cls: Class; name: SEL): IMP {.objcimport.}
proc class_respondsToSelector(cls: Class; sel: SEL): BOOL {.objcimport.}
template respondsToSelector*(cls: Class; sel: SEL): untyped =
class_respondsToSelector(cls, sel)
proc class_addProtocol(cls: Class; protocol: Protocol): BOOL {.objcimport.}
template addProtocol*(cls: Class; protocol: Protocol): untyped =
class_addProtocol(cls, protocol)
proc class_addProperty(cls: Class; name: cstring;
attributes: ptr objc_property_attribute_t;
attributeCount: cuint): BOOL {.objcimport.}
proc addProperty*(cls: Class; name: string; attributes: openArray[objc_property_attribute_t]): bool =
class_addProperty(cls, name.cstring, attributes[0].unsafeAddr, attributes.len.cuint) == YES
proc class_replaceProperty(cls: Class; name: cstring;
attributes: ptr objc_property_attribute_t;
attributeCount: cuint) {.objcimport.}
proc replaceProperty*(cls: Class; name: string; attributes: openArray[objc_property_attribute_t]) =
class_replaceProperty(cls, name.cstring, attributes[0].unsafeAddr, attributes.len.cuint)
proc class_conformsToProtocol(cls: Class; protocol: Protocol): BOOL {.objcimport.}
template conformsToProtocol*(cls: Class; protocol: Protocol): bool =
class_conformsToProtocol(cls, protocol) == YES
proc class_copyProtocolList(cls: Class; outCount: var cuint): ptr Protocol {.objcimport.}
proc protocolList*(cls: Class): seq[Protocol] =
var
count = 0.cuint
prots = class_copyProtocolList(cls, count)
if count == 0:
result = @[]
return result
result = newSeq[Protocol](count)
copyMem(result[0].addr, prots, sizeof(Protocol) * count.int)
c_free(prots)
proc class_getVersion(cls: Class): cint {.objcimport.}
template getVersion*(cls: Class): untyped =
class_getVersion(cls).int
proc class_setVersion(cls: Class; version: cint) {.objcimport.}
template setVersion*(cls: Class; version: int) =
class_setVersion(cls, version.cint)
proc objc_getFutureClass(name: cstring): Class {.objcimport.}
template getFutureClass*(name: string): untyped =
objc_getFutureClass(name.cstring)
proc objc_allocateClassPair(superclass: Class, name: cstring, extraBytes: csize): Class {.objcimport.}
template allocateClassPair*(superclass: Class, name: string, extraBytes: int): untyped =
objc_allocateClassPair(superclass, name.cstring, extrabytes.csize)
proc objc_disposeClassPair(cls: Class) {.objcimport.}
template disposeClassPair*(cls: Class) =
objc_disposeClassPair(cls)
proc objc_registerClassPair(cls: Class) {.objcimport.}
template registerClassPair*(cls: Class) =
objc_registerClassPair(cls)
proc objc_duplicateClass(original: Class; name: cstring; extraBytes: csize): Class {.objcimport.}
template duplicateClass*(original: Class; name: string; extraBytes: int): untyped =
objc_duplicateClass(original, name.cstring, extraBytes.csize)
proc class_createInstance(cls: Class; extraBytes: csize): ID {.objcimport.}
template createInstance*(cls: Class; extraBytes: csize): untyped =
class_createInstance(cls, extraBytes.csize)
proc objc_constructInstance(cls: Class; bytes: pointer): ID {.objcimport.}
template constructInstance*(cls: Class; bytes: pointer): untyped =
objc_constructInstance(cls, bytes)
proc objc_destructInstance(obj: ID): pointer {.objcimport.}
template destructInstance*(obj: ID): untyped =
objc_destructInstance(obj)
proc object_copy(obj: ID; size: csize): ID {.objcimport.}
template copy*(obj: ID; size: csize): untyped =
object_copy(obj, size.csize)
proc object_dispose(obj: ID): ID {.objcimport.}
template dispose*(obj: ID): untyped =
object_dispose(obj)
proc object_setInstanceVariable(obj: ID; name: cstring; value: pointer): Ivar {.objcimport.}
template setInstanceVariable*(obj: ID; name: string; value: pointer): untyped =
object_setInstanceVariable(obj, name.cstring, value)
proc object_getInstanceVariable(obj: ID; name: cstring; outValue: var pointer): Ivar {.objcimport.}
template getInstanceVariable*(obj: ID; name: string; outValue: var pointer): untyped =
object_getInstanceVariable(obj, name.cstring, outValue)
proc object_getIndexedIvars(obj: ID): pointer {.objcimport.}
template getIndexedIvars*(obj: ID): untyped =
object_getIndexedIvars(obj)
proc object_getIvar(obj: ID; ivar: Ivar): ID {.objcimport.}
template getIvar*(obj: ID; ivar: Ivar): untyped =
object_getIvar(obj, ivar)
proc object_setIvar(obj: ID; ivar: Ivar; value: ID) {.objcimport.}
template setIvar*(obj: ID; ivar: Ivar; value: ID) =
object_setIvar(obj, ivar, value)
proc object_getClassName(obj: ID): cstring {.objcimport.}
proc getClassName*(obj: ID): string =
result = $object_getClassName(obj)
proc objc_getClass(name: cstring): Class {.objcimport.}
template getClass*(name: string): untyped =
objc_getClass(name.cstring)
proc object_setClass(obj: ID; cls: Class): Class {.objcimport.}
template setClass*(obj: ID; cls: Class): untyped =
object_setClass(obj, cls)
proc objc_getClassList(buffer: ptr Class; bufferCount: cint): cint {.objcimport.}
proc getClassList*(): seq[Class] =
let count = objc_getClassList(nil, 0.cint)
if count == 0:
result = @[]
return result
result = newSeq[Class](count)
discard objc_getClassList(result[0].addr, result.len.cint)
proc objc_copyClassList(outCount: var cuint): ptr Class {.objcimport.}
proc copyClassList*(): seq[Class] =
var
count = 0.cuint
classes = objc_copyClassList(count)
if count == 0:
result = @[]
return result
result = newSeq[Class](count)
copyMem(result[0].addr, classes, sizeof(Class) * count.int)
c_free(classes)
proc objc_lookUpClass(name: cstring): Class {.objcimport.}
template lookUpClass*(name: cstring): untyped =
objc_lookUpClass(name.cstring)
proc object_getClass(obj: ID): Class {.objcimport.}
template getClass*(obj: ID): untyped =
object_getClass(obj)
proc objc_getRequiredClass(name: cstring): Class {.objcimport.}
template getRequiredClass*(name: string): untyped =
objc_getRequiredClass(name.cstring)
proc objc_getMetaClass(name: cstring): Class {.objcimport.}
template getMetaClass*(name: string): untyped =
objc_getMetaClass(name.cstring)
proc ivar_getName(v: Ivar): cstring {.objcimport.}
template getName*(v: Ivar): untyped =
$ivar_getName(v)
proc `$`*(v: Ivar): string =
getName(v)
proc ivar_getTypeEncoding(v: Ivar): cstring {.objcimport.}
template getTypeEncoding*(v: Ivar): untyped =
$ivar_getTypeEncoding(v)
proc ivar_getOffset(v: Ivar): ptrdiff_t {.objcimport.}
template getOffset*(v: Ivar): untyped =
ivar_getOffset(v)
proc objc_setAssociatedObject(obj: ID; key: pointer; value: ID; policy: objc_AssociationPolicy) {.objcimport.}
template setAssociatedObject*(obj: ID; key: pointer; value: ID; policy: objc_AssociationPolicy) =
objc_setAssociatedObject(obj, key, value, policy)
proc objc_getAssociatedObject(obj: ID; key: pointer): ID {.objcimport.}
template getAssociatedObject*(obj: ID; key: pointer): untyped =
objc_getAssociatedObject(obj, key)
proc objc_removeAssociatedObjects(obj: ID) {.objcimport.}
template removeAssociatedObjects*(obj: ID) =
objc_removeAssociatedObjects(obj)
proc objc_msgSend*(self: ID; op: SEL): ID {.objcimport,varargs.}
proc objc_msgSend_fpret*(self: ID; op: SEL): cdouble {.objcimport,varargs.}
proc objc_msgSend_stret*(self: ID; op: SEL) {.objcimport,varargs.}
proc objc_msgSendSuper*(super: var ObjcSuper; op: SEL): ID {.objcimport,varargs.}
proc objc_msgSendSuper_stret*(super: var ObjcSuper; op: SEL) {.objcimport,varargs.}
proc method_invoke*(receiver: ID; m: Method): ID {.objcimport,varargs.}
proc method_invoke_stret*(receiver: ID; m: Method) {.objcimport,varargs.}
proc sel_getName*(sel: SEL): cstring {.objcimport.}
template getName*(sel: SEL): untyped =
$sel_getName(sel)
proc `$`*(sel: SEL): string =
getName(sel)
proc sel_registerName(str: cstring): SEL {.objcimport.}
template registerName*(str: string): untyped =
sel_registerName(str.cstring)
proc `$$`*(str: string): SEL =
sel_registerName(str.cstring)
proc sel_getUid(str: cstring): SEL {.objcimport.}
template getUid*(str: string): untyped =
sel_getUid(str.cstring)
proc sel_isEqual(lhs: SEL; rhs: SEL): BOOL {.objcimport.}
template isEqual*(lhs, rhs: SEL): untyped =
sel_isEqual(lhs, rhs)
proc method_getName(m: Method): SEL {.objcimport.}
template getName*(m: Method): untyped =
$method_getName(m)
proc `$`*(m: Method): string =
getName(m)
proc method_getImplementation(m: Method): IMP {.objcimport.}
template getImplementation*(m: Method): untyped =
method_getImplementation(m)
proc method_getTypeEncoding(m: Method): cstring {.objcimport.}
template getTypeEncoding*(m: Method): untyped =
$method_getTypeEncoding(m)
proc method_copyReturnType(m: Method): cstring {.objcimport.}
proc copyReturnType*(m: Method): string =
var ret = method_copyReturnType(m)
result = $ret
c_free(ret)
proc method_copyArgumentType(m: Method; index: cuint): cstring {.objcimport.}
proc copyArgumentType*(m: Method; index: int): string =
var ret = method_copyArgumentType(m, index.cuint)
result = $ret
c_free(ret)
proc method_getReturnType(m: Method; dst: cstring; dst_len: csize) {.objcimport.}
proc getReturnType*(m: Method): string =
var ret: array[100, char]
method_getReturnType(m, cast[cstring](ret[0].addr), sizeof(ret))
result = $(cast[cstring](ret[0].addr))
proc method_getNumberOfArguments(m: Method): cuint {.objcimport.}
template getNumberOfArguments*(m: Method): untyped =
method_getNumberOfArguments(m).int
proc method_getArgumentType(m: Method; index: cuint; dst: cstring; dst_len: csize) {.objcimport.}
proc getArgumentType*(m: Method; index: int): string =
var ret: array[100, char]
method_getArgumentType(m, index.cuint, cast[cstring](ret[0].addr), sizeof(ret))
result = $(cast[cstring](ret[0].addr))
proc argumentTypes*(m: Method): seq[string] =
let count = getNumberOfArguments(m)
result = newSeq[string](count)
if count == 0:
result = @[]
return result
for i in 0.. <count:
result[i] = getArgumentType(m, i)
proc method_getDescription(m: Method): ptr objc_method_description {.objcimport.}
proc getDescription*(m: Method): MethodDescription =
var p = method_getDescription(m)
result.name = p.name
result.types = $p.types
proc method_setImplementation(m: Method; imp: IMP): IMP {.objcimport.}
template setImplementation*(m: Method; imp: IMP): untyped =
method_setImplementation(m, imp)
proc method_exchangeImplementations(m1: Method; m2: Method) {.objcimport.}
template exchangeImplementations*(m1: Method; m2: Method) =
method_exchangeImplementations(m1, m2)
proc objc_copyImageNames(outCount: var cuint): cstringArray {.objcimport.}
proc imageNames*(): seq[string] =
var
count = 0.cuint
images = objc_copyImageNames(count)
if count == 0:
result = @[]
return result
result = newSeq[string](count.int)
for i in 0.. <result.len:
result[i] = $images[i]
proc class_getImageName(cls: Class): cstring {.objcimport.}
template getImageName*(cls: Class): untyped =
$class_getImageName(cls)
proc objc_copyClassNamesForImage(image: cstring; outCount: var cuint): cstringArray {.objcimport.}
proc classNamesForImage*(image: string): seq[string] =
var
count = 0.cuint
classes = objc_copyClassNamesForImage(image.cstring, count)
if count == 0:
result = @[]
return result
result = newSeq[string](count.int)
for i in 0.. <result.len:
result[i] = $classes[i]
proc objc_getProtocol(name: cstring): Protocol {.objcimport.}
template getProtocol*(name: string): untyped =
objc_getProtocol(name.cstring)
proc objc_copyProtocolList(outCount: var cuint): ptr Protocol {.objcimport.}
proc protocolList*(): seq[Protocol] =
var
count = 0.cuint
prots = objc_copyProtocolList(count)
if count == 0:
result = @[]
return result
result = newSeq[Protocol](count.int)
copyMem(result[0].addr, prots, result.len * sizeof(Protocol))
c_free(prots)
proc objc_allocateProtocol(name: cstring): Protocol {.objcimport.}
template allocateProtocol*(name: string): untyped =
objc_allocateProtocol(name.cstring)
proc objc_registerProtocol*(proto: Protocol) {.objcimport.}
template registerProtocol*(proto: Protocol) =
objc_registerProtocol(proto)
proc protocol_addMethodDescription(proto: Protocol; name: SEL; types: cstring;
isRequiredMethod, isInstanceMethod: BOOL) {.objcimport.}
template addMethodDescription*(proto: Protocol; name: SEL; types: string;
isRequiredMethod, isInstanceMethod: BOOL) =
protocol_addMethodDescription(proto, name, types.cstring, isRequiredMethod, isInstanceMethod)
proc protocol_addProtocol(proto, addition: Protocol) {.objcimport.}
template addProtocol*(proto, addition: Protocol) =
protocol_addProtocol(proto, addition)
proc protocol_addProperty(proto: Protocol; name: cstring;
attributes: ptr objc_property_attribute_t;
attributeCount: cuint; isRequiredProperty: BOOL;
isInstanceProperty: BOOL) {.objcimport.}
proc addProperty*(proto: Protocol; name: string; attributes: openArray[objc_property_attribute_t],
isRequiredProperty, isInstanceProperty: BOOL) =
protocol_addProperty(proto, name, attributes[0].unsafeAddr, attributes.len.cuint,
isRequiredProperty, isInstanceProperty)
proc protocol_getName(p: Protocol): cstring {.objcimport.}
template getName*(p: Protocol): untyped =
$protocol_getName(p)
proc `$`*(p: Protocol): string =
getName(p)
proc protocol_isEqual(proto, other: Protocol): BOOL {.objcimport.}
template isEqual*(proto, other: Protocol): untyped =
protocol_isEqual(proto, other)
proc protocol_copyMethodDescriptionList(p: Protocol; isRequiredMethod, isInstanceMethod: BOOL;
outCount: var cuint): ptr objc_method_description {.objcimport.}
proc methodDescriptionList*(p: Protocol; isRequiredMethod, isInstanceMethod: BOOL): seq[MethodDescription] =
type
DescT {.unchecked.} = array[0..0, objc_method_description]
var
count = 0.cuint
raw = protocol_copyMethodDescriptionList(p, isRequiredMethod, isInstanceMethod, count)
descs = cast[DescT](raw)
if count == 0:
result = @[]
return result
result = newSeq[MethodDescription](count.int)
for i in 0.. <count.int:
result[i] = MethodDescription(name: descs[i].name, types: $descs[i].types)
c_free(raw)
proc protocol_getMethodDescription(p: Protocol; aSel: SEL;
isRequiredMethod, isInstanceMethod: BOOL): objc_method_description {.objcimport.}
template getMethodDescription*(p: Protocol; aSel: SEL; isRequiredMethod, isInstanceMethod: BOOL): untyped =
protocol_getMethodDescription(p, aSel, isRequiredMethod, isInstanceMethod)
proc protocol_copyPropertyList(proto: Protocol; outCount: var cuint): ptr Property {.objcimport.}
proc propertyList*(proto: Protocol): seq[Property] =
var
count = 0.cuint
props = protocol_copyPropertyList(proto, count)
if count == 0:
result = @[]
return result
result = newSeq[Property](count.int)
copyMem(result[0].addr, props, result.len * sizeof(Property))
c_free(props)
proc protocol_getProperty(proto: Protocol; name: cstring; isRequiredProperty, isInstanceProperty: BOOL): Property {.objcimport.}
template getProperty*(proto: Protocol; name: string; isRequiredProperty, isInstanceProperty: BOOL): untyped =
protocol_getProperty(proto, name.cstring, isRequiredProperty, isInstanceProperty)
proc protocol_copyProtocolList*(proto: Protocol, outCount: var cuint): ptr Protocol {.objcimport.}
proc protocolList*(proto: Protocol): seq[Protocol] =
var
count = 0.cuint
prots = protocol_copyProtocolList(proto, count)
if count == 0:
result = @[]
return result
result = newSeq[Protocol](count.int)
copyMem(result[0].addr, prots, result.len * sizeof(Protocol))
c_free(prots)
proc protocol_conformsToProtocol(proto, other: Protocol): BOOL {.objcimport.}
template conformsToProtocol*(proto, other: Protocol): untyped =
protocol_conformsToProtocol(proto, other)
proc property_getName(property: Property): cstring {.objcimport.}
template getName*(property: Property): untyped =
$property_getName(property)
proc `$`*(property: Property): string =
getName(property)
proc property_getAttributes(property: Property): cstring {.objcimport.}
template getAttributes*(property: Property): untyped =
$property_getAttributes(property)
proc property_copyAttributeList(property: Property; outCount: var cuint): ptr objc_property_attribute_t {.objcimport.}
proc attributeList*(property: Property): seq[PropertyAttribute] =
type AttrT {.unchecked.} = array[0..0, objc_property_attribute_t]
var
count = 0.cuint
raw = property_copyAttributeList(property, count)
attrs = cast[AttrT](raw)
if count == 0:
result = @[]
return result
result = newSeq[PropertyAttribute](count.int)
for i in 0.. <count.int:
result[i] = PropertyAttribute(name: $attrs[i].name, value: $attrs[i].value)
c_free(raw)
proc property_copyAttributeValue(property: Property; attributeName: cstring): cstring {.objcimport.}
proc attributeValue*(property: Property; attributeName: string): string =
var res = property_copyAttributeValue(property, attributeName.cstring)
result = $res
c_free(res)
proc objc_enumerationMutation(obj: ID) {.objcimport.}
template enumerationMutation*(obj: ID) =
objc_enumerationMutation(obj)
type
EnumerationHandler = proc(a2: ID) {.objccallback.}
proc objc_setEnumerationMutationHandler(handler: EnumerationHandler) {.objcimport.}
template setEnumerationMutationHandler*(handler: EnumerationHandler) =
objc_setEnumerationMutationHandler(handler)
proc imp_implementationWithBlock(blok: ID): IMP {.objcimport.}
template implementationWithBlock*(blok: ID): untyped =
imp_implementationWithBlock(blok)
proc imp_getBlock(anImp: IMP): ID {.objcimport.}
template getBlock*(anImp: IMP): untyped =
imp_getBlock(anImp)
proc imp_removeBlock(anImp: IMP): BOOL {.objcimport.}
template removeBlock*(anImp: IMP): untyped =
imp_removeBlock(anImp)
proc objc_loadWeak(location: var ID): ID {.objcimport.}
template loadWeak*(location: var ID): untyped =
objc_loadWeak(location)
proc objc_storeWeak(location: var ID; obj: ID): ID {.objcimport.}
template storeWeak*(location: var ID; obj: ID): untyped =
objc_storeWeak(location, obj)