Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Worklet API to RuntimeManager.h #1861

Merged
merged 25 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c4a08c7
Update UIResponder+Reanimated.mm
mrousavy Mar 23, 2021
8a5b3e3
Use std::make_shared for Scheduler
mrousavy Mar 23, 2021
2955e81
Indent
mrousavy Mar 23, 2021
ddd3cfc
private:
mrousavy Mar 23, 2021
8274b52
Re-indent NativeReanimatedModule
mrousavy Mar 23, 2021
1cec07a
new -> make_unique/make_shared
mrousavy Mar 23, 2021
4762801
new -> std::make_shared (NativeReanimatedModule ctor)
mrousavy Mar 23, 2021
b377ee6
Restructure RuntimeDecorator to be more general (ui -> worklet)
mrousavy Mar 23, 2021
5dde5dd
Fix private ctor call in make_shared
mrousavy Mar 23, 2021
24a6cea
Merge branch 'restructure-code' into restructure-runtimedecorator
mrousavy Mar 23, 2021
cb5475c
Create RuntimeManager.h
mrousavy Mar 23, 2021
61f54c4
Extract worklet specific properties from `NativeReanimatedModule` -> …
mrousavy Mar 23, 2021
6f37341
Update ShareableValue.cpp
mrousavy Mar 23, 2021
ca2d678
Include RuntimeDecorator
mrousavy Mar 23, 2021
7faf4e8
Merge branch 'restructure-runtimedecorator' into restructure-runtimem…
mrousavy Mar 23, 2021
fd79583
Fix maybeInitializeOnUIRuntime
mrousavy Mar 23, 2021
aa334c5
Revert "Update UIResponder+Reanimated.mm"
mrousavy Mar 23, 2021
dee5f7d
Merge branch 'restructure-code' into restructure-runtimedecorator
mrousavy Mar 23, 2021
fb199b0
Remove unused `NativeReanimatedModule *module`
mrousavy Mar 23, 2021
21fdbdd
Merge branch 'restructure-runtimedecorator' into restructure-runtimem…
mrousavy Mar 23, 2021
cfde6b7
Merge branch 'master' into restructure-runtimedecorator
mrousavy Mar 23, 2021
dc5642a
Merge branch 'restructure-runtimedecorator' into restructure-runtimem…
mrousavy Mar 23, 2021
af95390
Merge branch 'master' into restructure-runtimemanager
mrousavy Mar 23, 2021
b638ad7
Use `make_shared` instead of `new`
mrousavy Mar 30, 2021
737425e
Remove "created by" comment from Xcode
mrousavy Mar 31, 2021
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
11 changes: 4 additions & 7 deletions Common/cpp/NativeModules/NativeReanimatedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,18 @@ NativeReanimatedModule::NativeReanimatedModule(std::shared_ptr<CallInvoker> jsIn
std::unique_ptr<jsi::Runtime> rt,
std::shared_ptr<ErrorHandler> errorHandler,
std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)> propObtainer,
PlatformDepMethodsHolder platformDepMethodsHolder) : NativeReanimatedModuleSpec(jsInvoker),
runtime(std::move(rt)),
PlatformDepMethodsHolder platformDepMethodsHolder) :
NativeReanimatedModuleSpec(jsInvoker),
RuntimeManager(std::move(rt), errorHandler, scheduler),
mapperRegistry(std::make_shared<MapperRegistry>()),
eventHandlerRegistry(std::make_shared<EventHandlerRegistry>()),
requestRender(platformDepMethodsHolder.requestRender),
propObtainer(propObtainer),
errorHandler(errorHandler),
workletsCache(std::make_shared<WorkletsCache>()),
scheduler(scheduler)
propObtainer(propObtainer)
{
auto requestAnimationFrame = [=](FrameCallback callback) {
frameCallbacks.push_back(callback);
maybeRequestRender();
};

RuntimeDecorator::decorateUIRuntime(*runtime,
platformDepMethodsHolder.updaterFunction,
requestAnimationFrame,
Expand Down
5 changes: 3 additions & 2 deletions Common/cpp/SharedItems/FrozenObject.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include "FrozenObject.h"
#include "SharedParent.h"
#include "ShareableValue.h"
#include "RuntimeManager.h"

namespace reanimated {

FrozenObject::FrozenObject(jsi::Runtime &rt, const jsi::Object &object, NativeReanimatedModule *module) {
FrozenObject::FrozenObject(jsi::Runtime &rt, const jsi::Object &object, RuntimeManager *runtimeManager) {
auto propertyNames = object.getPropertyNames(rt);
for (size_t i = 0, count = propertyNames.size(rt); i < count; i++) {
auto propertyName = propertyNames.getValueAtIndex(rt, i).asString(rt);
std::string nameStr = propertyName.utf8(rt);
map[nameStr] = ShareableValue::adapt(rt, object.getProperty(rt, propertyName), module);
map[nameStr] = ShareableValue::adapt(rt, object.getProperty(rt, propertyName), runtimeManager);
this->containsHostFunction |= map[nameStr]->containsHostFunction;
}
}
Expand Down
26 changes: 13 additions & 13 deletions Common/cpp/SharedItems/MutableValue.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#include "MutableValue.h"
#include "SharedParent.h"
#include "ShareableValue.h"
#include "NativeReanimatedModule.h"
#include "RuntimeManager.h"
#include "RuntimeDecorator.h"

namespace reanimated {

void MutableValue::setValue(jsi::Runtime &rt, const jsi::Value &newValue) {
std::lock_guard<std::mutex> lock(readWriteMutex);
value = ShareableValue::adapt(rt, newValue, module);
value = ShareableValue::adapt(rt, newValue, runtimeManager);

std::shared_ptr<MutableValue> thiz = shared_from_this();
auto notifyListeners = [thiz] () {
for (auto listener : thiz->listeners) {
listener.second();
}
};

if (RuntimeDecorator::isWorkletRuntime(rt)) {
notifyListeners();
} else {
module->scheduler->scheduleOnUI([notifyListeners] {
runtimeManager->scheduler->scheduleOnUI([notifyListeners] {
notifyListeners();
});
}
}
}

jsi::Value MutableValue::getValue(jsi::Runtime &rt) {
Expand All @@ -35,12 +35,12 @@ void MutableValue::set(jsi::Runtime &rt, const jsi::PropNameID &name, const jsi:

if (RuntimeDecorator::isReactRuntime(rt)) {
if (propName == "value") {
auto shareable = ShareableValue::adapt(rt, newValue, module);
module->scheduler->scheduleOnUI([this, shareable] {
jsi::Runtime &rt = *this->module->runtime.get();
auto shareable = ShareableValue::adapt(rt, newValue, runtimeManager);
runtimeManager->scheduler->scheduleOnUI([this, shareable] {
jsi::Runtime &rt = *this->runtimeManager->runtime.get();
auto setterProxy = jsi::Object::createFromHostObject(rt, std::make_shared<MutableValueSetterProxy>(shared_from_this()));
jsi::Value newValue = shareable->getValue(rt);
module->valueSetter->getValue(rt)
runtimeManager->valueSetter->getValue(rt)
.asObject(rt)
.asFunction(rt)
.callWithThis(rt, setterProxy, newValue);
Expand All @@ -52,7 +52,7 @@ void MutableValue::set(jsi::Runtime &rt, const jsi::PropNameID &name, const jsi:
// UI thread
if (propName == "value") {
auto setterProxy = jsi::Object::createFromHostObject(rt, std::make_shared<MutableValueSetterProxy>(shared_from_this()));
module->valueSetter->getValue(rt)
runtimeManager->valueSetter->getValue(rt)
.asObject(rt)
.asFunction(rt)
.callWithThis(rt, setterProxy, newValue);
Expand Down Expand Up @@ -94,8 +94,8 @@ std::vector<jsi::PropNameID> MutableValue::getPropertyNames(jsi::Runtime &rt) {
return result;
}

MutableValue::MutableValue(jsi::Runtime &rt, const jsi::Value &initial, NativeReanimatedModule *module, std::shared_ptr<Scheduler> s):
StoreUser(s), module(module), value(ShareableValue::adapt(rt, initial, module)) {
MutableValue::MutableValue(jsi::Runtime &rt, const jsi::Value &initial, RuntimeManager *runtimeManager, std::shared_ptr<Scheduler> s):
StoreUser(s), runtimeManager(runtimeManager), value(ShareableValue::adapt(rt, initial, runtimeManager)) {
}

unsigned long int MutableValue::addListener(unsigned long id, std::function<void ()> listener) {
Expand Down
1 change: 0 additions & 1 deletion Common/cpp/SharedItems/RemoteObject.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "RemoteObject.h"
#include "SharedParent.h"
#include "NativeReanimatedModule.h"
#include "RuntimeDecorator.h"
#include <jsi/jsi.h>

Expand Down
58 changes: 29 additions & 29 deletions Common/cpp/SharedItems/ShareableValue.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "ShareableValue.h"
#include "SharedParent.h"
#include "NativeReanimatedModule.h"
#include "RuntimeManager.h"
#include "MutableValue.h"
#include "MutableValueSetterProxy.h"
#include "RemoteObject.h"
#include "FrozenObject.h"
#include "RuntimeDecorator.h"

namespace reanimated {

Expand Down Expand Up @@ -76,7 +77,7 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
if (objectType == ValueType::MutableValueType) {
type = ValueType::MutableValueType;
valueContainer = std::make_unique<MutableValueWrapper>(
std::make_shared<MutableValue>(rt, value, module, module->scheduler)
std::make_shared<MutableValue>(rt, value, runtimeManager, runtimeManager->scheduler)
);
} else if (value.isUndefined()) {
type = ValueType::UndefinedType;
Expand Down Expand Up @@ -113,8 +114,7 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
} else {
// a worklet
type = ValueType::WorkletFunctionType;

valueContainer = std::make_unique<FrozenObjectWrapper>(std::make_shared<FrozenObject>(rt, object, module));
valueContainer = std::make_unique<FrozenObjectWrapper>(std::make_shared<FrozenObject>(rt, object, runtimeManager));
auto& frozenObject = ValueWrapper::asFrozenObject(valueContainer);
containsHostFunction |= frozenObject->containsHostFunction;
if (isRNRuntime && !containsHostFunction) {
Expand All @@ -127,7 +127,7 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
valueContainer = std::make_unique<FrozenArrayWrapper>();
auto& frozenArray = ValueWrapper::asFrozenArray(valueContainer);
for (size_t i = 0, size = array.size(rt); i < size; i++) {
auto sv = adapt(rt, array.getValueAtIndex(rt, i), module);
auto sv = adapt(rt, array.getValueAtIndex(rt, i), runtimeManager);
containsHostFunction |= sv->containsHostFunction;
frozenArray.push_back(sv);
}
Expand All @@ -144,13 +144,13 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
} else if (objectType == ValueType::RemoteObjectType) {
type = ValueType::RemoteObjectType;
valueContainer = std::make_unique<RemoteObjectWrapper>(
std::make_shared<RemoteObject>(rt, object, module, module->scheduler)
std::make_shared<RemoteObject>(rt, object, runtimeManager, runtimeManager->scheduler)
);
} else {
// create frozen object based on a copy of a given object
type = ValueType::FrozenObjectType;
valueContainer = std::make_unique<FrozenObjectWrapper>(
std::make_shared<FrozenObject>(rt, object, module)
std::make_shared<FrozenObject>(rt, object, runtimeManager)
);
auto& frozenObject = ValueWrapper::asFrozenObject(valueContainer);
containsHostFunction |= frozenObject->containsHostFunction;
Expand All @@ -169,8 +169,8 @@ void ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType
}
}

std::shared_ptr<ShareableValue> ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, NativeReanimatedModule *module, ValueType valueType) {
auto sv = std::shared_ptr<ShareableValue>(new ShareableValue(module, module->scheduler));
std::shared_ptr<ShareableValue> ShareableValue::adapt(jsi::Runtime &rt, const jsi::Value &value, RuntimeManager *runtimeManager, ValueType valueType) {
auto sv = std::shared_ptr<ShareableValue>(new ShareableValue(runtimeManager, runtimeManager->scheduler));
sv->adapt(rt, value, valueType);
return sv;
}
Expand Down Expand Up @@ -259,10 +259,10 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
// function is accessed from a different runtime, we wrap function in host func that'd enqueue
// call on an appropriate thread

auto module = this->module;
auto runtimeManager = this->runtimeManager;
auto hostFunction = hostFunctionWrapper->value;

auto warnFunction = [module, hostFunction](
auto warnFunction = [runtimeManager, hostFunction](
jsi::Runtime &rt,
const jsi::Value &thisValue,
const jsi::Value *args,
Expand All @@ -280,13 +280,13 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
exceptionMessage += " from a different thread.\n\nOccurred in worklet location: ";
exceptionMessage += workletLocation;
exceptionMessage += CALLBACK_ERROR_SUFFIX;
module->errorHandler->setError(exceptionMessage);
module->errorHandler->raise();
runtimeManager->errorHandler->setError(exceptionMessage);
runtimeManager->errorHandler->raise();

return jsi::Value::undefined();
};

auto clb = [module, hostFunction, hostRuntime](
auto clb = [runtimeManager, hostFunction, hostRuntime](
jsi::Runtime &rt,
const jsi::Value &thisValue,
const jsi::Value *args,
Expand All @@ -297,7 +297,7 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {

std::vector<std::shared_ptr<ShareableValue>> params;
for (int i = 0; i < count; ++i) {
params.push_back(ShareableValue::adapt(rt, args[i], module));
params.push_back(ShareableValue::adapt(rt, args[i], runtimeManager));
}

std::function<void()> job = [hostFunction, hostRuntime, params] {
Expand All @@ -314,7 +314,7 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
// ToDo use returned value to return promise
};

module->scheduler->scheduleOnJS(job);
runtimeManager->scheduler->scheduleOnJS(job);
return jsi::Value::undefined();
};
jsi::Function wrapperFunction = jsi::Function::createFromHostFunction(rt, jsi::PropNameID::forAscii(rt, "hostFunction"), 0, warnFunction);
Expand All @@ -326,13 +326,13 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
}
}
case ValueType::WorkletFunctionType: {
auto module = this->module;
auto runtimeManager = this->runtimeManager;
auto& frozenObject = ValueWrapper::asFrozenObject(this->valueContainer);
if (RuntimeDecorator::isWorkletRuntime(rt)) {
// when running on UI thread we prep a function

auto jsThis = std::make_shared<jsi::Object>(frozenObject->shallowClone(*module->runtime));
std::shared_ptr<jsi::Function> funPtr(module->workletsCache->getFunction(rt, frozenObject));
auto jsThis = std::make_shared<jsi::Object>(frozenObject->shallowClone(*runtimeManager->runtime));
std::shared_ptr<jsi::Function> funPtr(runtimeManager->workletsCache->getFunction(rt, frozenObject));
auto name = funPtr->getProperty(rt, "name").asString(rt).utf8(rt);

auto clb = [=](
Expand Down Expand Up @@ -360,8 +360,8 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
if (location.isString()) {
str += "\nIn file: " + location.asString(rt).utf8(rt);
}
module->errorHandler->setError(str);
module->errorHandler->raise();
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
}

rt.global().setProperty(rt, "jsThis", oldJSThis); //clean jsThis
Expand All @@ -381,14 +381,14 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {

std::vector<std::shared_ptr<ShareableValue>> params;
for (int i = 0; i < count; ++i) {
params.push_back(ShareableValue::adapt(rt, args[i], module));
params.push_back(ShareableValue::adapt(rt, args[i], runtimeManager));
}

module->scheduler->scheduleOnUI([=] {
jsi::Runtime &rt = *module->runtime.get();
runtimeManager->scheduler->scheduleOnUI([=] {
jsi::Runtime &rt = *runtimeManager->runtime.get();
auto jsThis = createFrozenWrapper(rt, frozenObject).getObject(rt);
auto code = jsThis.getProperty(rt, "asString").asString(rt).utf8(rt);
std::shared_ptr<jsi::Function> funPtr(module->workletsCache->getFunction(rt, frozenObject));
std::shared_ptr<jsi::Function> funPtr(runtimeManager->workletsCache->getFunction(rt, frozenObject));

jsi::Value * args = new jsi::Value[params.size()];
for (int i = 0; i < params.size(); ++i) {
Expand All @@ -406,17 +406,17 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {

} catch(std::exception &e) {
std::string str = e.what();
module->errorHandler->setError(str);
module->errorHandler->raise();
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
} catch(...) {
// TODO find out a way to get the error's message on hermes
jsi::Value location = jsThis.getProperty(rt, "__location");
std::string str = "Javascript worklet error";
if (location.isString()) {
str += "\nIn file: " + location.asString(rt).utf8(rt);
}
module->errorHandler->setError(str);
module->errorHandler->raise();
runtimeManager->errorHandler->setError(str);
runtimeManager->errorHandler->raise();
}
rt.global().setProperty(rt, "jsThis", oldJSThis); //clean jsThis

Expand Down
4 changes: 2 additions & 2 deletions Common/cpp/Tools/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void Scheduler::setJSCallInvoker(std::shared_ptr<facebook::react::CallInvoker> j
jsCallInvoker_ = jsCallInvoker;
}

void Scheduler::setModule(std::shared_ptr<NativeReanimatedModule> module) {
this->module = module;
void Scheduler::setRuntimeManager(std::shared_ptr<RuntimeManager> runtimeManager) {
this->runtimeManager = runtimeManager;
}

Scheduler::~Scheduler() {}
Expand Down
11 changes: 2 additions & 9 deletions Common/cpp/headers/NativeModules/NativeReanimatedModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#include "NativeReanimatedModuleSpec.h"
#include "Scheduler.h"
#include "ErrorHandler.h"
#include "WorkletsCache.h"
#include "RuntimeDecorator.h"
#include "PlatformDepMethodsHolder.h"
#include <unistd.h>
#include <memory>
#include <vector>
#include "RuntimeManager.h"

namespace reanimated
{
Expand All @@ -20,7 +20,7 @@ class MutableValue;
class MapperRegistry;
class EventHandlerRegistry;

class NativeReanimatedModule : public NativeReanimatedModuleSpec
class NativeReanimatedModule : public NativeReanimatedModuleSpec, public RuntimeManager
{
friend ShareableValue;
friend MutableValue;
Expand Down Expand Up @@ -54,8 +54,6 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec
bool isAnyHandlerWaitingForEvent(std::string eventName);

void maybeRequestRender();
public:
std::unique_ptr<jsi::Runtime> runtime;
private:
std::shared_ptr<MapperRegistry> mapperRegistry;
std::shared_ptr<EventHandlerRegistry> eventHandlerRegistry;
Expand All @@ -64,11 +62,6 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec
std::vector<FrameCallback> frameCallbacks;
bool renderRequested = false;
std::function<jsi::Value(jsi::Runtime &, const int, const jsi::String &)> propObtainer;
public:
std::shared_ptr<ErrorHandler> errorHandler;
std::shared_ptr<WorkletsCache> workletsCache;
std::shared_ptr<ShareableValue> valueSetter;
std::shared_ptr<Scheduler> scheduler;
};

} // namespace reanimated
3 changes: 2 additions & 1 deletion Common/cpp/headers/SharedItems/FrozenObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "WorkletsCache.h"
#include "SharedParent.h"
#include "RuntimeManager.h"
#include <jsi/jsi.h>

using namespace facebook;
Expand All @@ -19,7 +20,7 @@ class FrozenObject : public jsi::HostObject {

public:

FrozenObject(jsi::Runtime &rt, const jsi::Object &object, NativeReanimatedModule *module);
FrozenObject(jsi::Runtime &rt, const jsi::Object &object, RuntimeManager *runtimeManager);
jsi::Object shallowClone(jsi::Runtime &rt);
bool containsHostFunction = false;
};
Expand Down
Loading