Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

namespace worklets {

WorkletRuntimeHolderNativeState::WorkletRuntimeHolderNativeState(
const std::weak_ptr<WorkletRuntime> &workletRuntime)
: weakWorkletRuntime_(workletRuntime) {}

std::weak_ptr<WorkletRuntime>
WorkletRuntimeHolderNativeState::getWorkletRuntime() const {
return weakWorkletRuntime_;
}

class AroundLock {
const std::shared_ptr<std::recursive_mutex> mutex_;

Expand Down Expand Up @@ -100,6 +109,13 @@ void WorkletRuntime::init(
auto optimizedJsiWorkletsModuleProxy =
jsi_utils::optimizedFromHostObject(rt, std::move(jsiWorkletsModuleProxy));

auto workletRuntimeHolder = jsi::Object(rt);
workletRuntimeHolder.setNativeState(
rt, std::make_shared<WorkletRuntimeHolderNativeState>(weak_from_this()));

rt.global().setProperty(
rt, "_WORKLET_RUNTIME_HOLDER", std::move(workletRuntimeHolder));

WorkletRuntimeDecorator::decorate(
rt,
name_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ namespace worklets {
* Forward declaration to avoid circular dependencies.
*/
class JSIWorkletsModuleProxy;
class WorkletRuntime;

class WorkletRuntimeHolderNativeState : public jsi::NativeState {
public:
explicit WorkletRuntimeHolderNativeState(
const std::weak_ptr<WorkletRuntime> &workletRuntime);

std::weak_ptr<WorkletRuntime> getWorkletRuntime() const;

private:
std::weak_ptr<WorkletRuntime> weakWorkletRuntime_;
};

class WorkletRuntime : public jsi::HostObject,
public std::enable_shared_from_this<WorkletRuntime> {
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native-worklets/src/publicGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ declare global {
* - Value _3_: Worker Worklet Runtime
*/
var __RUNTIME_KIND: RuntimeKind | 1 | 2 | 3;

/**
* Holds a reference to the WorkletRuntimeHolderNativeState instance used to
* obtain a weak reference to the WorkletRuntime.
*/
var _WORKLET_RUNTIME_HOLDER: object | undefined;
}
Loading