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