forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfoundation-objc.mm
More file actions
1369 lines (1123 loc) · 40.3 KB
/
foundation-objc.mm
File metadata and controls
1369 lines (1123 loc) · 40.3 KB
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include <foundation.h>
#include <foundation-auto.h>
#include <foundation-objc.h>
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF MCTypeInfoRef kMCObjcObjectTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef MCObjcObjectTypeInfo() { return kMCObjcObjectTypeInfo; }
MC_DLLEXPORT_DEF MCTypeInfoRef kMCObjcIdTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef MCObjcIdTypeInfo() { return kMCObjcIdTypeInfo; }
MC_DLLEXPORT_DEF MCTypeInfoRef kMCObjcRetainedIdTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef MCObjcRetainedIdTypeInfo() { return kMCObjcRetainedIdTypeInfo; }
MC_DLLEXPORT_DEF MCTypeInfoRef kMCObjcAutoreleasedIdTypeInfo;
MC_DLLEXPORT_DEF MCTypeInfoRef MCObjcAutoreleasedIdTypeInfo() { return kMCObjcAutoreleasedIdTypeInfo; }
struct __MCObjcObjectImpl
{
id object;
};
static __MCObjcObjectImpl *__MCObjcObjectGet(MCValueRef p_obj)
{
return static_cast<__MCObjcObjectImpl *>(MCValueGetExtraBytesPtr(p_obj));
}
static void __MCObjcObjectDestroy(MCValueRef p_value)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_value);
[t_impl->object release];
}
static bool __MCObjcObjectCopy(MCValueRef p_value, bool p_release, MCValueRef &r_copy)
{
if (p_release)
r_copy = p_value;
else
r_copy = MCValueRetain(p_value);
return true;
}
static bool __MCObjcObjectEqual(MCValueRef p_left, MCValueRef p_right)
{
if (p_left == p_right)
return true;
__MCObjcObjectImpl *t_left_impl = __MCObjcObjectGet(p_left);
__MCObjcObjectImpl *t_right_impl = __MCObjcObjectGet(p_right);
return t_left_impl->object == t_right_impl->object;
}
static hash_t __MCObjcObjectHash(MCValueRef p_value)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_value);
return MCHashPointer(t_impl->object);
}
bool __MCObjcObjectDescribe(MCValueRef p_value, MCStringRef &r_desc)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_value);
return MCStringFormat(r_desc, "<objc object %p>", t_impl->object);
}
static MCValueCustomCallbacks kMCObjcObjectCustomValueCallbacks =
{
false,
__MCObjcObjectDestroy,
__MCObjcObjectCopy,
__MCObjcObjectEqual,
__MCObjcObjectHash,
__MCObjcObjectDescribe,
nullptr,
nullptr,
};
////////////////////////////////////////////////////////////////////////////////
/* The Id foreign value is a unmanaged wrapper around 'id' - basically like
* Pointer but with a bridge to ObjcObject which explicitly retains its value. */
static bool
objc_id_initialize(void *contents)
{
*(void **)contents = nullptr;
return true;
}
static bool
objc_id_defined(void *contents)
{
return *(void **)contents != nullptr;
}
static void
objc_id_finalize(void *contents)
{
}
static bool
objc_id_move(const MCForeignTypeDescriptor* desc, void *from, void *to)
{
*static_cast<void **>(to) = *static_cast<void **>(from);
return true;
}
static bool
objc_id_copy(const MCForeignTypeDescriptor* desc, void *from, void *to)
{
*static_cast<void **>(to) = *static_cast<void **>(from);
return true;
}
static bool
objc_id_equal(const MCForeignTypeDescriptor* desc, void *from, void *to, bool& r_equal)
{
r_equal = *static_cast<void **>(to) == *static_cast<void **>(to);
return true;
}
static bool
objc_id_hash(const MCForeignTypeDescriptor* desc, void *value, hash_t& r_hash)
{
r_hash = MCHashPointer(*static_cast<void **>(value));
return true;
}
static bool
objc_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
{
return MCStringFormat(r_string, "<objc id %p>", *static_cast<void **>(value));
}
static bool
objc_retained_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
{
return MCStringFormat(r_string, "<objc retained id %p>", *static_cast<void **>(value));
}
static bool
objc_autoreleased_id_describe(const MCForeignTypeDescriptor* desc, void *value, MCStringRef& r_string)
{
return MCStringFormat(r_string, "<objc autoreleased id %p>", *static_cast<void **>(value));
}
static bool
objc_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
{
MCAssert(!p_release);
return MCObjcObjectCreateWithId(*(void **)contents, (MCObjcObjectRef&)r_value);
}
static bool
objc_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
{
MCAssert(!p_release);
*(void **)contents = MCObjcObjectGetId((MCObjcObjectRef)p_value);
return true;
}
static bool
objc_retained_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
{
MCAssert(!p_release);
return MCObjcObjectCreateWithRetainedId(*(void **)contents, (MCObjcObjectRef&)r_value);
}
static bool
objc_retained_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
{
MCAssert(!p_release);
*(void **)contents = MCObjcObjectGetRetainedId((MCObjcObjectRef)p_value);
return true;
}
static bool
objc_autoreleased_id_import(const MCForeignTypeDescriptor* desc, void *contents, bool p_release, MCValueRef& r_value)
{
MCAssert(!p_release);
return MCObjcObjectCreateWithAutoreleasedId(*(void **)contents, (MCObjcObjectRef&)r_value);
}
static bool
objc_autoreleased_id_export(const MCForeignTypeDescriptor* desc, MCValueRef p_value, bool p_release, void *contents)
{
MCAssert(!p_release);
*(void **)contents = MCObjcObjectGetAutoreleasedId((MCObjcObjectRef)p_value);
return true;
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF bool MCObjcObjectCreateWithId(void *p_object, MCObjcObjectRef &r_object)
{
MCObjcObjectRef t_obj;
if (!MCValueCreateCustom(kMCObjcObjectTypeInfo,
sizeof(__MCObjcObjectImpl),
t_obj))
{
return false;
}
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(t_obj);
t_impl->object = [(id)p_object retain];
r_object = t_obj;
return true;
}
MC_DLLEXPORT_DEF bool MCObjcObjectCreateWithRetainedId(void *p_object, MCObjcObjectRef &r_object)
{
MCObjcObjectRef t_obj;
if (!MCValueCreateCustom(kMCObjcObjectTypeInfo,
sizeof(__MCObjcObjectImpl),
t_obj))
{
return false;
}
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(t_obj);
t_impl->object = (id)p_object;
r_object = t_obj;
return true;
}
MC_DLLEXPORT_DEF bool MCObjcObjectCreateWithAutoreleasedId(void *p_object, MCObjcObjectRef &r_object)
{
return MCObjcObjectCreateWithId(p_object, r_object);
}
////////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF void *MCObjcObjectGetId(const MCObjcObjectRef p_obj)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_obj);
return t_impl->object;
}
MC_DLLEXPORT_DEF void *MCObjcObjectGetRetainedId(const MCObjcObjectRef p_obj)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_obj);
return [(id)t_impl->object retain];
}
MC_DLLEXPORT_DEF void *MCObjcObjectGetAutoreleasedId(const MCObjcObjectRef p_obj)
{
__MCObjcObjectImpl *t_impl = __MCObjcObjectGet(p_obj);
return [[(id)t_impl->object retain] autorelease];
}
////////////////////////////////////////////////////////////////////////////////
@interface com_runrev_livecode_MCObjcObjectActionProxy: NSObject
{
MCHandlerRef m_handler;
MCValueRef m_context;
}
- (id)initWithHandlerRef:(MCHandlerRef)aHandler context:(MCValueRef)aContext;
- (void)dealloc;
- (void)action:(id)sender;
@end
@implementation com_runrev_livecode_MCObjcObjectActionProxy
- (id)initWithHandlerRef:(MCHandlerRef)aHandler context:(MCValueRef)aContext
{
self = [super init];
if (self == nil)
return nil;
m_handler = MCValueRetain(aHandler);
m_context = aContext != nullptr ? MCValueRetain(aContext) : nullptr;
return self;
}
- (void)dealloc
{
MCValueRelease(m_handler);
if (m_context != nullptr)
{
MCValueRelease(m_context);
}
[super dealloc];
}
- (void)action:(id)sender
{
MCAutoValueRef t_sender_arg;
if (!MCObjcObjectCreateWithId(sender, (MCObjcObjectRef&)&t_sender_arg))
{
return;
}
MCValueRef t_args[2];
t_args[0] = *t_sender_arg;
if (m_context != nullptr)
{
t_args[1] = m_context;
}
MCAutoValueRef t_result;
if (!MCHandlerExternalInvoke(m_handler, t_args, m_context != nullptr ? 2 : 1, &t_result))
{
return;
}
}
@end
extern "C"
MC_DLLEXPORT_DEF MCObjcObjectRef MCObjcObjectCreateActionProxy(MCHandlerRef p_handler, MCValueRef p_context)
{
MCTypeInfoRef t_signature =
MCValueGetTypeInfo(p_handler);
uindex_t t_param_count =
MCHandlerTypeInfoGetParameterCount(t_signature);
if (p_context == nullptr)
{
p_context = kMCNull;
}
if ((t_param_count == 1 && p_context != kMCNull) ||
t_param_count > 2)
{
MCErrorThrowGeneric(MCSTR("invalid signature for action proxy handler"));
return nullptr;
}
if (t_param_count == 1)
{
p_context = nullptr;
}
if (MCHandlerTypeInfoGetParameterType(t_signature, 0) != kMCObjcObjectTypeInfo)
{
MCErrorThrowGeneric(MCSTR("first parameter for action proxy handler must be ObjcObject"));
return nullptr;
}
com_runrev_livecode_MCObjcObjectActionProxy *t_proxy =
[[com_runrev_livecode_MCObjcObjectActionProxy alloc] initWithHandlerRef:p_handler context:p_context];
MCObjcObjectRef t_proxy_object = nullptr;
if (!MCObjcObjectCreateWithRetainedId(t_proxy, t_proxy_object))
{
[t_proxy release];
return nullptr;
}
return t_proxy_object;
}
extern "C"
MC_DLLEXPORT_DEF uintptr_t MCObjcObjectGetActionProxySelector(void)
{
return (uintptr_t)sel_registerName("action:");
}
////////////////////////////////////////////////////////////////////////////////
#if defined(__MAC__)
#include <AppKit/AppKit.h>
#else
#import <Foundation/NSInvocation.h>
#import <Foundation/NSMethodSignature.h>
#endif
#include <objc/runtime.h>
@interface com_livecode_MCObjcDelegate: NSObject
{
MCArrayRef m_handlers;
MCValueRef m_context;
}
- (id)initWithHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext;
- (void)dealloc;
- (BOOL)respondsToSelector:(SEL)aSelector;
@end
@interface com_livecode_MCObjcFormalDelegate: com_livecode_MCObjcDelegate
{
Protocol *m_protocol;
}
- (id)initForProtocol:(Protocol *)aProtocol withHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext;
- (void)dealloc;
@end
@interface com_livecode_MCObjcInformalDelegate: com_livecode_MCObjcDelegate
{
MCArrayRef m_protocol;
}
- (id)initForProtocol:(MCArrayRef)aProtocol withHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext;
- (void)dealloc;
@end
static bool _get_type_from_code(char p_char, MCTypeInfoRef& r_type)
{
#define CASE(ctype, lctype) \
if (p_char == @encode(ctype)[0]) \
{ \
r_type = kMC##lctype##TypeInfo; \
return true; \
} \
else
CASE(char, CSChar);
CASE(unsigned char, CUChar);
CASE(short, CSShort);
CASE(unsigned short, CUShort);
CASE(int, CSInt);
CASE(unsigned int, CUInt);
CASE(long, CSLong);
CASE(unsigned long, CULong);
CASE(long long, CSLongLong);
CASE(unsigned long long, CULongLong);
CASE(float, Float);
CASE(double, Double);
CASE(bool, CBool);
CASE(char *, Pointer);
#undef CASE
if (p_char == '@' ||
p_char == '#')
{
r_type = kMCObjcObjectTypeInfo;
return true;
}
else if (p_char == ':')
{
r_type = kMCStringTypeInfo;
return true;
}
else if (p_char == 'v')
{
r_type = kMCNullTypeInfo;
return true;
}
else
{
MCLog("unknown type encoding %c", p_char);
return false;
}
}
static bool __MCObjcMapTypeCode(const char *p_typecode, uindex_t& r_length, MCTypeInfoRef& r_type)
{
MCTypeInfoRef t_type;
if (!_get_type_from_code(*p_typecode, t_type))
return false;
// Skip the size information
uindex_t t_length = 1;
while (t_length < strlen(p_typecode) && isdigit(*(p_typecode + t_length)))
t_length++;
r_type = t_type;
r_length = t_length;
return true;
}
static bool _convert_type(char p_type, void *p_buffer, MCValueRef& r_value)
{
#define CASE(ctype, lctype) \
if (p_type == @encode(ctype)[0]) \
{ \
return MCForeignValueCreate(kMC##lctype##TypeInfo, p_buffer, (MCForeignValueRef&)r_value); \
} \
else
CASE(char, CSChar);
CASE(unsigned char, CUChar);
CASE(short, CSShort);
CASE(unsigned short, CUShort);
CASE(int, CSInt);
CASE(unsigned int, CUInt);
CASE(long, CSLong);
CASE(unsigned long, CULong);
CASE(long long, CSLongLong);
CASE(unsigned long long, CULongLong);
CASE(float, Float);
CASE(double, Double);
CASE(bool, CBool);
CASE(char *, Pointer);
#undef CASE
if (p_type == '@' ||
p_type == '#')
{
if (!MCObjcObjectCreateWithId(*(void **)p_buffer, (MCObjcObjectRef&)r_value))
{
return false;
}
}
else if (p_type == ':')
{
if (!MCStringCreateWithCString(sel_getName(*(SEL *)p_buffer), (MCStringRef&)r_value))
{
return false;
}
}
else if (p_type == 'v')
{
r_value = MCValueRetain(kMCNull);
}
else
{
MCLog("unknown type encoding %c", p_type);
return false;
}
return true;
}
static bool _get_method_description(Protocol *p_protocol, SEL p_selector, BOOL p_instance, objc_method_description& r_desc)
{
objc_method_description t_desc =
protocol_getMethodDescription(p_protocol, p_selector, NO, p_instance);
if (t_desc.types == nullptr)
t_desc = protocol_getMethodDescription(p_protocol, p_selector, YES, p_instance);
if (t_desc.types == nullptr)
return false;
r_desc = t_desc;
return true;
}
static const char *_get_code_from_type(MCTypeInfoRef p_typeinfo)
{
#define CASE(ctype, lctype) \
if (p_typeinfo == kMC##lctype##TypeInfo) \
{ \
return @encode(ctype); \
} \
else
CASE(char, CSChar);
CASE(unsigned char, CUChar);
CASE(short, CSShort);
CASE(unsigned short, CUShort);
CASE(int, CSInt);
CASE(unsigned int, CUInt);
CASE(long, CSLong);
CASE(unsigned long, CULong);
CASE(long long, CSLongLong);
CASE(unsigned long long, CULongLong);
CASE(float, Float);
CASE(double, Double);
CASE(bool, CBool);
#undef CASE
if (p_typeinfo == kMCObjcIdTypeInfo ||
p_typeinfo == kMCObjcRetainedIdTypeInfo ||
p_typeinfo == kMCObjcAutoreleasedIdTypeInfo)
{
return @encode(id);
}
else if (p_typeinfo == kMCPointerTypeInfo)
{
return "^?";
}
else
{
MCLog("unknown encoding for type %@", p_typeinfo);
return nullptr;
}
}
static bool _get_code_string(const char *p_code, size_t p_size, MCStringRef& r_code)
{
return MCStringFormat(r_code, "%s%d", p_code, p_size);
}
static bool _get_code_string_from_type(MCTypeInfoRef p_typeinfo, MCStringRef& r_code)
{
if (p_typeinfo == kMCNullTypeInfo)
{
r_code = MCSTR("v");
return true;
}
MCResolvedTypeInfo t_resolved;
if (!MCTypeInfoResolve(p_typeinfo, t_resolved))
return false;
if (!MCTypeInfoIsForeign(t_resolved.type))
return false;
const MCForeignTypeDescriptor *t_descriptor =
MCForeignTypeInfoGetDescriptor(t_resolved.type);
const char *t_code = _get_code_from_type(p_typeinfo);
if (t_code == nullptr)
return false;
return _get_code_string(t_code, t_descriptor->size, r_code);
}
static bool _append_code_string(MCStringRef x_target, const char *p_code, size_t p_size)
{
return MCStringAppendFormat(x_target,"%s%d", p_code, p_size);
}
static bool _get_informal_protocol_method_types(MCStringRef p_selector, MCArrayRef p_protocol, MCStringRef& r_types)
{
MCNewAutoNameRef t_key;
if (!MCNameCreate(p_selector, &t_key))
return false;
MCValueRef t_handler;
if (!MCArrayFetchValue(p_protocol, false, *t_key, t_handler))
return false;
MCTypeInfoRef t_typeinfo = MCValueGetTypeInfo(t_handler);
if (!MCHandlerTypeInfoIsForeign(t_typeinfo))
return false;
MCAutoStringRef t_types;
if (!MCStringCreateMutable(0, &t_types))
return false;
MCTypeInfoRef t_param_typeinfo = MCHandlerTypeInfoGetReturnType(t_typeinfo);
MCAutoStringRef t_return_type;
if (!_get_code_string_from_type(t_param_typeinfo, &t_return_type))
return false;
if (!MCStringAppend(*t_types, *t_return_type))
return false;
if (!_append_code_string(*t_types, @encode(id), sizeof(id)))
return false;
if (!_append_code_string(*t_types, @encode(SEL), sizeof(SEL)))
return false;
// IMPORTANT: We only allow instance methods in the mapping, so the first
// parameter of the foreign handler typeinfo should be the target instance.
// So we must skip this parameter when figuring out the obj-c method signature,
// i.e. start at index 1
for (uindex_t i = 1; i< MCHandlerTypeInfoGetParameterCount(t_typeinfo); ++i)
{
MCAutoStringRef t_param_type;
if (!_get_code_string_from_type(MCHandlerTypeInfoGetParameterType(t_typeinfo, i),
&t_param_type))
return false;
if (!MCStringAppend(*t_types, *t_param_type))
return false;
}
return MCStringCopy(*t_types, r_types);
}
static MCHandlerRef _fetch_handler_for_selector(SEL p_selector, MCArrayRef p_mapping)
{
MCAutoStringRef t_selector;
if (!MCStringCreateWithCString(sel_getName(p_selector), &t_selector))
return nullptr;
MCNewAutoNameRef t_key;
if (!MCNameCreate(*t_selector, &t_key))
return nullptr;
MCValueRef t_handler;
if (!MCArrayFetchValue(p_mapping, false, *t_key, t_handler))
return nullptr;
return static_cast<MCHandlerRef>(t_handler);
}
@implementation com_livecode_MCObjcFormalDelegate
- (id)initForProtocol:(Protocol *)aProtocol withHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext
{
self = [super initWithHandlerMapping:aHandlerMapping withContext:aContext];
if (self == nil)
return nil;
m_protocol = aProtocol;
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
// Return the method signature of the protocol method we have an LCB
// implementation for. This causes forwardInvocation to be called.
objc_method_description t_desc;
if (!_get_method_description(m_protocol, aSelector, YES, t_desc) &&
!_get_method_description(m_protocol, aSelector, NO, t_desc))
return nullptr;
return [NSMethodSignature signatureWithObjCTypes:t_desc.types];
}
@end
@implementation com_livecode_MCObjcInformalDelegate
- (id)initForProtocol:(MCArrayRef)aProtocol withHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext
{
self = [super initWithHandlerMapping:aHandlerMapping withContext:aContext];
if (self == nil)
return nil;
m_protocol = MCValueRetain(aProtocol);
return self;
}
- (void)dealloc
{
MCValueRelease(m_protocol);
[super dealloc];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
// Construct the type signature
MCAutoStringRef t_selector;
if (!MCStringCreateWithCString(sel_getName(aSelector), &t_selector))
return nullptr;
MCAutoStringRef t_types;
if (!_get_informal_protocol_method_types(*t_selector, m_protocol, &t_types))
return nullptr;
MCAutoStringRefAsCString t_types_cstring;
if (!t_types_cstring.Lock(*t_types))
return nullptr;
return [NSMethodSignature signatureWithObjCTypes:*t_types_cstring];
}
@end
@implementation com_livecode_MCObjcDelegate
- (id)initWithHandlerMapping:(MCArrayRef)aHandlerMapping withContext:(MCValueRef)aContext
{
self = [super init];
if (self == nil)
return nil;
m_handlers = MCValueRetain(aHandlerMapping);
m_context = aContext != nullptr ? MCValueRetain(aContext) : aContext;
return self;
}
- (void)dealloc
{
MCValueRelease(m_handlers);
MCValueRelease(m_context);
[super dealloc];
}
-(MCHandlerRef)handlerFromSelector:(SEL)aSelector
{
return _fetch_handler_for_selector(aSelector, m_handlers);
}
-(BOOL)respondsToSelector:(SEL)aSelector
{
// Check the handler mapping to see if this delegate wants to handle
// the incoming selector
if ([self handlerFromSelector:aSelector] != nullptr)
return YES;
return [super respondsToSelector:aSelector];
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
// Never forward the message to an instance of the base class
return nil;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
NSMethodSignature *t_signature = [anInvocation methodSignature];
MCHandlerRef t_handler = [self handlerFromSelector:[anInvocation selector]];
if (t_handler == nullptr)
return;
MCAutoProperListRef t_msg_arguments;
if (!MCProperListCreateMutable(&t_msg_arguments))
{
return;
}
// Add the context as the first param if it was specified
if (m_context != nullptr &&
!MCProperListPushElementOntoBack(*t_msg_arguments, m_context))
{
return;
}
// Box the incoming params and put into a proper list. The first two args
// are the handling obj-c id and selector, so start at index 2.
for(NSUInteger t_arg_index = 2; t_arg_index < [t_signature numberOfArguments]; t_arg_index++)
{
const char *t_arg_type = [t_signature getArgumentTypeAtIndex:t_arg_index];
NSUInteger t_arg_size, t_arg_align;
NSGetSizeAndAlignment(t_arg_type, &t_arg_size, &t_arg_align);
char t_raw_value[t_arg_size];
[anInvocation getArgument:t_raw_value atIndex:t_arg_index];
char t_typecode = t_arg_type[0];
// Special-case signed char / bool conversion to handle BOOL type
// which is signed char on mac and bool on ios
if (MCHandlerTypeInfoGetParameterType(MCValueGetTypeInfo(t_handler), t_arg_index - 2)
== kMCCBoolTypeInfo && t_arg_type[0] == 'c')
{
t_typecode = 'B';
}
MCAutoValueRef t_arg_value;
if (!_convert_type(t_typecode, t_raw_value, &t_arg_value))
{
return;
}
if (!MCProperListPushElementOntoBack(*t_msg_arguments, *t_arg_value))
{
return;
}
}
MCProperListRef t_mutable_list;
if (!MCProperListMutableCopy(*t_msg_arguments, t_mutable_list))
return;
MCAutoValueRef t_result;
MCErrorRef t_error =
MCHandlerTryToExternalInvokeWithList(t_handler, t_mutable_list, &t_result);
MCValueRelease(t_mutable_list);
if (t_error != nil)
{
MCErrorThrow(t_error);
return;
}
// We have already checked type conformance at bind time
// Firstly, if the return value is already a foreign value, just set the
// invocation return value to its contents ptr
if (MCValueGetTypeCode(*t_result) == kMCValueTypeCodeForeignValue)
{
[anInvocation setReturnValue:MCForeignValueGetContentsPtr(*t_result)];
return;
}
// Otherwise fetch the required foreign typeinfo, and export the value ref
// to a foreign value ptr
const char *t_return_type = [t_signature methodReturnType];
MCTypeInfoRef t_typeinfo;
if (!_get_type_from_code(t_return_type[0], t_typeinfo))
{
MCLog("can't convert return type to %s", t_return_type[0]);
return;
}
// If void return, we are done
if (t_typeinfo == kMCNullTypeInfo)
return;
MCResolvedTypeInfo t_resolved_type;
if (!MCTypeInfoResolve(t_typeinfo, t_resolved_type))
return;
if (!MCTypeInfoIsForeign(t_resolved_type.type))
{
MCLog("can't return non-foreign type");
return;
}
const MCForeignTypeDescriptor *t_descriptor =
MCForeignTypeInfoGetDescriptor(t_typeinfo);
MCAutoBlock<void> t_contents;
if (!t_contents.Allocate(t_descriptor->size))
return;
if (!t_descriptor->doexport(t_descriptor, *t_result, false, *t_contents))
return;
[anInvocation setReturnValue:*t_contents];
}
@end
static bool __MCTypeInfoConformsToObjcTypeCode(MCTypeInfoRef p_typeinfo, const char*& x_desc)
{
MCTypeInfoRef t_expected;
uindex_t t_length;
if (!__MCObjcMapTypeCode(x_desc, t_length, t_expected))
return false;
bool t_conforms = false;
// Special-case signed char / bool conformance to handle BOOL type
// which is signed char on mac and bool on ios
if (t_expected == kMCCSCharTypeInfo && p_typeinfo == kMCCBoolTypeInfo)
{
t_conforms = true;
}
else
{
MCResolvedTypeInfo t_src, t_target;
if (!MCTypeInfoResolve(t_expected, t_src))
return false;
if (!MCTypeInfoResolve(p_typeinfo, t_target))
return false;
t_conforms = MCResolvedTypeInfoConforms(t_src, t_target);
}
if (!t_conforms)
return false;
x_desc += t_length;
return true;
}
static bool __HandlerConformsToMethodSignature(MCHandlerRef p_handler, const char* p_typecodes, bool p_is_instance, MCValueRef p_context)
{
/* TODO: Check how things work with protocol class methods */
MCTypeInfoRef t_handler_typeinfo = MCValueGetTypeInfo(p_handler);
// Check return type, the first code in the signature
bool t_handler_conforms = true;
MCTypeInfoRef t_return_type = MCHandlerTypeInfoGetReturnType(t_handler_typeinfo);
if (t_handler_conforms)
{
t_handler_conforms =
__MCTypeInfoConformsToObjcTypeCode(t_return_type, p_typecodes);
}
// The next two types are always id (self) and SEL (the selector)
if (t_handler_conforms)
{
t_handler_conforms =
__MCTypeInfoConformsToObjcTypeCode(kMCObjcObjectTypeInfo, p_typecodes);
}
if (t_handler_conforms)
{
t_handler_conforms =
__MCTypeInfoConformsToObjcTypeCode(kMCStringTypeInfo, p_typecodes);
}
// Now check all the parameter types.
uindex_t t_param_count = MCHandlerTypeInfoGetParameterCount(t_handler_typeinfo);
// Skip the first param if we have a context
for (uindex_t i = p_context != nullptr ? 1 : 0;
t_handler_conforms && i < t_param_count;
++i)
{
MCTypeInfoRef t_param_typeinfo =
MCHandlerTypeInfoGetParameterType(t_handler_typeinfo, i);
if (t_handler_conforms)
{
t_handler_conforms =
__MCTypeInfoConformsToObjcTypeCode(t_param_typeinfo, p_typecodes);
}