-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
WorkletsCache.cpp
41 lines (34 loc) · 1.27 KB
/
WorkletsCache.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
#include "WorkletsCache.h"
#include "FrozenObject.h"
#include "ShareableValue.h"
#include <string>
#include <utility>
using namespace facebook;
namespace reanimated {
jsi::Value eval(jsi::Runtime &rt, const char *code) {
return rt.global().getPropertyAsFunction(rt, "eval").call(rt, code);
}
jsi::Function function(jsi::Runtime &rt, const std::string &code) {
return eval(rt, ("(" + code + ")").c_str()).getObject(rt).getFunction(rt);
}
std::shared_ptr<jsi::Function> WorkletsCache::getFunction(
jsi::Runtime &rt,
std::shared_ptr<FrozenObject> frozenObj) {
long long workletHash =
ValueWrapper::asNumber(frozenObj->map["__workletHash"]->valueContainer);
if (worklets.count(workletHash) == 0) {
auto codeBuffer = std::make_shared<const jsi::StringBuffer>(
"(" +
ValueWrapper::asString(frozenObj->map["asString"]->valueContainer) +
")");
auto func = rt.evaluateJavaScript(
codeBuffer,
ValueWrapper::asString(
frozenObj->map["__location"]->valueContainer))
.asObject(rt)
.asFunction(rt);
worklets[workletHash] = std::make_shared<jsi::Function>(std::move(func));
}
return worklets[workletHash];
}
} // namespace reanimated