-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathWKTJsiHostObject.cpp
133 lines (109 loc) · 4.03 KB
/
WKTJsiHostObject.cpp
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
#include "WKTJsiHostObject.h"
#include <utility>
// To be able to find objects that aren't cleaned up correctly,
// we can set this value to 1 and debug the constructor/destructor
#define JSI_DEBUG_ALLOCATIONS 0
namespace RNWorklet {
#if JSI_DEBUG_ALLOCATIONS
int objCounter = 0;
std::vector<JsiHostObject *> objects;
#endif
JsiHostObject::JsiHostObject() {
#if JSI_DEBUG_ALLOCATIONS
objects.push_back(this);
objCounter++;
#endif
}
JsiHostObject::~JsiHostObject() {
#if JSI_DEBUG_ALLOCATIONS
for (size_t i = 0; i < objects.size(); ++i) {
if (objects.at(i) == this) {
objects.erase(objects.begin() + i);
break;
}
}
objCounter--;
#endif
}
void JsiHostObject::dispose() {
if (!_disposed) {
dispose(_disposed);
_disposed = true;
}
}
void JsiHostObject::set(jsi::Runtime &rt, const jsi::PropNameID &name,
const jsi::Value &value) {
auto nameStr = name.utf8(rt);
/** Check the static setters map */
const JsiPropertySettersMap &setters = getExportedPropertySettersMap();
auto setter = setters.find(nameStr);
if (setter != setters.end()) {
auto dispatcher = std::bind(setter->second, this, std::placeholders::_1,
std::placeholders::_2);
return dispatcher(rt, value);
}
}
jsi::Value JsiHostObject::get(jsi::Runtime &runtime,
const jsi::PropNameID &name) {
auto nameStr = name.utf8(runtime);
// get mapped runtime / function cache
auto hostFunctionCache =
_hostFunctionCache.find(static_cast<void *>(&runtime));
if (hostFunctionCache == _hostFunctionCache.end()) {
std::map<std::string, jsi::Function> map;
_hostFunctionCache.emplace(static_cast<void *>(&runtime), std::move(map));
hostFunctionCache = _hostFunctionCache.find(static_cast<void *>(&runtime));
}
// Check function cache
auto cachedFunc = hostFunctionCache->second.find(nameStr);
if (cachedFunc != hostFunctionCache->second.end()) {
return cachedFunc->second.asFunction(runtime);
}
// Check the static getters map
const JsiPropertyGettersMap &getters = getExportedPropertyGettersMap();
auto getter = getters.find(nameStr);
if (getter != getters.end()) {
auto dispatcher = std::bind(getter->second, this, std::placeholders::_1);
return dispatcher(runtime);
}
// Check the static function map
const JsiFunctionMap &funcs = getExportedFunctionMap();
auto func = funcs.find(nameStr);
if (func != funcs.end()) {
auto dispatcher =
std::bind(func->second, reinterpret_cast<JsiHostObject *>(this),
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4);
// Add to cache - it is important to cache the results from the
// createFromHostFunction function which takes some time.
return hostFunctionCache->second
.emplace(nameStr, jsi::Function::createFromHostFunction(runtime, name,
0, dispatcher))
.first->second.asFunction(runtime);
}
return jsi::Value::undefined();
}
std::vector<jsi::PropNameID>
JsiHostObject::getPropertyNames(jsi::Runtime &runtime) {
// statically exported functions
const auto &funcs = getExportedFunctionMap();
// Statically exported property getters
const auto &getters = getExportedPropertyGettersMap();
// Statically exported property setters
const auto &setters = getExportedPropertySettersMap();
std::vector<jsi::PropNameID> propNames;
propNames.reserve(funcs.size() + getters.size() + setters.size());
for (auto it = funcs.cbegin(); it != funcs.cend(); ++it) {
propNames.push_back(jsi::PropNameID::forAscii(runtime, it->first));
}
for (auto it = getters.cbegin(); it != getters.cend(); ++it) {
propNames.push_back(jsi::PropNameID::forUtf8(runtime, it->first));
}
for (auto it = getters.cbegin(); it != getters.cend(); ++it) {
if (getters.count(it->first) == 0) {
propNames.push_back(jsi::PropNameID::forUtf8(runtime, it->first));
}
}
return propNames;
}
} // namespace RNWorklet