Skip to content

Commit e6a4c81

Browse files
authored
[0.76] Backport use a legacy native module binding that always returns null in bridgeless mode (#13976)
* Use a legacy native module binding that always returns null in bridgeless mode (#13905) * Use a legacy native module binding that always returns null in bridgeless mode * Change files * format * SampleTurboModule only works as a turbomodule, so do not install it when using web debugger (#13911) * SampleTurboModule only works as a turbomodule, so do not install it when using web debugger * Change files * fix
1 parent 1a83ce8 commit e6a4c81

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Use a legacy native module binding that always returns null in bridgeless mode",
4+
"packageName": "react-native-windows",
5+
"email": "30809111+acoates-ms@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "SampleTurboModule only works as a turbomodule, so do not install it when using web debugger",
4+
"packageName": "react-native-windows",
5+
"email": "30809111+acoates-ms@users.noreply.github.com",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,12 @@ void ReactInstanceWin::LoadModules(
400400
}
401401
#endif
402402

403-
registerTurboModule(
404-
L"SampleTurboModule",
405-
winrt::Microsoft::ReactNative::MakeTurboModuleProvider<::Microsoft::ReactNative::SampleTurboModule>());
403+
if (!m_options.UseWebDebugger()) {
404+
turboModulesProvider->AddModuleProvider(
405+
L"SampleTurboModule",
406+
winrt::Microsoft::ReactNative::MakeTurboModuleProvider<::Microsoft::ReactNative::SampleTurboModule>(),
407+
false);
408+
}
406409

407410
if (devSettings->useTurboModulesOnly) {
408411
::Microsoft::ReactNative::ExceptionsManager::SetRedBoxHander(
@@ -680,8 +683,16 @@ void ReactInstanceWin::InitializeBridgeless() noexcept {
680683
return turboModuleManager->getModule(name);
681684
};
682685

686+
// Use a legacy native module binding that always returns null
687+
// This means that calls to NativeModules.XXX will always return null, rather than crashing on access
688+
auto legacyNativeModuleBinding =
689+
[](const std::string & /*name*/) -> std::shared_ptr<facebook::react::TurboModule> { return nullptr; };
690+
683691
facebook::react::TurboModuleBinding::install(
684-
runtime, std::function(binding), nullptr, m_options.TurboModuleProvider->LongLivedObjectCollection());
692+
runtime,
693+
std::function(binding),
694+
std::function(legacyNativeModuleBinding),
695+
m_options.TurboModuleProvider->LongLivedObjectCollection());
685696

686697
auto componentDescriptorRegistry =
687698
Microsoft::ReactNative::WindowsComponentDescriptorRegistry::FromProperties(

0 commit comments

Comments
 (0)