-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStruct.mm
More file actions
566 lines (482 loc) · 16.4 KB
/
Struct.mm
File metadata and controls
566 lines (482 loc) · 16.4 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
#include "Struct.h"
#include <algorithm>
#include <cstring>
#include <vector>
#include "Interop.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>
namespace nativescript {
void ObjCBridgeState::registerStructGlobals(napi_env env, napi_value global) {
MDSectionOffset offset = metadata->structsOffset;
while (offset < metadata->unionsOffset) {
// Sometimes there is padding after file ends.
if (metadata->getOffset(offset) == 0) break;
MDSectionOffset originalOffset = offset;
auto name = metadata->getString(offset);
offset += sizeof(MDSectionOffset);
auto size = metadata->getArraySize(offset);
offset += sizeof(uint16_t);
std::string nameStr = name;
structOffsets[nameStr] = originalOffset;
bool next = true;
while (next) {
MDSectionOffset nameOffset = metadata->getOffset(offset);
offset += sizeof(MDSectionOffset);
next = (nameOffset & mdSectionOffsetNext) != 0;
nameOffset &= ~mdSectionOffsetNext;
if (nameOffset == MD_SECTION_OFFSET_NULL) break;
auto name = metadata->resolveString(nameOffset);
offset += sizeof(uint16_t);
TypeConv::Make(env, metadata, &offset);
}
bool hasPrimaryName = false;
napi_has_named_property(env, global, name, &hasPrimaryName);
if (!hasPrimaryName) {
napi_property_descriptor prop = {
.utf8name = name,
.method = nullptr,
.getter = JS_structGetter,
.setter = nullptr,
.value = nullptr,
.attributes = (napi_property_attributes)(napi_enumerable | napi_configurable),
.data = (void*)((size_t)originalOffset),
};
napi_define_properties(env, global, 1, &prop);
}
if (!nameStr.empty() && nameStr[0] == '_') {
std::string alias = nameStr.substr(1);
if (!alias.empty()) {
bool hasAlias = false;
napi_has_named_property(env, global, alias.c_str(), &hasAlias);
if (!hasAlias) {
napi_property_descriptor aliasProp = {
.utf8name = alias.c_str(),
.method = nullptr,
.getter = JS_structGetter,
.setter = nullptr,
.value = nullptr,
.attributes = (napi_property_attributes)(napi_enumerable | napi_configurable),
.data = (void*)((size_t)originalOffset),
};
napi_define_properties(env, global, 1, &aliasProp);
}
}
}
if (nameStr.size() < 6 || nameStr.compare(nameStr.size() - 6, 6, "Struct") != 0) {
std::string alias = nameStr + "Struct";
bool hasAlias = false;
napi_has_named_property(env, global, alias.c_str(), &hasAlias);
if (!hasAlias) {
napi_property_descriptor aliasProp = {
.utf8name = alias.c_str(),
.method = nullptr,
.getter = JS_structGetter,
.setter = nullptr,
.value = nullptr,
.attributes = (napi_property_attributes)(napi_enumerable | napi_configurable),
.data = (void*)((size_t)originalOffset),
};
napi_define_properties(env, global, 1, &aliasProp);
}
}
}
}
void ObjCBridgeState::registerUnionGlobals(napi_env env, napi_value global) {
MDSectionOffset offset = metadata->unionsOffset;
while (true) {
// It's the last section, always ends with 4 bytes of 0.
if (metadata->getOffset(offset) == 0) break;
MDSectionOffset originalOffset = offset;
auto name = metadata->getString(offset);
offset += sizeof(MDSectionOffset);
auto size = metadata->getArraySize(offset);
offset += sizeof(uint16_t);
std::string nameStr = name;
unionOffsets[nameStr] = originalOffset;
bool next = true;
while (next) {
MDSectionOffset nameOffset = metadata->getOffset(offset);
next = (nameOffset & mdSectionOffsetNext) != 0;
nameOffset &= ~mdSectionOffsetNext;
auto name = metadata->resolveString(nameOffset);
offset += sizeof(MDSectionOffset);
TypeConv::Make(env, metadata, &offset);
}
napi_property_descriptor prop = {
.utf8name = name,
.method = nullptr,
.getter = JS_unionGetter,
.setter = nullptr,
.value = nullptr,
.attributes = (napi_property_attributes)(napi_enumerable | napi_configurable),
.data = (void*)((size_t)originalOffset),
};
napi_define_properties(env, global, 1, &prop);
}
}
NAPI_FUNCTION(structGetter) {
NAPI_PREAMBLE
void* data;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
auto bridgeState = ObjCBridgeState::InstanceData(env);
auto structInfo = bridgeState->getStructInfo(env, offset);
return StructObject::getJSClass(env, structInfo);
}
NAPI_FUNCTION(unionGetter) {
NAPI_PREAMBLE
void* data;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
auto bridgeState = ObjCBridgeState::InstanceData(env);
auto structInfo = bridgeState->getUnionInfo(env, offset);
return StructObject::getJSClass(env, structInfo);
}
StructInfo* getStructInfoFromMetadata(napi_env env, MDMetadataReader* metadata,
MDSectionOffset offset) {
auto originalOffset = offset;
auto structInfo = new StructInfo();
auto name = metadata->getString(offset);
structInfo->name = name;
offset += sizeof(MDSectionOffset);
structInfo->size = metadata->getArraySize(offset);
offset += sizeof(uint16_t);
bool next = true;
while (next) {
MDSectionOffset nameOffset = metadata->getOffset(offset);
next = (nameOffset & mdSectionOffsetNext) != 0;
if (next) {
nameOffset &= ~mdSectionOffsetNext;
}
if (nameOffset == MD_SECTION_OFFSET_NULL) {
break;
}
auto fieldInfo = StructFieldInfo();
fieldInfo.name = metadata->resolveString(nameOffset);
offset += sizeof(MDSectionOffset);
fieldInfo.offset = metadata->getArraySize(offset);
offset += sizeof(uint16_t);
fieldInfo.type = TypeConv::Make(env, metadata, &offset);
structInfo->fields.push_back(fieldInfo);
}
return structInfo;
}
StructInfo* getStructInfoFromUnionMetadata(napi_env env, MDMetadataReader* metadata,
MDSectionOffset offset) {
auto originalOffset = offset;
auto structInfo = new StructInfo();
auto name = metadata->getString(offset);
structInfo->name = name;
offset += sizeof(MDSectionOffset);
structInfo->size = metadata->getArraySize(offset);
offset += sizeof(uint16_t);
bool next = true;
while (next) {
MDSectionOffset nameOffset = metadata->getOffset(offset);
offset += sizeof(MDSectionOffset);
next = (nameOffset & mdSectionOffsetNext) != 0;
if (next) {
nameOffset &= ~mdSectionOffsetNext;
}
if (nameOffset == 0) {
break;
}
auto fieldInfo = StructFieldInfo();
fieldInfo.name = metadata->resolveString(nameOffset);
fieldInfo.offset = 0;
fieldInfo.type = TypeConv::Make(env, metadata, &offset);
structInfo->fields.push_back(fieldInfo);
}
return structInfo;
}
void StructObject_finalize(napi_env env, void* data, void* hint) {
auto structObject = (StructObject*)data;
delete structObject;
}
NAPI_FUNCTION(StructConstructor) {
NAPI_PREAMBLE
napi_value jsThis;
napi_value argv[1];
StructInfo* info;
size_t argc = 1;
napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, (void**)&info);
napi_value arg;
if (argc > 0) {
arg = argv[0];
} else {
napi_get_undefined(env, &arg);
}
napi_valuetype argType;
napi_typeof(env, arg, &argType);
StructObject* object;
if (argType == napi_object) {
if (Pointer::isInstance(env, arg)) {
Pointer* pointer = Pointer::unwrap(env, arg);
if (pointer == nullptr) {
napi_throw_error(env, nullptr, "Invalid pointer-backed struct argument");
return nullptr;
}
object = new StructObject(info, pointer->data, env, arg);
} else if (Reference::isInstance(env, arg)) {
Reference* reference = Reference::unwrap(env, arg);
if (reference == nullptr || reference->data == nullptr) {
napi_throw_error(env, nullptr, "Reference is not initialized");
return nullptr;
}
object = new StructObject(info, reference->data, env, arg);
} else {
object = new StructObject(env, info, arg);
}
} else {
object = new StructObject(info);
}
napi_ref ref;
napi_wrap(env, jsThis, object, StructObject_finalize, nullptr, &ref);
return jsThis;
}
NAPI_FUNCTION(StructPropertyGetter) {
NAPI_PREAMBLE
napi_value jsThis;
StructFieldInfo* info;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, (void**)&info);
auto object = StructObject::unwrap(env, jsThis);
auto value = object->get(env, info);
if (StructObject::isInstance(env, value)) {
napi_set_named_property(env, value, "__ns_parent_struct", jsThis);
}
return value;
}
NAPI_FUNCTION(StructPropertySetter) {
NAPI_PREAMBLE
napi_value jsThis, arg;
StructFieldInfo* info;
size_t argc = 1;
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, (void**)&info);
auto object = StructObject::unwrap(env, jsThis);
object->set(env, info, arg);
return nullptr;
}
NAPI_FUNCTION(StructCustomInspect) {
napi_value jsThis;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &jsThis, nullptr);
auto object = StructObject::unwrap(env, jsThis);
std::string str = "struct ";
str += object->info->name;
str += " {}";
napi_value result;
napi_create_string_utf8(env, str.c_str(), str.length(), &result);
return result;
}
NAPI_FUNCTION(StructEquals) {
napi_value jsThis, argv[2];
size_t argc = 2;
void* data = nullptr;
napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, &data);
StructInfo* info = static_cast<StructInfo*>(data);
if (info == nullptr || argc < 2) {
napi_value result;
napi_get_boolean(env, false, &result);
return result;
}
auto serialize = [&](napi_value value, std::vector<uint8_t>& out) -> bool {
out.assign(info->size, 0);
StructObject* structObject = StructObject::unwrap(env, value);
if (structObject != nullptr) {
size_t copySize =
std::min(static_cast<size_t>(info->size), static_cast<size_t>(structObject->info->size));
memcpy(out.data(), structObject->data, copySize);
return true;
}
napi_valuetype type = napi_undefined;
napi_typeof(env, value, &type);
if (type != napi_object) {
return false;
}
StructObject(env, info, value, out.data());
bool pending = false;
napi_is_exception_pending(env, &pending);
return !pending;
};
std::vector<uint8_t> left;
std::vector<uint8_t> right;
bool okLeft = serialize(argv[0], left);
bool okRight = serialize(argv[1], right);
napi_value result;
napi_get_boolean(env, okLeft && okRight && memcmp(left.data(), right.data(), info->size) == 0,
&result);
return result;
}
inline StructObject::StructObject(StructInfo* info, void* data, napi_env env,
napi_value backingValue) {
this->info = info;
this->env = env;
if (data == nullptr) {
this->data = malloc(info->size);
memset(this->data, 0, this->info->size);
this->owned = true;
} else {
this->data = data;
this->owned = false;
if (env != nullptr && backingValue != nullptr) {
napi_create_reference(env, backingValue, 1, &this->backingRef);
}
}
}
StructObject::StructObject(napi_env env, StructInfo* info, napi_value object, void* memory) {
this->info = info;
this->env = env;
if (memory == nullptr) {
this->owned = true;
this->data = malloc(info->size);
} else {
this->owned = false;
this->data = memory;
}
memset(this->data, 0, this->info->size);
for (auto& field : info->fields) {
bool hasProp = false;
napi_has_named_property(env, object, field.name, &hasProp);
if (!hasProp) {
continue;
}
napi_value property;
napi_get_named_property(env, object, field.name, &property);
set(env, &field, property);
}
}
StructObject::~StructObject() {
if (this->backingRef != nullptr && this->env != nullptr) {
napi_delete_reference(this->env, this->backingRef);
}
if (this->owned) free(this->data);
}
napi_value StructObject::get(napi_env env, StructFieldInfo* field) {
auto data = (char*)this->data + field->offset;
return field->type->toJS(env, data, kStructZeroCopy);
}
void StructObject::set(napi_env env, StructFieldInfo* field, napi_value value) {
auto data = (char*)this->data + field->offset;
bool shouldFree = false;
field->type->toNative(env, value, data, &shouldFree, &shouldFree);
}
StructObject* StructObject::unwrap(napi_env env, napi_value object) {
StructObject* result;
auto status = napi_unwrap(env, object, (void**)&result);
if (status != napi_ok) return nullptr;
return result;
}
napi_value StructObject::defineJSClass(napi_env env, StructInfo* info) {
auto properties = (napi_property_descriptor*)malloc((info->fields.size() + 2) *
sizeof(napi_property_descriptor));
for (int i = 0; i < info->fields.size(); i++) {
auto field = info->fields[i];
auto prop = &properties[i];
prop->utf8name = field.name;
prop->name = nullptr;
prop->method = nullptr;
prop->value = nullptr;
prop->attributes = (napi_property_attributes)(napi_enumerable | napi_writable);
prop->data = &info->fields[i];
prop->getter = JS_StructPropertyGetter;
prop->setter = JS_StructPropertySetter;
}
auto prop = &properties[info->fields.size()];
prop->utf8name = nullptr;
prop->name = jsSymbolFor(env, "nodejs.util.inspect.custom");
prop->method = JS_StructCustomInspect;
prop->getter = nullptr;
prop->setter = nullptr;
prop->value = nullptr;
prop->attributes = napi_default;
prop->data = nullptr;
napi_value size;
napi_create_int32(env, info->size, &size);
auto sizeofProp = &properties[info->fields.size() + 1];
sizeofProp->utf8name = nullptr;
sizeofProp->name = jsSymbolFor(env, "sizeof");
sizeofProp->method = nullptr;
sizeofProp->getter = nullptr;
sizeofProp->setter = nullptr;
sizeofProp->value = size;
sizeofProp->attributes = napi_enumerable;
sizeofProp->data = nullptr;
napi_value result;
napi_define_class(env, info->name, NAPI_AUTO_LENGTH, JS_StructConstructor, (void*)info,
info->fields.size() + 2, properties, &result);
const napi_property_descriptor classProps[] = {
{
.utf8name = "equals",
.method = JS_StructEquals,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = info,
},
{
.utf8name = nullptr,
.name = jsSymbolFor(env, "sizeof"),
.method = nullptr,
.getter = nullptr,
.setter = nullptr,
.value = size,
.attributes = napi_enumerable,
.data = nullptr,
},
};
napi_define_properties(env, result, 2, classProps);
free(properties);
return result;
}
bool StructObject::isInstance(napi_env env, napi_value object) {
napi_valuetype valueType = napi_undefined;
napi_typeof(env, object, &valueType);
if (valueType != napi_object && valueType != napi_function) {
return false;
}
napi_value sizeofSymbol = jsSymbolFor(env, "sizeof");
bool hasProp = false;
if (napi_has_property(env, object, sizeofSymbol, &hasProp) != napi_ok) {
return false;
}
return hasProp;
}
napi_value StructObject::getJSClass(napi_env env, StructInfo* info) {
if (info->jsClass != nullptr) {
return get_ref_value(env, info->jsClass);
}
auto result = defineJSClass(env, info);
info->jsClass = make_ref(env, result);
return result;
}
napi_value StructObject::fromNative(napi_env env, StructInfo* info, void* data, bool owned) {
napi_value result;
napi_value cls = getJSClass(env, info);
napi_new_instance(env, cls, 0, nullptr, &result);
auto object = StructObject::unwrap(env, result);
if (object == nullptr) {
return result;
}
if (owned) {
if (object->owned) {
memcpy(object->data, data, info->size);
} else {
object->data = malloc(info->size);
memcpy(object->data, data, info->size);
object->owned = true;
}
} else {
if (object->owned && object->data != nullptr) {
free(object->data);
}
object->data = data;
object->owned = false;
}
return result;
}
} // namespace nativescript