-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathMetadata.mm
More file actions
350 lines (308 loc) · 13.5 KB
/
Copy pathMetadata.mm
File metadata and controls
350 lines (308 loc) · 13.5 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
#include "Metadata.h"
#include <UIKit/UIKit.h>
#include <sys/stat.h>
#include "Helpers.h"
#include "SymbolLoader.h"
#include "UnfairLock.h"
namespace tns {
using namespace std;
void LogMetadataUnavailable(const char* identifierString, uint8_t majorVersion,
uint8_t minorVersion, const char* baseName) {
Log(@"** \"%s\" introduced in iOS SDK %d.%d is currently unavailable, attempting to load its "
@"base: \"%s\". **",
identifierString, majorVersion, minorVersion, baseName);
}
/**
* \brief Gets the system version of the current device, expressed on the iOS
* version scale used by metadata introducedIn values.
*/
static UInt8 getSystemVersion() {
static UInt8 iosVersion;
if (iosVersion != 0) {
return iosVersion;
}
NSString* version = [[UIDevice currentDevice] systemVersion];
NSArray* versionTokens =
[version componentsSeparatedByCharactersInSet:[NSCharacterSet
characterSetWithCharactersInString:@"."]];
UInt8 majorVersion = (UInt8)[versionTokens[0] intValue];
UInt8 minorVersion = (UInt8)[versionTokens[1] intValue];
#if TARGET_OS_VISION
// Metadata availability is expressed in iOS versions. visionOS majors
// before the unified 26 numbering trail iOS by 16 (visionOS 1.x ~ iOS
// 17.x, visionOS 2.x ~ iOS 18.x); from 26 onward the numbering aligns.
if (majorVersion < 26) {
majorVersion += 16;
}
#endif
return iosVersion = encodeVersion(majorVersion, minorVersion);
}
robin_hood::unordered_map<std::string, MembersCollection> getMetasByJSNames(
MembersCollection members) {
robin_hood::unordered_map<std::string, MembersCollection> result;
for (auto member : members) {
result[member->jsName()].insert(member);
}
return result;
}
// Meta
bool Meta::isAvailable() const {
UInt8 introducedIn = this->introducedIn();
UInt8 systemVersion = getSystemVersion();
return introducedIn == 0 || introducedIn <= systemVersion;
}
// MethodMeta class
bool MethodMeta::isImplementedInClass(Class klass, bool isStatic) const {
// class can be null for Protocol prototypes, treat all members in a protocol as implemented
if (klass == nullptr) {
return true;
}
// Some members are implemented by extension of classes defined in a different
// module than the class, ensure they've been initialized
SymbolLoader::instance().ensureModule(this->topLevelModule());
if (isStatic) {
return [klass respondsToSelector:this->selector()] ||
([klass resolveClassMethod:this->selector()]);
} else {
if ([klass instancesRespondToSelector:this->selector()] ||
[klass resolveInstanceMethod:this->selector()]) {
return true;
}
// Last resort - allocate an object and ask it if it supports the selector.
// There are two kinds of scenarios that need this additional check:
// 1. The `alloc` method is overridden and returns an instance of another class
// E.g. [NSAttributedString alloc] returns a NSConcreteAttributedString*
// 2. The message is forwarded to another object. E.g. `UITextField` forwards
// `autocapitalizationType` to an instance of `UITextInputTraits`
static robin_hood::unordered_map<Class, id> sampleInstances;
// The lock is never held across [klass alloc]: alloc can trigger
// +initialize, which may run arbitrary code that re-enters this method.
static UnfairMutex sampleInstancesMutex;
id sampleInstance = nil;
bool cached = false;
{
std::lock_guard<UnfairMutex> lock(sampleInstancesMutex);
auto it = sampleInstances.find(klass);
if (it != sampleInstances.end()) {
sampleInstance = it->second;
cached = true;
}
}
if (!cached) {
@try {
id instance = [klass alloc];
std::lock_guard<UnfairMutex> lock(sampleInstancesMutex);
// A losing emplace (a +initialize re-entry or another thread populated
// the entry first) LEAKS `instance`, knowingly: it was never init'd, so
// releasing it would run -dealloc against zero-filled ivars of an
// arbitrary class on an arbitrary thread.
// https://github.com/NativeScript/ios/issues/459
sampleInstance = sampleInstances.emplace(klass, instance).first->second;
} @catch (id err) {
return false;
}
}
return [sampleInstance respondsToSelector:this->selector()];
}
}
// BaseClassMeta
std::set<const ProtocolMeta*> BaseClassMeta::protocolsSet() const {
std::set<const ProtocolMeta*> protocols;
this->forEachProtocol(
[&protocols](const ProtocolMeta* protocolMeta) { protocols.insert(protocolMeta); },
/*additionalProtocols*/ nullptr);
return protocols;
}
std::set<const ProtocolMeta*> BaseClassMeta::deepProtocolsSet() const {
std::set<const ProtocolMeta*> protocols;
this->deepProtocolsSet(protocols);
return protocols;
}
void BaseClassMeta::deepProtocolsSet(std::set<const ProtocolMeta*>& protocols) const {
if (this->type() == Interface) {
auto interfaceMeta = static_cast<const InterfaceMeta*>(this);
if (auto baseMeta = interfaceMeta->baseMeta()) {
baseMeta->deepProtocolsSet(protocols);
}
}
this->forEachProtocol(
[&protocols](const ProtocolMeta* protocolMeta) {
protocolMeta->deepProtocolsSet(protocols);
protocols.insert(protocolMeta);
},
/*additionalProtocols*/ nullptr);
}
const MemberMeta* BaseClassMeta::member(const char* identifier, size_t length, MemberType type,
bool includeProtocols, bool onlyIfAvailable,
const ProtocolMetas& additionalProtocols) const {
MembersCollection members = this->members(identifier, length, type, includeProtocols,
onlyIfAvailable, additionalProtocols);
// It's expected to receive only one occurence when member is used. If more than one results can
// be found consider (1) using BaseClassMeta::members to process all of them; or (2) fixing
// metadata generator to disambiguate and remove the redundant one(s); or (3) modify this method
// so that it doesn't arbitrary choose one and drop the other(s) but deterministically decides
// which one has to be returned.
ASSERT(members.size() <= 1);
return members.size() > 0 ? *members.begin() : nullptr;
}
void collectInheritanceChainMembers(const char* identifier, size_t length, MemberType type,
bool onlyIfAvailable, const BaseClassMeta* meta,
std::function<void(const MemberMeta*)> collectMember) {
const ArrayOfPtrTo<MemberMeta>* members = nullptr;
// Scan method overloads (methods with different selectors and number of arguments which have the
// same jsName) in base classes. Properties cannot be overridden like that so there's no need to
// traverse the hierarchy.
bool shouldScanForOverrides = true;
switch (type) {
case MemberType::InstanceMethod:
members = &meta->instanceMethods->castTo<PtrTo<MemberMeta>>();
break;
case MemberType::StaticMethod:
members = &meta->staticMethods->castTo<PtrTo<MemberMeta>>();
break;
case MemberType::InstanceProperty:
shouldScanForOverrides = false;
members = &meta->instanceProps->castTo<PtrTo<MemberMeta>>();
break;
case MemberType::StaticProperty:
shouldScanForOverrides = false;
members = &meta->staticProps->castTo<PtrTo<MemberMeta>>();
break;
}
int resultIndex = -1;
resultIndex = members->binarySearchLeftmost([&](const PtrTo<MemberMeta>& member) {
return compareIdentifiers(member->jsName(), identifier, length);
});
if (resultIndex >= 0) {
for (const MemberMeta* m = (*members)[resultIndex].valuePtr();
resultIndex < members->count &&
(strncmp(m->jsName(), identifier, length) == 0 && strlen(m->jsName()) == length);
m = (*members)[++resultIndex].valuePtr()) {
if (m->isAvailable() || !onlyIfAvailable) {
collectMember(m);
}
}
if (shouldScanForOverrides && meta->type() == MetaType::Interface) {
const BaseClassMeta* superClass = static_cast<const InterfaceMeta*>(meta)->baseMeta();
if (superClass) {
collectInheritanceChainMembers(identifier, length, type, onlyIfAvailable, superClass,
collectMember);
}
}
}
// Instance members of NSObject can be called as static as well (e.g. [NSString
// performSelector:@selector(alloc)]
static const char* nsObject = "NSObject";
static const size_t nsObjectLen = strlen(nsObject);
if (strncmp(meta->name(), nsObject, nsObjectLen + 1) == 0) {
if (type == MemberType::StaticMethod) {
collectInheritanceChainMembers(identifier, length, MemberType::InstanceMethod,
onlyIfAvailable, meta, collectMember);
} else if (type == MemberType::StaticProperty) {
collectInheritanceChainMembers(identifier, length, MemberType::InstanceProperty,
onlyIfAvailable, meta, collectMember);
}
}
}
const MembersCollection BaseClassMeta::members(const char* identifier, size_t length,
MemberType type, bool includeProtocols,
bool onlyIfAvailable,
const ProtocolMetas& additionalProtocols) const {
MembersCollection result;
if (type == MemberType::InstanceMethod || type == MemberType::StaticMethod) {
// We need to return base class members as well. Otherwise,
// if an overloaded method is overriden by a derived class
// the FunctionWrapper's *functionsContainer* will contain
// overriden members metas only.
std::map<int, const MemberMeta*> membersMap;
collectInheritanceChainMembers(
identifier, length, type, onlyIfAvailable, this, [&](const MemberMeta* member) {
const MethodMeta* method = static_cast<const MethodMeta*>(member);
ArrayCount count = method->encodings()->count;
membersMap.emplace(count, member);
});
for (std::map<int, const MemberMeta*>::iterator it = membersMap.begin(); it != membersMap.end();
++it) {
result.insert(it->second);
}
} else { // member is a property
collectInheritanceChainMembers(identifier, length, type, onlyIfAvailable, this,
[&](const MemberMeta* member) { result.insert(member); });
}
if (result.size() > 0) {
return result;
}
// search in protocols
if (includeProtocols) {
this->forEachProtocol(
[&result, identifier, length, type, includeProtocols,
onlyIfAvailable](const ProtocolMeta* protocolMeta) {
const MembersCollection members = protocolMeta->members(
identifier, length, type, includeProtocols, onlyIfAvailable, ProtocolMetas());
result.insert(members.begin(), members.end());
},
&additionalProtocols);
}
return result;
}
std::vector<const PropertyMeta*> BaseClassMeta::instancePropertiesWithProtocols(
std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses,
const ProtocolMetas& additionalProtocols) const {
this->instanceProperties(container, klasses);
this->forEachProtocol(
[&container, klasses](const ProtocolMeta* protocolMeta) {
protocolMeta->instancePropertiesWithProtocols(container, klasses, ProtocolMetas());
},
&additionalProtocols);
return container;
}
std::vector<const PropertyMeta*> BaseClassMeta::staticPropertiesWithProtocols(
std::vector<const PropertyMeta*>& container, KnownUnknownClassPair klasses,
const ProtocolMetas& additionalProtocols) const {
this->staticProperties(container, klasses);
this->forEachProtocol(
[&container, klasses](const ProtocolMeta* protocolMeta) {
protocolMeta->staticPropertiesWithProtocols(container, klasses, ProtocolMetas());
},
&additionalProtocols);
return container;
}
vector<const MethodMeta*> BaseClassMeta::initializers(vector<const MethodMeta*>& container,
KnownUnknownClassPair klasses) const {
// search in instance methods
int16_t firstInitIndex = this->initializersStartIndex;
if (firstInitIndex != -1) {
for (int i = firstInitIndex; i < instanceMethods->count; i++) {
const MethodMeta* method = instanceMethods.value()[i].valuePtr();
if (!method->isInitializer()) {
break;
}
if (method->isAvailableInClasses(klasses, /*isStatic*/ false)) {
container.push_back(method);
}
}
}
return container;
}
vector<const MethodMeta*> BaseClassMeta::initializersWithProtocols(
vector<const MethodMeta*>& container, KnownUnknownClassPair klasses,
const ProtocolMetas& additionalProtocols) const {
this->initializers(container, klasses);
for (Array<String>::iterator it = this->protocols->begin(); it != this->protocols->end(); it++) {
const ProtocolMeta* protocolMeta =
MetaFile::instance()->globalTableJs()->findProtocol((*it).valuePtr());
if (protocolMeta != nullptr)
protocolMeta->initializersWithProtocols(container, klasses, ProtocolMetas());
}
for (const ProtocolMeta* protocolMeta : additionalProtocols) {
protocolMeta->initializersWithProtocols(container, klasses, ProtocolMetas());
}
return container;
}
static MetaFile* metaFileInstance(nullptr);
MetaFile* MetaFile::instance() { return metaFileInstance; }
MetaFile* MetaFile::setInstance(void* metadataPtr) {
metaFileInstance = reinterpret_cast<MetaFile*>(metadataPtr);
return metaFileInstance;
}
} // namespace tns