react-native-windows 0.82.1 → 0.83.0-preview.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/.flowconfig +2 -7
  2. package/Libraries/Animated/components/AnimatedFlatList.js +5 -3
  3. package/Libraries/Animated/components/AnimatedImage.js +4 -3
  4. package/Libraries/Animated/components/AnimatedSectionList.js +2 -2
  5. package/Libraries/Animated/components/AnimatedText.js +7 -3
  6. package/Libraries/Animated/components/AnimatedView.js +3 -2
  7. package/Libraries/Animated/createAnimatedComponent.js +24 -12
  8. package/Libraries/Animated/nodes/AnimatedColor.js +26 -10
  9. package/Libraries/Components/AccessibilityInfo/AccessibilityInfo.js +43 -15
  10. package/Libraries/Components/AccessibilityInfo/AccessibilityInfo.windows.js +43 -15
  11. package/Libraries/Components/ActivityIndicator/ActivityIndicator.d.ts +2 -2
  12. package/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.d.ts +2 -2
  13. package/Libraries/Components/Glyph/Glyph.js +4 -1
  14. package/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.d.ts +2 -2
  15. package/Libraries/Components/RefreshControl/RefreshControl.d.ts +3 -3
  16. package/Libraries/Components/SafeAreaView/SafeAreaView.d.ts +2 -2
  17. package/Libraries/Components/ScrollView/ScrollView.js +1 -0
  18. package/Libraries/Components/ScrollView/ScrollView.windows.js +1 -0
  19. package/Libraries/Components/ScrollView/ScrollViewStickyHeader.js +6 -6
  20. package/Libraries/Components/Switch/Switch.d.ts +2 -2
  21. package/Libraries/Components/Switch/Switch.windows.js +1 -1
  22. package/Libraries/Components/TextInput/TextInput.d.ts +2 -5
  23. package/Libraries/Components/TextInput/TextInput.js +6 -0
  24. package/Libraries/Components/TextInput/TextInput.windows.js +6 -0
  25. package/Libraries/Components/Touchable/TouchableBounce.js +7 -7
  26. package/Libraries/Components/Touchable/TouchableBounce.windows.js +7 -7
  27. package/Libraries/Components/Touchable/TouchableWithoutFeedback.windows.js +1 -1
  28. package/Libraries/Components/View/ReactNativeStyleAttributes.js +19 -1
  29. package/Libraries/Components/View/View.d.ts +2 -2
  30. package/Libraries/Components/View/View.windows.js +0 -1
  31. package/Libraries/Components/View/ViewNativeComponent.js +13 -1
  32. package/Libraries/Core/ReactNativeVersion.js +3 -3
  33. package/Libraries/Core/setUpPerformance.js +2 -0
  34. package/Libraries/Debugging/DebuggingOverlay.js +14 -14
  35. package/Libraries/Debugging/DebuggingOverlayRegistry.js +8 -2
  36. package/Libraries/EventEmitter/RCTDeviceEventEmitter.js +5 -2
  37. package/Libraries/Image/Image.d.ts +3 -3
  38. package/Libraries/Image/ImageInjection.js +3 -6
  39. package/Libraries/Image/ImageTypes.flow.js +3 -7
  40. package/Libraries/Lists/FlatList.js +8 -8
  41. package/Libraries/Lists/SectionList.d.ts +5 -1
  42. package/Libraries/Lists/ViewabilityHelper.js +1 -1
  43. package/Libraries/Lists/VirtualizedList.js +1 -0
  44. package/Libraries/LogBox/UI/AnsiHighlight.js +4 -1
  45. package/Libraries/NativeComponent/BaseViewConfig.android.js +11 -2
  46. package/Libraries/NativeComponent/NativeComponentRegistry.d.ts +98 -0
  47. package/Libraries/NativeComponent/NativeComponentRegistry.js +2 -0
  48. package/Libraries/NativeComponent/NativeComponentRegistryUnstable.js +3 -1
  49. package/Libraries/NativeComponent/ViewConfigIgnore.windows.js +45 -0
  50. package/Libraries/Network/RCTNetworking.android.js +3 -1
  51. package/Libraries/Network/RCTNetworking.ios.js +3 -0
  52. package/Libraries/Network/RCTNetworking.windows.js +3 -0
  53. package/Libraries/Network/XMLHttpRequest.js +1 -41
  54. package/Libraries/Pressability/usePressability.js +14 -3
  55. package/Libraries/ReactNative/PaperUIManager.windows.js +3 -3
  56. package/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricPublicInstance.js +20 -82
  57. package/Libraries/ReactNative/getNativeComponentAttributes.js +12 -0
  58. package/Libraries/Renderer/implementations/ReactFabric-dev.js +6759 -4478
  59. package/Libraries/Renderer/implementations/ReactFabric-prod.js +3169 -3119
  60. package/Libraries/Renderer/implementations/ReactFabric-profiling.js +4732 -3535
  61. package/Libraries/Renderer/implementations/ReactNativeRenderer-dev.js +6646 -4070
  62. package/Libraries/Renderer/implementations/ReactNativeRenderer-prod.js +3136 -2825
  63. package/Libraries/Renderer/implementations/ReactNativeRenderer-profiling.js +4761 -3312
  64. package/Libraries/Renderer/shims/ReactNative.js +3 -1
  65. package/Libraries/Renderer/shims/ReactNativeTypes.js +2 -1
  66. package/Libraries/Renderer/shims/ReactNativeTypes.windows.js +2 -1
  67. package/Libraries/StyleSheet/StyleSheetTypes.d.ts +1 -1
  68. package/Libraries/StyleSheet/StyleSheetTypes.js +44 -0
  69. package/Libraries/StyleSheet/processBackgroundPosition.js +284 -0
  70. package/Libraries/StyleSheet/processBackgroundRepeat.js +105 -0
  71. package/Libraries/StyleSheet/processBackgroundSize.js +104 -0
  72. package/Libraries/Text/Text.d.ts +2 -2
  73. package/Libraries/Text/TextNativeComponent.js +10 -0
  74. package/Libraries/TurboModule/TurboModuleRegistry.js +3 -9
  75. package/Libraries/Utilities/DevLoadingView.js +14 -6
  76. package/Libraries/Utilities/HMRClient.js +13 -5
  77. package/Microsoft.ReactNative/Base/CxxReactIncludes.h +11 -0
  78. package/Microsoft.ReactNative/CompositionComponentView.idl +2 -0
  79. package/Microsoft.ReactNative/CompositionHwndHost.idl +1 -0
  80. package/Microsoft.ReactNative/Fabric/ComponentView.cpp +1 -1
  81. package/Microsoft.ReactNative/Fabric/ComponentView.h +1 -1
  82. package/Microsoft.ReactNative/Fabric/Composition/CompositionHwndHost.cpp +10 -40
  83. package/Microsoft.ReactNative/Fabric/Composition/CompositionViewComponentView.cpp +3 -80
  84. package/Microsoft.ReactNative/Fabric/Composition/ContentIslandComponentView.cpp +45 -12
  85. package/Microsoft.ReactNative/Fabric/Composition/ContentIslandComponentView.h +8 -0
  86. package/Microsoft.ReactNative/Fabric/Composition/Modal/WindowsModalHostViewComponentView.cpp +61 -74
  87. package/Microsoft.ReactNative/Fabric/Composition/ParagraphComponentView.h +4 -0
  88. package/Microsoft.ReactNative/Fabric/Composition/ReactCompositionViewComponentBuilder.h +1 -0
  89. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.cpp +4 -3
  90. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeIsland.h +2 -1
  91. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeWindow.cpp +245 -0
  92. package/Microsoft.ReactNative/Fabric/Composition/ReactNativeWindow.h +80 -0
  93. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentDescriptor.h +20 -36
  94. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp +70 -49
  95. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.h +4 -1
  96. package/Microsoft.ReactNative/Fabric/Composition/UriImageManager.cpp +5 -0
  97. package/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp +7 -2
  98. package/Microsoft.ReactNative/IReactCompositionViewComponentBuilder.idl +1 -0
  99. package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +1 -1
  100. package/Microsoft.ReactNative/Modules/ImageViewManagerModule.cpp +1 -1
  101. package/Microsoft.ReactNative/Modules/LogBoxModule.cpp +20 -94
  102. package/Microsoft.ReactNative/Modules/LogBoxModule.h +1 -3
  103. package/Microsoft.ReactNative/Pch/pch.h +2 -0
  104. package/Microsoft.ReactNative/ReactHost/CrashManager.cpp +5 -0
  105. package/Microsoft.ReactNative/ReactHost/ReactNativeHeaders.h +1 -0
  106. package/Microsoft.ReactNative/ReactNativeAppBuilder.cpp +0 -41
  107. package/Microsoft.ReactNative/ReactNativeAppBuilder.idl +0 -11
  108. package/Microsoft.ReactNative/ReactNativeIsland.idl +2 -3
  109. package/Microsoft.ReactNative/ReactNativeWin32App.cpp +31 -101
  110. package/Microsoft.ReactNative/ReactNativeWin32App.h +2 -13
  111. package/Microsoft.ReactNative/ReactNativeWindow.idl +44 -0
  112. package/Microsoft.ReactNative.Cxx/AutoDraw.h +9 -1
  113. package/Microsoft.ReactNative.Cxx/ReactCommon/CallInvoker.h +13 -16
  114. package/Microsoft.ReactNative.Cxx/ReactCommon/TurboModule.h +24 -36
  115. package/Microsoft.ReactNative.Cxx/ReactCommon/TurboModuleUtils.h +5 -8
  116. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/AString.h +8 -6
  117. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Array.h +32 -49
  118. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Base.h +27 -76
  119. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Bool.h +4 -2
  120. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/CallbackWrapper.h +19 -18
  121. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Class.h +25 -48
  122. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Convert.h +38 -31
  123. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Error.h +11 -6
  124. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/EventEmitter.h +47 -45
  125. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Function.h +69 -89
  126. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/HighResTimeStamp.h +8 -8
  127. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/LongLivedObject.h +6 -6
  128. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Number.h +16 -8
  129. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Object.h +17 -24
  130. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Promise.h +17 -17
  131. package/Microsoft.ReactNative.Cxx/ReactCommon/react/bridging/Value.h +20 -29
  132. package/Microsoft.ReactNative.Cxx/ReactCommon/react/debug/react_native_assert.h +2 -7
  133. package/Microsoft.ReactNative.Cxx/ReactCommon/react/timing/primitives.h +127 -115
  134. package/PropertySheets/External/Microsoft.ReactNative.Composition.CppLib.props +10 -0
  135. package/PropertySheets/External/Microsoft.ReactNative.Uwp.CppLib.props +10 -0
  136. package/PropertySheets/Generated/PackageVersion.g.props +5 -5
  137. package/PropertySheets/Warnings.props +2 -1
  138. package/ReactCommon/ReactCommon.vcxproj +21 -12
  139. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/CxxNativeModule.cpp +2 -2
  140. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/Instance.cpp +381 -0
  141. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.cpp +49 -0
  142. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSIndexedRAMBundle.cpp +145 -0
  143. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/MethodCall.cpp +100 -0
  144. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ModuleRegistry.cpp +256 -0
  145. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/NativeToJsBridge.cpp +13 -4
  146. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/RAMBundleRegistry.cpp +93 -0
  147. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ReactMarker.cpp +149 -0
  148. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/TraceSection.h +25 -35
  149. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsi/jsi/test/testlib.cpp +86 -67
  150. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsiexecutor/jsireact/JSIExecutor.cpp +629 -0
  151. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsiexecutor/jsireact/JSINativeModules.cpp +123 -0
  152. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.cpp +84 -68
  153. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/NetworkIOAgent.h +31 -35
  154. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/Utf8.h +4 -5
  155. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsinspector-modern/network/HttpUtils.cpp +2 -1
  156. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/core/ReactCommon/TurboModule.h +24 -36
  157. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/dom/NativeDOM.h +28 -66
  158. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/webperformance/NativePerformance.cpp +414 -0
  159. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/componentregistry/ComponentDescriptorRegistry.cpp +3 -3
  160. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/image/conversions.h +8 -4
  161. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/text/ParagraphShadowNode.cpp +19 -16
  162. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/text/platform/android/react/renderer/components/text/ParagraphState.h +8 -9
  163. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/AccessibilityPrimitives.h +25 -95
  164. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/accessibilityPropsConversions.h +85 -42
  165. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventDispatcher.cpp +81 -0
  166. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventQueueProcessor.cpp +140 -0
  167. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/imagemanager/primitives.h +25 -31
  168. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/uimanager/UIManager.cpp +746 -0
  169. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/ReactInstance.cpp +702 -0
  170. package/Scripts/Tfs/Layout-Desktop-Headers.ps1 +1 -15
  171. package/Scripts/creaternwapp.cmd +1 -1
  172. package/Scripts/perf/compare-results.js +357 -0
  173. package/Scripts/perf/create-perf-test.js +343 -0
  174. package/Scripts/perf/post-pr-comment.js +210 -0
  175. package/Shared/Hermes/HermesRuntimeTargetDelegate.cpp +8 -0
  176. package/Shared/Hermes/HermesRuntimeTargetDelegate.h +3 -0
  177. package/Shared/Shared.vcxitems +24 -12
  178. package/Shared/Shared.vcxitems.filters +11 -3
  179. package/codegen/NativeIntersectionObserverSpec.g.h +2 -0
  180. package/codegen/NativeNetworkingIOSSpec.g.h +2 -0
  181. package/codegen/NativePerformanceSpec.g.h +6 -0
  182. package/codegen/NativeReactNativeFeatureFlagsSpec.g.h +229 -139
  183. package/codegen/react/components/rnwcore/ActivityIndicatorView.g.h +2 -1
  184. package/codegen/react/components/rnwcore/AndroidDrawerLayout.g.h +42 -25
  185. package/codegen/react/components/rnwcore/AndroidHorizontalScrollContentView.g.h +2 -1
  186. package/codegen/react/components/rnwcore/AndroidProgressBar.g.h +2 -1
  187. package/codegen/react/components/rnwcore/AndroidSwipeRefreshLayout.g.h +11 -6
  188. package/codegen/react/components/rnwcore/AndroidSwitch.g.h +11 -6
  189. package/codegen/react/components/rnwcore/DebuggingOverlay.g.h +1 -0
  190. package/codegen/react/components/rnwcore/InputAccessory.g.h +2 -1
  191. package/codegen/react/components/rnwcore/ModalHostView.g.h +40 -23
  192. package/codegen/react/components/rnwcore/Props.cpp +6 -1
  193. package/codegen/react/components/rnwcore/Props.h +1 -0
  194. package/codegen/react/components/rnwcore/PullToRefreshView.g.h +11 -6
  195. package/codegen/react/components/rnwcore/SafeAreaView.g.h +1 -0
  196. package/codegen/react/components/rnwcore/Switch.g.h +11 -6
  197. package/codegen/react/components/rnwcore/UnimplementedNativeView.g.h +2 -1
  198. package/codegen/react/components/rnwcore/VirtualView.g.h +41 -8
  199. package/codegen/react/components/rnwcore/VirtualViewExperimental.g.h +45 -8
  200. package/codegen/rnwcoreJSI.h +3973 -6059
  201. package/index.js +6 -0
  202. package/index.windows.js +6 -0
  203. package/jest/mockComponent.js +6 -6
  204. package/jest/setup.js +15 -10
  205. package/package.json +27 -27
  206. package/src/private/components/virtualview/VirtualView.js +22 -27
  207. package/src/private/components/virtualview/VirtualViewExperimentalNativeComponent.js +6 -0
  208. package/src/private/featureflags/ReactNativeFeatureFlags.js +100 -19
  209. package/src/private/featureflags/specs/NativeReactNativeFeatureFlags.js +18 -3
  210. package/src/private/setup/setUpDefaultReactNativeEnvironment.js +6 -0
  211. package/src/private/specs_DEPRECATED/components/SwitchNativeComponent.js +1 -1
  212. package/src/private/specs_DEPRECATED/modules/NativeNetworkingIOS.js +1 -0
  213. package/src/private/webapis/dom/nodes/ReactNativeElement.js +12 -2
  214. package/src/private/webapis/intersectionobserver/IntersectionObserver.js +76 -15
  215. package/src/private/webapis/intersectionobserver/internals/IntersectionObserverManager.js +1 -0
  216. package/src/private/webapis/intersectionobserver/specs/NativeIntersectionObserver.js +1 -0
  217. package/src/private/webapis/performance/ResourceTiming.js +31 -4
  218. package/src/private/webapis/performance/internals/RawPerformanceEntry.js +4 -1
  219. package/src/private/webapis/performance/specs/NativePerformance.js +3 -0
  220. package/stubs/double-conversion/double-conversion.h +5 -0
  221. package/templates/cpp-app/template.config.js +1 -1
  222. package/templates/cpp-app/windows/MyApp/MyApp.vcxproj +3 -1
  223. package/templates/cpp-lib/template.config.js +1 -1
  224. package/templates/cpp-lib/windows/MyLib/MyLib.vcxproj +1 -1
  225. package/types/index.d.ts +1 -0
  226. package/types/public/ReactNativeTypes.d.ts +115 -2
  227. package/Libraries/ReactNative/ReactFabricPublicInstance/ReactFabricHostComponent.js +0 -152
  228. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/conversions.h +0 -1574
  229. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/ShadowNode.cpp +0 -528
  230. package/Scripts/OpenSSL.nuspec +0 -39
  231. package/Scripts/OpenSSL.targets +0 -36
  232. package/codegen/rnwcoreJSI-generated.cpp +0 -3470
