react-native-windows 0.82.1 → 0.82.3

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 (32) hide show
  1. package/Microsoft.ReactNative/Base/CxxReactIncludes.h +11 -0
  2. package/Microsoft.ReactNative/Fabric/Composition/CompositionEventHandler.cpp +52 -2
  3. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp +70 -49
  4. package/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.h +4 -1
  5. package/Microsoft.ReactNative/Fabric/platform/react/renderer/textlayoutmanager/WindowsTextLayoutManager.cpp +7 -2
  6. package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +1 -1
  7. package/Microsoft.ReactNative/Pch/pch.h +2 -0
  8. package/Microsoft.ReactNative/ReactHost/CrashManager.cpp +5 -0
  9. package/Microsoft.ReactNative/ReactHost/ReactNativeHeaders.h +1 -0
  10. package/PropertySheets/External/Microsoft.ReactNative.Composition.CppLib.props +10 -0
  11. package/PropertySheets/External/Microsoft.ReactNative.Uwp.CppLib.props +10 -0
  12. package/PropertySheets/Generated/PackageVersion.g.props +3 -3
  13. package/ReactCommon/ReactCommon.vcxproj +10 -10
  14. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/Instance.cpp +379 -0
  15. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSExecutor.cpp +47 -0
  16. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/JSIndexedRAMBundle.cpp +143 -0
  17. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/MethodCall.cpp +98 -0
  18. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ModuleRegistry.cpp +254 -0
  19. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/NativeToJsBridge.cpp +9 -0
  20. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/RAMBundleRegistry.cpp +91 -0
  21. package/ReactCommon/TEMP_UntilReactCommonUpdate/cxxreact/ReactMarker.cpp +147 -0
  22. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsiexecutor/jsireact/JSIExecutor.cpp +622 -0
  23. package/ReactCommon/TEMP_UntilReactCommonUpdate/jsiexecutor/jsireact/JSINativeModules.cpp +121 -0
  24. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/nativemodule/webperformance/NativePerformance.cpp +409 -0
  25. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/components/view/BaseViewProps.cpp +628 -0
  26. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventDispatcher.cpp +79 -0
  27. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/core/EventQueueProcessor.cpp +138 -0
  28. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/renderer/uimanager/UIManager.cpp +732 -0
  29. package/ReactCommon/TEMP_UntilReactCommonUpdate/react/runtime/ReactInstance.cpp +687 -0
  30. package/Shared/Shared.vcxitems +9 -9
  31. package/package.json +1 -1
  32. package/templates/cpp-app/windows/MyApp/MyApp.vcxproj +2 -0
