Skip to content

Commit b788488

Browse files
authored
feat(Worklets): stable api bindings for Reanimated (#8996)
This PR is a part of: - #8919 Requires - #8949 ## Summary This PR introduces stable API definitions file `StableApi.h` in Worklets which in turn can be copied or included by libraries to link with symbols from Worklets without requiring implementation headers. The version of this header isn't release-ready for the moment - these are only bindings used by Reanimated. We're going to look into what do other libraries need too. ## Test plan CI
1 parent cf029ea commit b788488

27 files changed

Lines changed: 318 additions & 134 deletions

packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ AnimatedSensorModule::AnimatedSensorModule(const PlatformDepMethodsHolder &platf
1111
platformUnregisterSensorFunction_(platformDepMethodsHolder.unregisterSensor) {}
1212

1313
jsi::Value AnimatedSensorModule::registerSensor(
14-
jsi::Runtime &rt,
14+
jsi::Runtime &rnRuntime,
1515
const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
1616
const jsi::Value &sensorTypeValue,
1717
const jsi::Value &interval,
1818
const jsi::Value &iosReferenceFrame,
1919
const jsi::Value &sensorDataHandler) {
2020
SensorType sensorType = static_cast<SensorType>(sensorTypeValue.asNumber());
2121

22-
auto serializableHandler = extractSerializableOrThrow<SerializableWorklet>(
23-
rt, sensorDataHandler, "[Reanimated] Sensor event handler must be a worklet.");
22+
auto serializableHandler = extractSerializable(
23+
rnRuntime,
24+
sensorDataHandler,
25+
"[Reanimated] Sensor event handler must be a worklet.",
26+
Serializable::ValueType::WorkletType);
2427

2528
int sensorId = platformRegisterSensorFunction_(
2629
static_cast<int>(sensorType),
@@ -33,7 +36,7 @@ jsi::Value AnimatedSensorModule::registerSensor(
3336
return;
3437
}
3538

36-
jsi::Runtime &uiRuntime = uiWorkletRuntime->getJSIRuntime();
39+
jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime);
3740
jsi::Object value(uiRuntime);
3841
if (sensorType == SensorType::ROTATION_VECTOR) {
3942
// TODO: timestamp should be provided by the platform implementation
@@ -53,7 +56,7 @@ jsi::Value AnimatedSensorModule::registerSensor(
5356
}
5457
value.setProperty(uiRuntime, "interfaceOrientation", orientationDegrees);
5558

56-
uiWorkletRuntime->runSync(serializableHandler, value);
59+
runSyncOnRuntime(uiWorkletRuntime, serializableHandler, jsi::Value(uiRuntime, value));
5760
});
5861
if (sensorId != -1) {
5962
sensorsIds_.insert(sensorId);

packages/react-native-reanimated/Common/cpp/reanimated/AnimatedSensor/AnimatedSensorModule.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
#include <reanimated/Tools/PlatformDepMethodsHolder.h>
44

5-
#include <worklets/SharedItems/Serializable.h>
6-
#include <worklets/WorkletRuntime/WorkletRuntime.h>
5+
#include <worklets/Compat/StableApi.h>
76

87
#include <jsi/jsi.h>
98

@@ -32,8 +31,8 @@ class AnimatedSensorModule {
3231
explicit AnimatedSensorModule(const PlatformDepMethodsHolder &platformDepMethodsHolder);
3332

3433
jsi::Value registerSensor(
35-
jsi::Runtime &rt,
36-
const std::shared_ptr<WorkletRuntime> &uiWorkletRuntime,
34+
jsi::Runtime &rnRuntime,
35+
const std::shared_ptr<WorkletRuntime> &uiRuntime,
3736
const jsi::Value &sensorType,
3837
const jsi::Value &interval,
3938
const jsi::Value &iosReferenceFrame,

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/values/CSSValueVariant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <reanimated/CSS/svg/values/SVGStops.h>
1414
#include <reanimated/CSS/svg/values/SVGStrokeDashArray.h>
1515

16-
#include <worklets/Tools/JSISerializer.h>
16+
#include <worklets/Compat/StableApi.h>
1717

1818
#include <string>
1919
#include <utility>
@@ -41,7 +41,7 @@ CSSValueVariant<AllowedTypes...>::CSSValueVariant(jsi::Runtime &rt, const jsi::V
4141
// Try constructing with each allowed type until one succeeds
4242
if (!(tryOne.template operator()<AllowedTypes>() || ...)) {
4343
throw std::runtime_error(
44-
"[Reanimated] No compatible type found for construction from: " + worklets::stringifyJSIValue(rt, jsiValue));
44+
"[Reanimated] No compatible type found for construction from: " + worklets::JSIValueToStdString(rt, jsiValue));
4545
}
4646
}
4747

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <reanimated/CSS/interpolation/PropertyInterpolator.h>
22

3-
#include <worklets/Tools/JSISerializer.h>
3+
#include <worklets/Compat/StableApi.h>
44

55
#include <memory>
66
#include <utility>
@@ -35,7 +35,7 @@ std::vector<std::pair<double, jsi::Value>> PropertyInterpolator::parseJSIKeyfram
3535
throw std::invalid_argument(
3636
"[Reanimated] Received invalid keyframes object for property: " + getPropertyPathString() +
3737
".\n\nExpected an array of objects with 'offset' and 'value' properties, got: " +
38-
worklets::stringifyJSIValue(rt, keyframes));
38+
worklets::JSIValueToStdString(rt, keyframes));
3939
}
4040

4141
const auto keyframeArray = keyframes.asObject(rt).asArray(rt);

packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void UIEventHandler::process(
1111
const std::shared_ptr<WorkletRuntime> &uiRuntime,
1212
const double eventTimestamp,
1313
const jsi::Value &eventValue) const {
14-
uiRuntime->runSync(handlerFunction_, jsi::Value(eventTimestamp), eventValue);
14+
runSyncOnRuntime(uiRuntime, handlerFunction_, jsi::Value(eventTimestamp), eventValue);
1515
}
1616

1717
uint64_t UIEventHandler::getHandlerId() const {

packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandler.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#pragma once
22

33
#include <jsi/jsi.h>
4-
#include <worklets/SharedItems/Serializable.h>
5-
#include <worklets/WorkletRuntime/WorkletRuntime.h>
4+
#include <worklets/Compat/StableApi.h>
65

76
#include <memory>
87
#include <string>
@@ -15,14 +14,14 @@ class UIEventHandler {
1514
const uint64_t handlerId_;
1615
const uint64_t emitterReactTag_;
1716
const std::string eventName_;
18-
const std::shared_ptr<worklets::SerializableWorklet> handlerFunction_;
17+
const std::shared_ptr<worklets::Serializable> handlerFunction_;
1918

2019
public:
2120
UIEventHandler(
2221
const uint64_t handlerId,
2322
const std::string &eventName,
2423
const uint64_t emitterReactTag,
25-
const std::shared_ptr<worklets::SerializableWorklet> &handlerFunction)
24+
const std::shared_ptr<worklets::Serializable> &handlerFunction)
2625
: handlerId_(handlerId),
2726
emitterReactTag_(emitterReactTag),
2827
eventName_(eventName),

packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ void UIEventHandlerRegistry::processEvent(
7777
}
7878
}
7979

80-
jsi::Runtime &rt = uiWorkletRuntime->getJSIRuntime();
81-
eventPayload.asObject(rt).setProperty(rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
80+
jsi::Runtime &uiRuntime = getJSIRuntimeFromWorkletRuntime(uiWorkletRuntime);
81+
eventPayload.asObject(uiRuntime).setProperty(
82+
uiRuntime, "eventName", jsi::String::createFromUtf8(uiRuntime, eventName));
8283
for (const auto &handler : handlersForEvent) {
8384
handler->process(uiWorkletRuntime, eventTimestamp, eventPayload);
8485
}

packages/react-native-reanimated/Common/cpp/reanimated/Events/UIEventHandlerRegistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

33
#include <jsi/jsi.h>
4-
#include <worklets/WorkletRuntime/WorkletRuntime.h>
4+
#include <worklets/Compat/StableApi.h>
55

66
#include <map>
77
#include <memory>

packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <react/renderer/mounting/ShadowView.h>
55
#include <reanimated/LayoutAnimations/LayoutAnimationType.h>
66

7-
#include <worklets/SharedItems/Serializable.h>
7+
#include <worklets/Compat/StableApi.h>
88

99
#include <jsi/jsi.h>
1010
#include <stdio.h>

packages/react-native-reanimated/Common/cpp/reanimated/LayoutAnimations/LayoutAnimationsProxyCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <react/renderer/uimanager/UIManager.h>
77
#include <reanimated/LayoutAnimations/LayoutAnimationsManager.h>
88
#include <reanimated/Tools/PlatformDepMethodsHolder.h>
9-
#include <worklets/Tools/UIScheduler.h>
9+
#include <worklets/Compat/StableApi.h>
1010

1111
#include <memory>
1212
#include <optional>

0 commit comments

Comments
 (0)