@@ -0,0 +1,702 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+
9
+ #if _MSC_VER
10
+ #pragma warning(push)
11
+ #pragma warning(disable : 4996) // deprecated APIs
12
+ #endif
13
+ #include "ReactInstance.h"
14
+
15
+ #include <ReactCommon/RuntimeExecutor.h>
16
+ #include <cxxreact/ErrorUtils.h>
17
+ #include <cxxreact/JSBigString.h>
18
+ #include <cxxreact/JSExecutor.h>
19
+ #include <cxxreact/ReactMarker.h>
20
+ #include <cxxreact/TraceSection.h>
21
+ #include <glog/logging.h>
22
+ #include <jsi/JSIDynamic.h>
23
+ #include <jsi/hermes.h>
24
+ #include <jsi/instrumentation.h>
25
+ #include <jsinspector-modern/HostTarget.h>
26
+ #include <jsireact/JSIExecutor.h>
27
+ #include <react/featureflags/ReactNativeFeatureFlags.h>
28
+ #include <react/renderer/core/ShadowNode.h>
29
+ #include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
30
+ #include <react/timing/primitives.h>
31
+ #include <react/utils/jsi-utils.h>
32
+ #include <iostream>
33
+ #include <memory>
34
+ #include <utility>
35
+
36
+ namespace facebook::react {
37
+
38
+ namespace {
39
+
40
+ std::shared_ptr<RuntimeScheduler> createRuntimeScheduler(
41
+ RuntimeExecutor runtimeExecutor,
42
+ RuntimeSchedulerTaskErrorHandler taskErrorHandler) {
43
+ std::shared_ptr<RuntimeScheduler> scheduler =
44
+ std::make_shared<RuntimeScheduler>(
45
+ std::move(runtimeExecutor),
46
+ HighResTimeStamp::now,
47
+ std::move(taskErrorHandler));
48
+ scheduler->setPerformanceEntryReporter(
49
+ // FIXME: Move creation of PerformanceEntryReporter to here and
50
+ // guarantee that its lifetime is the same as the runtime.
51
+ PerformanceEntryReporter::getInstance().get());
52
+
53
+ return scheduler;
54
+ }
55
+
56
+ } // namespace
57
+
58
+ ReactInstance::ReactInstance(
59
+ std::unique_ptr<JSRuntime> runtime,
60
+ std::shared_ptr<MessageQueueThread> jsMessageQueueThread,
61
+ std::shared_ptr<TimerManager> timerManager,
62
+ JsErrorHandler::OnJsError onJsError,
63
+ jsinspector_modern::HostTarget* parentInspectorTarget)
64
+ : runtime_(std::move(runtime)),
65
+ jsMessageQueueThread_(std::move(jsMessageQueueThread)),
66
+ timerManager_(std::move(timerManager)),
67
+ jsErrorHandler_(std::make_shared<JsErrorHandler>(std::move(onJsError))),
68
+ parentInspectorTarget_(parentInspectorTarget) {
69
+ RuntimeExecutor runtimeExecutor =
70
+ [weakRuntime = std::weak_ptr(runtime_),
71
+ weakTimerManager = std::weak_ptr(timerManager_),
72
+ weakJsThread = std::weak_ptr(jsMessageQueueThread_),
73
+ jsErrorHandler = jsErrorHandler_](auto callback) {
74
+ if (weakRuntime.expired()) {
75
+ return;
76
+ }
77
+
78
+ if (auto jsThread = weakJsThread.lock()) {
79
+ jsThread->runOnQueue([jsErrorHandler,
80
+ weakRuntime,
81
+ weakTimerManager,
82
+ callback = std::move(callback)]() {
83
+ auto runtime = weakRuntime.lock();
84
+ if (!runtime) {
85
+ return;
86
+ }
87
+
88
+ jsi::Runtime& jsiRuntime = runtime->getRuntime();
89
+ TraceSection s("ReactInstance::_runtimeExecutor[Callback]");
90
+ try {
91
+ ShadowNode::setUseRuntimeShadowNodeReferenceUpdateOnThread(true);
92
+ callback(jsiRuntime);
93
+ } catch (jsi::JSError& originalError) {
94
+ jsErrorHandler->handleError(jsiRuntime, originalError, true);
95
+ } catch (std::exception& ex) {
96
+ jsi::JSError error(
97
+ jsiRuntime, std::string("Non-js exception: ") + ex.what());
98
+ jsErrorHandler->handleError(jsiRuntime, error, true);
99
+ }
100
+ });
101
+ }
102
+ };
103
+
104
+ if (parentInspectorTarget_ != nullptr) {
105
+ auto executor = parentInspectorTarget_->executorFromThis();
106
+
107
+ auto bufferedRuntimeExecutorThatWaitsForInspectorSetup =
108
+ std::make_shared<BufferedRuntimeExecutor>(runtimeExecutor);
109
+ auto runtimeExecutorThatExecutesAfterInspectorSetup =
110
+ [bufferedRuntimeExecutorThatWaitsForInspectorSetup](
111
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
112
+ bufferedRuntimeExecutorThatWaitsForInspectorSetup->execute(
113
+ std::move(callback));
114
+ };
115
+
116
+ runtimeScheduler_ = createRuntimeScheduler(
117
+ runtimeExecutorThatExecutesAfterInspectorSetup,
118
+ [jsErrorHandler = jsErrorHandler_](
119
+ jsi::Runtime& runtime, jsi::JSError& error) {
120
+ jsErrorHandler->handleError(runtime, error, true);
121
+ });
122
+
123
+ auto runtimeExecutorThatGoesThroughRuntimeScheduler =
124
+ [runtimeScheduler = runtimeScheduler_.get()](
125
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
126
+ runtimeScheduler->scheduleWork(std::move(callback));
127
+ };
128
+
129
+ // This code can execute from any thread, so we need to make sure we set up
130
+ // the inspector logic in the right one. The callback executes immediately
131
+ // if we are already in the right thread.
132
+ executor([this,
133
+ runtimeExecutorThatGoesThroughRuntimeScheduler,
134
+ bufferedRuntimeExecutorThatWaitsForInspectorSetup](
135
+ jsinspector_modern::HostTarget& hostTarget) {
136
+ // Callbacks scheduled through the page target executor are generally
137
+ // not guaranteed to run (e.g.: if the page target is destroyed)
138
+ // but in this case it is because the page target cannot be destroyed
139
+ // before the instance finishes its setup:
140
+ // * On iOS it's because we do the setup synchronously.
141
+ // * On Android it's because we explicitly wait for the instance
142
+ // creation task to finish before starting the destruction.
143
+ inspectorTarget_ = &hostTarget.registerInstance(*this);
144
+ runtimeInspectorTarget_ = &inspectorTarget_->registerRuntime(
145
+ runtime_->getRuntimeTargetDelegate(),
146
+ runtimeExecutorThatGoesThroughRuntimeScheduler);
147
+ bufferedRuntimeExecutorThatWaitsForInspectorSetup->flush();
148
+ });
149
+ } else {
150
+ runtimeScheduler_ = createRuntimeScheduler(
151
+ runtimeExecutor,
152
+ [jsErrorHandler = jsErrorHandler_](
153
+ jsi::Runtime& runtime, jsi::JSError& error) {
154
+ jsErrorHandler->handleError(runtime, error, true);
155
+ });
156
+ }
157
+
158
+ bufferedRuntimeExecutor_ = std::make_shared<BufferedRuntimeExecutor>(
159
+ [runtimeScheduler = runtimeScheduler_.get()](
160
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
161
+ runtimeScheduler->scheduleWork(std::move(callback));
162
+ });
163
+ }
164
+
165
+ void ReactInstance::unregisterFromInspector() {
166
+ if (inspectorTarget_ != nullptr) {
167
+ assert(runtimeInspectorTarget_);
168
+ inspectorTarget_->unregisterRuntime(*runtimeInspectorTarget_);
169
+
170
+ assert(parentInspectorTarget_);
171
+ parentInspectorTarget_->unregisterInstance(*inspectorTarget_);
172
+
173
+ inspectorTarget_ = nullptr;
174
+ }
175
+ }
176
+
177
+ RuntimeExecutor ReactInstance::getUnbufferedRuntimeExecutor() noexcept {
178
+ return [runtimeScheduler = runtimeScheduler_.get()](
179
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
180
+ runtimeScheduler->scheduleWork(std::move(callback));
181
+ };
182
+ }
183
+
184
+ // This BufferedRuntimeExecutor ensures that the main JS bundle finished
185
+ // execution before any JS queued into it from C++ are executed. Use
186
+ // getUnbufferedRuntimeExecutor() instead if you do not need the main JS bundle
187
+ // to have finished. e.g. setting global variables into JS runtime.
188
+ RuntimeExecutor ReactInstance::getBufferedRuntimeExecutor() noexcept {
189
+ return [weakBufferedRuntimeExecutor_ =
190
+ std::weak_ptr<BufferedRuntimeExecutor>(bufferedRuntimeExecutor_)](
191
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
192
+ if (auto strongBufferedRuntimeExecutor_ =
193
+ weakBufferedRuntimeExecutor_.lock()) {
194
+ strongBufferedRuntimeExecutor_->execute(std::move(callback));
195
+ }
196
+ };
197
+ }
198
+
199
+ // TODO(T184010230): Should the RuntimeScheduler returned from this method be
200
+ // buffered?
201
+ std::shared_ptr<RuntimeScheduler>
202
+ ReactInstance::getRuntimeScheduler() noexcept {
203
+ return runtimeScheduler_;
204
+ }
205
+
206
+ namespace {
207
+
208
+ // Copied from JSIExecutor.cpp
209
+ // basename_r isn't in all iOS SDKs, so use this simple version instead.
210
+ std::string simpleBasename(const std::string& path) {
211
+ size_t pos = path.rfind('/');
212
+ return (pos != std::string::npos) ? path.substr(pos) : path;
213
+ }
214
+
215
+ } // namespace
216
+
217
+ /**
218
+ * Load the JS bundle and flush buffered JS calls, future JS calls won't be
219
+ * buffered after calling this.
220
+ * Note that this method is asynchronous. However, a completion callback
221
+ * isn't needed because all calls into JS should be dispatched to the JSThread,
222
+ * preferably via the runtimeExecutor_.
223
+ */
224
+ void ReactInstance::loadScript(
225
+ std::unique_ptr<const JSBigString> script,
226
+ const std::string& sourceURL,
227
+ std::function<void(jsi::Runtime& runtime)>&& beforeLoad,
228
+ std::function<void(jsi::Runtime& runtime)>&& afterLoad) {
229
+ auto buffer = std::make_shared<BigStringBuffer>(std::move(script));
230
+ std::string scriptName = simpleBasename(sourceURL);
231
+
232
+ runtimeScheduler_->scheduleWork([this,
233
+ scriptName,
234
+ sourceURL,
235
+ buffer = std::move(buffer),
236
+ weakBufferedRuntimeExecuter =
237
+ std::weak_ptr<BufferedRuntimeExecutor>(
238
+ bufferedRuntimeExecutor_),
239
+ beforeLoad,
240
+ afterLoad](jsi::Runtime& runtime) {
241
+ if (beforeLoad) {
242
+ beforeLoad(runtime);
243
+ }
244
+ TraceSection s("ReactInstance::loadScript");
245
+ bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl != nullptr);
246
+ if (hasLogger) {
247
+ ReactMarker::logTaggedMarkerBridgeless(
248
+ ReactMarker::RUN_JS_BUNDLE_START, scriptName.c_str());
249
+ ReactMarker::logMarkerBridgeless(ReactMarker::INIT_REACT_RUNTIME_START);
250
+ ReactMarker::logMarkerBridgeless(ReactMarker::APP_STARTUP_START);
251
+ }
252
+
253
+ // Check if the shermes unit is avaliable.
254
+ auto* shUnitAPI = jsi::castInterface<hermes::IHermesSHUnit>(&runtime);
255
+ auto* shUnitCreator = shUnitAPI ? shUnitAPI->getSHUnitCreator() : nullptr;
256
+ if (shUnitCreator) {
257
+ LOG(WARNING) << "ReactInstance: evaluateSHUnit";
258
+ auto* hermesAPI = jsi::castInterface<hermes::IHermes>(&runtime);
259
+ hermesAPI->evaluateSHUnit(shUnitCreator);
260
+ } else {
261
+ LOG(WARNING) << "ReactInstance: evaluateJavaScript() with JS bundle";
262
+ runtime.evaluateJavaScript(buffer, sourceURL);
263
+ }
264
+
265
+ /**
266
+ * TODO(T183610671): We need a safe/reliable way to enable the js
267
+ * pipeline from javascript. Remove this after we figure that out, or
268
+ * after we just remove the js pipeline.
269
+ */
270
+ if (!jsErrorHandler_->hasHandledFatalError()) {
271
+ jsErrorHandler_->setRuntimeReady();
272
+ }
273
+
274
+ if (hasLogger) {
275
+ ReactMarker::logTaggedMarkerBridgeless(
276
+ ReactMarker::RUN_JS_BUNDLE_STOP, scriptName.c_str());
277
+ ReactMarker::logMarkerBridgeless(ReactMarker::INIT_REACT_RUNTIME_STOP);
278
+ ReactMarker::logMarkerBridgeless(ReactMarker::APP_STARTUP_STOP);
279
+ }
280
+ if (auto strongBufferedRuntimeExecuter =
281
+ weakBufferedRuntimeExecuter.lock()) {
282
+ strongBufferedRuntimeExecuter->flush();
283
+ }
284
+ if (afterLoad) {
285
+ afterLoad(runtime);
286
+ }
287
+ });
288
+ }
289
+
290
+ /*
291
+ * Calls a method on a JS module that has been registered with
292
+ * `registerCallableModule`. Used to invoke a JS function from platform code.
293
+ */
294
+ void ReactInstance::callFunctionOnModule(
295
+ const std::string& moduleName,
296
+ const std::string& methodName,
297
+ folly::dynamic&& args) {
298
+ if (bufferedRuntimeExecutor_ == nullptr) {
299
+ LOG(ERROR)
300
+ << "Calling callFunctionOnModule with null BufferedRuntimeExecutor";
301
+ return;
302
+ }
303
+
304
+ bufferedRuntimeExecutor_->execute([this,
305
+ moduleName = moduleName,
306
+ methodName = methodName,
307
+ args = std::move(args)](
308
+ jsi::Runtime& runtime) {
309
+ TraceSection s(
310
+ "ReactInstance::callFunctionOnModule",
311
+ "moduleName",
312
+ moduleName,
313
+ "methodName",
314
+ methodName);
315
+ auto it = callableModules_.find(moduleName);
316
+ if (it == callableModules_.end()) {
317
+ std::ostringstream knownModules;
318
+ int i = 0;
319
+ for (it = callableModules_.begin(); it != callableModules_.end();
320
+ it++, i++) {
321
+ const char* space = (i > 0 ? ", " : " ");
322
+ knownModules << space << it->first;
323
+ }
324
+ throw jsi::JSError(
325
+ runtime,
326
+ "Failed to call into JavaScript module method " + moduleName + "." +
327
+ methodName +
328
+ "(). Module has not been registered as callable. Registered callable JavaScript modules (n = " +
329
+ std::to_string(callableModules_.size()) +
330
+ "):" + knownModules.str() +
331
+ ". Did you forget to call `registerCallableModule`?");
332
+ }
333
+
334
+ if (std::holds_alternative<jsi::Function>(it->second)) {
335
+ auto module =
336
+ std::get<jsi::Function>(it->second).call(runtime).asObject(runtime);
337
+ it->second = std::move(module);
338
+ }
339
+
340
+ auto& module = std::get<jsi::Object>(it->second);
341
+ auto method = module.getPropertyAsFunction(runtime, methodName.c_str());
342
+
343
+ std::vector<jsi::Value> jsArgs;
344
+ for (auto& arg : args) {
345
+ jsArgs.push_back(jsi::valueFromDynamic(runtime, arg));
346
+ }
347
+ method.callWithThis(
348
+ runtime, module, (const jsi::Value*)jsArgs.data(), jsArgs.size());
349
+ });
350
+ }
351
+
352
+ void ReactInstance::registerSegment(
353
+ uint32_t segmentId,
354
+ const std::string& segmentPath) {
355
+ LOG(WARNING) << "Starting to run ReactInstance::registerSegment with segment "
356
+ << segmentId;
357
+ runtimeScheduler_->scheduleWork([=](jsi::Runtime& runtime) {
358
+ TraceSection s("ReactInstance::registerSegment");
359
+ auto tag = std::to_string(segmentId);
360
+ auto script = JSBigFileString::fromPath(segmentPath);
361
+ if (script->size() == 0) {
362
+ throw std::invalid_argument(
363
+ "Empty segment registered with ID " + tag + " from " + segmentPath);
364
+ }
365
+ auto buffer = std::make_shared<BigStringBuffer>(std::move(script));
366
+
367
+ bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl != nullptr);
368
+ if (hasLogger) {
369
+ ReactMarker::logTaggedMarkerBridgeless(
370
+ ReactMarker::REGISTER_JS_SEGMENT_START, tag.c_str());
371
+ }
372
+ LOG(WARNING) << "Starting to evaluate segment " << segmentId
373
+ << " in ReactInstance::registerSegment";
374
+ #pragma clang diagnostic push
375
+ #pragma clang diagnostic ignored "-Wdeprecated-declarations"
376
+ runtime.evaluateJavaScript(
377
+ buffer, JSExecutor::getSyntheticBundlePath(segmentId, segmentPath));
378
+ #pragma clang diagnostic pop
379
+ LOG(WARNING) << "Finished evaluating segment " << segmentId
380
+ << " in ReactInstance::registerSegment";
381
+ if (hasLogger) {
382
+ ReactMarker::logTaggedMarkerBridgeless(
383
+ ReactMarker::REGISTER_JS_SEGMENT_STOP, tag.c_str());
384
+ }
385
+ });
386
+ }
387
+
388
+ namespace {
389
+ void defineReactInstanceFlags(
390
+ jsi::Runtime& runtime,
391
+ const ReactInstance::JSRuntimeFlags& options) noexcept {
392
+ defineReadOnlyGlobal(runtime, "RN$Bridgeless", jsi::Value(true));
393
+
394
+ if (options.isProfiling) {
395
+ defineReadOnlyGlobal(runtime, "__RCTProfileIsProfiling", jsi::Value(true));
396
+ }
397
+
398
+ if (options.runtimeDiagnosticFlags.length() > 0) {
399
+ defineReadOnlyGlobal(
400
+ runtime,
401
+ "RN$DiagnosticFlags",
402
+ jsi::String::createFromUtf8(runtime, options.runtimeDiagnosticFlags));
403
+ }
404
+ }
405
+
406
+ bool isTruthy(jsi::Runtime& runtime, const jsi::Value& value) {
407
+ auto Boolean = runtime.global().getPropertyAsFunction(runtime, "Boolean");
408
+ return Boolean.call(runtime, value).getBool();
409
+ }
410
+
411
+ } // namespace
412
+
413
+ void ReactInstance::initializeRuntime(
414
+ JSRuntimeFlags options,
415
+ BindingsInstallFunc bindingsInstallFunc) noexcept {
416
+ runtimeScheduler_->scheduleWork([this,
417
+ options = std::move(options),
418
+ bindingsInstallFunc =
419
+ std::move(bindingsInstallFunc)](
420
+ jsi::Runtime& runtime) {
421
+ TraceSection s("ReactInstance::initializeRuntime");
422
+
423
+ bindNativePerformanceNow(runtime);
424
+
425
+ RuntimeSchedulerBinding::createAndInstallIfNeeded(
426
+ runtime, runtimeScheduler_);
427
+
428
+ runtime_->unstable_initializeOnJsThread();
429
+
430
+ defineReactInstanceFlags(runtime, options);
431
+
432
+ defineReadOnlyGlobal(
433
+ runtime,
434
+ "RN$useAlwaysAvailableJSErrorHandling",
435
+ jsi::Value(
436
+ ReactNativeFeatureFlags::useAlwaysAvailableJSErrorHandling()));
437
+
438
+ defineReadOnlyGlobal(
439
+ runtime,
440
+ "RN$isRuntimeReady",
441
+ jsi::Function::createFromHostFunction(
442
+ runtime,
443
+ jsi::PropNameID::forAscii(runtime, "isRuntimeReady"),
444
+ 0,
445
+ [jsErrorHandler = jsErrorHandler_](
446
+ jsi::Runtime& /*runtime*/,
447
+ const jsi::Value& /*unused*/,
448
+ const jsi::Value* /*args*/,
449
+ size_t /*count*/) {
450
+ return jsErrorHandler->isRuntimeReady();
451
+ }));
452
+
453
+ defineReadOnlyGlobal(
454
+ runtime,
455
+ "RN$hasHandledFatalException",
456
+ jsi::Function::createFromHostFunction(
457
+ runtime,
458
+ jsi::PropNameID::forAscii(runtime, "hasHandledFatalException"),
459
+ 0,
460
+ [jsErrorHandler = jsErrorHandler_](
461
+ jsi::Runtime& /*runtime*/,
462
+ const jsi::Value& /*unused*/,
463
+ const jsi::Value* /*args*/,
464
+ size_t /*count*/) {
465
+ return jsErrorHandler->hasHandledFatalError();
466
+ }));
467
+
468
+ defineReadOnlyGlobal(
469
+ runtime,
470
+ "RN$notifyOfFatalException",
471
+ jsi::Function::createFromHostFunction(
472
+ runtime,
473
+ jsi::PropNameID::forAscii(runtime, "notifyOfFatalException"),
474
+ 0,
475
+ [jsErrorHandler = jsErrorHandler_](
476
+ jsi::Runtime& /*runtime*/,
477
+ const jsi::Value& /*unused*/,
478
+ const jsi::Value* /*args*/,
479
+ size_t /*count*/) {
480
+ jsErrorHandler->notifyOfFatalError();
481
+ return jsi::Value::undefined();
482
+ }));
483
+
484
+ defineReadOnlyGlobal(
485
+ runtime,
486
+ "RN$inExceptionHandler",
487
+ jsi::Function::createFromHostFunction(
488
+ runtime,
489
+ jsi::PropNameID::forAscii(runtime, "inExceptionHandler"),
490
+ 0,
491
+ [jsErrorHandler = jsErrorHandler_](
492
+ jsi::Runtime& /*runtime*/,
493
+ const jsi::Value& /*unused*/,
494
+ const jsi::Value* /*args*/,
495
+ size_t /*count*/) {
496
+ return jsErrorHandler->inErrorHandler();
497
+ }));
498
+
499
+ // TODO(T196834299): We should really use a C++ turbomodule for this
500
+ defineReadOnlyGlobal(
501
+ runtime,
502
+ "RN$handleException",
503
+ jsi::Function::createFromHostFunction(
504
+ runtime,
505
+ jsi::PropNameID::forAscii(runtime, "handleException"),
506
+ 3,
507
+ [jsErrorHandler = jsErrorHandler_](
508
+ jsi::Runtime& runtime,
509
+ const jsi::Value& /*unused*/,
510
+ const jsi::Value* args,
511
+ size_t count) {
512
+ if (count < 2) {
513
+ throw jsi::JSError(
514
+ runtime,
515
+ "handleException requires 3 arguments: error, isFatal, logToConsole (optional)");
516
+ }
517
+
518
+ auto isFatal = isTruthy(runtime, args[1]);
519
+
520
+ if (!ReactNativeFeatureFlags::
521
+ useAlwaysAvailableJSErrorHandling()) {
522
+ if (jsErrorHandler->isRuntimeReady()) {
523
+ return jsi::Value(false);
524
+ }
525
+ }
526
+
527
+ auto jsError =
528
+ jsi::JSError(runtime, jsi::Value(runtime, args[0]));
529
+
530
+ if (count == 2) {
531
+ jsErrorHandler->handleError(runtime, jsError, isFatal);
532
+ } else {
533
+ auto logToConsole = isTruthy(runtime, args[2]);
534
+ jsErrorHandler->handleError(
535
+ runtime, jsError, isFatal, logToConsole);
536
+ }
537
+
538
+ return jsi::Value(true);
539
+ }));
540
+
541
+ defineReadOnlyGlobal(
542
+ runtime,
543
+ "RN$registerExceptionListener",
544
+ jsi::Function::createFromHostFunction(
545
+ runtime,
546
+ jsi::PropNameID::forAscii(runtime, "registerExceptionListener"),
547
+ 1,
548
+ [errorListeners = std::vector<std::shared_ptr<jsi::Function>>(),
549
+ jsErrorHandler = jsErrorHandler_](
550
+ jsi::Runtime& runtime,
551
+ const jsi::Value& /*unused*/,
552
+ const jsi::Value* args,
553
+ size_t count) mutable {
554
+ if (count < 1) {
555
+ throw jsi::JSError(
556
+ runtime,
557
+ "registerExceptionListener: requires 1 argument: fn");
558
+ }
559
+
560
+ if (!args[0].isObject() ||
561
+ !args[0].getObject(runtime).isFunction(runtime)) {
562
+ throw jsi::JSError(
563
+ runtime,
564
+ "registerExceptionListener: The first argument must be a function");
565
+ }
566
+
567
+ auto errorListener = std::make_shared<jsi::Function>(
568
+ args[0].getObject(runtime).getFunction(runtime));
569
+ errorListeners.emplace_back(errorListener);
570
+
571
+ jsErrorHandler->registerErrorListener(
572
+ [weakErrorListener = std::weak_ptr<jsi::Function>(
573
+ errorListener)](jsi::Runtime& runtime, jsi::Value data) {
574
+ if (auto strongErrorListener = weakErrorListener.lock()) {
575
+ strongErrorListener->call(runtime, data);
576
+ }
577
+ });
578
+
579
+ return jsi::Value::undefined();
580
+ }));
581
+
582
+ defineReadOnlyGlobal(
583
+ runtime,
584
+ "RN$registerCallableModule",
585
+ jsi::Function::createFromHostFunction(
586
+ runtime,
587
+ jsi::PropNameID::forAscii(runtime, "registerCallableModule"),
588
+ 2,
589
+ [this](
590
+ jsi::Runtime& runtime,
591
+ const jsi::Value& /*unused*/,
592
+ const jsi::Value* args,
593
+ size_t count) {
594
+ if (count != 2) {
595
+ throw jsi::JSError(
596
+ runtime,
597
+ "registerCallableModule requires exactly 2 arguments");
598
+ }
599
+ if (!args[0].isString()) {
600
+ throw jsi::JSError(
601
+ runtime,
602
+ "The first argument to registerCallableModule must be a string (the name of the JS module).");
603
+ }
604
+ auto name = args[0].asString(runtime).utf8(runtime);
605
+ if (!args[1].isObject() ||
606
+ !args[1].getObject(runtime).isFunction(runtime)) {
607
+ throw jsi::JSError(
608
+ runtime,
609
+ "The second argument to registerCallableModule must be a function that returns the JS module.");
610
+ }
611
+ callableModules_.emplace(
612
+ std::move(name),
613
+ args[1].getObject(runtime).getFunction(runtime));
614
+ return jsi::Value::undefined();
615
+ }));
616
+
617
+ timerManager_->attachGlobals(runtime);
618
+
619
+ bindingsInstallFunc(runtime);
620
+ });
621
+ }
622
+
623
+ void ReactInstance::handleMemoryPressureJs(int pressureLevel) {
624
+ // The level is an enum value passed by the Android OS to an onTrimMemory
625
+ // event callback. Defined in ComponentCallbacks2.
626
+ enum AndroidMemoryPressure {
627
+ TRIM_MEMORY_BACKGROUND = 40,
628
+ TRIM_MEMORY_COMPLETE = 80,
629
+ TRIM_MEMORY_MODERATE = 60,
630
+ TRIM_MEMORY_RUNNING_CRITICAL = 15,
631
+ TRIM_MEMORY_RUNNING_LOW = 10,
632
+ TRIM_MEMORY_RUNNING_MODERATE = 5,
633
+ TRIM_MEMORY_UI_HIDDEN = 20,
634
+ };
635
+ const char* levelName = nullptr;
636
+ switch (pressureLevel) {
637
+ case TRIM_MEMORY_BACKGROUND:
638
+ levelName = "TRIM_MEMORY_BACKGROUND";
639
+ break;
640
+ case TRIM_MEMORY_COMPLETE:
641
+ levelName = "TRIM_MEMORY_COMPLETE";
642
+ break;
643
+ case TRIM_MEMORY_MODERATE:
644
+ levelName = "TRIM_MEMORY_MODERATE";
645
+ break;
646
+ case TRIM_MEMORY_RUNNING_CRITICAL:
647
+ levelName = "TRIM_MEMORY_RUNNING_CRITICAL";
648
+ break;
649
+ case TRIM_MEMORY_RUNNING_LOW:
650
+ levelName = "TRIM_MEMORY_RUNNING_LOW";
651
+ break;
652
+ case TRIM_MEMORY_RUNNING_MODERATE:
653
+ levelName = "TRIM_MEMORY_RUNNING_MODERATE";
654
+ break;
655
+ case TRIM_MEMORY_UI_HIDDEN:
656
+ levelName = "TRIM_MEMORY_UI_HIDDEN";
657
+ break;
658
+ default:
659
+ levelName = "UNKNOWN";
660
+ break;
661
+ }
662
+
663
+ switch (pressureLevel) {
664
+ case TRIM_MEMORY_RUNNING_LOW:
665
+ case TRIM_MEMORY_RUNNING_MODERATE:
666
+ case TRIM_MEMORY_UI_HIDDEN:
667
+ // For non-severe memory trims, do nothing.
668
+ LOG(INFO) << "Memory warning (pressure level: " << levelName
669
+ << ") received by JS VM, ignoring because it's non-severe";
670
+ break;
671
+ case TRIM_MEMORY_BACKGROUND:
672
+ case TRIM_MEMORY_COMPLETE:
673
+ case TRIM_MEMORY_MODERATE:
674
+ case TRIM_MEMORY_RUNNING_CRITICAL:
675
+ // For now, pressureLevel is unused by collectGarbage.
676
+ // This may change in the future if the JS GC has different styles of
677
+ // collections.
678
+ LOG(INFO) << "Memory warning (pressure level: " << levelName
679
+ << ") received by JS VM, running a GC";
680
+ runtimeScheduler_->scheduleWork([=](jsi::Runtime& runtime) {
681
+ TraceSection s("ReactInstance::handleMemoryPressure");
682
+ runtime.instrumentation().collectGarbage(levelName);
683
+ });
684
+ break;
685
+ default:
686
+ // Use the raw number instead of the name here since the name is
687
+ // meaningless.
688
+ LOG(WARNING) << "Memory warning (pressure level: " << pressureLevel
689
+ << ") received by JS VM, unrecognized pressure level";
690
+ break;
691
+ }
692
+ }
693
+
694
+ void* ReactInstance::getJavaScriptContext() {
695
+ return &runtime_->getRuntime();
696
+ }
697
+
698
+ } // namespace facebook::react
699
+
700
+ #if _MSC_VER
701
+ #pragma warning(pop)
702
+ #endif