Repository navigation
Expand file tree
/
Copy pathVariable.mm
More file actions
112 lines (93 loc) · 2.76 KB
/
Copy pathVariable.mm
File metadata and controls
112 lines (93 loc) · 2.76 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
#include "Variable.h"
#include "MetadataReader.h"
#include "ObjCBridge.h"
#include "js_native_api.h"
namespace objc_bridge {
void ObjCBridgeState::registerVarGlobals(napi_env env, napi_value global) {
auto offset = metadata->constantsOffset;
while (offset < metadata->enumsOffset) {
MDSectionOffset originalOffset = offset;
auto name = metadata->getString(offset);
offset += sizeof(MDSectionOffset);
auto evalKind = metadata->getVariableEvalKind(offset);
offset += sizeof(MDVariableEvalKind);
napi_property_descriptor prop = {
.utf8name = name,
.attributes =
(napi_property_attributes)(napi_enumerable | napi_configurable),
.getter = JS_variableGetter,
.setter = nullptr,
.value = nullptr,
.data = (void *)((size_t)originalOffset),
.method = nullptr,
};
napi_define_properties(env, global, 1, &prop);
switch (evalKind) {
case mdEvalDouble: {
offset += sizeof(double);
break;
}
case mdEvalInt64: {
offset += sizeof(int64_t);
break;
}
case mdEvalString: {
offset += sizeof(MDSectionOffset);
break;
}
default:
TypeConv::Make(env, metadata, &offset);
break;
}
}
}
NAPI_FUNCTION(variableGetter) {
void *data;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, nullptr, &data);
MDSectionOffset offset = (MDSectionOffset)((size_t)data);
MDSectionOffset originalOffset = offset;
auto bridgeState = ObjCBridgeState::InstanceData(env);
auto cached = bridgeState->mdValueCache[offset];
if (cached != nullptr) {
return get_ref_value(env, cached);
}
// Name
auto name = bridgeState->metadata->getString(offset);
offset += sizeof(MDSectionOffset);
// Eval kind
auto evalKind = bridgeState->metadata->getVariableEvalKind(offset);
offset += sizeof(MDVariableEvalKind);
napi_value result = nullptr;
switch (evalKind) {
case mdEvalDouble: {
auto value = bridgeState->metadata->getDouble(offset);
napi_create_double(env, value, &result);
break;
}
case mdEvalInt64: {
auto value = bridgeState->metadata->getInt64(offset);
napi_create_int64(env, value, &result);
break;
}
case mdEvalString: {
auto value = bridgeState->metadata->getString(offset);
napi_create_string_utf8(env, value, NAPI_AUTO_LENGTH, &result);
if (result != nullptr) {
bridgeState->mdValueCache[originalOffset] = make_ref(env, result);
}
break;
}
default: {
auto type = TypeConv::Make(env, bridgeState->metadata, &offset);
auto value = dlsym(bridgeState->self_dl, name);
if (value == nullptr) {
napi_get_null(env, &result);
} else {
result = type->toJS(env, value);
}
return result;
}
}
return result;
}
} // namespace objc_bridge