-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
refactor: explicit invalidations for native and cpp #6850
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,13 @@ using namespace facebook; | |
using namespace react; | ||
|
||
WorkletsModule::WorkletsModule( | ||
jni::alias_ref<WorkletsModule::javaobject> jThis, | ||
jsi::Runtime *rnRuntime, | ||
const std::string &valueUnpackerCode, | ||
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread, | ||
const std::shared_ptr<facebook::react::CallInvoker> &jsCallInvoker, | ||
const std::shared_ptr<worklets::JSScheduler> &jsScheduler, | ||
const std::shared_ptr<UIScheduler> &uiScheduler) | ||
: javaPart_(jni::make_global(jThis)), | ||
rnRuntime_(rnRuntime), | ||
: rnRuntime_(rnRuntime), | ||
workletsModuleProxy_(std::make_shared<WorkletsModuleProxy>( | ||
*rnRuntime, | ||
valueUnpackerCode, | ||
|
@@ -38,7 +36,7 @@ WorkletsModule::WorkletsModule( | |
} | ||
|
||
jni::local_ref<WorkletsModule::jhybriddata> WorkletsModule::initHybrid( | ||
jni::alias_ref<jhybridobject> jThis, | ||
jni::alias_ref<jhybridobject> /*jThis*/, | ||
jlong jsContext, | ||
const std::string &valueUnpackerCode, | ||
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread, | ||
|
@@ -52,7 +50,6 @@ jni::local_ref<WorkletsModule::jhybriddata> WorkletsModule::initHybrid( | |
std::make_shared<worklets::JSScheduler>(*rnRuntime, jsCallInvoker); | ||
auto uiScheduler = androidUIScheduler->cthis()->getUIScheduler(); | ||
return makeCxxInstance( | ||
jThis, | ||
rnRuntime, | ||
Comment on lines
52
to
53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change looks weird, do we really need |
||
valueUnpackerCode, | ||
messageQueueThread, | ||
|
@@ -61,8 +58,14 @@ jni::local_ref<WorkletsModule::jhybriddata> WorkletsModule::initHybrid( | |
uiScheduler); | ||
} | ||
|
||
void WorkletsModule::invalidateCpp() { | ||
workletsModuleProxy_.reset(); | ||
} | ||
|
||
void WorkletsModule::registerNatives() { | ||
registerHybrid({makeNativeMethod("initHybrid", WorkletsModule::initHybrid)}); | ||
registerHybrid( | ||
{makeNativeMethod("initHybrid", WorkletsModule::initHybrid), | ||
makeNativeMethod("invalidateCpp", WorkletsModule::invalidateCpp)}); | ||
} | ||
|
||
} // namespace worklets |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ class WorkletsModule : public jni::HybridClass<WorkletsModule> { | |||||
"Lcom/swmansion/worklets/WorkletsModule;"; | ||||||
|
||||||
static jni::local_ref<jhybriddata> initHybrid( | ||||||
jni::alias_ref<jhybridobject> jThis, | ||||||
jni::alias_ref<jhybridobject> /*jThis*/, | ||||||
jlong jsContext, | ||||||
const std::string &valueUnpackerCode, | ||||||
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread, | ||||||
|
@@ -46,19 +46,19 @@ class WorkletsModule : public jni::HybridClass<WorkletsModule> { | |||||
} | ||||||
|
||||||
private: | ||||||
friend HybridBase; | ||||||
jni::global_ref<WorkletsModule::javaobject> javaPart_; | ||||||
jsi::Runtime *rnRuntime_; | ||||||
std::shared_ptr<WorkletsModuleProxy> workletsModuleProxy_; | ||||||
|
||||||
explicit WorkletsModule( | ||||||
jni::alias_ref<WorkletsModule::jhybridobject> jThis, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out we don't need to keep the reference to Java part of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it still have to be a |
||||||
jsi::Runtime *rnRuntime, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, let's pass the runtime as a reference instead of a pointer, i.e. |
||||||
const std::string &valueUnpackerCode, | ||||||
jni::alias_ref<JavaMessageQueueThread::javaobject> messageQueueThread, | ||||||
const std::shared_ptr<facebook::react::CallInvoker> &jsCallInvoker, | ||||||
const std::shared_ptr<worklets::JSScheduler> &jsScheduler, | ||||||
const std::shared_ptr<UIScheduler> &uiScheduler); | ||||||
|
||||||
void invalidateCpp(); | ||||||
|
||||||
friend HybridBase; | ||||||
jsi::Runtime *rnRuntime_; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, let's store runtime as a reference.
Suggested change
|
||||||
std::shared_ptr<WorkletsModuleProxy> workletsModuleProxy_; | ||||||
}; | ||||||
|
||||||
} // namespace worklets |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -69,6 +69,12 @@ public boolean installTurboModule(String valueUnpackerCode) { | |||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public void invalidate() { | ||||||||||||||||||||||||
// We have to destroy extra runtimes when invalidate is called. If we clean | ||||||||||||||||||||||||
// it up later instead there's a chance the runtime will retain references | ||||||||||||||||||||||||
// to invalidated memory and will crash on its destruction. | ||||||||||||||||||||||||
invalidateCpp(); | ||||||||||||||||||||||||
mAndroidUIScheduler.deactivate(); | ||||||||||||||||||||||||
Comment on lines
+72
to
76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a newline here as well
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
private native void invalidateCpp(); | ||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -52,4 +52,13 @@ @implementation WorkletsModule { | |||||||||||||||||||||||
return @YES; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
- (void)invalidate | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
// We have to destroy extra runtimes when invalidate is called. If we clean | ||||||||||||||||||||||||
// it up later instead there's a chance the runtime will retain references | ||||||||||||||||||||||||
// to invalidated memory and will crash on destruction. | ||||||||||||||||||||||||
workletsModuleProxy_.reset(); | ||||||||||||||||||||||||
[super invalidate]; | ||||||||||||||||||||||||
Comment on lines
+57
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a newline here since
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When cleaning during invalidation stage, it shouldn't be possible for two instances to exist at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just
instanceCount_ == 0
?