Repository navigation
Expand file tree
/
Copy pathClassBuilder.mm
More file actions
276 lines (227 loc) · 8.87 KB
/
Copy pathClassBuilder.mm
File metadata and controls
276 lines (227 loc) · 8.87 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
#include "ClassBuilder.h"
#include "Metadata.h"
#include "ObjCBridge.h"
#include "Util.h"
#include "js_native_api.h"
#include "node_api_util.h"
#import <Foundation/Foundation.h>
#include <objc/runtime.h>
namespace objc_bridge {
ClassBuilder::ClassBuilder(napi_env env, napi_value constructor) {
this->env = env;
bridgeState = ObjCBridgeState::InstanceData(env);
metadataOffset = MD_SECTION_OFFSET_NULL;
napi_value superConstructor;
napi_get_prototype(env, constructor, &superConstructor);
Class superClassNative = nullptr;
napi_unwrap(env, superConstructor, (void **)&superClassNative);
if (superClassNative == nullptr) {
// If the class does not inherit from a native class,
// by default it inherits from NSObject.
superClassNative = [NSObject class];
}
superclass = bridgeState->classesByPointer[superClassNative];
napi_value className;
napi_get_named_property(env, constructor, "name", &className);
static char classNameBuf[512];
napi_get_value_string_utf8(env, className, classNameBuf, 512, nullptr);
name = classNameBuf;
name += "_";
name += std::to_string(rand());
nativeClass = objc_allocateClassPair(superClassNative, name.c_str(), 0);
if (nativeClass == nullptr) {
napi_throw_error(env, nullptr, "Failed to allocate class");
return;
}
class_addProtocol(nativeClass, @protocol(ObjCBridgeClassBuilderProtocol));
objc_registerClassPair(nativeClass);
napi_remove_wrap(env, constructor, nullptr);
napi_wrap(env, constructor, (void *)nativeClass, nullptr, nullptr, nullptr);
napi_value prototype;
napi_get_named_property(env, constructor, "prototype", &prototype);
this->constructor = make_ref(env, constructor);
this->prototype = make_ref(env, prototype);
}
ClassBuilder::~ClassBuilder() {
if (nativeClass != nullptr) {
objc_disposeClassPair(nativeClass);
napi_delete_reference(env, constructor);
napi_delete_reference(env, prototype);
}
}
void ClassBuilder::addProtocol(ObjCProtocol *protocol) {
if (!protocols.contains(protocol)) {
protocols.emplace(protocol);
// Not always there is a `Protocol` object for the protocol, so we
// need to check if it exists first.
Protocol *proto = objc_getProtocol(protocol->name.c_str());
if (proto != nullptr) {
class_addProtocol(nativeClass, proto);
}
}
}
MethodDescriptor *ClassBuilder::lookupMethodDescriptor(std::string &name) {
// 1. First we look up if there was a custom definition for the method
// in ObjCExposedMethods static of the custom class.
auto findExposedMethod = exposedMethods.find(name);
if (findExposedMethod != exposedMethods.end()) {
return &findExposedMethod->second;
}
// 2. Then walk through the class hierarchy and see if we can find the
// method in the superclass chain.
ObjCClass *currentClass = superclass;
while (currentClass != nullptr) {
auto findMethod = currentClass->members.find(name);
if (findMethod != currentClass->members.end() &&
!findMethod->second.classMethod &&
!findMethod->second.methodOrGetter.isProperty) {
return &findMethod->second.methodOrGetter;
}
currentClass = currentClass->superclass;
}
// 3. And finally, look into all protocols implemented (directly or
// indirectly) and try to find the method there.
std::function<MethodDescriptor *(std::unordered_set<ObjCProtocol *> &
protocols)>
processProtocols = [&](std::unordered_set<ObjCProtocol *> &protocols)
-> MethodDescriptor * {
for (auto protocol : protocols) {
auto findMethod = protocol->members.find(name);
if (findMethod != protocol->members.end() &&
!findMethod->second.classMethod &&
!findMethod->second.methodOrGetter.isProperty) {
return &findMethod->second.methodOrGetter;
}
MethodDescriptor *desc = processProtocols(protocol->protocols);
if (desc != nullptr)
return desc;
}
return (MethodDescriptor *)nullptr;
};
return processProtocols(protocols);
}
void ClassBuilder::addMethod(std::string &name, MethodDescriptor *desc,
napi_value key, napi_value func) {
switch (desc->kind) {
case kMethodDescEncoding: {
const char *encoding = desc->encoding.c_str();
auto closure = new Closure(encoding, false);
closure->env = env;
if (func != nullptr)
closure->func = make_ref(env, func);
else
closure->propertyName = name;
closure->thisConstructor = constructor;
class_replaceMethod(nativeClass, desc->selector, (IMP)closure->fnptr,
encoding);
break;
}
case kMethodDescSignatureOffset: {
std::string encoding;
auto closure = new Closure(bridgeState->metadata, desc->signatureOffset,
false, &encoding, true, desc->isProperty);
closure->env = env;
if (func != nullptr)
closure->func = make_ref(env, func);
else
closure->propertyName = name;
closure->thisConstructor = constructor;
class_replaceMethod(nativeClass, desc->selector, (IMP)closure->fnptr,
encoding.c_str());
break;
}
}
}
void ClassBuilder::build() {
if (isFinal)
return;
isFinal = true;
napi_value constructor = get_ref_value(env, this->constructor),
prototype = get_ref_value(env, this->prototype);
// 1. Extract method definitions from ObjCExposedMethods static
napi_value exposedMethods, exposedMethodNames;
bool hasExposedMethods = false;
napi_has_named_property(env, constructor, "ObjCExposedMethods",
&hasExposedMethods);
if (hasExposedMethods) {
napi_get_named_property(env, constructor, "ObjCExposedMethods",
&exposedMethods);
napi_get_all_property_names(
env, exposedMethods, napi_key_own_only, napi_key_skip_symbols,
napi_key_numbers_to_strings, &exposedMethodNames);
uint32_t exposedMethodCount = 0;
napi_get_array_length(env, exposedMethodNames, &exposedMethodCount);
for (uint32_t i = 0; i < exposedMethodCount; i++) {
napi_value exposedMethodName;
napi_get_element(env, exposedMethodNames, i, &exposedMethodName);
static char exposedMethodNameBuf[512];
napi_get_value_string_utf8(env, exposedMethodName, exposedMethodNameBuf,
512, nullptr);
std::string name = exposedMethodNameBuf;
SEL selector = sel_registerName(name.c_str());
std::string encoding;
napi_value def, params, returns;
napi_get_named_property(env, exposedMethods, name.c_str(), &def);
napi_get_named_property(env, def, "params", ¶ms);
napi_get_named_property(env, def, "returns", &returns);
uint32_t paramCount = 0;
napi_get_array_length(env, params, ¶mCount);
encoding += getEncodedType(env, returns);
encoding += "@:";
for (uint32_t j = 0; j < paramCount; j++) {
napi_value param;
napi_get_element(env, params, j, ¶m);
std::string enctype = getEncodedType(env, param);
if (enctype == "v") {
napi_throw_error(env, nullptr,
"Void type not allowed in method params");
return;
}
encoding += enctype;
}
this->exposedMethods[name] = MethodDescriptor(selector, encoding);
}
}
// 2. Extract the protocol references from ObjCProtocols static
napi_value protocols, protocol;
bool hasProtocols = false;
napi_has_named_property(env, constructor, "ObjCProtocols", &hasProtocols);
if (hasProtocols) {
napi_get_named_property(env, constructor, "ObjCProtocols", &protocols);
uint32_t i = 0;
uint32_t length = 0;
napi_get_array_length(env, protocols, &length);
while (i < length) {
napi_get_element(env, protocols, i, &protocol);
static char protocolNameBuf[512];
napi_get_value_string_utf8(env, protocol, protocolNameBuf, 512, nullptr);
ObjCProtocol *proto = nullptr;
napi_unwrap(env, protocol, (void **)&proto);
if (proto != nullptr)
addProtocol(proto);
i++;
}
}
// 3. Walk over methods defined on the prototype and add them to the ObjC
// class if we can find the corresponding method descriptor.
napi_value properties;
napi_get_all_property_names(env, prototype, napi_key_own_only,
napi_key_skip_symbols,
napi_key_numbers_to_strings, &properties);
uint32_t propertyCount = 0;
napi_get_array_length(env, properties, &propertyCount);
uint32_t i = 0;
while (i < propertyCount) {
napi_value property;
napi_get_element(env, properties, i, &property);
static char propertyNameBuf[512];
napi_get_value_string_utf8(env, property, propertyNameBuf, 512, nullptr);
std::string name = propertyNameBuf;
MethodDescriptor *desc = lookupMethodDescriptor(name);
if (desc != nullptr) {
addMethod(name, desc, property);
}
i++;
}
}
} // namespace objc_bridge