-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathWKTJsiWorkletApi.h
More file actions
158 lines (129 loc) · 4.83 KB
/
WKTJsiWorkletApi.h
File metadata and controls
158 lines (129 loc) · 4.83 KB
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
#pragma once
#include <jsi/jsi.h>
#include <memory>
#include <string>
#include <vector>
#include "WKTJsiHostObject.h"
#include "WKTJsiJsDecorator.h"
#include "WKTJsiPromiseWrapper.h"
#include "WKTJsiSharedValue.h"
#include "WKTJsiWorklet.h"
#include "WKTJsiWorkletContext.h"
#include "WKTJsiWrapper.h"
namespace RNWorklet {
namespace jsi = facebook::jsi;
class JsiWorkletApi : public JsiHostObject {
public:
// Name of the Worklet API member (where to install on global)
static const char *WorkletsApiName;
/**
* Installs the worklet API into the provided runtime
*/
static void installApi(jsi::Runtime &runtime);
/**
Returns the worklet API
*/
static std::shared_ptr<JsiWorkletApi> getInstance();
/**
Invalidate the api instance.
*/
static void invalidateInstance();
JSI_HOST_FUNCTION(createContext) {
if (count == 0) {
throw jsi::JSError(
runtime, "createWorkletContext expects the context name parameter.");
}
if (!arguments[0].isString()) {
throw jsi::JSError(runtime,
"createWorkletContext expects the context name "
"parameter as a string.");
}
auto nameStr = arguments[0].asString(runtime).utf8(runtime);
return jsi::Object::createFromHostObject(runtime,
createWorkletContext(nameStr));
};
JSI_HOST_FUNCTION(createSharedValue) {
return jsi::Object::createFromHostObject(
*JsiWorkletContext::getDefaultInstance()->getJsRuntime(),
std::make_shared<JsiSharedValue>(
arguments[0], JsiWorkletContext::getDefaultInstance()));
};
JSI_HOST_FUNCTION(createRunInJsFn) {
if (count == 0) {
throw jsi::JSError(runtime, "createRunInJsFn expects one parameter.");
}
// Get the worklet function
if (!arguments[0].isObject()) {
throw jsi::JSError(
runtime,
"createRunInJsFn expects a function as its first parameter.");
}
if (!arguments[0].asObject(runtime).isFunction(runtime)) {
throw jsi::JSError(
runtime,
"createRunInJsFn expects a function as its first parameter.");
}
auto caller =
JsiWorkletContext::createCallInContext(runtime, arguments[0], nullptr);
// Now let us create the caller function.
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "runInJsFn"), 0,
JSI_HOST_FUNCTION_LAMBDA {
return caller(runtime, thisValue, arguments, count);
});
}
JSI_HOST_FUNCTION(createRunInContextFn) {
if (count == 0) {
throw jsi::JSError(
runtime, "createRunInContextFn expects at least one parameter.");
}
// Get the active context
auto activeContext =
count == 2 && arguments[1].isObject()
? arguments[1].asObject(runtime).getHostObject<JsiWorkletContext>(
runtime)
: JsiWorkletContext::getDefaultInstance();
if (activeContext == nullptr) {
throw jsi::JSError(runtime,
"createRunInContextFn called with invalid context.");
}
auto caller = JsiWorkletContext::createCallInContext(runtime, arguments[0],
activeContext.get());
// Now let us create the caller function.
return jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "runInContextFn"), 0,
JSI_HOST_FUNCTION_LAMBDA {
return caller(runtime, thisValue, arguments, count);
});
}
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiWorkletApi, createSharedValue),
JSI_EXPORT_FUNC(JsiWorkletApi, createContext),
JSI_EXPORT_FUNC(JsiWorkletApi, createRunInContextFn),
JSI_EXPORT_FUNC(JsiWorkletApi, createRunInJsFn))
JSI_PROPERTY_GET(defaultContext) {
return jsi::Object::createFromHostObject(
runtime, JsiWorkletContext::getDefaultInstance());
}
JSI_PROPERTY_GET(currentContext) {
auto current = JsiWorkletContext::getCurrent(runtime);
if (!current)
return jsi::Value::undefined();
return jsi::Object::createFromHostObject(runtime,
current->shared_from_this());
}
JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(JsiWorkletApi,
defaultContext),
JSI_EXPORT_PROP_GET(JsiWorkletApi,
currentContext))
/**
Creates a new worklet context
@name Name of the context
@returns A new worklet context that has been initialized and decorated.
*/
std::shared_ptr<JsiWorkletContext>
createWorkletContext(const std::string &name);
private:
// Instance/singletong
static std::shared_ptr<JsiWorkletApi> instance;
};
} // namespace RNWorklet