Repository navigation
Expand file tree
/
Copy pathClassMember.mm
More file actions
405 lines (321 loc) · 12 KB
/
Copy pathClassMember.mm
File metadata and controls
405 lines (321 loc) · 12 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
#include "ClassMember.h"
#include "ClassBuilder.h"
#include "MetadataReader.h"
#include "ObjCBridge.h"
#include "TypeConv.h"
#include "Util.h"
#include "js_native_api.h"
#include "js_native_api_types.h"
#include "node_api_util.h"
#import <Foundation/Foundation.h>
#include <cstring>
#include <objc/objc.h>
#include <objc/runtime.h>
namespace objc_bridge {
napi_value JS_NSObject_alloc(napi_env env, napi_callback_info cbinfo) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
id self;
napi_unwrap(env, jsThis, (void **)&self);
bool supercall = class_conformsToProtocol(self, @protocol(ObjCBridgeClassBuilderProtocol));
if (supercall) {
ObjCBridgeState *state = ObjCBridgeState::InstanceData(env);
ClassBuilder *builder = (ClassBuilder *)state->classesByPointer[self];
if (!builder->isFinal)
builder->build();
}
id result = [self alloc];
return ObjCBridgeState::InstanceData(env)->getObject(env, result, jsThis,
kOwnedObject);
}
void ObjCClassMember::defineMembers(napi_env env, ObjCClassMemberMap &memberMap,
MDSectionOffset offset,
napi_value constructor) {
auto bridgeState = ObjCBridgeState::InstanceData(env);
napi_value prototype;
napi_get_named_property(env, constructor, "prototype", &prototype);
bool next = true;
while (next) {
auto flags = bridgeState->metadata->getMemberFlag(offset);
if (flags == mdMemberFlagNull)
break;
next = (flags & mdMemberNext) != 0;
offset += sizeof(flags);
napi_value jsObject;
if ((flags & mdMemberStatic) != 0) {
jsObject = constructor;
} else {
jsObject = prototype;
}
if ((flags & mdMemberProperty) != 0) {
bool readonly = (flags & mdMemberReadonly) != 0;
const char *name = bridgeState->metadata->getString(offset);
offset += sizeof(MDSectionOffset); // name
MDSectionOffset getterSignature, setterSignature;
const char *getterSelector = bridgeState->metadata->getString(offset);
offset += sizeof(MDSectionOffset); // getterSelector
getterSignature = bridgeState->metadata->getOffset(offset);
offset += sizeof(MDSectionOffset); // getterSignature
const char *setterSelector = nullptr;
if (!readonly) {
setterSelector = bridgeState->metadata->getString(offset);
offset += sizeof(MDSectionOffset); // setterSelector
setterSignature = bridgeState->metadata->getOffset(offset);
offset += sizeof(MDSectionOffset); // setterSignature
}
if (memberMap.contains(name)) {
memberMap.erase(name);
}
const auto &kv = memberMap.emplace(
name, ObjCClassMember(
bridgeState, sel_registerName(getterSelector),
!readonly ? sel_registerName(setterSelector) : nullptr,
getterSignature + bridgeState->metadata->signaturesOffset,
!readonly ? setterSignature +
bridgeState->metadata->signaturesOffset
: 0,
flags));
napi_property_descriptor property = {
.utf8name = name,
.name = nil,
.method = nil,
.getter = jsGetter,
.setter = readonly ? nil : jsSetter,
.value = nil,
.attributes =
(napi_property_attributes)(napi_configurable | napi_enumerable),
.data = &kv.first->second,
};
napi_define_properties(env, jsObject, 1, &property);
} else {
auto selector = bridgeState->metadata->getString(offset);
offset += sizeof(MDSectionOffset); // selector
auto signature = bridgeState->metadata->getOffset(offset);
offset += sizeof(MDSectionOffset); // signature
auto name = jsifySelector(selector);
bool hasProperty = false;
napi_has_named_property(env, jsObject, name.c_str(), &hasProperty);
if (hasProperty) {
continue;
}
const auto &kv = memberMap.emplace(
name,
ObjCClassMember(bridgeState, sel_registerName(selector),
signature + bridgeState->metadata->signaturesOffset,
flags));
napi_property_descriptor property = {
.utf8name = name.c_str(),
.name = nil,
.method =
(flags & metagen::mdMemberIsInit) != 0 ? jsCallInit : jsCall,
.getter = nil,
.setter = nil,
.value = nil,
.attributes =
(napi_property_attributes)(napi_configurable | napi_writable |
napi_enumerable),
.data = &kv.first->second,
};
if (name == "alloc") {
property.method = JS_NSObject_alloc;
}
napi_define_properties(env, jsObject, 1, &property);
}
}
}
inline void objcNativeCall(napi_env env, napi_value jsThis, Cif *cif, id self,
void **avalues, void *rvalue) {
bool classMethod = class_isMetaClass(object_getClass(self));
bool supercall =
classMethod
? class_conformsToProtocol(self,
@protocol(ObjCBridgeClassBuilderProtocol))
: class_conformsToProtocol(object_getClass(self),
@protocol(ObjCBridgeClassBuilderProtocol));
if (supercall && classMethod) {
ObjCBridgeState *state = ObjCBridgeState::InstanceData(env);
ClassBuilder *builder = (ClassBuilder *)state->classesByPointer[self];
if (!builder->isFinal)
builder->build();
}
#if defined(__x86_64__)
bool isStret = cif->returnType->type->size > 16 &&
cif->returnType->type->type == FFI_TYPE_STRUCT;
#endif
if (!supercall) {
#if defined(__x86_64__)
if (isStret) {
ffi_call(&cif->cif, FFI_FN(objc_msgSend_stret), rvalue, avalues);
} else {
ffi_call(&cif->cif, FFI_FN(objc_msgSend), rvalue, avalues);
}
#else
ffi_call(&cif->cif, FFI_FN(objc_msgSend), rvalue, avalues);
#endif
} else {
struct objc_super superobj = {self,
class_getSuperclass(object_getClass(self))};
auto superobjPtr = &superobj;
avalues[0] = (void *)&superobjPtr;
#if defined(__x86_64__)
if (isStret) {
ffi_call(&cif->cif, FFI_FN(objc_msgSendSuper_stret), rvalue, avalues);
} else {
ffi_call(&cif->cif, FFI_FN(objc_msgSendSuper), rvalue, avalues);
}
#else
ffi_call(&cif->cif, FFI_FN(objc_msgSendSuper), rvalue, avalues);
#endif
}
}
napi_value ObjCClassMember::jsCallInit(napi_env env,
napi_callback_info cbinfo) {
napi_value jsThis;
ObjCClassMember *method;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, (void **)&method);
id self = nil;
napi_unwrap(env, jsThis, (void **)&self);
if (self == nil) {
napi_throw_error(env, nullptr, "self is nil");
return nullptr;
}
Cif *cif = method->cif;
if (cif == nullptr) {
cif = method->cif = method->bridgeState->getMethodCif(
env, method->methodOrGetter.signatureOffset);
}
size_t argc = cif->argc;
napi_get_cb_info(env, cbinfo, &argc, cif->argv, &jsThis, nullptr);
void *avalues[cif->cif.nargs];
avalues[0] = (void *)&self;
avalues[1] = (void *)&method->methodOrGetter.selector;
bool shouldFreeAny = false;
bool shouldFree[cif->argc];
if (cif->argc > 0) {
for (unsigned int i = 0; i < cif->argc; i++) {
shouldFree[i] = false;
avalues[i + 2] = cif->avalues[i];
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i + 2],
&shouldFree[i], &shouldFreeAny);
}
}
id rvalue;
objcNativeCall(env, jsThis, cif, self, avalues, &rvalue);
for (unsigned int i = 0; i < cif->argc; i++) {
if (shouldFree[i]) {
cif->argTypes[i]->free(env, *((void **)avalues[i + 2]));
}
}
if (rvalue == nil) {
napi_value result;
napi_get_null(env, &result);
return result;
}
napi_value constructor = jsThis;
if (!method->classMethod)
napi_get_named_property(env, jsThis, "constructor", &constructor);
napi_value result =
method->bridgeState->getObject(env, rvalue, constructor, kUnownedObject);
if (rvalue != self) {
[self retain];
[rvalue release];
}
return result;
}
napi_value ObjCClassMember::jsCall(napi_env env, napi_callback_info cbinfo) {
napi_value jsThis;
ObjCClassMember *method;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, (void **)&method);
id self = nil;
napi_unwrap(env, jsThis, (void **)&self);
if (self == nil) {
napi_throw_error(env, nullptr, "self is nil");
return nullptr;
}
Cif *cif = method->cif;
if (cif == nullptr) {
cif = method->cif = method->bridgeState->getMethodCif(
env, method->methodOrGetter.signatureOffset);
}
size_t argc = cif->argc;
napi_get_cb_info(env, cbinfo, &argc, cif->argv, &jsThis, nullptr);
void *avalues[cif->cif.nargs];
void *rvalue = cif->rvalue;
avalues[0] = (void *)&self;
avalues[1] = (void *)&method->methodOrGetter.selector;
bool shouldFreeAny = false;
bool shouldFree[cif->argc];
if (cif->argc > 0) {
for (unsigned int i = 0; i < cif->argc; i++) {
shouldFree[i] = false;
avalues[i + 2] = cif->avalues[i];
cif->argTypes[i]->toNative(env, cif->argv[i], avalues[i + 2],
&shouldFree[i], &shouldFreeAny);
}
}
objcNativeCall(env, jsThis, cif, self, avalues, rvalue);
for (unsigned int i = 0; i < cif->argc; i++) {
if (shouldFree[i]) {
cif->argTypes[i]->free(env, *((void **)avalues[i + 2]));
}
}
if (cif->returnType->kind == mdTypeInstanceObject) {
napi_value constructor = jsThis;
if (!method->classMethod)
napi_get_named_property(env, jsThis, "constructor", &constructor);
id obj = *((id *)rvalue);
return method->bridgeState->getObject(env, obj, constructor,
method->returnOwned ? kOwnedObject
: kUnownedObject);
}
return cif->returnType->toJS(env, rvalue,
method->returnOwned ? kReturnOwned : 0);
}
napi_value ObjCClassMember::jsGetter(napi_env env, napi_callback_info cbinfo) {
napi_value jsThis;
ObjCClassMember *method;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, (void **)&method);
id self;
napi_unwrap(env, jsThis, (void **)&self);
Cif *cif = method->cif;
if (cif == nullptr) {
cif = method->cif = method->bridgeState->getMethodCif(
env, method->methodOrGetter.signatureOffset);
}
void *avalues[2] = {&self, &method->methodOrGetter.selector};
void *rvalue = cif->rvalue;
objcNativeCall(env, jsThis, cif, self, avalues, rvalue);
if (cif->returnType->kind == mdTypeInstanceObject) {
napi_value constructor = jsThis;
if (!method->classMethod) {
napi_get_named_property(env, jsThis, "constructor", &constructor);
}
return method->bridgeState->getObject(env, *((id *)rvalue), constructor,
method->returnOwned ? kOwnedObject
: kUnownedObject);
}
return cif->returnType->toJS(env, rvalue, 0);
}
napi_value ObjCClassMember::jsSetter(napi_env env, napi_callback_info cbinfo) {
napi_value jsThis, argv;
size_t argc = 1;
ObjCClassMember *method;
napi_get_cb_info(env, cbinfo, &argc, &argv, &jsThis, (void **)&method);
id self;
napi_unwrap(env, jsThis, (void **)&self);
Cif *cif = method->setterCif;
if (cif == nullptr) {
cif = method->setterCif =
method->bridgeState->getMethodCif(env, method->setter.signatureOffset);
}
void *avalues[3] = {&self, &method->setter.selector, cif->avalues[0]};
void *rvalue = nullptr;
bool shouldFree = false;
cif->argTypes[0]->toNative(env, argv, avalues[2], &shouldFree, &shouldFree);
objcNativeCall(env, jsThis, cif, self, avalues, rvalue);
if (shouldFree) {
cif->argTypes[0]->free(env, *((void **)avalues[2]));
}
return nullptr;
}
} // namespace objc_bridge