-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Shareables.h
357 lines (292 loc) · 9.8 KB
/
Shareables.h
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
#pragma once
#include <jsi/jsi.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "WorkletRuntimeRegistry.h"
using namespace facebook;
namespace reanimated {
jsi::Function getValueUnpacker(jsi::Runtime &rt);
#ifndef NDEBUG
jsi::Function getCallGuard(jsi::Runtime &rt);
#endif // NDEBUG
// If possible, please use `WorkletRuntime::runGuarded` instead.
template <typename... Args>
inline jsi::Value runOnRuntimeGuarded(
jsi::Runtime &rt,
const jsi::Value &function,
Args &&...args) {
// We only use callGuard in debug mode, otherwise we call the provided
// function directly. CallGuard provides a way of capturing exceptions in
// JavaScript and propagating them to the main React Native thread such that
// they can be presented using RN's LogBox.
#ifndef NDEBUG
return getCallGuard(rt).call(rt, function, args...);
#else
return function.asObject(rt).asFunction(rt).call(rt, args...);
#endif
}
inline void cleanupIfRuntimeExists(
jsi::Runtime *rt,
std::unique_ptr<jsi::Value> &value) {
if (rt != nullptr && !WorkletRuntimeRegistry::isRuntimeAlive(rt)) {
// The below use of unique_ptr.release prevents the smart pointer from
// calling the destructor of the kept object. This effectively results in
// leaking some memory. We do this on purpose, as sometimes we would keep
// references to JSI objects past the lifetime of its runtime (e.g.,
// shared values references from the RN VM holds reference to JSI objects
// on the UI runtime). When the UI runtime is terminated, the orphaned JSI
// objects would crash the app when their destructors are called, because
// they call into a memory that's managed by the terminated runtime. We
// accept the tradeoff of leaking memory here, as it has a limited impact.
// This scenario can only occur when the React instance is torn down which
// happens in development mode during app reloads, or in production when
// the app is being shut down gracefully by the system. An alternative
// solution would require us to keep track of all JSI values that are in
// use which would require additional data structure and compute spent on
// bookkeeping that only for the sake of destroying the values in time
// before the runtime is terminated. Note that the underlying memory that
// jsi::Value refers to is managed by the VM and gets freed along with the
// runtime.
value.release();
}
}
class Shareable {
protected:
virtual jsi::Value toJSValue(jsi::Runtime &rt) = 0;
public:
virtual ~Shareable();
enum ValueType {
UndefinedType,
NullType,
BooleanType,
NumberType,
// SymbolType, TODO
BigIntType,
StringType,
ObjectType,
ArrayType,
WorkletType,
RemoteFunctionType,
HandleType,
HostObjectType,
HostFunctionType,
ArrayBufferType,
};
explicit Shareable(ValueType valueType) : valueType_(valueType) {}
virtual jsi::Value getJSValue(jsi::Runtime &rt) {
return toJSValue(rt);
}
inline ValueType valueType() const {
return valueType_;
}
static std::shared_ptr<Shareable> undefined();
protected:
ValueType valueType_;
};
template <typename BaseClass>
class RetainingShareable : virtual public BaseClass {
private:
jsi::Runtime *primaryRuntime_;
jsi::Runtime *secondaryRuntime_;
std::unique_ptr<jsi::Value> secondaryValue_;
public:
template <typename... Args>
explicit RetainingShareable(jsi::Runtime &rt, Args &&...args)
: BaseClass(rt, std::forward<Args>(args)...), primaryRuntime_(&rt) {}
jsi::Value getJSValue(jsi::Runtime &rt);
~RetainingShareable() {
cleanupIfRuntimeExists(secondaryRuntime_, secondaryValue_);
}
};
class ShareableJSRef : public jsi::HostObject {
private:
const std::shared_ptr<Shareable> value_;
public:
explicit ShareableJSRef(const std::shared_ptr<Shareable> &value)
: value_(value) {}
virtual ~ShareableJSRef();
std::shared_ptr<Shareable> value() const {
return value_;
}
static jsi::Object newHostObject(
jsi::Runtime &rt,
const std::shared_ptr<Shareable> &value) {
return jsi::Object::createFromHostObject(
rt, std::make_shared<ShareableJSRef>(value));
}
};
jsi::Value makeShareableClone(
jsi::Runtime &rt,
const jsi::Value &value,
const jsi::Value &shouldRetainRemote,
const jsi::Value &nativeStateSource);
std::shared_ptr<Shareable> extractShareableOrThrow(
jsi::Runtime &rt,
const jsi::Value &maybeShareableValue,
const std::string &errorMessage =
"[Reanimated] Expecting the object to be of type ShareableJSRef.");
template <typename T>
std::shared_ptr<T> extractShareableOrThrow(
jsi::Runtime &rt,
const jsi::Value &shareableRef,
const std::string &errorMessage =
"[Reanimated] Provided shareable object is of an incompatible type.") {
auto res = std::dynamic_pointer_cast<T>(
extractShareableOrThrow(rt, shareableRef, errorMessage));
if (!res) {
throw std::runtime_error(errorMessage);
}
return res;
}
class ShareableArray : public Shareable {
public:
ShareableArray(jsi::Runtime &rt, const jsi::Array &array);
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
std::vector<std::shared_ptr<Shareable>> data_;
};
class ShareableObject : public Shareable {
public:
ShareableObject(jsi::Runtime &rt, const jsi::Object &object);
ShareableObject(
jsi::Runtime &rt,
const jsi::Object &object,
const jsi::Value &nativeStateSource);
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
std::vector<std::pair<std::string, std::shared_ptr<Shareable>>> data_;
std::shared_ptr<jsi::NativeState> nativeState_;
};
class ShareableHostObject : public Shareable {
public:
ShareableHostObject(
jsi::Runtime &,
const std::shared_ptr<jsi::HostObject> &hostObject)
: Shareable(HostObjectType), hostObject_(hostObject) {}
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
const std::shared_ptr<jsi::HostObject> hostObject_;
};
class ShareableHostFunction : public Shareable {
public:
ShareableHostFunction(jsi::Runtime &rt, jsi::Function function)
: Shareable(HostFunctionType),
hostFunction_(
(assert(function.isHostFunction(rt)),
function.getHostFunction(rt))),
name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
paramCount_(function.getProperty(rt, "length").asNumber()) {}
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
const jsi::HostFunctionType hostFunction_;
const std::string name_;
const unsigned int paramCount_;
};
class ShareableArrayBuffer : public Shareable {
public:
ShareableArrayBuffer(
jsi::Runtime &rt,
#if REACT_NATIVE_MINOR_VERSION >= 72
const jsi::ArrayBuffer &arrayBuffer
#else
jsi::ArrayBuffer arrayBuffer
#endif
)
: Shareable(ArrayBufferType),
data_(
arrayBuffer.data(rt),
arrayBuffer.data(rt) + arrayBuffer.size(rt)) {
}
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
const std::vector<uint8_t> data_;
};
class ShareableWorklet : public ShareableObject {
public:
ShareableWorklet(jsi::Runtime &rt, const jsi::Object &worklet)
: ShareableObject(rt, worklet) {
valueType_ = WorkletType;
}
jsi::Value toJSValue(jsi::Runtime &rt) override;
};
class ShareableRemoteFunction
: public Shareable,
public std::enable_shared_from_this<ShareableRemoteFunction> {
private:
jsi::Runtime *runtime_;
#ifndef NDEBUG
const std::string name_;
#endif
std::unique_ptr<jsi::Value> function_;
public:
ShareableRemoteFunction(jsi::Runtime &rt, jsi::Function &&function)
: Shareable(RemoteFunctionType),
runtime_(&rt),
#ifndef NDEBUG
name_(function.getProperty(rt, "name").asString(rt).utf8(rt)),
#endif
function_(std::make_unique<jsi::Value>(rt, std::move(function))) {
}
~ShareableRemoteFunction() {
cleanupIfRuntimeExists(runtime_, function_);
}
jsi::Value toJSValue(jsi::Runtime &rt) override;
};
class ShareableHandle : public Shareable {
private:
// We don't release the initializer since the handle can get
// initialized in parallel on multiple threads. However this is not a problem,
// since the final value is taken from a cache on the runtime which guarantees
// sequential access.
std::unique_ptr<ShareableObject> initializer_;
std::unique_ptr<jsi::Value> remoteValue_;
mutable std::mutex initializationMutex_;
jsi::Runtime *remoteRuntime_;
public:
ShareableHandle(jsi::Runtime &rt, const jsi::Object &initializerObject)
: Shareable(HandleType),
initializer_(std::make_unique<ShareableObject>(rt, initializerObject)) {
}
~ShareableHandle() {
cleanupIfRuntimeExists(remoteRuntime_, remoteValue_);
}
jsi::Value toJSValue(jsi::Runtime &rt) override;
};
class ShareableString : public Shareable {
public:
explicit ShareableString(const std::string &string)
: Shareable(StringType), data_(string) {}
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
const std::string data_;
};
class ShareableBigInt : public Shareable {
public:
explicit ShareableBigInt(jsi::Runtime &rt, const jsi::BigInt &bigint)
: Shareable(BigIntType), string_(bigint.toString(rt).utf8(rt)) {}
jsi::Value toJSValue(jsi::Runtime &rt) override;
protected:
const std::string string_;
};
class ShareableScalar : public Shareable {
public:
explicit ShareableScalar(double number) : Shareable(NumberType) {
data_.number = number;
}
explicit ShareableScalar(bool boolean) : Shareable(BooleanType) {
data_.boolean = boolean;
}
ShareableScalar() : Shareable(UndefinedType) {}
explicit ShareableScalar(std::nullptr_t) : Shareable(NullType) {}
jsi::Value toJSValue(jsi::Runtime &);
protected:
union Data {
bool boolean;
double number;
};
private:
Data data_;
};
} // namespace reanimated