Repository navigation
Expand file tree
/
Copy pathStruct.mm
More file actions
402 lines (335 loc) · 11.2 KB
/
Copy pathStruct.mm
File metadata and controls
402 lines (335 loc) · 11.2 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
#include "Struct.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"
#include <cstring>
#import <Foundation/Foundation.h>
namespace objc_bridge {
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);
}
napi_property_descriptor prop = {
.utf8name = name,
.attributes =
(napi_property_attributes)(napi_enumerable | napi_configurable),
.method = nullptr,
.setter = nullptr,
.value = nullptr,
.data = (void *)((size_t)originalOffset),
.getter = JS_structGetter,
};
napi_define_properties(env, global, 1, &prop);
}
}
void ObjCBridgeState::registerUnionGlobals(napi_env env, napi_value global) {
MDSectionOffset offset = metadata->unionsOffset;
while (offset < metadata->size) {
// 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;
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,
.attributes =
(napi_property_attributes)(napi_enumerable | napi_configurable),
.method = nullptr,
.setter = nullptr,
.value = nullptr,
.data = (void *)((size_t)originalOffset),
.getter = JS_unionGetter,
};
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, arg;
StructInfo *info;
size_t argc = 1;
napi_get_cb_info(env, cbinfo, &argc, &arg, &jsThis, (void **)&info);
napi_valuetype argType;
napi_typeof(env, arg, &argType);
StructObject *object;
if (argType == napi_object) {
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);
return object->get(env, info);
}
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;
}
inline StructObject::StructObject(StructInfo *info, void *data) {
this->info = info;
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;
}
}
StructObject::StructObject(napi_env env, StructInfo *info, napi_value object,
void *memory) {
this->info = info;
if (memory == nullptr) {
this->owned = true;
this->data = malloc(info->size);
} else {
this->owned = false;
this->data = memory;
}
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->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);
napi_define_properties(env, result, 1, sizeofProp);
free(properties);
return result;
}
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 (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 {
object->~StructObject();
object->data = data;
object->owned = false;
}
return result;
}
} // namespace objc_bridge