mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
cb7f3f4499
Summary:
## Android API
```
// Before we initialize TurboModuleManager
ReactFeatureFlags.useTurboModuleJSCodegen = true
```
## iOS API
```
// Before we initialize RCTBridge
RCTEnableTurboModuleJSCodegen(true);
```
## How is the JS Codegen actually enabled?
The above native flags are translated to the following global variable in JavaScript:
```
global.RN$JSTurboModuleCodegenEnabled = true;
```
Then, all our NativeModule specs are transpiled to contain this logic:
```
interface Foo extends TurboModule {
// ...
}
function __getModuleSchema() {
if (!global.RN$JSTurboModuleCodegenEnabled) {
return undefined;
}
// Return the schema of this spec.
return {...};
}
export default TurboModuleRegistry.get<Foo>('foo', __getModuleSchema());
```
Then, in our C++ JavaTurboModule, and ObjCTurboModule classes, we use the TurboModule JS codegen when the jsi::Object schema is provided from JavaScript in the TurboModuleRegistry.get call.
Changelog: [Internal]
Reviewed By: PeteTheHeat
Differential Revision: D24636307
fbshipit-source-id: 80dcd604cc1121b8a69df875bbfc87e9bb8e4814
94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#include "TurboModuleBinding.h"
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
#include <ReactCommon/LongLivedObject.h>
|
|
#include <cxxreact/SystraceSection.h>
|
|
|
|
using namespace facebook;
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/**
|
|
* Public API to install the TurboModule system.
|
|
*/
|
|
TurboModuleBinding::TurboModuleBinding(
|
|
const TurboModuleProviderFunctionType &&moduleProvider)
|
|
: moduleProvider_(std::move(moduleProvider)) {}
|
|
|
|
void TurboModuleBinding::install(
|
|
jsi::Runtime &runtime,
|
|
const TurboModuleProviderFunctionType &&moduleProvider,
|
|
bool enableJSTurboModuleCodegen) {
|
|
runtime.global().setProperty(
|
|
runtime,
|
|
"RN$JSTurboModuleCodegenEnabled",
|
|
jsi::Value(enableJSTurboModuleCodegen));
|
|
|
|
runtime.global().setProperty(
|
|
runtime,
|
|
"__turboModuleProxy",
|
|
jsi::Function::createFromHostFunction(
|
|
runtime,
|
|
jsi::PropNameID::forAscii(runtime, "__turboModuleProxy"),
|
|
1,
|
|
[binding =
|
|
std::make_shared<TurboModuleBinding>(std::move(moduleProvider))](
|
|
jsi::Runtime &rt,
|
|
const jsi::Value &thisVal,
|
|
const jsi::Value *args,
|
|
size_t count) {
|
|
return binding->jsProxy(rt, thisVal, args, count);
|
|
}));
|
|
}
|
|
|
|
TurboModuleBinding::~TurboModuleBinding() {
|
|
LongLivedObjectCollection::get().clear();
|
|
}
|
|
|
|
std::shared_ptr<TurboModule> TurboModuleBinding::getModule(
|
|
const std::string &name,
|
|
const jsi::Value *schema) {
|
|
std::shared_ptr<TurboModule> module = nullptr;
|
|
{
|
|
SystraceSection s("TurboModuleBinding::getModule", "module", name);
|
|
module = moduleProvider_(name, schema);
|
|
}
|
|
return module;
|
|
}
|
|
|
|
jsi::Value TurboModuleBinding::jsProxy(
|
|
jsi::Runtime &runtime,
|
|
const jsi::Value &thisVal,
|
|
const jsi::Value *args,
|
|
size_t count) {
|
|
if (count < 1) {
|
|
throw std::invalid_argument(
|
|
"__turboModuleProxy must be called with at least 1 argument");
|
|
}
|
|
std::string moduleName = args[0].getString(runtime).utf8(runtime);
|
|
jsi::Value nullSchema = jsi::Value::undefined();
|
|
|
|
std::shared_ptr<TurboModule> module =
|
|
(count >= 2 ? getModule(moduleName, &args[1])
|
|
: getModule(moduleName, &nullSchema));
|
|
|
|
if (module == nullptr) {
|
|
return jsi::Value::null();
|
|
}
|
|
|
|
return jsi::Object::createFromHostObject(runtime, std::move(module));
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|