-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathMetadata.h
More file actions
1000 lines (822 loc) · 30.1 KB
/
Metadata.h
File metadata and controls
1000 lines (822 loc) · 30.1 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
#ifndef Metadata_h
#define Metadata_h
#include <stack>
#include <string>
#include <type_traits>
#include <thread>
#include <mutex>
#include "robin_hood.h"
#include <map>
#include <set>
#include <vector>
#include "KnownUnknownClassPair.h"
#include "SpinLock.h"
namespace tns {
static const int MetaTypeMask = 0b00000111;
template <typename V>
static const V& getProperFunctionFromContainer(const std::vector<V>& container, int argsCount, std::function<int(const V&)> paramsCounter) {
const V* callee = nullptr;
for (const V& func : container) {
auto candidateArgs = paramsCounter(func);
auto calleeArgs = 0;
if (candidateArgs == argsCount) {
callee = &func;
break;
} else if (!callee) {
// no candidates so far, take it whatever it is
callee = &func;
calleeArgs = candidateArgs;
} else if (argsCount < candidateArgs && (calleeArgs < argsCount || candidateArgs < calleeArgs)) {
// better candidate - looking for the least number of arguments which is more than the amount actually passed
callee = &func;
calleeArgs = candidateArgs;
} else if (calleeArgs < candidateArgs) {
// better candidate - looking for the maximum number of arguments which less than the amount actually passed (if one with more cannot be found)
callee = &func;
calleeArgs = candidateArgs;
}
}
return *callee;
}
inline uint8_t encodeVersion(uint8_t majorVersion, uint8_t minorVersion) {
return (majorVersion << 3) | minorVersion;
}
inline uint8_t getMajorVersion(uint8_t encodedVersion) {
return encodedVersion >> 3;
}
inline uint8_t getMinorVersion(uint8_t encodedVersion) {
return encodedVersion & 0b111;
}
// Bit indices in flags section
enum MetaFlags {
HasDemangledName = 8,
HasName = 7,
// IsIosAppExtensionAvailable = 6, the flag exists in metadata generator but we never use it in the runtime
FunctionReturnsUnmanaged = 3,
FunctionIsVariadic = 5,
FunctionOwnsReturnedCocoaObject = 4,
MemberIsOptional = 0, // Mustn't equal any Method or Property flag since it can be applicable to both
MethodIsInitializer = 1,
MethodIsVariadic = 2,
MethodIsNullTerminatedVariadic = 3,
MethodOwnsReturnedCocoaObject = 4,
MethodHasErrorOutParameter = 5,
PropertyHasGetter = 2,
PropertyHasSetter = 3,
};
/// This enum describes the possible ObjectiveC entity types.
enum MetaType {
Undefined = 0,
Struct = 1,
Union = 2,
Function = 3,
JsCode = 4,
Var = 5,
Interface = 6,
ProtocolType = 7,
Vector = 8
};
enum MemberType {
InstanceMethod = 0,
StaticMethod = 1,
InstanceProperty = 2,
StaticProperty = 3
};
enum BinaryTypeEncodingType : uint8_t {
VoidEncoding,
BoolEncoding,
ShortEncoding,
UShortEncoding,
IntEncoding,
UIntEncoding,
LongEncoding,
ULongEncoding,
LongLongEncoding,
ULongLongEncoding,
CharEncoding,
UCharEncoding,
UnicharEncoding,
CharSEncoding,
CStringEncoding,
FloatEncoding,
DoubleEncoding,
InterfaceDeclarationReference,
StructDeclarationReference,
UnionDeclarationReference,
PointerEncoding,
VaListEncoding,
SelectorEncoding,
ClassEncoding,
ProtocolEncoding,
InstanceTypeEncoding,
IdEncoding,
ConstantArrayEncoding,
IncompleteArrayEncoding,
FunctionPointerEncoding,
BlockEncoding,
AnonymousStructEncoding,
AnonymousUnionEncoding,
ExtVectorEncoding
};
#pragma pack(push, 1)
template <typename T>
struct PtrTo;
struct Meta;
struct InterfaceMeta;
struct ProtocolMeta;
struct ModuleMeta;
struct LibraryMeta;
struct TypeEncoding;
typedef std::vector<const ProtocolMeta*> ProtocolMetas;
typedef int32_t ArrayCount;
static const void* offset(const void* from, ptrdiff_t offset) {
return reinterpret_cast<const char*>(from) + offset;
}
template <typename T>
struct Array {
class iterator {
private:
const T* current;
public:
iterator(const T* item)
: current(item) {
}
bool operator==(const iterator& other) const {
return current == other.current;
}
bool operator!=(const iterator& other) const {
return !(*this == other);
}
iterator& operator++() {
current++;
return *this;
}
iterator operator++(int) {
iterator tmp(current);
operator++();
return tmp;
}
const T& operator*() const {
return *current;
}
};
ArrayCount count;
const T* first() const {
return reinterpret_cast<const T*>(&count + 1);
}
const T& operator[](int index) const {
return *(first() + index);
}
Array<T>::iterator begin() const {
return first();
}
Array<T>::iterator end() const {
return first() + count;
}
template <typename V>
const Array<V>& castTo() const {
return *reinterpret_cast<const Array<V>*>(this);
}
int sizeInBytes() const {
return sizeof(Array<T>) + sizeof(T) * count;
}
int binarySearch(std::function<int(const T&)> comparer) const {
int left = 0, right = count - 1, mid;
while (left <= right) {
mid = (right + left) / 2;
const T& current = (*this)[mid];
int comparisonResult = comparer(current);
if (comparisonResult < 0) {
left = mid + 1;
} else if (comparisonResult > 0) {
right = mid - 1;
} else {
return mid;
}
}
return -(left + 1);
}
int binarySearchLeftmost(std::function<int(const T&)> comparer) const {
int mid = binarySearch(comparer);
while (mid > 0 && comparer((*this)[mid - 1]) == 0) {
mid -= 1;
}
return mid;
}
};
template <typename T>
using ArrayOfPtrTo = Array<PtrTo<T>>;
using String = PtrTo<char>;
enum GlobalTableType {
ByJsName,
ByNativeName,
};
template <GlobalTableType TYPE>
struct GlobalTable {
class iterator {
private:
const GlobalTable<TYPE>* _globalTable;
int _topLevelIndex;
int _bucketIndex;
void findNext();
const Meta* getCurrent();
public:
iterator(const GlobalTable<TYPE>* globalTable)
: iterator(globalTable, 0, 0) {
findNext();
}
iterator(const GlobalTable<TYPE>* globalTable, int32_t topLevelIndex, int32_t bucketIndex)
: _globalTable(globalTable)
, _topLevelIndex(topLevelIndex)
, _bucketIndex(bucketIndex) {
findNext();
}
bool operator==(const iterator& other) const;
bool operator!=(const iterator& other) const;
iterator& operator++();
iterator operator++(int) {
iterator tmp(_globalTable, _topLevelIndex, _bucketIndex);
operator++();
return tmp;
}
const Meta* operator*();
};
iterator begin() const {
return iterator(this);
}
iterator end() const {
return iterator(this, this->buckets.count, 0);
}
ArrayOfPtrTo<ArrayOfPtrTo<Meta>> buckets;
const InterfaceMeta* findInterfaceMeta(const char* identifierString) const;
const InterfaceMeta* findInterfaceMeta(const char* identifierString, size_t length, unsigned hash) const;
const ProtocolMeta* findProtocol(const char* identifierString) const;
const ProtocolMeta* findProtocol(const char* identifierString, size_t length, unsigned hash) const;
const Meta* findMeta(const char* identifierString, bool onlyIfAvailable = true) const;
const Meta* findMeta(const char* identifierString, size_t length, unsigned hash, bool onlyIfAvailable = true) const;
int sizeInBytes() const {
return buckets.sizeInBytes();
}
static bool compareName(const Meta& meta, const char* identifierString, size_t length);
};
struct ModuleTable {
ArrayOfPtrTo<ModuleMeta> modules;
int sizeInBytes() const {
return modules.sizeInBytes();
}
};
struct MetaFile {
private:
GlobalTable<GlobalTableType::ByJsName> _globalTableJs;
public:
static MetaFile* instance();
static MetaFile* setInstance(void* metadataPtr);
const GlobalTable<GlobalTableType::ByJsName>* globalTableJs() const {
return &this->_globalTableJs;
}
const GlobalTable<GlobalTableType::ByNativeName>* globalTableNativeProtocols() const {
const GlobalTable<GlobalTableType::ByJsName>* gt = this->globalTableJs();
return reinterpret_cast<const GlobalTable<GlobalTableType::ByNativeName>*>(offset(gt, gt->sizeInBytes()));
}
const GlobalTable<GlobalTableType::ByNativeName>* globalTableNativeInterfaces() const {
const GlobalTable<GlobalTableType::ByNativeName>* gt = this->globalTableNativeProtocols();
return reinterpret_cast<const GlobalTable<GlobalTableType::ByNativeName>*>(offset(gt, gt->sizeInBytes()));
}
const ModuleTable* topLevelModulesTable() const {
const GlobalTable<GlobalTableType::ByNativeName>* gt = this->globalTableNativeInterfaces();
return reinterpret_cast<const ModuleTable*>(offset(gt, gt->sizeInBytes()));
}
const void* heap() const {
const ModuleTable* mt = this->topLevelModulesTable();
return offset(mt, mt->sizeInBytes());
}
};
template <typename T>
struct PtrTo {
int32_t offset;
inline bool isNull() const {
return offset == 0;
}
inline PtrTo<T> operator+(int value) const {
return add(value);
}
inline const T* operator->() const {
return valuePtr();
}
inline PtrTo<T> add(int value) const {
return PtrTo<T>{ .offset = this->offset + value * sizeof(T) };
}
inline PtrTo<T> addBytes(int bytes) const {
return PtrTo<T>{ .offset = this->offset + bytes };
}
template <typename V>
inline PtrTo<V>& castTo() const {
return reinterpret_cast<PtrTo<V>>(this);
}
inline const T* valuePtr() const {
return isNull() ? nullptr : reinterpret_cast<const T*>(tns::offset(MetaFile::instance()->heap(), this->offset));
}
inline const T& value() const {
return *valuePtr();
}
};
template <typename T>
struct TypeEncodingsList {
T count;
const TypeEncoding* first() const {
return reinterpret_cast<const TypeEncoding*>(this + 1);
}
};
union TypeEncodingDetails {
struct IdDetails {
PtrTo<Array<String>> _protocols;
} idDetails;
struct IncompleteArrayDetails {
const TypeEncoding* getInnerType() const {
return reinterpret_cast<const TypeEncoding*>(this);
}
} incompleteArray;
struct ConstantArrayDetails {
int32_t size;
const TypeEncoding* getInnerType() const {
return reinterpret_cast<const TypeEncoding*>(this + 1);
}
} constantArray;
struct ExtVectorDetails {
int32_t size;
const TypeEncoding* getInnerType() const {
return reinterpret_cast<const TypeEncoding*>(this + 1);
}
} extVector;
struct DeclarationReferenceDetails {
String name;
} declarationReference;
struct InterfaceDeclarationReferenceDetails {
String name;
PtrTo<Array<String>> _protocols;
} interfaceDeclarationReference;
struct PointerDetails {
const TypeEncoding* getInnerType() const {
return reinterpret_cast<const TypeEncoding*>(this);
}
} pointer;
struct BlockDetails {
TypeEncodingsList<uint8_t> signature;
} block;
struct FunctionPointerDetails {
TypeEncodingsList<uint8_t> signature;
} functionPointer;
struct AnonymousRecordDetails {
uint8_t fieldsCount;
const String* getFieldNames() const {
return reinterpret_cast<const String*>(this + 1);
}
const TypeEncoding* getFieldsEncodings() const {
return reinterpret_cast<const TypeEncoding*>(getFieldNames() + this->fieldsCount);
}
} anonymousRecord;
};
struct TypeEncoding {
BinaryTypeEncodingType type;
TypeEncodingDetails details;
const TypeEncoding* next() const {
const TypeEncoding* afterTypePtr = reinterpret_cast<const TypeEncoding*>(offset(this, sizeof(type)));
switch (this->type) {
case BinaryTypeEncodingType::IdEncoding: {
return reinterpret_cast<const TypeEncoding*>(offset(afterTypePtr, sizeof(TypeEncodingDetails::IdDetails)));
}
case BinaryTypeEncodingType::ConstantArrayEncoding: {
return this->details.constantArray.getInnerType()->next();
}
case BinaryTypeEncodingType::ExtVectorEncoding: {
return this->details.extVector.getInnerType()->next();
}
case BinaryTypeEncodingType::IncompleteArrayEncoding: {
return this->details.incompleteArray.getInnerType()->next();
}
case BinaryTypeEncodingType::PointerEncoding: {
return this->details.pointer.getInnerType()->next();
}
case BinaryTypeEncodingType::BlockEncoding: {
const TypeEncoding* current = this->details.block.signature.first();
for (int i = 0; i < this->details.block.signature.count; i++) {
current = current->next();
}
return current;
}
case BinaryTypeEncodingType::FunctionPointerEncoding: {
const TypeEncoding* current = this->details.functionPointer.signature.first();
for (int i = 0; i < this->details.functionPointer.signature.count; i++) {
current = current->next();
}
return current;
}
case BinaryTypeEncodingType::InterfaceDeclarationReference: {
return reinterpret_cast<const TypeEncoding*>(offset(afterTypePtr, sizeof(TypeEncodingDetails::InterfaceDeclarationReferenceDetails)));
}
case BinaryTypeEncodingType::StructDeclarationReference:
case BinaryTypeEncodingType::UnionDeclarationReference: {
return reinterpret_cast<const TypeEncoding*>(offset(afterTypePtr, sizeof(TypeEncodingDetails::DeclarationReferenceDetails)));
}
case BinaryTypeEncodingType::AnonymousStructEncoding:
case BinaryTypeEncodingType::AnonymousUnionEncoding: {
const TypeEncoding* current = this->details.anonymousRecord.getFieldsEncodings();
for (int i = 0; i < this->details.anonymousRecord.fieldsCount; i++) {
current = current->next();
}
return current;
}
default: {
return afterTypePtr;
}
}
}
};
struct ModuleMeta {
public:
uint8_t flags;
String name;
PtrTo<ArrayOfPtrTo<LibraryMeta>> libraries;
const char* getName() const {
return name.valuePtr();
}
bool isFramework() const {
return (flags & 1) > 0;
}
bool isSystem() const {
return (flags & 2) > 0;
}
};
struct LibraryMeta {
public:
uint8_t flags;
String name;
const char* getName() const {
return name.valuePtr();
}
bool isFramework() const {
return (flags & 1) > 0;
}
};
enum NameIndex {
JsName,
Name,
DemangledName,
NameIndexCount,
};
struct JsNameAndNativeNames {
String strings[NameIndexCount];
};
union MetaNames {
String name;
PtrTo<JsNameAndNativeNames> names;
};
struct Meta {
private:
MetaNames _names;
PtrTo<ModuleMeta> _topLevelModule;
uint16_t _flags;
uint8_t _introduced;
public:
inline MetaType type() const {
return (MetaType)(this->_flags & MetaTypeMask);
}
const char* typeName() const {
switch (type()) {
case Undefined:
return "Undefined";
case Struct:
return "Struct";
case Union:
return "Union";
case Function:
return "Function";
case JsCode:
return "JsCode";
case Var:
return "Var";
case Interface:
return "Interface";
case ProtocolType:
return "ProtocolType";
case Vector:
return "Vector";
default:
return "Unknown";
}
}
inline const ModuleMeta* topLevelModule() const {
return this->_topLevelModule.valuePtr();
}
inline bool hasName() const {
return this->flag(MetaFlags::HasName);
}
inline bool hasDemangledName() const {
return this->flag(MetaFlags::HasDemangledName);
}
inline bool flag(int index) const {
return (this->_flags & (1 << index)) > 0;
}
inline const char* jsName() const {
return this->getNameByIndex(JsName);
}
inline const char* name() const {
return this->getNameByIndex(Name);
}
inline const char* demangledName() const {
return this->getNameByIndex(DemangledName);
}
/**
* \brief The version number in which this entity was introduced.
*/
inline uint8_t introducedIn() const {
return this->_introduced;
}
/**
* \brief Checks if the specified object is callable
* from the current device.
*
* To be callable, an object must either:
* > not have platform availability specified;
* > have been introduced in this or prior version;
*/
bool isAvailable() const;
private:
inline const char* getNameByIndex(enum NameIndex index) const {
int i = index;
if (!this->hasName() && !this->hasDemangledName()) {
return this->_names.name.valuePtr();
}
if (!this->hasDemangledName() && i >= DemangledName) {
i--;
}
if (!this->hasName() && i >= Name) {
i--;
}
return this->_names.names.value().strings[i].valuePtr();
}
};
struct RecordMeta : Meta {
private:
PtrTo<Array<String>> _fieldsNames;
PtrTo<TypeEncodingsList<ArrayCount>> _fieldsEncodings;
public:
inline const Array<String>& fieldNames() const {
return _fieldsNames.value();
}
inline size_t fieldsCount() const {
return fieldNames().count;
}
inline const TypeEncodingsList<ArrayCount>* fieldsEncodings() const {
return _fieldsEncodings.valuePtr();
}
};
struct StructMeta : RecordMeta {
};
struct UnionMeta : RecordMeta {
};
struct FunctionMeta : Meta {
private:
PtrTo<TypeEncodingsList<ArrayCount>> _encoding;
public:
bool isVariadic() const {
return this->flag(MetaFlags::FunctionIsVariadic);
}
const TypeEncodingsList<ArrayCount>* encodings() const {
return _encoding.valuePtr();
}
bool ownsReturnedCocoaObject() const {
return this->flag(MetaFlags::FunctionOwnsReturnedCocoaObject);
}
bool returnsUnmanaged() const {
return this->flag(MetaFlags::FunctionReturnsUnmanaged);
}
};
struct JsCodeMeta : Meta {
private:
String _jsCode;
public:
inline const char* jsCode() const {
return _jsCode.valuePtr();
}
};
struct VarMeta : Meta {
private:
PtrTo<TypeEncoding> _encoding;
public:
inline const TypeEncoding* encoding() const {
return _encoding.valuePtr();
}
};
struct MemberMeta : Meta {
inline bool isOptional() const {
return this->flag(MetaFlags::MemberIsOptional);
}
};
struct MethodMeta : MemberMeta {
private:
PtrTo<TypeEncodingsList<ArrayCount>> _encodings;
String _constructorTokens;
public:
inline bool isVariadic() const {
return this->flag(MetaFlags::MethodIsVariadic);
}
inline bool isVariadicNullTerminated() const {
return this->flag(MetaFlags::MethodIsNullTerminatedVariadic);
}
inline bool hasErrorOutParameter() const {
return this->flag(MetaFlags::MethodHasErrorOutParameter);
}
inline bool isInitializer() const {
return this->flag(MetaFlags::MethodIsInitializer);
}
inline bool ownsReturnedCocoaObject() const {
return this->flag(MetaFlags::MethodOwnsReturnedCocoaObject);
}
inline SEL selector() const {
static robin_hood::unordered_map<const MethodMeta*, SEL> methodMetaSelectorCache;
// this method takes a few ns to run and is almost never called by another thread
// this means that locking a mutex is almost always unecessary so we use a spinlock
// that will most likely never spin
static SpinMutex p;
SpinLock lock(p);
SEL ret = nullptr;
auto it = methodMetaSelectorCache.find(this);
if(it != methodMetaSelectorCache.end()) {
return it->second;
}
auto selectorAsStr = this->selectorAsString();
ret = sel_registerName(selectorAsStr);
// save to cache
methodMetaSelectorCache.emplace(this, ret);
return ret;
}
// just a more convenient way to get the selector of method
inline const char* selectorAsString() const {
return this->name();
}
inline const TypeEncodingsList<ArrayCount>* encodings() const {
return this->_encodings.valuePtr();
}
inline const char* constructorTokens() const {
return this->_constructorTokens.valuePtr();
}
bool isImplementedInClass(Class klass, bool isStatic) const;
inline bool isAvailableInClass(Class klass, bool isStatic) const {
return this->isAvailable() && this->isImplementedInClass(klass, isStatic);
}
inline bool isAvailableInClasses(KnownUnknownClassPair klasses, bool isStatic) const {
return this->isAvailableInClass(klasses.known, isStatic) || (klasses.unknown != nullptr && this->isAvailableInClass(klasses.unknown, isStatic));
}
};
typedef std::set<const MemberMeta*> MembersCollection;
robin_hood::unordered_map<std::string, MembersCollection> getMetasByJSNames(MembersCollection methods);
struct PropertyMeta : MemberMeta {
PtrTo<MethodMeta> method1;
PtrTo<MethodMeta> method2;
public:
inline bool hasGetter() const {
return this->flag(MetaFlags::PropertyHasGetter);
}
inline bool hasSetter() const {
return this->flag(MetaFlags::PropertyHasSetter);
}
inline const MethodMeta* getter() const {
return this->hasGetter() ? method1.valuePtr() : nullptr;
}
inline const MethodMeta* setter() const {
return (this->hasSetter()) ? (this->hasGetter() ? method2.valuePtr() : method1.valuePtr()) : nullptr;
}
inline bool isImplementedInClass(Class klass, bool isStatic) const {
bool getterAvailable = this->hasGetter() && this->getter()->isImplementedInClass(klass, isStatic);
bool setterAvailable = this->hasSetter() && this->setter()->isImplementedInClass(klass, isStatic);
return getterAvailable || setterAvailable;
}
inline bool isAvailableInClass(Class klass, bool isStatic) const {
return this->isAvailable() && this->isImplementedInClass(klass, isStatic);
}
inline bool isAvailableInClasses(KnownUnknownClassPair klasses, bool isStatic) const {
return this->isAvailableInClass(klasses.known, isStatic) || (klasses.unknown != nullptr && this->isAvailableInClass(klasses.unknown, isStatic));
}
};
struct BaseClassMeta : Meta {
PtrTo<ArrayOfPtrTo<MethodMeta>> instanceMethods;
PtrTo<ArrayOfPtrTo<MethodMeta>> staticMethods;
PtrTo<ArrayOfPtrTo<PropertyMeta>> instanceProps;
PtrTo<ArrayOfPtrTo<PropertyMeta>> staticProps;
PtrTo<Array<String>> protocols;
int16_t initializersStartIndex;
template <typename T>
void forEachProtocol(const T& fun, const ProtocolMetas* additionalProtocols) const {
for (Array<String>::iterator it = this->protocols->begin(); it != this->protocols->end(); ++it) {
if (const ProtocolMeta* protocolMeta = MetaFile::instance()->globalTableJs()->findProtocol((*it).valuePtr())) {
fun(protocolMeta);
}
}
if (additionalProtocols) {
for (const ProtocolMeta* protocolMeta : *additionalProtocols) {
fun(protocolMeta);
}
}
}
std::set<const ProtocolMeta*> protocolsSet() const;
std::set<const ProtocolMeta*> deepProtocolsSet() const;
void deepProtocolsSet(std::set<const ProtocolMeta*>& protocols) const;
const MemberMeta* member(const char* identifier, size_t length, MemberType type, bool includeProtocols, bool onlyIfAvailable, const ProtocolMetas& additionalProtocols) const;
const MethodMeta* member(const char* identifier, size_t length, MemberType type, size_t paramsCount, bool includeProtocols, bool onlyIfAvailable, const ProtocolMetas& additionalProtocols) const;
const MembersCollection members(const char* identifier, size_t length, MemberType type, bool includeProtocols, bool onlyIfAvailable, const ProtocolMetas& additionalProtocols) const;
const MemberMeta* member(const char* identifier, MemberType type, bool includeProtocols, const ProtocolMetas& additionalProtocols) const {
return this->member(identifier, strlen(identifier), type, includeProtocols, /*onlyIfAvailable*/ true, additionalProtocols);
}
/// instance methods
// Remove all optional methods/properties which are not implemented in the class
template <typename TMemberMeta>
static void filterUnavailableMembers(MembersCollection& members, KnownUnknownClassPair klasses, bool isStatic) {
for (auto it{members.begin()}, end{members.end()}; it != end;) {
const MemberMeta* memberMeta = *it;
bool isAvailable = static_cast<const TMemberMeta*>(memberMeta)->isAvailableInClasses(klasses, isStatic);
if (!isAvailable) {
it = members.erase(it);
}
else {
++it;
}
}
}
/// instance properties
const PropertyMeta* instanceProperty(const char* identifier, KnownUnknownClassPair klasses, bool includeProtocols, const ProtocolMetas& additionalProtocols) const {
auto propMeta = static_cast<const PropertyMeta*>(this->member(identifier, MemberType::InstanceProperty, includeProtocols, additionalProtocols));
return propMeta && propMeta->isAvailableInClasses(klasses, /*isStatic*/ false) ? propMeta : nullptr;
}
/// static properties
const PropertyMeta* staticProperty(const char* identifier, KnownUnknownClassPair klasses, bool includeProtocols, const ProtocolMetas& additionalProtocols) const {
auto propMeta = static_cast<const PropertyMeta*>(this->member(identifier, MemberType::StaticProperty, includeProtocols, additionalProtocols));
return propMeta && propMeta->isAvailableInClasses(klasses, /*isStatic*/ true) ? propMeta : nullptr;
}
/// vectors
std::vector<const PropertyMeta*> instanceProperties(KnownUnknownClassPair klasses) const {
std::vector<const PropertyMeta*> properties;
return this->instanceProperties(properties, klasses);
}
std::vector<const PropertyMeta*> instancePropertiesWithProtocols(KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const {
std::vector<const PropertyMeta*> properties;
return this->instancePropertiesWithProtocols(properties, klasses, additionalProtocols);
}
std::vector<const PropertyMeta*> instanceProperties(std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses) const {
for (Array<PtrTo<PropertyMeta>>::iterator it = this->instanceProps->begin(); it != this->instanceProps->end(); it++) {
if ((*it)->isAvailableInClasses(klasses, /*isStatic*/ false)) {
container.push_back((*it).valuePtr());
}
}
return container;
}
std::vector<const PropertyMeta*> instancePropertiesWithProtocols(std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const;
std::vector<const PropertyMeta*> staticProperties(KnownUnknownClassPair klasses) const {
std::vector<const PropertyMeta*> properties;
return this->staticProperties(properties, klasses);
}
std::vector<const PropertyMeta*> staticPropertiesWithProtocols(KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const {
std::vector<const PropertyMeta*> properties;
return this->staticPropertiesWithProtocols(properties, klasses, additionalProtocols);
}
std::vector<const PropertyMeta*> staticProperties(std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses) const {
for (Array<PtrTo<PropertyMeta>>::iterator it = this->staticProps->begin(); it != this->staticProps->end(); it++) {
if ((*it)->isAvailableInClasses(klasses, /*isStatic*/ true)) {
container.push_back((*it).valuePtr());
}
}
return container;
}
std::vector<const PropertyMeta*> staticPropertiesWithProtocols(std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const;
std::vector<const MethodMeta*> initializers(KnownUnknownClassPair klasses) const {
std::vector<const MethodMeta*> initializers;
return this->initializers(initializers, klasses);
}
std::vector<const MethodMeta*> initializersWithProtocols(KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const {
std::vector<const MethodMeta*> initializers;
return this->initializersWithProtocols(initializers, klasses, additionalProtocols);
}
std::vector<const MethodMeta*> initializers(std::vector<const MethodMeta*>& container, KnownUnknownClassPair klasses) const;
std::vector<const MethodMeta*> initializersWithProtocols(std::vector<const MethodMeta*>& container, KnownUnknownClassPair klasses, const ProtocolMetas& additionalProtocols) const;
};
struct ProtocolMeta : BaseClassMeta {
};
struct InterfaceMeta : BaseClassMeta {
private:
String _baseName;
public:
const char* baseName() const {
return _baseName.valuePtr();
}
const InterfaceMeta* baseMeta() const {
if (this->baseName() != nullptr) {
const InterfaceMeta* baseMeta = MetaFile::instance()->globalTableJs()->findInterfaceMeta(this->baseName());
return baseMeta;
}
return nullptr;
}
};
#pragma pack(pop)
} // namespace tns
#include "MetadataInlines.h"
#endif /* Metadata_h */