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
60 lines
1.3 KiB
C++
60 lines
1.3 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <ReactCommon/TurboModule.h>
|
|
#include <jsi/jsi.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
class JSCallInvoker;
|
|
|
|
/**
|
|
* Represents the JavaScript binding for the TurboModule system.
|
|
*/
|
|
class TurboModuleBinding {
|
|
public:
|
|
/*
|
|
* Installs TurboModuleBinding into JavaScript runtime.
|
|
* Thread synchronization must be enforced externally.
|
|
*/
|
|
static void install(
|
|
jsi::Runtime &runtime,
|
|
const TurboModuleProviderFunctionType &&moduleProvider,
|
|
bool enableJSTurboModuleCodegen);
|
|
|
|
TurboModuleBinding(const TurboModuleProviderFunctionType &&moduleProvider);
|
|
virtual ~TurboModuleBinding();
|
|
|
|
/**
|
|
* Get an TurboModule instance for the given module name.
|
|
*/
|
|
std::shared_ptr<TurboModule> getModule(
|
|
const std::string &name,
|
|
const jsi::Value *schema);
|
|
|
|
private:
|
|
/**
|
|
* A lookup function exposed to JS to get an instance of a TurboModule
|
|
* for the given name.
|
|
*/
|
|
jsi::Value jsProxy(
|
|
jsi::Runtime &runtime,
|
|
const jsi::Value &thisVal,
|
|
const jsi::Value *args,
|
|
size_t count);
|
|
|
|
TurboModuleProviderFunctionType moduleProvider_;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|