-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathClassBuilder.mm
More file actions
991 lines (853 loc) · 41.7 KB
/
ClassBuilder.mm
File metadata and controls
991 lines (853 loc) · 41.7 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
#include "ClassBuilder.h"
#include <Foundation/Foundation.h>
#include <numeric>
#include <sstream>
#include "ArgConverter.h"
#include "Caches.h"
#include "FastEnumerationAdapter.h"
#include "Helpers.h"
#include "Interop.h"
#include "NativeScriptException.h"
#include "ObjectManager.h"
#include "Runtime.h"
#include "TNSDerivedClass.h"
using namespace v8;
namespace tns {
Local<FunctionTemplate> ClassBuilder::GetExtendFunction(Isolate* isolate,
const InterfaceMeta* interfaceMeta) {
CacheItem* item = new CacheItem(interfaceMeta, nullptr);
Caches::Get(isolate)->registerCacheBoundObject(item);
Local<External> ext = External::New(isolate, item);
return FunctionTemplate::New(isolate, ClassBuilder::ExtendCallback, ext);
}
void ClassBuilder::ExtendCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
tns::Assert(info.Length() > 0 && info[0]->IsObject() && info.This()->IsFunction(), isolate);
try {
Local<Context> context = isolate->GetCurrentContext();
CacheItem* item = static_cast<CacheItem*>(info.Data().As<External>()->Value());
Local<Object> implementationObject = info[0].As<Object>();
Local<v8::Function> baseFunc = info.This().As<v8::Function>();
std::string baseClassName = tns::ToString(isolate, baseFunc->GetName());
BaseDataWrapper* baseWrapper = tns::GetValue(isolate, baseFunc);
if (baseWrapper != nullptr && baseWrapper->Type() == WrapperType::ObjCClass) {
ObjCClassWrapper* classWrapper = static_cast<ObjCClassWrapper*>(baseWrapper);
if (classWrapper->ExtendedClass()) {
throw NativeScriptException("Cannot extend an already extended class");
}
}
Local<Object> nativeSignature;
std::string staticClassName;
if (info.Length() > 1 && info[1]->IsObject()) {
nativeSignature = info[1].As<Object>();
Local<Value> explicitClassName;
tns::Assert(nativeSignature->Get(context, tns::ToV8String(isolate, "name"))
.ToLocal(&explicitClassName),
isolate);
if (!explicitClassName.IsEmpty() && !explicitClassName->IsNullOrUndefined()) {
staticClassName = tns::ToString(isolate, explicitClassName);
}
}
auto cache = Caches::Get(isolate);
auto isolateId = cache->getIsolateId();
Class extendedClass = ClassBuilder::GetExtendedClass(baseClassName, staticClassName, std::to_string(isolateId) + "_");
class_addProtocol(extendedClass, @protocol(TNSDerivedClass));
class_addProtocol(object_getClass(extendedClass), @protocol(TNSDerivedClass));
if (!nativeSignature.IsEmpty()) {
ClassBuilder::ExposeDynamicMembers(context, extendedClass, implementationObject,
nativeSignature);
} else {
ClassBuilder::ExposeDynamicMethods(context, extendedClass, Local<Value>(), Local<Value>(),
implementationObject);
}
Local<v8::Function> baseCtorFunc =
cache->CtorFuncs.find(item->meta_->name())->second->Get(isolate);
CacheItem* cacheItem = new CacheItem(nullptr, extendedClass);
Caches::Get(isolate)->registerCacheBoundObject(cacheItem);
Local<External> ext = External::New(isolate, cacheItem);
Local<FunctionTemplate> extendedClassCtorFuncTemplate =
FunctionTemplate::New(isolate, ExtendedClassConstructorCallback, ext);
extendedClassCtorFuncTemplate->InstanceTemplate()->SetInternalFieldCount(1);
Local<v8::Function> extendClassCtorFunc;
if (!extendedClassCtorFuncTemplate->GetFunction(context).ToLocal(&extendClassCtorFunc)) {
tns::Assert(false, isolate);
}
Local<Value> baseProto;
bool success =
baseCtorFunc->Get(context, tns::ToV8String(isolate, "prototype")).ToLocal(&baseProto);
tns::Assert(success, isolate);
if (!implementationObject->SetPrototype(context, baseProto).To(&success) || !success) {
tns::Assert(false, isolate);
}
if (!implementationObject
->SetAccessor(context, tns::ToV8String(isolate, "super"), SuperAccessorGetterCallback,
nullptr, ext)
.To(&success) ||
!success) {
tns::Assert(false, isolate);
}
extendClassCtorFunc->SetName(tns::ToV8String(isolate, class_getName(extendedClass)));
Local<Value> extendFuncPrototypeValue;
success = extendClassCtorFunc->Get(context, tns::ToV8String(isolate, "prototype"))
.ToLocal(&extendFuncPrototypeValue);
tns::Assert(success && extendFuncPrototypeValue->IsObject(), isolate);
Local<Object> extendFuncPrototype = extendFuncPrototypeValue.As<Object>();
if (!extendFuncPrototype->SetPrototype(context, implementationObject).To(&success) ||
!success) {
tns::Assert(false, isolate);
}
if (!extendClassCtorFunc->SetPrototype(context, baseCtorFunc).To(&success) || !success) {
tns::Assert(false, isolate);
}
std::string extendedClassName = class_getName(extendedClass);
ObjCClassWrapper* wrapper = new ObjCClassWrapper(extendedClass, true);
tns::SetValue(isolate, extendClassCtorFunc, wrapper);
auto extendedPersistent =
std::make_unique<Persistent<v8::Function>>(isolate, extendClassCtorFunc);
extendedPersistent->SetWrapperClassId(Constants::ClassTypes::DataWrapper);
cache->CtorFuncs.emplace(extendedClassName, std::move(extendedPersistent));
cache->ClassPrototypes.emplace(
extendedClassName, std::make_unique<Persistent<Object>>(isolate, extendFuncPrototype));
info.GetReturnValue().Set(extendClassCtorFunc);
} catch (NativeScriptException& ex) {
ex.ReThrowToV8(isolate);
}
}
void ClassBuilder::ExtendedClassConstructorCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
try {
CacheItem* item = static_cast<CacheItem*>(info.Data().As<External>()->Value());
Class klass = item->data_;
ArgConverter::ConstructObject(context, info, klass);
} catch (NativeScriptException& ex) {
ex.ReThrowToV8(isolate);
}
}
void ClassBuilder::RegisterBaseTypeScriptExtendsFunction(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
auto cache = Caches::Get(isolate);
if (cache->OriginalExtendsFunc.get() != nullptr) {
return;
}
std::string extendsFuncScript = "(function() { "
" function __extends(d, b) { "
" for (var p in b) {"
" if (b.hasOwnProperty(p)) {"
" d[p] = b[p];"
" }"
" }"
" function __() { this.constructor = d; }"
" d.prototype = b === null ? Object.create(b) : "
"(__.prototype = b.prototype, new __());"
" } "
" return __extends;"
"})()";
Local<Script> script;
tns::Assert(Script::Compile(context, tns::ToV8String(isolate, extendsFuncScript.c_str()))
.ToLocal(&script),
isolate);
Local<Value> extendsFunc;
tns::Assert(script->Run(context).ToLocal(&extendsFunc) && extendsFunc->IsFunction(), isolate);
cache->OriginalExtendsFunc =
std::make_unique<Persistent<v8::Function>>(isolate, extendsFunc.As<v8::Function>());
}
void ClassBuilder::RegisterNativeTypeScriptExtendsFunction(Local<Context> context) {
Isolate* isolate = context->GetIsolate();
Local<Object> global = context->Global();
Local<v8::Function> extendsFunc =
v8::Function::New(context, [](const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
IsolateWrapper isolateWrapper(isolate);
tns::Assert(info.Length() == 2, isolate);
Local<Context> context = isolate->GetCurrentContext();
auto cache = Caches::Get(isolate);
BaseDataWrapper* wrapper = tns::GetValue(isolate, info[1].As<Object>());
if (!wrapper) {
// We are not extending a native object -> call the base __extends function
Persistent<v8::Function>* poExtendsFunc = cache->OriginalExtendsFunc.get();
tns::Assert(poExtendsFunc != nullptr, isolate);
Local<v8::Function> originalExtendsFunc = poExtendsFunc->Get(isolate);
Local<Value> args[] = {info[0], info[1]};
originalExtendsFunc->Call(context, context->Global(), info.Length(), args)
.ToLocalChecked();
return;
}
ObjCClassWrapper* classWrapper = static_cast<ObjCClassWrapper*>(wrapper);
Class baseClass = classWrapper->Klass();
std::string baseClassName = class_getName(baseClass);
Local<v8::Function> extendedClassCtorFunc = info[0].As<v8::Function>();
std::string extendedClassName = tns::ToString(isolate, extendedClassCtorFunc->GetName());
auto isolateId = cache->getIsolateId();
__block Class extendedClass =
ClassBuilder::GetExtendedClass(baseClassName, extendedClassName, std::to_string(isolateId) + "_");
class_addProtocol(extendedClass, @protocol(TNSDerivedClass));
class_addProtocol(object_getClass(extendedClass), @protocol(TNSDerivedClass));
extendedClassName = class_getName(extendedClass);
tns::SetValue(isolate, extendedClassCtorFunc, new ObjCClassWrapper(extendedClass, true));
const Meta* baseMeta = ArgConverter::FindMeta(baseClass);
const InterfaceMeta* interfaceMeta = static_cast<const InterfaceMeta*>(baseMeta);
Local<v8::Function> baseCtorFunc =
cache->CtorFuncs.find(interfaceMeta->name())->second->Get(isolate);
tns::Assert(extendedClassCtorFunc->SetPrototype(context, baseCtorFunc).ToChecked(),
isolate);
Local<v8::String> prototypeProp = tns::ToV8String(isolate, "prototype");
Local<Value> extendedClassCtorFuncPrototypeValue;
bool success = extendedClassCtorFunc->Get(context, prototypeProp)
.ToLocal(&extendedClassCtorFuncPrototypeValue);
tns::Assert(success && extendedClassCtorFuncPrototypeValue->IsObject(), isolate);
Local<Object> extendedClassCtorFuncPrototype =
extendedClassCtorFuncPrototypeValue.As<Object>();
Local<Value> prototypePropValue;
success = baseCtorFunc->Get(context, prototypeProp).ToLocal(&prototypePropValue);
tns::Assert(success && prototypePropValue->IsObject(), isolate);
success =
extendedClassCtorFuncPrototype->SetPrototype(context, prototypePropValue.As<Object>())
.FromMaybe(false);
tns::Assert(success, isolate);
cache->ClassPrototypes.emplace(
extendedClassName,
std::make_unique<Persistent<Object>>(isolate, extendedClassCtorFuncPrototype));
Persistent<v8::Function>* poExtendedClassCtorFunc =
new Persistent<v8::Function>(isolate, extendedClassCtorFunc);
poExtendedClassCtorFunc->SetWrapperClassId(Constants::ClassTypes::DataWrapper);
cache->CtorFuncs.emplace(extendedClassName, poExtendedClassCtorFunc);
IMP newInitialize = imp_implementationWithBlock(^(id self) {
if (!isolateWrapper.IsValid()) {
return;
}
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Caches::Get(isolate)->GetContext();
Local<v8::Function> extendedClassCtorFunc = poExtendedClassCtorFunc->Get(isolate);
Local<Value> exposedMethods;
bool success =
extendedClassCtorFunc->Get(context, tns::ToV8String(isolate, "ObjCExposedMethods"))
.ToLocal(&exposedMethods);
tns::Assert(success, isolate);
Local<Value> implementationObject;
success = extendedClassCtorFunc->Get(context, tns::ToV8String(isolate, "prototype"))
.ToLocal(&implementationObject);
tns::Assert(success, isolate);
if (implementationObject.IsEmpty() || exposedMethods.IsEmpty()) {
return;
}
Local<Value> exposedProtocols;
success = extendedClassCtorFunc->Get(context, tns::ToV8String(isolate, "ObjCProtocols"))
.ToLocal(&exposedProtocols);
tns::Assert(success, isolate);
ClassBuilder::ExposeDynamicMethods(context, extendedClass, exposedMethods,
exposedProtocols, implementationObject.As<Object>());
});
class_addMethod(object_getClass(extendedClass), @selector(initialize), newInitialize,
"v@:");
/// We swizzle the retain and release methods for the following reason:
/// When we instantiate a native class via a JavaScript call we add it to the object
/// instances map thus incrementing the retainCount by 1. Then, when the native object is
/// referenced somewhere else its count will become more than 1. Since we want to keep the
/// corresponding JavaScript object alive even if it is not used anywhere, we call GcProtect
/// on it. Whenever the native object is released so that its retainCount is 1 (the object
/// instances map), we unprotect the corresponding JavaScript object in order to make both
/// of them destroyable/GC-able. When the JavaScript object is GC-ed we release the native
/// counterpart as well.
void (*retain)(id, SEL) =
(void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(retain));
IMP newRetain = imp_implementationWithBlock(^(id self) {
if (!isolateWrapper.IsValid()) {
return retain(self, @selector(retain));
}
if ([self retainCount] == 1) {
auto runtime = Runtime::GetRuntime(isolate);
auto runtimeLoop = runtime->RuntimeLoop();
void* weakSelf = (__bridge void*)self;
auto gcProtect = ^() {
auto innerCache = isolateWrapper.GetCache();
auto it = innerCache->Instances.find((id)weakSelf);
if (it != innerCache->Instances.end()) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Value> value = it->second->Get(isolate);
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
objcWrapper->GcProtect();
}
}
};
if (CFRunLoopGetCurrent() != runtimeLoop) {
tns::ExecuteOnRunLoop(runtimeLoop, gcProtect);
} else {
gcProtect();
}
}
return retain(self, @selector(retain));
});
class_addMethod(extendedClass, @selector(retain), newRetain, "@@:");
void (*release)(id, SEL) =
(void (*)(id, SEL))FindNotOverridenMethod(extendedClass, @selector(release));
IMP newRelease = imp_implementationWithBlock(^(id self) {
if (!isolateWrapper.IsValid()) {
release(self, @selector(release));
return;
}
if ([self retainCount] == 2) {
void* weakSelf = (__bridge void*)self;
auto gcUnprotect = ^() {
auto innerCache = isolateWrapper.GetCache();
auto it = innerCache->Instances.find((id)weakSelf);
if (it != innerCache->Instances.end()) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
if (it->second != nullptr) {
Local<Value> value = it->second->Get(isolate);
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
objcWrapper->GcUnprotect();
}
}
}
};
auto runtime = Runtime::GetRuntime(isolate);
auto runtimeLoop = runtime->RuntimeLoop();
if (CFRunLoopGetCurrent() != runtimeLoop) {
tns::ExecuteOnRunLoop(runtimeLoop, gcUnprotect);
} else {
auto innerCache = isolateWrapper.GetCache();
auto it = innerCache->Instances.find(self);
if (it != innerCache->Instances.end()) {
v8::Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
if (it->second != nullptr) {
Local<Value> value = it->second->Get(isolate);
BaseDataWrapper* wrapper = tns::GetValue(isolate, value);
if (wrapper != nullptr && wrapper->Type() == WrapperType::ObjCObject) {
ObjCDataWrapper* objcWrapper = static_cast<ObjCDataWrapper*>(wrapper);
objcWrapper->GcUnprotect();
}
}
}
}
}
release(self, @selector(release));
});
class_addMethod(extendedClass, @selector(release), newRelease, "v@:");
info.GetReturnValue().SetUndefined();
}).ToLocalChecked();
PropertyAttribute flags = static_cast<PropertyAttribute>(PropertyAttribute::DontDelete);
bool success =
global->DefineOwnProperty(context, tns::ToV8String(isolate, "__extends"), extendsFunc, flags)
.FromMaybe(false);
tns::Assert(success, isolate);
}
void ClassBuilder::ExposeDynamicMembers(v8::Local<v8::Context> context, Class extendedClass,
Local<Object> implementationObject,
Local<Object> nativeSignature) {
Isolate* isolate = context->GetIsolate();
Local<Value> exposedMethods;
bool success = nativeSignature->Get(context, tns::ToV8String(isolate, "exposedMethods"))
.ToLocal(&exposedMethods);
tns::Assert(success, isolate);
Local<Value> exposedProtocols;
success = nativeSignature->Get(context, tns::ToV8String(isolate, "protocols"))
.ToLocal(&exposedProtocols);
tns::Assert(success, isolate);
ClassBuilder::ExposeDynamicMethods(context, extendedClass, exposedMethods, exposedProtocols,
implementationObject);
}
std::string ClassBuilder::GetTypeEncoding(const TypeEncoding* typeEncoding) {
BinaryTypeEncodingType type = typeEncoding->type;
switch (type) {
case BinaryTypeEncodingType::VoidEncoding: {
return @encode(void);
}
case BinaryTypeEncodingType::BoolEncoding: {
return @encode(bool);
}
case BinaryTypeEncodingType::UnicharEncoding:
case BinaryTypeEncodingType::UShortEncoding: {
return @encode(ushort);
}
case BinaryTypeEncodingType::ShortEncoding: {
return @encode(short);
}
case BinaryTypeEncodingType::UIntEncoding: {
return @encode(uint);
}
case BinaryTypeEncodingType::IntEncoding: {
return @encode(int);
}
#if defined(__LP64__)
case BinaryTypeEncodingType::ULongEncoding: {
return @encode(uint64_t);
}
case BinaryTypeEncodingType::LongEncoding: {
return @encode(int64_t);
}
#else
case BinaryTypeEncodingType::ULongEncoding: {
return @encode(uint32_t);
}
case BinaryTypeEncodingType::LongEncoding: {
return @encode(int32_t);
}
#endif
case BinaryTypeEncodingType::ULongLongEncoding: {
return @encode(unsigned long long);
}
case BinaryTypeEncodingType::LongLongEncoding: {
return @encode(long long);
}
case BinaryTypeEncodingType::UCharEncoding: {
return @encode(unsigned char);
}
case BinaryTypeEncodingType::CharEncoding: {
return @encode(char);
}
case BinaryTypeEncodingType::FloatEncoding: {
return @encode(float);
}
case BinaryTypeEncodingType::DoubleEncoding: {
return @encode(double);
}
case BinaryTypeEncodingType::CStringEncoding: {
return @encode(char*);
}
case BinaryTypeEncodingType::ClassEncoding: {
return @encode(Class);
}
case BinaryTypeEncodingType::SelectorEncoding: {
return @encode(SEL);
}
case BinaryTypeEncodingType::BlockEncoding: {
return @encode(dispatch_block_t);
}
case BinaryTypeEncodingType::StructDeclarationReference: {
const char* structName = typeEncoding->details.declarationReference.name.valuePtr();
const Meta* meta = ArgConverter::GetMeta(structName);
tns::Assert(meta != nullptr && meta->type() == MetaType::Struct);
const StructMeta* structMeta = static_cast<const StructMeta*>(meta);
const TypeEncoding* fieldEncoding = structMeta->fieldsEncodings()->first();
std::stringstream ss;
ss << "{" << structName << "=";
for (int i = 0; i < structMeta->fieldsCount(); i++) {
ss << GetTypeEncoding(fieldEncoding);
fieldEncoding = fieldEncoding->next();
}
ss << "}";
return ss.str();
}
case BinaryTypeEncodingType::PointerEncoding: {
std::stringstream ss;
ss << "^";
const TypeEncoding* innerType = typeEncoding->details.pointer.getInnerType();
ss << GetTypeEncoding(innerType);
return ss.str();
}
case BinaryTypeEncodingType::ConstantArrayEncoding: {
const TypeEncoding* innerType = typeEncoding->details.constantArray.getInnerType();
std::stringstream ss;
ss << "[";
ss << typeEncoding->details.constantArray.size << GetTypeEncoding(innerType);
ss << "]";
return ss.str();
}
case BinaryTypeEncodingType::IncompleteArrayEncoding: {
const TypeEncoding* innerType = typeEncoding->details.incompleteArray.getInnerType();
std::stringstream ss;
ss << "^";
ss << GetTypeEncoding(innerType);
return ss.str();
}
case BinaryTypeEncodingType::ProtocolEncoding:
case BinaryTypeEncodingType::InterfaceDeclarationReference:
case BinaryTypeEncodingType::InstanceTypeEncoding:
case BinaryTypeEncodingType::IdEncoding: {
return "@";
}
default:
// TODO: Handle the other possible types
tns::Assert(false);
return "";
}
}
std::string ClassBuilder::GetTypeEncoding(const TypeEncoding* typeEncoding, int argsCount) {
std::stringstream compilerEncoding;
compilerEncoding << GetTypeEncoding(typeEncoding);
compilerEncoding << "@:"; // id self, SEL _cmd
for (int i = 0; i < argsCount; i++) {
typeEncoding = typeEncoding->next();
compilerEncoding << GetTypeEncoding(typeEncoding);
}
return compilerEncoding.str();
}
BinaryTypeEncodingType ClassBuilder::GetTypeEncodingType(Isolate* isolate, Local<Value> value) {
if (BaseDataWrapper* wrapper = tns::GetValue(isolate, value)) {
if (wrapper->Type() == WrapperType::ObjCClass) {
return BinaryTypeEncodingType::IdEncoding;
} else if (wrapper->Type() == WrapperType::ObjCProtocol) {
return BinaryTypeEncodingType::IdEncoding;
} else if (wrapper->Type() == WrapperType::Primitive) {
PrimitiveDataWrapper* pdw = static_cast<PrimitiveDataWrapper*>(wrapper);
return pdw->TypeEncoding()->type;
} else if (wrapper->Type() == WrapperType::ObjCObject) {
return BinaryTypeEncodingType::IdEncoding;
} else if (wrapper->Type() == WrapperType::PointerType) {
return BinaryTypeEncodingType::PointerEncoding;
}
}
// TODO: Unknown encoding type
tns::Assert(false, isolate);
return BinaryTypeEncodingType::VoidEncoding;
}
void ClassBuilder::ExposeDynamicMethods(Local<Context> context, Class extendedClass,
Local<Value> exposedMethods, Local<Value> exposedProtocols,
Local<Object> implementationObject) {
Isolate* isolate = context->GetIsolate();
std::vector<const ProtocolMeta*> protocols;
if (!exposedProtocols.IsEmpty() && exposedProtocols->IsArray()) {
Local<v8::Array> protocolsArray = exposedProtocols.As<v8::Array>();
for (uint32_t i = 0; i < protocolsArray->Length(); i++) {
Local<Value> element;
bool success = protocolsArray->Get(context, i).ToLocal(&element);
tns::Assert(success && !element.IsEmpty() && element->IsFunction(), isolate);
Local<v8::Function> protoObj = element.As<v8::Function>();
BaseDataWrapper* wrapper = tns::GetValue(isolate, protoObj);
tns::Assert(wrapper && wrapper->Type() == WrapperType::ObjCProtocol, isolate);
ObjCProtocolWrapper* protoWrapper = static_cast<ObjCProtocolWrapper*>(wrapper);
Protocol* proto = protoWrapper->Proto();
if (proto != nil && !class_conformsToProtocol(extendedClass, proto)) {
class_addProtocol(extendedClass, proto);
class_addProtocol(object_getClass(extendedClass), proto);
}
protocols.push_back(protoWrapper->ProtoMeta());
}
}
if (!exposedMethods.IsEmpty() && exposedMethods->IsObject()) {
Local<v8::Array> methodNames;
if (!exposedMethods.As<Object>()->GetOwnPropertyNames(context).ToLocal(&methodNames)) {
tns::Assert(false, isolate);
}
for (int i = 0; i < methodNames->Length(); i++) {
Local<Value> methodName;
bool success = methodNames->Get(context, i).ToLocal(&methodName);
tns::Assert(success, isolate);
Local<Value> methodSignature;
success = exposedMethods.As<Object>()->Get(context, methodName).ToLocal(&methodSignature);
tns::Assert(success && methodSignature->IsObject(), isolate);
Local<Value> method;
success = implementationObject->Get(context, methodName).ToLocal(&method);
tns::Assert(success, isolate);
if (method.IsEmpty() || !method->IsFunction()) {
Log(@"No implementation found for exposed method \"%s\"",
tns::ToString(isolate, methodName).c_str());
continue;
}
Local<Value> returnsVal;
success = methodSignature.As<Object>()
->Get(context, tns::ToV8String(isolate, "returns"))
.ToLocal(&returnsVal);
tns::Assert(success, isolate);
Local<Value> paramsVal;
success = methodSignature.As<Object>()
->Get(context, tns::ToV8String(isolate, "params"))
.ToLocal(¶msVal);
tns::Assert(success, isolate);
if (returnsVal.IsEmpty() || !returnsVal->IsObject()) {
// Incorrect exposedMethods definition: missing returns property
tns::Assert(false, isolate);
}
int argsCount = 0;
if (!paramsVal.IsEmpty() && paramsVal->IsArray()) {
argsCount = paramsVal.As<v8::Array>()->Length();
}
BinaryTypeEncodingType returnType = GetTypeEncodingType(isolate, returnsVal);
std::string methodNameStr = tns::ToString(isolate, methodName);
SEL selector = sel_registerName(methodNameStr.c_str());
// TODO: Investigate and maybe find a way to free this
// Currently this is added to the meta cache by the Interop::CreateMethod
// but when the isolate dies we might need to free it? Or maybe this is cached throughout all
// isolates
TypeEncoding* typeEncoding =
reinterpret_cast<TypeEncoding*>(calloc((argsCount + 1), sizeof(TypeEncoding)));
typeEncoding->type = returnType;
if (!paramsVal.IsEmpty() && paramsVal->IsArray()) {
Local<v8::Array> params = paramsVal.As<v8::Array>();
TypeEncoding* next = typeEncoding;
for (int i = 0; i < params->Length(); i++) {
next = const_cast<TypeEncoding*>(next->next());
Local<Value> param;
success = params->Get(context, i).ToLocal(¶m);
tns::Assert(success, isolate);
next->type = GetTypeEncodingType(isolate, param);
}
}
std::shared_ptr<Persistent<Value>> poCallback =
std::make_shared<Persistent<Value>>(isolate, method);
MethodCallbackWrapper* userData =
new MethodCallbackWrapper(isolate, poCallback, 2, argsCount, typeEncoding);
IMP methodBody =
Interop::CreateMethod(2, argsCount, typeEncoding, ArgConverter::MethodCallback, userData);
std::string typeInfo = GetTypeEncoding(typeEncoding, argsCount);
tns::Assert(class_addMethod(extendedClass, selector, methodBody, typeInfo.c_str()), isolate);
}
}
const Meta* m = ArgConverter::FindMeta(extendedClass);
if (m == nullptr) {
return;
}
const BaseClassMeta* extendedClassMeta = static_cast<const BaseClassMeta*>(m);
Local<v8::Array> propertyNames;
Local<Value> symbolIterator;
bool success =
implementationObject->Get(context, Symbol::GetIterator(isolate)).ToLocal(&symbolIterator);
tns::Assert(success, isolate);
if (!symbolIterator.IsEmpty() && symbolIterator->IsFunction()) {
Local<v8::Function> symbolIteratorFunc = symbolIterator.As<v8::Function>();
class_addProtocol(extendedClass, @protocol(NSFastEnumeration));
class_addProtocol(object_getClass(extendedClass), @protocol(NSFastEnumeration));
Persistent<v8::Function>* poIteratorFunc =
new Persistent<v8::Function>(isolate, symbolIteratorFunc);
IMP imp = imp_implementationWithBlock(^NSUInteger(id self, NSFastEnumerationState* state,
__unsafe_unretained id buffer[],
NSUInteger length) {
return tns::FastEnumerationAdapter(isolate, self, state, buffer, length, poIteratorFunc);
});
struct objc_method_description fastEnumerationMethodDescription = protocol_getMethodDescription(
@protocol(NSFastEnumeration), @selector(countByEnumeratingWithState:objects:count:), YES,
YES);
tns::Assert(
class_addMethod(extendedClass, @selector(countByEnumeratingWithState:objects:count:), imp,
fastEnumerationMethodDescription.types),
isolate);
}
tns::Assert(implementationObject->GetOwnPropertyNames(context).ToLocal(&propertyNames), isolate);
for (uint32_t i = 0; i < propertyNames->Length(); i++) {
Local<Value> key;
bool success = propertyNames->Get(context, i).ToLocal(&key);
tns::Assert(success, isolate);
if (!key->IsName()) {
continue;
}
std::string methodName = tns::ToString(isolate, key);
Local<Value> propertyDescriptor;
tns::Assert(implementationObject->GetOwnPropertyDescriptor(context, key.As<v8::Name>())
.ToLocal(&propertyDescriptor),
isolate);
if (propertyDescriptor.IsEmpty() || propertyDescriptor->IsNullOrUndefined()) {
continue;
}
Local<Value> getter;
success = propertyDescriptor.As<Object>()
->Get(context, tns::ToV8String(isolate, "get"))
.ToLocal(&getter);
tns::Assert(success, isolate);
Local<Value> setter;
success = propertyDescriptor.As<Object>()
->Get(context, tns::ToV8String(isolate, "set"))
.ToLocal(&setter);
tns::Assert(success, isolate);
if ((!getter.IsEmpty() || !setter.IsEmpty()) &&
(getter->IsFunction() || setter->IsFunction())) {
std::vector<const PropertyMeta*> propertyMetas;
VisitProperties(methodName, extendedClassMeta, propertyMetas, protocols);
ExposeProperties(isolate, extendedClass, propertyMetas, implementationObject, getter, setter);
continue;
}
Local<Value> method;
success = propertyDescriptor.As<Object>()
->Get(context, tns::ToV8String(isolate, "value"))
.ToLocal(&method);
tns::Assert(success, isolate);
if (method.IsEmpty() || !method->IsFunction()) {
continue;
}
std::vector<const MethodMeta*> methodMetas;
VisitMethods(extendedClass, methodName, extendedClassMeta, methodMetas, protocols);
for (int j = 0; j < methodMetas.size(); j++) {
const MethodMeta* methodMeta = methodMetas[j];
std::shared_ptr<Persistent<Value>> poCallback =
std::make_shared<Persistent<Value>>(isolate, method);
const TypeEncoding* typeEncoding = methodMeta->encodings()->first();
uint8_t argsCount = methodMeta->encodings()->count - 1;
MethodCallbackWrapper* userData =
new MethodCallbackWrapper(isolate, poCallback, 2, argsCount, typeEncoding);
SEL selector = methodMeta->selector();
IMP methodBody =
Interop::CreateMethod(2, argsCount, typeEncoding, ArgConverter::MethodCallback, userData);
std::string typeInfo = GetTypeEncoding(typeEncoding, argsCount);
class_replaceMethod(extendedClass, selector, methodBody, typeInfo.c_str());
}
}
}
void ClassBuilder::VisitProperties(std::string propertyName, const BaseClassMeta* meta,
std::vector<const PropertyMeta*>& propertyMetas,
std::vector<const ProtocolMeta*> exposedProtocols) {
for (auto it = meta->instanceProps->begin(); it != meta->instanceProps->end(); it++) {
const PropertyMeta* propertyMeta = (*it).valuePtr();
if (propertyMeta->jsName() == propertyName &&
std::find(propertyMetas.begin(), propertyMetas.end(), propertyMeta) ==
propertyMetas.end()) {
propertyMetas.push_back(propertyMeta);
}
}
for (auto protoIt = meta->protocols->begin(); protoIt != meta->protocols->end(); protoIt++) {
const char* protocolName = (*protoIt).valuePtr();
const Meta* m = ArgConverter::GetMeta(protocolName);
if (!m) {
continue;
}
const ProtocolMeta* protocolMeta = static_cast<const ProtocolMeta*>(m);
VisitProperties(propertyName, protocolMeta, propertyMetas, exposedProtocols);
}
for (auto it = exposedProtocols.begin(); it != exposedProtocols.end(); it++) {
const ProtocolMeta* protocolMeta = *it;
VisitProperties(propertyName, protocolMeta, propertyMetas, std::vector<const ProtocolMeta*>());
}
if (meta->type() == MetaType::Interface) {
const InterfaceMeta* interfaceMeta = static_cast<const InterfaceMeta*>(meta);
const BaseClassMeta* baseMeta = interfaceMeta->baseMeta();
if (baseMeta != nullptr) {
VisitProperties(propertyName, baseMeta, propertyMetas, exposedProtocols);
}
}
}
void ClassBuilder::VisitMethods(Class extendedClass, std::string methodName,
const BaseClassMeta* meta,
std::vector<const MethodMeta*>& methodMetas,
std::vector<const ProtocolMeta*> exposedProtocols) {
for (auto it = meta->instanceMethods->begin(); it != meta->instanceMethods->end(); it++) {
const MethodMeta* methodMeta = (*it).valuePtr();
if (methodMeta->jsName() == methodName) {
if (std::find(methodMetas.begin(), methodMetas.end(), methodMeta) == methodMetas.end()) {
methodMetas.push_back(methodMeta);
}
}
}
for (auto protoIt = meta->protocols->begin(); protoIt != meta->protocols->end(); protoIt++) {
const char* protocolName = (*protoIt).valuePtr();
const Meta* m = ArgConverter::GetMeta(protocolName);
if (!m) {
continue;
}
const ProtocolMeta* protocolMeta = static_cast<const ProtocolMeta*>(m);
VisitMethods(extendedClass, methodName, protocolMeta, methodMetas, exposedProtocols);
}
for (auto it = exposedProtocols.begin(); it != exposedProtocols.end(); it++) {
const ProtocolMeta* protocolMeta = *it;
VisitMethods(extendedClass, methodName, protocolMeta, methodMetas,
std::vector<const ProtocolMeta*>());
}
if (meta->type() == MetaType::Interface) {
const InterfaceMeta* interfaceMeta = static_cast<const InterfaceMeta*>(meta);
const BaseClassMeta* baseMeta = interfaceMeta->baseMeta();
if (baseMeta != nullptr) {
VisitMethods(extendedClass, methodName, interfaceMeta->baseMeta(), methodMetas,
exposedProtocols);
}
}
}
void ClassBuilder::ExposeProperties(Isolate* isolate, Class extendedClass,
std::vector<const PropertyMeta*> propertyMetas,
Local<Object> implementationObject, Local<Value> getter,
Local<Value> setter) {
for (int j = 0; j < propertyMetas.size(); j++) {
const PropertyMeta* propertyMeta = propertyMetas[j];
std::string propertyName = propertyMeta->name();
bool hasGetter = !getter.IsEmpty() && getter->IsFunction() && propertyMeta->hasGetter();
bool hasSetter = !setter.IsEmpty() && setter->IsFunction() && propertyMeta->hasSetter();
std::shared_ptr<Persistent<Object>> poImplementationObject;
if (hasGetter || hasSetter) {
poImplementationObject = std::make_shared<Persistent<Object>>(isolate, implementationObject);
}
if (hasGetter) {
std::shared_ptr<Persistent<v8::Function>> poGetterFunc =
std::make_shared<Persistent<v8::Function>>(isolate, getter.As<v8::Function>());
PropertyCallbackContext* userData =
new PropertyCallbackContext(isolate, poGetterFunc, poImplementationObject, propertyMeta);
FFIMethodCallback getterCallback = [](ffi_cif* cif, void* retValue, void** argValues,
void* userData) {
PropertyCallbackContext* context = static_cast<PropertyCallbackContext*>(userData);
v8::Locker locker(context->isolate_);
Isolate::Scope isolate_scope(context->isolate_);
HandleScope handle_scope(context->isolate_);
Local<v8::Function> getterFunc = context->callback_->Get(context->isolate_);
Local<Value> res;
id thiz = *static_cast<const id*>(argValues[0]);
auto cache = Caches::Get(context->isolate_);
auto it = cache->Instances.find(thiz);
Local<Object> self_ = it != cache->Instances.end()
? it->second->Get(context->isolate_).As<Object>()
: context->implementationObject_->Get(context->isolate_);
Local<Context> v8Context = Caches::Get(context->isolate_)->GetContext();
tns::Assert(getterFunc->Call(v8Context, self_, 0, nullptr).ToLocal(&res),
context->isolate_);
const TypeEncoding* typeEncoding = context->meta_->getter()->encodings()->first();
ArgConverter::SetValue(v8Context, retValue, res, typeEncoding);
};
const TypeEncoding* typeEncoding = propertyMeta->getter()->encodings()->first();
IMP impGetter = Interop::CreateMethod(2, 0, typeEncoding, getterCallback, userData);
class_addMethod(extendedClass, propertyMeta->getter()->selector(), impGetter, "@@:");
}
if (hasSetter) {
std::shared_ptr<Persistent<v8::Function>> poSetterFunc =
std::make_shared<Persistent<v8::Function>>(isolate, setter.As<v8::Function>());
PropertyCallbackContext* userData =
new PropertyCallbackContext(isolate, poSetterFunc, poImplementationObject, propertyMeta);
FFIMethodCallback setterCallback = [](ffi_cif* cif, void* retValue, void** argValues,
void* userData) {
PropertyCallbackContext* context = static_cast<PropertyCallbackContext*>(userData);
v8::Locker locker(context->isolate_);
Isolate::Scope isolate_scope(context->isolate_);
HandleScope handle_scope(context->isolate_);
Local<v8::Function> setterFunc = context->callback_->Get(context->isolate_);
Local<Value> res;
id thiz = *static_cast<const id*>(argValues[0]);
auto cache = Caches::Get(context->isolate_);
auto it = cache->Instances.find(thiz);
Local<Object> self_ = it != cache->Instances.end()
? it->second->Get(context->isolate_).As<Object>()
: context->implementationObject_->Get(context->isolate_);
uint8_t* argBuffer = (uint8_t*)argValues[2];
const TypeEncoding* typeEncoding = context->meta_->setter()->encodings()->first()->next();
BaseCall call(argBuffer);
Local<Context> v8Context = Caches::Get(context->isolate_)->GetContext();
Local<Value> jsWrapper = Interop::GetResult(v8Context, typeEncoding, &call, true);
Local<Value> params[1] = {jsWrapper};
tns::Assert(setterFunc->Call(context->isolate_->GetCurrentContext(), self_, 1, params)
.ToLocal(&res),
context->isolate_);
};
const TypeEncoding* typeEncoding = propertyMeta->setter()->encodings()->first();
IMP impSetter = Interop::CreateMethod(2, 1, typeEncoding, setterCallback, userData);
class_addMethod(extendedClass, propertyMeta->setter()->selector(), impSetter, "v@:@");
}
}
}
void ClassBuilder::SuperAccessorGetterCallback(Local<v8::Name> property,
const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Local<Context> context = isolate->GetCurrentContext();
Local<Object> thiz = info.This();
std::shared_ptr<Persistent<Value>> poValue = ArgConverter::CreateEmptyObject(context);
Local<Object> superValue = poValue->Get(isolate).As<Object>();
superValue
->SetPrototype(context,
thiz->GetPrototype().As<Object>()->GetPrototype().As<Object>()->GetPrototype())
.ToChecked();
superValue->SetInternalField(0, thiz->GetInternalField(0));
superValue->SetInternalField(1, tns::ToV8String(isolate, "super"));
info.GetReturnValue().Set(superValue);
}
IMP ClassBuilder::FindNotOverridenMethod(Class klass, SEL method) {
while (class_conformsToProtocol(klass, @protocol(TNSDerivedClass))) {
klass = class_getSuperclass(klass);
}
return class_getMethodImplementation(klass, method);
}
std::atomic<unsigned long long> ClassBuilder::classNameCounter_{0};
} // namespace tns