@@ -0,0 +1,379 @@
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
+ #if _MSC_VER
9
+ #pragma warning(push)
10
+ #pragma warning(disable : 4996) // deprecated APIs
11
+ #endif
12
+ #include "Instance.h"
13
+
14
+ #ifndef RCT_FIT_RM_OLD_RUNTIME
15
+
16
+ #include "ErrorUtils.h"
17
+ #include "JSBigString.h"
18
+ #include "JSBundleType.h"
19
+ #include "JSExecutor.h"
20
+ #include "MessageQueueThread.h"
21
+ #include "MethodCall.h"
22
+ #include "NativeToJsBridge.h"
23
+ #include "RAMBundleRegistry.h"
24
+ #include "RecoverableError.h"
25
+ #include "TraceSection.h"
26
+
27
+ #include <cxxreact/JSIndexedRAMBundle.h>
28
+ #include <folly/json.h>
29
+ #include <react/debug/react_native_assert.h>
30
+
31
+ #include <glog/logging.h>
32
+
33
+ #include <condition_variable>
34
+ #include <exception>
35
+ #include <memory>
36
+ #include <mutex>
37
+ #include <string>
38
+
39
+ namespace facebook::react {
40
+
41
+ Instance::~Instance() {
42
+ if (nativeToJsBridge_) {
43
+ nativeToJsBridge_->destroy();
44
+ }
45
+ }
46
+
47
+ void Instance::unregisterFromInspector() {
48
+ if (inspectorTarget_ != nullptr) {
49
+ assert(runtimeInspectorTarget_);
50
+ inspectorTarget_->unregisterRuntime(*runtimeInspectorTarget_);
51
+ assert(parentInspectorTarget_);
52
+ parentInspectorTarget_->unregisterInstance(*inspectorTarget_);
53
+ parentInspectorTarget_ = nullptr;
54
+ inspectorTarget_ = nullptr;
55
+ }
56
+ }
57
+
58
+ void Instance::initializeBridge(
59
+ std::unique_ptr<InstanceCallback> callback,
60
+ std::shared_ptr<JSExecutorFactory> jsef,
61
+ std::shared_ptr<MessageQueueThread> jsQueue,
62
+ std::shared_ptr<ModuleRegistry> moduleRegistry,
63
+ jsinspector_modern::HostTarget* parentInspectorTarget) {
64
+ callback_ = std::move(callback);
65
+ moduleRegistry_ = std::move(moduleRegistry);
66
+ parentInspectorTarget_ = parentInspectorTarget;
67
+
68
+ jsQueue->runOnQueueSync([this, &jsef, jsQueue]() mutable {
69
+ nativeToJsBridge_ = std::make_shared<NativeToJsBridge>(
70
+ jsef.get(), moduleRegistry_, jsQueue, callback_);
71
+
72
+ // If a parent inspector HostTarget is provided, perform inspector
73
+ // initialization synchronously.
74
+ if (parentInspectorTarget_ != nullptr) {
75
+ auto inspectorExecutor = parentInspectorTarget_->executorFromThis();
76
+ std::mutex inspectorInitializedMutex;
77
+ std::condition_variable inspectorInitializedCv;
78
+ bool inspectorInitialized = false;
79
+
80
+ // Schedule work on the inspector thread. NOTE: We expect this callback
81
+ // to always execute, given the invariant that `initializeBridge` (this
82
+ // method) completes before `unregisterFromInspector` is called.
83
+ // - On iOS, instance creation and invalidation both run on the main
84
+ // queue (`RCTCxxBridge::start,invalidate` use `RCTAssertMainQueue`).
85
+ // - On Android, `ReactContext` must be initialized with a constructed
86
+ // `CatalystInstance` (in which `Instance::initializeBridge` has
87
+ // completed) before `destroy` can be called.
88
+ inspectorExecutor([this,
89
+ &inspectorInitialized,
90
+ &inspectorInitializedMutex,
91
+ &inspectorInitializedCv](
92
+ jsinspector_modern::HostTarget& hostTarget) {
93
+ // NOTE: By passing *this, we strongly assume the Instance will still
94
+ // be alive by the time this executes.
95
+ // - On iOS, instance creation is done synchronously
96
+ // (`RCTCxxBridge::_initializeBridgeLocked`).
97
+ // - On Android, we explicitly wait for instance creation before
98
+ // destruction (`ReactInstanceManager::mReactContextLock`).
99
+ inspectorTarget_ = &hostTarget.registerInstance(*this);
100
+ RuntimeExecutor runtimeExecutorIfJsi = getRuntimeExecutor();
101
+ runtimeInspectorTarget_ = &inspectorTarget_->registerRuntime(
102
+ nativeToJsBridge_->getInspectorTargetDelegate(),
103
+ runtimeExecutorIfJsi ? runtimeExecutorIfJsi : [](auto) {});
104
+
105
+ // Signal that initialization is complete
106
+ {
107
+ std::lock_guard lock(inspectorInitializedMutex);
108
+ inspectorInitialized = true;
109
+ }
110
+ inspectorInitializedCv.notify_one();
111
+ });
112
+
113
+ // Wait for the initialization work to complete
114
+ {
115
+ std::unique_lock lock(inspectorInitializedMutex);
116
+ inspectorInitializedCv.wait(
117
+ lock, [&inspectorInitialized] { return inspectorInitialized; });
118
+ }
119
+ }
120
+
121
+ // Initialize the JavaScript runtime after we've initialized the inspector
122
+ nativeToJsBridge_->initializeRuntime();
123
+
124
+ // After NativeToJsBridge is created, the jsi::Runtime should exist. Also,
125
+ // the JS message queue thread exists. So, it's safe to schedule all queued
126
+ // up JS calls.
127
+ jsCallInvoker_->setNativeToJsBridgeAndFlushCalls(nativeToJsBridge_);
128
+
129
+ std::scoped_lock lock(m_syncMutex);
130
+ m_syncReady = true;
131
+ m_syncCV.notify_all();
132
+ });
133
+
134
+ CHECK(nativeToJsBridge_);
135
+ }
136
+
137
+ void Instance::loadBundle(
138
+ std::unique_ptr<RAMBundleRegistry> bundleRegistry,
139
+ std::unique_ptr<const JSBigString> startupScript,
140
+ std::string sourceURL) {
141
+ callback_->incrementPendingJSCalls();
142
+ TraceSection s("Instance::loadBundle", "sourceURL", sourceURL);
143
+ nativeToJsBridge_->loadBundle(
144
+ std::move(bundleRegistry),
145
+ std::move(startupScript),
146
+ std::move(sourceURL));
147
+ }
148
+
149
+ void Instance::loadBundleSync(
150
+ std::unique_ptr<RAMBundleRegistry> bundleRegistry,
151
+ std::unique_ptr<const JSBigString> startupScript,
152
+ std::string sourceURL) {
153
+ std::unique_lock<std::mutex> lock(m_syncMutex);
154
+ m_syncCV.wait(lock, [this] { return m_syncReady; });
155
+
156
+ TraceSection s("Instance::loadBundleSync", "sourceURL", sourceURL);
157
+ nativeToJsBridge_->loadBundleSync(
158
+ std::move(bundleRegistry),
159
+ std::move(startupScript),
160
+ std::move(sourceURL));
161
+ }
162
+
163
+ void Instance::setSourceURL(std::string sourceURL) {
164
+ callback_->incrementPendingJSCalls();
165
+ TraceSection s("Instance::setSourceURL", "sourceURL", sourceURL);
166
+
167
+ nativeToJsBridge_->loadBundle(nullptr, nullptr, std::move(sourceURL));
168
+ }
169
+
170
+ void Instance::loadScriptFromString(
171
+ std::unique_ptr<const JSBigString> string,
172
+ std::string sourceURL,
173
+ bool loadSynchronously) {
174
+ TraceSection s("Instance::loadScriptFromString", "sourceURL", sourceURL);
175
+ if (loadSynchronously) {
176
+ loadBundleSync(nullptr, std::move(string), std::move(sourceURL));
177
+ } else {
178
+ loadBundle(nullptr, std::move(string), std::move(sourceURL));
179
+ }
180
+ }
181
+
182
+ void Instance::loadRAMBundleFromString(
183
+ std::unique_ptr<const JSBigString> script,
184
+ const std::string& sourceURL) {
185
+ auto bundle = std::make_unique<JSIndexedRAMBundle>(std::move(script));
186
+ auto startupScript = bundle->getStartupCode();
187
+ auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle));
188
+ loadRAMBundle(std::move(registry), std::move(startupScript), sourceURL, true);
189
+ }
190
+
191
+ void Instance::loadRAMBundleFromFile(
192
+ const std::string& sourcePath,
193
+ const std::string& sourceURL,
194
+ bool loadSynchronously) {
195
+ auto bundle = std::make_unique<JSIndexedRAMBundle>(sourcePath.c_str());
196
+ auto startupScript = bundle->getStartupCode();
197
+ auto registry = RAMBundleRegistry::multipleBundlesRegistry(
198
+ std::move(bundle), JSIndexedRAMBundle::buildFactory());
199
+ loadRAMBundle(
200
+ std::move(registry),
201
+ std::move(startupScript),
202
+ sourceURL,
203
+ loadSynchronously);
204
+ }
205
+
206
+ void Instance::loadRAMBundle(
207
+ std::unique_ptr<RAMBundleRegistry> bundleRegistry,
208
+ std::unique_ptr<const JSBigString> startupScript,
209
+ std::string startupScriptSourceURL,
210
+ bool loadSynchronously) {
211
+ if (loadSynchronously) {
212
+ loadBundleSync(
213
+ std::move(bundleRegistry),
214
+ std::move(startupScript),
215
+ std::move(startupScriptSourceURL));
216
+ } else {
217
+ loadBundle(
218
+ std::move(bundleRegistry),
219
+ std::move(startupScript),
220
+ std::move(startupScriptSourceURL));
221
+ }
222
+ }
223
+
224
+ void Instance::setGlobalVariable(
225
+ std::string propName,
226
+ std::unique_ptr<const JSBigString> jsonValue) {
227
+ nativeToJsBridge_->setGlobalVariable(
228
+ std::move(propName), std::move(jsonValue));
229
+ }
230
+
231
+ void* Instance::getJavaScriptContext() {
232
+ return nativeToJsBridge_ ? nativeToJsBridge_->getJavaScriptContext()
233
+ : nullptr;
234
+ }
235
+
236
+ bool Instance::isInspectable() {
237
+ return nativeToJsBridge_ ? nativeToJsBridge_->isInspectable() : false;
238
+ }
239
+
240
+ bool Instance::isBatchActive() {
241
+ return nativeToJsBridge_ ? nativeToJsBridge_->isBatchActive() : false;
242
+ }
243
+
244
+ void Instance::callJSFunction(
245
+ std::string&& module,
246
+ std::string&& method,
247
+ folly::dynamic&& params) {
248
+ callback_->incrementPendingJSCalls();
249
+ nativeToJsBridge_->callFunction(
250
+ std::move(module), std::move(method), std::move(params));
251
+ }
252
+
253
+ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic&& params) {
254
+ TraceSection s("Instance::callJSCallback");
255
+ callback_->incrementPendingJSCalls();
256
+ nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params));
257
+ }
258
+
259
+ void Instance::registerBundle(
260
+ uint32_t bundleId,
261
+ const std::string& bundlePath) {
262
+ nativeToJsBridge_->registerBundle(bundleId, bundlePath);
263
+ }
264
+
265
+ const ModuleRegistry& Instance::getModuleRegistry() const {
266
+ return *moduleRegistry_;
267
+ }
268
+
269
+ ModuleRegistry& Instance::getModuleRegistry() {
270
+ return *moduleRegistry_;
271
+ }
272
+
273
+ void Instance::handleMemoryPressure(int pressureLevel) {
274
+ if (nativeToJsBridge_) {
275
+ // This class resets `nativeToJsBridge_` only in the destructor,
276
+ // hence a race is not possible there.
277
+ nativeToJsBridge_->handleMemoryPressure(pressureLevel);
278
+ }
279
+ }
280
+
281
+ std::shared_ptr<CallInvoker> Instance::getJSCallInvoker() {
282
+ return std::static_pointer_cast<CallInvoker>(jsCallInvoker_);
283
+ }
284
+
285
+ RuntimeExecutor Instance::getRuntimeExecutor() {
286
+ // HACK: RuntimeExecutor is not compatible with non-JSIExecutor, we return
287
+ // a null callback, which the caller should handle.
288
+ if (getJavaScriptContext() == nullptr) {
289
+ return nullptr;
290
+ }
291
+
292
+ std::weak_ptr<NativeToJsBridge> weakNativeToJsBridge = nativeToJsBridge_;
293
+ return [weakNativeToJsBridge](
294
+ std::function<void(jsi::Runtime & runtime)>&& callback) {
295
+ if (auto strongNativeToJsBridge = weakNativeToJsBridge.lock()) {
296
+ strongNativeToJsBridge->runOnExecutorQueue(
297
+ [callback = std::move(callback)](JSExecutor* executor) {
298
+ // Assumes the underlying executor is a JSIExecutor
299
+ auto* runtime =
300
+ static_cast<jsi::Runtime*>(executor->getJavaScriptContext());
301
+ try {
302
+ react_native_assert(runtime != nullptr);
303
+ callback(*runtime);
304
+ executor->flush();
305
+ } catch (jsi::JSError& originalError) {
306
+ handleJSError(*runtime, originalError, true);
307
+ }
308
+ });
309
+ }
310
+ };
311
+ }
312
+
313
+ std::shared_ptr<NativeMethodCallInvoker>
314
+ Instance::getDecoratedNativeMethodCallInvoker(
315
+ std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker) {
316
+ return nativeToJsBridge_->getDecoratedNativeMethodCallInvoker(
317
+ nativeMethodCallInvoker);
318
+ }
319
+
320
+ void Instance::JSCallInvoker::setNativeToJsBridgeAndFlushCalls(
321
+ std::weak_ptr<NativeToJsBridge> nativeToJsBridge) {
322
+ std::scoped_lock guard(m_mutex);
323
+
324
+ m_shouldBuffer = false;
325
+ m_nativeToJsBridge = nativeToJsBridge;
326
+ while (!m_workBuffer.empty()) {
327
+ scheduleAsync(std::move(m_workBuffer.front()));
328
+ m_workBuffer.pop_front();
329
+ }
330
+ }
331
+
332
+ void Instance::JSCallInvoker::invokeSync(CallFunc&& /*work*/) {
333
+ // TODO: Replace JS Callinvoker with RuntimeExecutor.
334
+ throw std::runtime_error(
335
+ "Synchronous native -> JS calls are currently not supported.");
336
+ }
337
+
338
+ void Instance::JSCallInvoker::invokeAsync(CallFunc&& work) noexcept {
339
+ std::scoped_lock guard(m_mutex);
340
+
341
+ /**
342
+ * Why is is necessary to queue up async work?
343
+ *
344
+ * 1. TurboModuleManager must be created synchronously after the Instance,
345
+ * before we load the source code. This is when the NativeModule system
346
+ * is initialized. RCTDevLoadingView shows bundle download progress.
347
+ * 2. TurboModuleManager requires a JS CallInvoker.
348
+ * 3. The JS CallInvoker requires the NativeToJsBridge, which is created on
349
+ * the JS thread in Instance::initializeBridge.
350
+ *
351
+ * Therefore, although we don't call invokeAsync before the JS bundle is
352
+ * executed, this buffering is implemented anyways to ensure that work
353
+ * isn't discarded.
354
+ */
355
+ if (m_shouldBuffer) {
356
+ m_workBuffer.push_back(std::move(work));
357
+ return;
358
+ }
359
+
360
+ scheduleAsync(std::move(work));
361
+ }
362
+
363
+ void Instance::JSCallInvoker::scheduleAsync(CallFunc&& work) noexcept {
364
+ if (auto strongNativeToJsBridge = m_nativeToJsBridge.lock()) {
365
+ strongNativeToJsBridge->runOnExecutorQueue(
366
+ [work = std::move(work)](JSExecutor* executor) {
367
+ auto* runtime = (jsi::Runtime*)executor->getJavaScriptContext();
368
+ work(*runtime);
369
+ executor->flush();
370
+ });
371
+ }
372
+ }
373
+
374
+ } // namespace facebook::react
375
+
376
+ #endif // RCT_FIT_RM_OLD_RUNTIME
377
+ #if _MSC_VER
378
+ #pragma warning(pop)
379
+ #endif
@@ -0,0 +1,47 @@
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
+ #if _MSC_VER
9
+ #pragma warning(push)
10
+ #pragma warning(disable : 4996) // deprecated APIs
11
+ #endif
12
+ #include "JSExecutor.h"
13
+
14
+ #include "RAMBundleRegistry.h"
15
+
16
+ #include <jsinspector-modern/ReactCdp.h>
17
+
18
+ #include <array>
19
+
20
+ namespace facebook::react {
21
+
22
+ std::string JSExecutor::getSyntheticBundlePath(
23
+ uint32_t bundleId,
24
+ const std::string& bundlePath) {
25
+ #ifndef RCT_FIT_RM_OLD_RUNTIME
26
+ if (bundleId == RAMBundleRegistry::MAIN_BUNDLE_ID) {
27
+ return bundlePath;
28
+ }
29
+ #endif // RCT_FIT_RM_OLD_RUNTIME
30
+
31
+ std::array<char, 32> buffer{};
32
+ std::snprintf(buffer.data(), buffer.size(), "seg-%u.js", bundleId);
33
+ return buffer.data();
34
+ }
35
+
36
+ jsinspector_modern::RuntimeTargetDelegate&
37
+ JSExecutor::getRuntimeTargetDelegate() {
38
+ if (!runtimeTargetDelegate_) {
39
+ runtimeTargetDelegate_.emplace(getDescription());
40
+ }
41
+ return *runtimeTargetDelegate_;
42
+ }
43
+
44
+ } // namespace facebook::react
45
+ #if _MSC_VER
46
+ #pragma warning(pop)
47
+ #endif
@@ -0,0 +1,143 @@
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
+ #if _MSC_VER
9
+ #pragma warning(push)
10
+ #pragma warning(disable : 4996) // deprecated APIs
11
+ #endif
12
+ #include "JSIndexedRAMBundle.h"
13
+
14
+ #ifndef RCT_FIT_RM_OLD_RUNTIME
15
+
16
+ #include <glog/logging.h>
17
+ #include <fstream>
18
+ #include <memory>
19
+ #include <sstream>
20
+
21
+ #include <folly/lang/Bits.h>
22
+ #include <glog/logging.h>
23
+
24
+ namespace facebook::react {
25
+
26
+ std::function<std::unique_ptr<JSModulesUnbundle>(std::string)>
27
+ JSIndexedRAMBundle::buildFactory() {
28
+ return [](const std::string& bundlePath) {
29
+ return std::make_unique<JSIndexedRAMBundle>(bundlePath.c_str());
30
+ };
31
+ }
32
+
33
+ JSIndexedRAMBundle::JSIndexedRAMBundle(const char* sourcePath) {
34
+ m_bundle = std::make_unique<std::ifstream>(sourcePath, std::ifstream::binary);
35
+ if (!m_bundle) {
36
+ throw std::ios_base::failure(
37
+ std::string("Bundle ") + sourcePath +
38
+ "cannot be opened: " + std::to_string(m_bundle->rdstate()));
39
+ }
40
+ init();
41
+ }
42
+
43
+ JSIndexedRAMBundle::JSIndexedRAMBundle(
44
+ std::unique_ptr<const JSBigString> script) {
45
+ // tmpStream is needed because m_bundle is std::istream type
46
+ // which has no member 'write'
47
+ std::unique_ptr<std::stringstream> tmpStream =
48
+ std::make_unique<std::stringstream>();
49
+ tmpStream->write(script->c_str(), script->size());
50
+ m_bundle = std::move(tmpStream);
51
+ if (!m_bundle) {
52
+ throw std::ios_base::failure(
53
+ "Bundle from string cannot be opened: " +
54
+ std::to_string(m_bundle->rdstate()));
55
+ }
56
+ init();
57
+ }
58
+
59
+ void JSIndexedRAMBundle::init() {
60
+ // read in magic header, number of entries, and length of the startup section
61
+ uint32_t header[3];
62
+ static_assert(
63
+ sizeof(header) == 12,
64
+ "header size must exactly match the input file format");
65
+
66
+ readBundle(reinterpret_cast<char*>(header), sizeof(header));
67
+ const size_t numTableEntries = folly::Endian::little(header[1]);
68
+ const size_t startupCodeSize = folly::Endian::little(header[2]);
69
+
70
+ // allocate memory for meta data and lookup table.
71
+ m_table = ModuleTable(numTableEntries);
72
+ m_baseOffset = sizeof(header) + m_table.byteLength();
73
+
74
+ // read the lookup table from the file
75
+ readBundle(reinterpret_cast<char*>(m_table.data.get()), m_table.byteLength());
76
+
77
+ // read the startup code
78
+ m_startupCode = std::make_unique<JSBigBufferString>(startupCodeSize - 1);
79
+
80
+ readBundle(m_startupCode->data(), startupCodeSize - 1);
81
+ }
82
+
83
+ JSIndexedRAMBundle::Module JSIndexedRAMBundle::getModule(
84
+ uint32_t moduleId) const {
85
+ Module ret;
86
+ ret.name = std::to_string(moduleId) + ".js";
87
+ ret.code = getModuleCode(moduleId);
88
+ return ret;
89
+ }
90
+
91
+ std::unique_ptr<const JSBigString> JSIndexedRAMBundle::getStartupCode() {
92
+ CHECK(m_startupCode)
93
+ << "startup code for a RAM Bundle can only be retrieved once";
94
+ return std::move(m_startupCode);
95
+ }
96
+
97
+ std::string JSIndexedRAMBundle::getModuleCode(const uint32_t id) const {
98
+ const auto moduleData = id < m_table.numEntries ? &m_table.data[id] : nullptr;
99
+
100
+ // entries without associated code have offset = 0 and length = 0
101
+ const uint32_t length =
102
+ moduleData != nullptr ? folly::Endian::little(moduleData->length) : 0;
103
+ if (length == 0) {
104
+ throw std::ios_base::failure(
105
+ "Error loading module" + std::to_string(id) + "from RAM Bundle");
106
+ }
107
+
108
+ std::string ret(length - 1, '\0');
109
+ readBundle(
110
+ &ret.front(),
111
+ length - 1,
112
+ m_baseOffset + folly::Endian::little(moduleData->offset));
113
+ return ret;
114
+ }
115
+
116
+ void JSIndexedRAMBundle::readBundle(char* buffer, const std::streamsize bytes)
117
+ const {
118
+ if (!m_bundle->read(buffer, bytes)) {
119
+ if ((m_bundle->rdstate() & std::ios::eofbit) != 0) {
120
+ throw std::ios_base::failure("Unexpected end of RAM Bundle file");
121
+ }
122
+ throw std::ios_base::failure(
123
+ "Error reading RAM Bundle: " + std::to_string(m_bundle->rdstate()));
124
+ }
125
+ }
126
+
127
+ void JSIndexedRAMBundle::readBundle(
128
+ char* buffer,
129
+ const std::streamsize bytes,
130
+ const std::ifstream::pos_type position) const {
131
+ if (!m_bundle->seekg(position)) {
132
+ throw std::ios_base::failure(
133
+ "Error reading RAM Bundle: " + std::to_string(m_bundle->rdstate()));
134
+ }
135
+ readBundle(buffer, bytes);
136
+ }
137
+
138
+ } // namespace facebook::react
139
+
140
+ #endif // RCT_FIT_RM_OLD_RUNTIME
141
+ #if _MSC_VER
142
+ #pragma warning(pop)
143
+ #endif
@@ -0,0 +1,98 @@
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
+ #if _MSC_VER
9
+ #pragma warning(push)
10
+ #pragma warning(disable : 4996) // deprecated APIs
11
+ #endif
12
+ #include "MethodCall.h"
13
+
14
+ #ifndef RCT_FIT_RM_OLD_RUNTIME
15
+
16
+ #include <folly/json.h>
17
+ #include <stdexcept>
18
+
19
+ namespace facebook::react {
20
+
21
+ #define REQUEST_MODULE_IDS 0
22
+ #define REQUEST_METHOD_IDS 1
23
+ #define REQUEST_PARAMS 2
24
+ #define REQUEST_CALLID 3
25
+
26
+ static const char* errorPrefix = "Malformed calls from JS: ";
27
+
28
+ std::vector<MethodCall> parseMethodCalls(folly::dynamic&& jsonData) {
29
+ if (jsonData.isNull()) {
30
+ return {};
31
+ }
32
+
33
+ if (!jsonData.isArray()) {
34
+ throw std::invalid_argument(
35
+ std::string(errorPrefix) + " input isn't array but " +
36
+ jsonData.typeName());
37
+ }
38
+
39
+ if (jsonData.size() < REQUEST_PARAMS + 1) {
40
+ throw std::invalid_argument(
41
+ std::string(errorPrefix) +
42
+ "size == " + std::to_string(jsonData.size()));
43
+ }
44
+
45
+ auto& moduleIds = jsonData[REQUEST_MODULE_IDS];
46
+ auto& methodIds = jsonData[REQUEST_METHOD_IDS];
47
+ auto& params = jsonData[REQUEST_PARAMS];
48
+ int callId = -1;
49
+
50
+ if (!moduleIds.isArray() || !methodIds.isArray() || !params.isArray()) {
51
+ throw std::invalid_argument(
52
+ std::string(errorPrefix) + "not all fields are arrays.\n\n" +
53
+ folly::toJson(jsonData));
54
+ }
55
+
56
+ if (moduleIds.size() != methodIds.size() ||
57
+ moduleIds.size() != params.size()) {
58
+ throw std::invalid_argument(
59
+ std::string(errorPrefix) + "field sizes are different.\n\n" +
60
+ folly::toJson(jsonData));
61
+ }
62
+
63
+ if (jsonData.size() > REQUEST_CALLID) {
64
+ if (!jsonData[REQUEST_CALLID].isNumber()) {
65
+ throw std::invalid_argument(
66
+ std::string(errorPrefix) + "invalid callId" +
67
+ jsonData[REQUEST_CALLID].typeName());
68
+ }
69
+ callId = (int)jsonData[REQUEST_CALLID].asInt();
70
+ }
71
+
72
+ std::vector<MethodCall> methodCalls;
73
+ for (size_t i = 0; i < moduleIds.size(); i++) {
74
+ if (!params[i].isArray()) {
75
+ throw std::invalid_argument(
76
+ std::string(errorPrefix) + "method arguments isn't array but " +
77
+ params[i].typeName());
78
+ }
79
+
80
+ methodCalls.emplace_back(
81
+ static_cast<int>(moduleIds[i].asInt()),
82
+ static_cast<int>(methodIds[i].asInt()),
83
+ std::move(params[i]),
84
+ callId);
85
+
86
+ // only increment callid if contains valid callid as callid is optional
87
+ callId += (callId != -1) ? 1 : 0;
88
+ }
89
+
90
+ return methodCalls;
91
+ }
92
+
93
+ } // namespace facebook::react
94
+
95
+ #endif // RCT_FIT_RM_OLD_RUNTIME
96
+ #if _MSC_VER
97
+ #pragma warning(pop)
98
+ #endif