Refactor: Move HostFunction management logic to TurboModule::get()

Summary:
## Rationale
Better separation of concerns. Less confusion. All the logic around TurboModule HostFunction management is in TurboModule::get(). And TurboModule::createHostFunction() is only responsible for creating the HostFunction. Also, there aren't two TurboModule::get functions, each with different/unclear responsibilities.

## Motivation
The interop layer (i.e: D43918998) will have to override TurboModule::createHostFunction():
- TurboModule::createHostFunction() relies on static functions for method dispatch.
- The interop layer cannot use static functions for method dispatch: interop modules' methods are only be discoverable at runtime.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D43918940

fbshipit-source-id: f2ffd210329a10967e9b2f0c925f4990cb470083
This commit is contained in:
Ramanpreet Nara
2023-03-11 16:59:39 -08:00
committed by Facebook GitHub Bot
parent cb18bf8c09
commit 43ef2ffe78
2 changed files with 12 additions and 11 deletions
@@ -15,11 +15,11 @@ TurboModule::TurboModule(
std::shared_ptr<CallInvoker> jsInvoker)
: name_(std::move(name)), jsInvoker_(std::move(jsInvoker)) {}
jsi::Value TurboModule::get(
jsi::Value TurboModule::createHostFunction(
jsi::Runtime &runtime,
const jsi::PropNameID &propName,
const MethodMetadata &meta) {
auto result = jsi::Function::createFromHostFunction(
return jsi::Function::createFromHostFunction(
runtime,
propName,
static_cast<unsigned int>(meta.argCount),
@@ -28,13 +28,6 @@ jsi::Value TurboModule::get(
const jsi::Value &thisVal,
const jsi::Value *args,
size_t count) { return meta.invoker(rt, *this, args, count); });
// If we have a JS wrapper, cache the result of this lookup
// We don't cache misses, to allow for methodMap_ to dynamically be extended
if (jsRepresentation_) {
jsRepresentation_->lock(runtime).asObject(runtime).setProperty(
runtime, propName, result);
}
return result;
}
void TurboModule::emitDeviceEvent(
@@ -54,7 +54,15 @@ class JSI_EXPORT TurboModule : public facebook::jsi::HostObject {
// Method was not found, let JS decide what to do.
return facebook::jsi::Value::undefined();
} else {
return get(runtime, propName, p->second);
auto moduleMethod = createHostFunction(runtime, propName, p->second);
// If we have a JS wrapper, cache the result of this lookup
// We don't cache misses, to allow for methodMap_ to dynamically be
// extended
if (jsRepresentation_) {
jsRepresentation_->lock(runtime).asObject(runtime).setProperty(
runtime, propName, moduleMethod);
}
return moduleMethod;
}
}
}
@@ -108,7 +116,7 @@ class JSI_EXPORT TurboModule : public facebook::jsi::HostObject {
friend class TurboModuleBinding;
std::unique_ptr<jsi::WeakObject> jsRepresentation_;
facebook::jsi::Value get(
facebook::jsi::Value createHostFunction(
facebook::jsi::Runtime &runtime,
const facebook::jsi::PropNameID &propName,
const MethodMetadata &meta);