1*753557f6STomasz Sapeta #include "WorkletsCache.h"
2*753557f6STomasz Sapeta #include "FrozenObject.h"
3*753557f6STomasz Sapeta #include "ShareableValue.h"
4*753557f6STomasz Sapeta
5*753557f6STomasz Sapeta #include <string>
6*753557f6STomasz Sapeta #include <utility>
7*753557f6STomasz Sapeta
8*753557f6STomasz Sapeta using namespace ABI47_0_0facebook;
9*753557f6STomasz Sapeta
10*753557f6STomasz Sapeta namespace ABI47_0_0reanimated {
11*753557f6STomasz Sapeta
eval(jsi::Runtime & rt,const char * code)12*753557f6STomasz Sapeta jsi::Value eval(jsi::Runtime &rt, const char *code) {
13*753557f6STomasz Sapeta return rt.global().getPropertyAsFunction(rt, "eval").call(rt, code);
14*753557f6STomasz Sapeta }
15*753557f6STomasz Sapeta
function(jsi::Runtime & rt,const std::string & code)16*753557f6STomasz Sapeta jsi::Function function(jsi::Runtime &rt, const std::string &code) {
17*753557f6STomasz Sapeta return eval(rt, ("(" + code + ")").c_str()).getObject(rt).getFunction(rt);
18*753557f6STomasz Sapeta }
19*753557f6STomasz Sapeta
getFunction(jsi::Runtime & rt,std::shared_ptr<FrozenObject> frozenObj)20*753557f6STomasz Sapeta std::shared_ptr<jsi::Function> WorkletsCache::getFunction(
21*753557f6STomasz Sapeta jsi::Runtime &rt,
22*753557f6STomasz Sapeta std::shared_ptr<FrozenObject> frozenObj) {
23*753557f6STomasz Sapeta long long workletHash =
24*753557f6STomasz Sapeta ValueWrapper::asNumber(frozenObj->map["__workletHash"]->valueContainer);
25*753557f6STomasz Sapeta if (worklets.count(workletHash) == 0) {
26*753557f6STomasz Sapeta auto codeBuffer = std::make_shared<const jsi::StringBuffer>(
27*753557f6STomasz Sapeta "(" +
28*753557f6STomasz Sapeta ValueWrapper::asString(frozenObj->map["asString"]->valueContainer) +
29*753557f6STomasz Sapeta ")");
30*753557f6STomasz Sapeta auto func = rt.evaluateJavaScript(
31*753557f6STomasz Sapeta codeBuffer,
32*753557f6STomasz Sapeta ValueWrapper::asString(
33*753557f6STomasz Sapeta frozenObj->map["__location"]->valueContainer))
34*753557f6STomasz Sapeta .asObject(rt)
35*753557f6STomasz Sapeta .asFunction(rt);
36*753557f6STomasz Sapeta worklets[workletHash] = std::make_shared<jsi::Function>(std::move(func));
37*753557f6STomasz Sapeta }
38*753557f6STomasz Sapeta return worklets[workletHash];
39*753557f6STomasz Sapeta }
40*753557f6STomasz Sapeta
41*753557f6STomasz Sapeta } // namespace reanimated
42