react-native-windows 0.71.37 → 0.71.38

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 (47) hide show
  1. package/Directory.Build.props +5 -0
  2. package/Microsoft.ReactNative/IReactDispatcher.cpp +4 -0
  3. package/Microsoft.ReactNative/IReactDispatcher.h +1 -0
  4. package/Microsoft.ReactNative/Microsoft.ReactNative.vcxproj +2 -4
  5. package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.cpp +30 -11
  6. package/Microsoft.ReactNative/ReactHost/ReactInstanceWin.h +0 -2
  7. package/Microsoft.ReactNative/Views/DevMenu.cpp +3 -3
  8. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiLoader.cpp +16 -0
  9. package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems +43 -12
  10. package/Microsoft.ReactNative.Cxx/Microsoft.ReactNative.Cxx.vcxitems.filters +17 -6
  11. package/PropertySheets/External/Microsoft.ReactNative.Uwp.CSharpApp.targets +1 -1
  12. package/PropertySheets/External/Microsoft.ReactNative.Uwp.CppApp.targets +1 -1
  13. package/PropertySheets/External/Microsoft.ReactNative.WinAppSDK.CSharpApp.targets +1 -1
  14. package/PropertySheets/Generated/PackageVersion.g.props +3 -3
  15. package/PropertySheets/JSEngine.props +4 -4
  16. package/PropertySheets/React.Cpp.props +1 -0
  17. package/PropertySheets/Warnings.props +6 -0
  18. package/ReactCommon/ReactCommon.vcxproj +53 -1
  19. package/ReactCommon/cgmanifest.json +15 -0
  20. package/Scripts/Tfs/Layout-MSRN-Headers.ps1 +36 -0
  21. package/Shared/DevSupportManager.cpp +2 -9
  22. package/Shared/DevSupportManager.h +2 -6
  23. package/Shared/HermesRuntimeHolder.cpp +344 -84
  24. package/Shared/HermesRuntimeHolder.h +32 -21
  25. package/Shared/HermesSamplingProfiler.cpp +66 -14
  26. package/Shared/HermesSamplingProfiler.h +5 -3
  27. package/Shared/InspectorPackagerConnection.cpp +62 -108
  28. package/Shared/InspectorPackagerConnection.h +9 -21
  29. package/Shared/JSI/RuntimeHolder.h +2 -2
  30. package/Shared/JSI/ScriptStore.h +18 -20
  31. package/Shared/JSI/V8RuntimeHolder.cpp +260 -0
  32. package/Shared/JSI/V8RuntimeHolder.h +37 -0
  33. package/Shared/OInstance.cpp +16 -32
  34. package/Shared/SafeLoadLibrary.cpp +77 -0
  35. package/Shared/SafeLoadLibrary.h +19 -0
  36. package/Shared/Shared.vcxitems +19 -8
  37. package/Shared/Shared.vcxitems.filters +23 -30
  38. package/package.json +2 -2
  39. package/template/cs-app-WinAppSDK/proj/ExperimentalFeatures.props +1 -1
  40. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.cpp +0 -2103
  41. package/Microsoft.ReactNative.Cxx/JSI/NodeApiJsiRuntime.h +0 -73
  42. package/Shared/HermesShim.cpp +0 -118
  43. package/Shared/HermesShim.h +0 -21
  44. package/Shared/JSI/NapiJsiV8RuntimeHolder.cpp +0 -209
  45. package/Shared/JSI/NapiJsiV8RuntimeHolder.h +0 -44
  46. package/Shared/V8JSIRuntimeHolder.cpp +0 -70
  47. package/Shared/V8JSIRuntimeHolder.h +0 -53
@@ -0,0 +1,260 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #include "V8RuntimeHolder.h"
5
+ #include <ApiLoaders/V8Api.h>
6
+ #include <NodeApiJsiRuntime.h>
7
+ #include <crash/verifyElseCrash.h>
8
+ #include "SafeLoadLibrary.h"
9
+
10
+ using namespace Microsoft::NodeApiJsi;
11
+
12
+ #define CRASH_ON_ERROR(result) VerifyElseCrash(result == napi_ok);
13
+
14
+ namespace Microsoft::ReactNative {
15
+ namespace {
16
+
17
+ class V8FuncResolver : public IFuncResolver {
18
+ public:
19
+ V8FuncResolver() : libHandle_(LoadLibraryAsPeerFirst(L"v8jsi.dll")) {}
20
+
21
+ FuncPtr getFuncPtr(const char *funcName) override {
22
+ return reinterpret_cast<FuncPtr>(GetProcAddress(libHandle_, funcName));
23
+ }
24
+
25
+ private:
26
+ HMODULE libHandle_;
27
+ };
28
+
29
+ V8Api &initV8Api() noexcept {
30
+ static V8FuncResolver funcResolver;
31
+ static V8Api s_v8Api(&funcResolver);
32
+ V8Api::setCurrent(&s_v8Api);
33
+ return s_v8Api;
34
+ }
35
+
36
+ V8Api &getV8Api() noexcept {
37
+ static V8Api &s_v8Api = initV8Api();
38
+ return s_v8Api;
39
+ }
40
+
41
+ class V8Task {
42
+ public:
43
+ V8Task(void *taskData, jsr_task_run_cb taskRunCallback, jsr_data_delete_cb taskDataDeleteCallback, void *deleterData)
44
+ : taskData_(taskData),
45
+ taskRunCallback_(taskRunCallback),
46
+ taskDataDeleteCallback_(taskDataDeleteCallback),
47
+ deleterData_(deleterData) {}
48
+
49
+ V8Task(const V8Task &other) = delete;
50
+ V8Task &operator=(const V8Task &other) = delete;
51
+
52
+ ~V8Task() {
53
+ if (taskDataDeleteCallback_ != nullptr) {
54
+ taskDataDeleteCallback_(taskData_, deleterData_);
55
+ }
56
+ }
57
+
58
+ void Run() const {
59
+ if (taskRunCallback_ != nullptr) {
60
+ taskRunCallback_(taskData_);
61
+ }
62
+ }
63
+
64
+ private:
65
+ void *taskData_;
66
+ jsr_task_run_cb taskRunCallback_;
67
+ jsr_data_delete_cb taskDataDeleteCallback_;
68
+ void *deleterData_;
69
+ };
70
+
71
+ class V8TaskRunner {
72
+ public:
73
+ static void Create(jsr_config config, std::shared_ptr<facebook::react::MessageQueueThread> queue) {
74
+ CRASH_ON_ERROR(
75
+ getV8Api().jsr_config_set_task_runner(config, new V8TaskRunner(std::move(queue)), &PostTask, &Delete, nullptr));
76
+ }
77
+
78
+ private:
79
+ V8TaskRunner(std::shared_ptr<facebook::react::MessageQueueThread> queue) : queue_(std::move(queue)) {}
80
+
81
+ static void NAPI_CDECL PostTask(
82
+ void *taskRunnerData,
83
+ void *taskData,
84
+ jsr_task_run_cb taskRunCallback,
85
+ jsr_data_delete_cb taskDataDeleteCallback,
86
+ void *deleterData) {
87
+ auto task = std::make_shared<V8Task>(taskData, taskRunCallback, taskDataDeleteCallback, deleterData);
88
+ reinterpret_cast<V8TaskRunner *>(taskRunnerData)->queue_->runOnQueue([task = std::move(task)] { task->Run(); });
89
+ }
90
+
91
+ static void NAPI_CDECL Delete(void *taskRunner, void * /*deleterData*/) {
92
+ delete reinterpret_cast<V8TaskRunner *>(taskRunner);
93
+ }
94
+
95
+ private:
96
+ std::shared_ptr<facebook::react::MessageQueueThread> queue_;
97
+ };
98
+
99
+ struct V8JsiBuffer : facebook::jsi::Buffer {
100
+ static std::shared_ptr<const facebook::jsi::Buffer>
101
+ Create(const uint8_t *buffer, size_t bufferSize, jsr_data_delete_cb bufferDeleteCallback, void *deleterData) {
102
+ return std::shared_ptr<const facebook::jsi::Buffer>(
103
+ new V8JsiBuffer(buffer, bufferSize, bufferDeleteCallback, deleterData));
104
+ }
105
+
106
+ V8JsiBuffer(
107
+ const uint8_t *buffer,
108
+ size_t bufferSize,
109
+ jsr_data_delete_cb bufferDeleteCallback,
110
+ void *deleterData) noexcept
111
+ : buffer_(buffer),
112
+ bufferSize_(bufferSize),
113
+ bufferDeleteCallback_(bufferDeleteCallback),
114
+ deleterData_(deleterData) {}
115
+
116
+ ~V8JsiBuffer() override {
117
+ if (bufferDeleteCallback_) {
118
+ bufferDeleteCallback_(const_cast<uint8_t *>(buffer_), deleterData_);
119
+ }
120
+ }
121
+
122
+ const uint8_t *data() const override {
123
+ return buffer_;
124
+ }
125
+
126
+ size_t size() const override {
127
+ return bufferSize_;
128
+ }
129
+
130
+ private:
131
+ const uint8_t *buffer_;
132
+ size_t bufferSize_;
133
+ jsr_data_delete_cb bufferDeleteCallback_;
134
+ void *deleterData_;
135
+ };
136
+
137
+ class V8ScriptCache {
138
+ public:
139
+ static void Create(jsr_config config, std::shared_ptr<facebook::jsi::PreparedScriptStore> scriptStore) {
140
+ CRASH_ON_ERROR(getV8Api().jsr_config_set_script_cache(
141
+ config, new V8ScriptCache(std::move(scriptStore)), &LoadScript, &StoreScript, &Delete, nullptr));
142
+ }
143
+
144
+ private:
145
+ V8ScriptCache(std::shared_ptr<facebook::jsi::PreparedScriptStore> scriptStore)
146
+ : scriptStore_(std::move(scriptStore)) {}
147
+
148
+ static void NAPI_CDECL LoadScript(
149
+ void *scriptCache,
150
+ const char *sourceUrl,
151
+ uint64_t sourceHash,
152
+ const char *runtimeName,
153
+ uint64_t runtimeVersion,
154
+ const char *cacheTag,
155
+ const uint8_t **buffer,
156
+ size_t *bufferSize,
157
+ jsr_data_delete_cb *bufferDeleteCallback,
158
+ void **deleterData) {
159
+ auto &scriptStore = reinterpret_cast<V8ScriptCache *>(scriptCache)->scriptStore_;
160
+ std::shared_ptr<const facebook::jsi::Buffer> preparedScript = scriptStore->tryGetPreparedScript(
161
+ facebook::jsi::ScriptSignature{sourceUrl, sourceHash},
162
+ facebook::jsi::JSRuntimeSignature{runtimeName, runtimeVersion},
163
+ cacheTag);
164
+ if (preparedScript) {
165
+ *buffer = preparedScript->data();
166
+ *bufferSize = preparedScript->size();
167
+ *bufferDeleteCallback = [](void * /*data*/, void *deleterData) noexcept {
168
+ delete reinterpret_cast<std::shared_ptr<const facebook::jsi::Buffer> *>(deleterData);
169
+ };
170
+ *deleterData = new std::shared_ptr<const facebook::jsi::Buffer>(std::move(preparedScript));
171
+ } else {
172
+ *buffer = nullptr;
173
+ *bufferSize = 0;
174
+ *bufferDeleteCallback = nullptr;
175
+ *deleterData = nullptr;
176
+ }
177
+ }
178
+
179
+ static void NAPI_CDECL StoreScript(
180
+ void *scriptCache,
181
+ const char *sourceUrl,
182
+ uint64_t sourceHash,
183
+ const char *runtimeName,
184
+ uint64_t runtimeVersion,
185
+ const char *cacheTag,
186
+ const uint8_t *buffer,
187
+ size_t bufferSize,
188
+ jsr_data_delete_cb bufferDeleteCallback,
189
+ void *deleterData) {
190
+ auto &scriptStore = reinterpret_cast<V8ScriptCache *>(scriptCache)->scriptStore_;
191
+ scriptStore->persistPreparedScript(
192
+ V8JsiBuffer::Create(buffer, bufferSize, bufferDeleteCallback, deleterData),
193
+ facebook::jsi::ScriptSignature{sourceUrl, sourceHash},
194
+ facebook::jsi::JSRuntimeSignature{runtimeName, runtimeVersion},
195
+ cacheTag);
196
+ }
197
+
198
+ static void NAPI_CDECL Delete(void *scriptCache, void * /*deleterData*/) {
199
+ delete reinterpret_cast<V8ScriptCache *>(scriptCache);
200
+ }
201
+
202
+ private:
203
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> scriptStore_;
204
+ };
205
+
206
+ } // namespace
207
+
208
+ V8RuntimeHolder::V8RuntimeHolder(
209
+ std::shared_ptr<facebook::react::DevSettings> devSettings,
210
+ std::shared_ptr<facebook::react::MessageQueueThread> jsQueue,
211
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore,
212
+ bool enableMultiThreadingSupport) noexcept
213
+ : m_weakDevSettings(devSettings),
214
+ m_jsQueue(std::move(jsQueue)),
215
+ m_preparedScriptStore(std::move(preparedScriptStore)),
216
+ m_enableMultiThreadingSupport(enableMultiThreadingSupport) {}
217
+
218
+ void V8RuntimeHolder::initRuntime() noexcept {
219
+ std::shared_ptr<facebook::react::DevSettings> devSettings = m_weakDevSettings.lock();
220
+ VerifyElseCrash(devSettings);
221
+
222
+ V8Api &api = getV8Api();
223
+ V8Api::setCurrent(&api);
224
+ jsr_config config{};
225
+ CRASH_ON_ERROR(api.jsr_create_config(&config));
226
+ CRASH_ON_ERROR(api.jsr_config_enable_inspector(config, devSettings->useDirectDebugger));
227
+ CRASH_ON_ERROR(api.jsr_config_set_inspector_runtime_name(config, devSettings->debuggerRuntimeName.c_str()));
228
+ CRASH_ON_ERROR(api.jsr_config_set_inspector_port(config, devSettings->debuggerPort));
229
+ CRASH_ON_ERROR(api.jsr_config_set_inspector_break_on_start(config, devSettings->debuggerBreakOnNextLine));
230
+ CRASH_ON_ERROR(api.v8_config_enable_multithreading(config, m_enableMultiThreadingSupport));
231
+
232
+ if (m_jsQueue) {
233
+ V8TaskRunner::Create(config, m_jsQueue);
234
+ }
235
+ if (m_preparedScriptStore) {
236
+ V8ScriptCache::Create(config, m_preparedScriptStore);
237
+ }
238
+ jsr_runtime runtime{};
239
+ CRASH_ON_ERROR(api.jsr_create_runtime(config, &runtime));
240
+ CRASH_ON_ERROR(api.jsr_delete_config(config));
241
+
242
+ napi_env env{};
243
+ CRASH_ON_ERROR(api.jsr_runtime_get_node_api_env(runtime, &env));
244
+
245
+ m_jsiRuntime =
246
+ makeNodeApiJsiRuntime(env, &api, [runtime]() { CRASH_ON_ERROR(V8Api::current()->jsr_delete_runtime(runtime)); });
247
+ m_ownThreadId = std::this_thread::get_id();
248
+ }
249
+
250
+ facebook::react::JSIEngineOverride V8RuntimeHolder::getRuntimeType() noexcept {
251
+ return facebook::react::JSIEngineOverride::V8NodeApi;
252
+ }
253
+
254
+ std::shared_ptr<facebook::jsi::Runtime> V8RuntimeHolder::getRuntime() noexcept {
255
+ std::call_once(m_onceFlag, [this]() { initRuntime(); });
256
+ VerifyElseCrash(m_jsiRuntime);
257
+ return m_jsiRuntime;
258
+ }
259
+
260
+ } // namespace Microsoft::ReactNative
@@ -0,0 +1,37 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #pragma once
5
+
6
+ #include "RuntimeHolder.h"
7
+ #include "ScriptStore.h"
8
+
9
+ #include <cxxreact/MessageQueueThread.h>
10
+
11
+ namespace Microsoft::ReactNative {
12
+
13
+ class V8RuntimeHolder : public Microsoft::JSI::RuntimeHolderLazyInit {
14
+ public: // RuntimeHolderLazyInit implementation.
15
+ std::shared_ptr<facebook::jsi::Runtime> getRuntime() noexcept override;
16
+ facebook::react::JSIEngineOverride getRuntimeType() noexcept override;
17
+
18
+ V8RuntimeHolder(
19
+ std::shared_ptr<facebook::react::DevSettings> devSettings,
20
+ std::shared_ptr<facebook::react::MessageQueueThread> jsQueue,
21
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore,
22
+ bool enableMultiThreadingSupport) noexcept;
23
+
24
+ private:
25
+ void initRuntime() noexcept;
26
+
27
+ private:
28
+ std::shared_ptr<facebook::jsi::Runtime> m_jsiRuntime;
29
+ std::once_flag m_onceFlag{};
30
+ std::thread::id m_ownThreadId{};
31
+ std::weak_ptr<facebook::react::DevSettings> m_weakDevSettings;
32
+ std::shared_ptr<facebook::react::MessageQueueThread> m_jsQueue;
33
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> m_preparedScriptStore;
34
+ bool m_enableMultiThreadingSupport;
35
+ };
36
+
37
+ } // namespace Microsoft::ReactNative
@@ -51,13 +51,11 @@
51
51
  #include "HermesRuntimeHolder.h"
52
52
 
53
53
  #if defined(USE_V8)
54
- #include <JSI/NapiJsiV8RuntimeHolder.h>
55
-
56
- #include "BaseScriptStoreImpl.h"
57
- #include "V8JSIRuntimeHolder.h"
54
+ #include <JSI/V8RuntimeHolder.h>
58
55
  #endif
59
56
  #include <ReactCommon/CallInvoker.h>
60
57
  #include <ReactCommon/TurboModuleBinding.h>
58
+ #include "BaseScriptStoreImpl.h"
61
59
  #include "ChakraRuntimeHolder.h"
62
60
 
63
61
  #include <tracing/tracing.h>
@@ -308,46 +306,32 @@ InstanceImpl::InstanceImpl(
308
306
  } else {
309
307
  assert(m_devSettings->jsiEngineOverride != JSIEngineOverride::Default);
310
308
  switch (m_devSettings->jsiEngineOverride) {
311
- case JSIEngineOverride::Hermes:
312
- m_devSettings->jsiRuntimeHolder = std::make_shared<HermesRuntimeHolder>(m_devSettings, m_jsThread);
313
- break;
314
- case JSIEngineOverride::V8: {
315
- #if defined(USE_V8)
316
- std::unique_ptr<facebook::jsi::ScriptStore> scriptStore = nullptr;
317
- std::unique_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore = nullptr;
309
+ case JSIEngineOverride::Hermes: {
310
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore;
318
311
 
319
- char tempPath[MAX_PATH];
320
- if (GetTempPathA(MAX_PATH, tempPath)) {
321
- preparedScriptStore = std::make_unique<facebook::react::BasePreparedScriptStoreImpl>(tempPath);
312
+ wchar_t tempPath[MAX_PATH];
313
+ if (GetTempPathW(MAX_PATH, tempPath)) {
314
+ preparedScriptStore =
315
+ std::make_shared<facebook::react::BasePreparedScriptStoreImpl>(winrt::to_string(tempPath));
322
316
  }
323
317
 
324
- m_devSettings->jsiRuntimeHolder = std::make_shared<facebook::react::V8JSIRuntimeHolder>(
325
- m_devSettings, m_jsThread, std::move(scriptStore), std::move(preparedScriptStore));
318
+ m_devSettings->jsiRuntimeHolder = std::make_shared<Microsoft::ReactNative::HermesRuntimeHolder>(
319
+ m_devSettings, m_jsThread, std::move(preparedScriptStore));
326
320
  break;
327
- #else
328
- assert(false); // V8 is not available in this build, fallthrough
329
- [[fallthrough]];
330
- #endif
331
321
  }
322
+ case JSIEngineOverride::V8:
332
323
  case JSIEngineOverride::V8NodeApi: {
333
324
  #if defined(USE_V8)
334
- std::unique_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore;
325
+ std::shared_ptr<facebook::jsi::PreparedScriptStore> preparedScriptStore;
335
326
 
336
327
  wchar_t tempPath[MAX_PATH];
337
- if (GetTempPathW(static_cast<DWORD>(std::size(tempPath)), tempPath)) {
328
+ if (GetTempPathW(MAX_PATH, tempPath)) {
338
329
  preparedScriptStore =
339
- std::make_unique<facebook::react::BasePreparedScriptStoreImpl>(winrt::to_string(tempPath));
340
- }
341
-
342
- if (!preparedScriptStore) {
343
- if (m_devSettings->errorCallback)
344
- m_devSettings->errorCallback("Could not initialize prepared script store");
345
-
346
- break;
330
+ std::make_shared<facebook::react::BasePreparedScriptStoreImpl>(winrt::to_string(tempPath));
347
331
  }
348
332
 
349
- m_devSettings->jsiRuntimeHolder = make_shared<NapiJsiV8RuntimeHolder>(
350
- m_devSettings, m_jsThread, nullptr /*scriptStore*/, std::move(preparedScriptStore));
333
+ m_devSettings->jsiRuntimeHolder = make_shared<Microsoft::ReactNative::V8RuntimeHolder>(
334
+ m_devSettings, m_jsThread, std::move(preparedScriptStore), false);
351
335
  break;
352
336
  #else
353
337
  if (m_devSettings->errorCallback)
@@ -0,0 +1,77 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #include "SafeLoadLibrary.h"
5
+ #include "PathCch.h"
6
+
7
+ EXTERN_C IMAGE_DOS_HEADER __ImageBase;
8
+
9
+ namespace Microsoft::ReactNative {
10
+
11
+ // Unsafe calls to LoadLibrary/LoadLibraryEx.
12
+ // The default behavior of LoadLibrary, or LoadLibraryEx without flags, is to try and find the dependency by iterating
13
+ // through a search order. This search order contains the current working directory. In the classic attack, a malicious
14
+ // DLL is dropped in the likely - controllable current working directory. The malicious DLL has the same name as a
15
+ // missing dependency or dependency that is not in the same directory as the executable. When the call to LoadLibrary
16
+ // is reached, the malicious DLL is loaded preferentially, and code execution occurs.
17
+ //
18
+ // The SafeLoadLibrary is the preferred ways to manually load dependencies.
19
+ // The API does not search the current working directory when resolving dependencies.
20
+ // The implementation is "borrowed" from Office MsoSafeLoadLibrary.
21
+
22
+ /**
23
+ List of new flags that control where to search for DLLs. Requires KB2533623.
24
+ */
25
+ const DWORD SafeLoadLibraryFlags = LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
26
+ LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_SEARCH_USER_DIRS;
27
+
28
+ HMODULE SafeLoadLibrary(const wchar_t *fileName, DWORD dwFlags) noexcept {
29
+ // When calling LoadLibrary, OR in LOAD_LIBRARY_SEARCH_DEFAULT_DIRS which enables all of the
30
+ // safe behaviors. Note that this flag is not compatible with LOAD_WITH_ALTERED_SEARCH_PATH.
31
+ #pragma push_macro("LoadLibraryExW")
32
+ #undef LoadLibraryExW
33
+ HMODULE module =
34
+ ::LoadLibraryExW(fileName, 0, (dwFlags | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS) & ~LOAD_WITH_ALTERED_SEARCH_PATH);
35
+ if (module == nullptr && GetLastError() == ERROR_INVALID_PARAMETER) {
36
+ // Could have failed with actual bad parameters or an unpatched OS without KB2533623.
37
+ if (::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), "AddDllDirectory") == nullptr) {
38
+ // Unpatched OS, remove all new flags.
39
+ module = ::LoadLibraryExW(fileName, 0, dwFlags & ~SafeLoadLibraryFlags);
40
+ }
41
+ }
42
+ #pragma pop_macro("LoadLibraryExW")
43
+
44
+ return module;
45
+ }
46
+
47
+ // Gets the full file name in the same directory as the current DLL.
48
+ // It returns nullptr if we cannot create the full file name.
49
+ wchar_t *GePeerFullFileName(const wchar_t *fileName, wchar_t *buffer, size_t bufferSize) {
50
+ size_t nameSize =
51
+ ::GetModuleFileNameW(reinterpret_cast<HINSTANCE>(&__ImageBase), buffer, static_cast<DWORD>(bufferSize));
52
+ if (nameSize == 0 || nameSize == bufferSize)
53
+ return nullptr;
54
+
55
+ if (::PathCchRemoveFileSpec(buffer, bufferSize) != S_OK)
56
+ return nullptr;
57
+ if (::PathCchCombineEx(buffer, bufferSize, buffer, fileName, PATHCCH_ALLOW_LONG_PATHS) != S_OK)
58
+ return nullptr;
59
+
60
+ return buffer;
61
+ }
62
+
63
+ HMODULE LoadLibraryAsPeerFirst(const wchar_t *fileName) noexcept {
64
+ HMODULE lib{nullptr};
65
+ constexpr size_t nameBufferSize = 4096;
66
+ wchar_t nameBuffer[nameBufferSize] = L"";
67
+
68
+ if (wchar_t *fullFileName = GePeerFullFileName(fileName, nameBuffer, nameBufferSize))
69
+ lib = SafeLoadLibrary(fullFileName);
70
+
71
+ if (!lib)
72
+ lib = SafeLoadLibrary(fileName);
73
+
74
+ return lib;
75
+ }
76
+
77
+ } // namespace Microsoft::ReactNative
@@ -0,0 +1,19 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+
4
+ #ifndef WIN32_LEAN_AND_MEAN
5
+ #define WIN32_LEAN_AND_MEAN
6
+ #endif
7
+
8
+ #include <windows.h>
9
+
10
+ namespace Microsoft::ReactNative {
11
+
12
+ // Safe LoadLibraryEx wrapper that must be used instead of LoadLibrary.
13
+ extern HMODULE SafeLoadLibrary(const wchar_t *fileName, DWORD flags = 0) noexcept;
14
+
15
+ // Try to load the dll from the same folder as the current DLL first,
16
+ // and if it is not found, then use the SafeLoadLibrary.
17
+ extern HMODULE LoadLibraryAsPeerFirst(const wchar_t *fileName) noexcept;
18
+
19
+ } // namespace Microsoft::ReactNative
@@ -29,14 +29,13 @@
29
29
  <ClCompile Include="$(MSBuildThisFileDirectory)Hasher.cpp" />
30
30
  <ClCompile Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.cpp" />
31
31
  <ClCompile Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.cpp" />
32
- <ClCompile Include="$(MSBuildThisFileDirectory)HermesShim.cpp" />
33
32
  <ClCompile Include="$(MSBuildThisFileDirectory)InspectorPackagerConnection.cpp" />
34
33
  <ClCompile Include="$(MSBuildThisFileDirectory)InstanceManager.cpp" />
35
34
  <ClCompile Include="$(MSBuildThisFileDirectory)JSBigAbiString.cpp" />
36
35
  <ClCompile Include="$(MSBuildThisFileDirectory)JSI\ChakraApi.cpp" />
37
36
  <ClCompile Include="$(MSBuildThisFileDirectory)JSI\ChakraJsiRuntime_edgemode.cpp" />
38
37
  <ClCompile Include="$(MSBuildThisFileDirectory)JSI\ChakraRuntime.cpp" />
39
- <ClCompile Include="$(MSBuildThisFileDirectory)JSI\NapiJsiV8RuntimeHolder.cpp">
38
+ <ClCompile Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.cpp">
40
39
  <ExcludedFromBuild Condition="'$(UseV8)' != 'true'">true</ExcludedFromBuild>
41
40
  </ClCompile>
42
41
  <ClCompile Include="$(MSBuildThisFileDirectory)LayoutAnimation.cpp" />
@@ -67,6 +66,7 @@
67
66
  <ClCompile Include="$(MSBuildThisFileDirectory)OInstance.cpp" />
68
67
  <ClCompile Include="$(MSBuildThisFileDirectory)PackagerConnection.cpp" />
69
68
  <ClCompile Include="$(MSBuildThisFileDirectory)RuntimeOptions.cpp" />
69
+ <ClCompile Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.cpp" />
70
70
  <ClCompile Include="$(MSBuildThisFileDirectory)Threading\BatchingQueueThread.cpp" />
71
71
  <ClCompile Include="$(MSBuildThisFileDirectory)Threading\MessageDispatchQueue.cpp" />
72
72
  <ClCompile Include="$(MSBuildThisFileDirectory)Threading\MessageQueueThreadFactory.cpp" />
@@ -74,12 +74,10 @@
74
74
  <ClCompile Include="$(MSBuildThisFileDirectory)TurboModuleManager.cpp" />
75
75
  <ClCompile Include="$(MSBuildThisFileDirectory)Utils.cpp" />
76
76
  <ClCompile Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.cpp" />
77
- <ClCompile Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.cpp">
78
- <ExcludedFromBuild Condition="'$(UseV8)' != 'true'">true</ExcludedFromBuild>
79
- </ClCompile>
80
77
  </ItemGroup>
81
78
  <ItemGroup>
82
79
  <ClInclude Include="$(MSBuildThisFileDirectory)..\include\Shared\cdebug.h" />
80
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\JsiApi.h" />
83
81
  <ClInclude Include="$(MSBuildThisFileDirectory)AbiSafe.h" />
84
82
  <ClInclude Include="$(MSBuildThisFileDirectory)AsyncStorageModule.h" />
85
83
  <ClInclude Include="$(MSBuildThisFileDirectory)AsyncStorage\AsyncStorageManager.h" />
@@ -88,7 +86,6 @@
88
86
  <ClInclude Include="$(MSBuildThisFileDirectory)BaseFileReaderResource.h" />
89
87
  <ClInclude Include="$(MSBuildThisFileDirectory)CppRuntimeOptions.h" />
90
88
  <ClInclude Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.h" />
91
- <ClInclude Include="$(MSBuildThisFileDirectory)HermesShim.h" />
92
89
  <ClInclude Include="$(MSBuildThisFileDirectory)IBlobPersistor.h" />
93
90
  <ClInclude Include="$(MSBuildThisFileDirectory)IFileReaderResource.h" />
94
91
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ByteArrayBuffer.h" />
@@ -96,7 +93,7 @@
96
93
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ChakraRuntime.h" />
97
94
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ChakraRuntimeArgs.h" />
98
95
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ChakraRuntimeFactory.h" />
99
- <ClInclude Include="$(MSBuildThisFileDirectory)JSI\NapiJsiV8RuntimeHolder.h" />
96
+ <ClInclude Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.h" />
100
97
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\RuntimeHolder.h" />
101
98
  <ClInclude Include="$(MSBuildThisFileDirectory)JSI\ScriptStore.h" />
102
99
  <ClInclude Include="$(MSBuildThisFileDirectory)Modules\BlobModule.h" />
@@ -153,6 +150,7 @@
153
150
  <ClInclude Include="$(MSBuildThisFileDirectory)NativeModuleProvider.h" />
154
151
  <ClInclude Include="$(MSBuildThisFileDirectory)OInstance.h" />
155
152
  <ClInclude Include="$(MSBuildThisFileDirectory)Pch\pch.h" />
153
+ <ClInclude Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.h" />
156
154
  <ClInclude Include="$(MSBuildThisFileDirectory)ShadowNode.h" />
157
155
  <ClInclude Include="$(MSBuildThisFileDirectory)ShadowNodeRegistry.h" />
158
156
  <ClInclude Include="$(MSBuildThisFileDirectory)targetver.h" />
@@ -167,7 +165,6 @@
167
165
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils.h" />
168
166
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils\CppWinrtLessExceptions.h" />
169
167
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils\WinRTConversions.h" />
170
- <ClInclude Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.h" />
171
168
  <ClInclude Include="$(MSBuildThisFileDirectory)WebSocketJSExecutorFactory.h" />
172
169
  <ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Modules\ReactRootViewTagGenerator.cpp" />
173
170
  <ClCompile Include="$(ReactNativeWindowsDir)Microsoft.ReactNative\Utils\ImageUtils.cpp" />
@@ -314,4 +311,18 @@
314
311
  <SubType>Code</SubType>
315
312
  </ClCompile>
316
313
  </ItemGroup>
314
+ <ItemGroup>
315
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.h" />
316
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.inc" />
317
+ <ClCompile Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.cpp">
318
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
319
+ </ClCompile>
320
+ </ItemGroup>
321
+ <ItemGroup Condition="'$(UseV8)' == 'true'">
322
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\V8Api.h" />
323
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\V8Api.inc" />
324
+ <ClCompile Include="$(NodeApiJsiDir)src\ApiLoaders\V8Api.cpp">
325
+ <PrecompiledHeader>NotUsing</PrecompiledHeader>
326
+ </ClCompile>
327
+ </ItemGroup>
317
328
  </Project>
@@ -43,9 +43,6 @@
43
43
  <ClCompile Include="$(MSBuildThisFileDirectory)CxxMessageQueue.cpp">
44
44
  <Filter>Source Files</Filter>
45
45
  </ClCompile>
46
- <ClCompile Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.cpp">
47
- <Filter>Source Files</Filter>
48
- </ClCompile>
49
46
  <ClCompile Include="$(MSBuildThisFileDirectory)JSBigAbiString.cpp">
50
47
  <Filter>Source Files</Filter>
51
48
  </ClCompile>
@@ -58,9 +55,6 @@
58
55
  <ClCompile Include="$(MSBuildThisFileDirectory)Utils.cpp">
59
56
  <Filter>Source Files</Filter>
60
57
  </ClCompile>
61
- <ClCompile Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.cpp">
62
- <Filter>Source Files</Filter>
63
- </ClCompile>
64
58
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\AsyncStorageModuleWin32.cpp">
65
59
  <Filter>Source Files\Modules</Filter>
66
60
  </ClCompile>
@@ -118,15 +112,6 @@
118
112
  <ClCompile Include="$(MSBuildThisFileDirectory)InspectorPackagerConnection.cpp">
119
113
  <Filter>Source Files</Filter>
120
114
  </ClCompile>
121
- <ClCompile Include="$(MSBuildThisFileDirectory)JSI\NapiJsiV8RuntimeHolder.cpp">
122
- <Filter>Source Files\JSI</Filter>
123
- </ClCompile>
124
- <ClCompile Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.cpp">
125
- <Filter>Source Files</Filter>
126
- </ClCompile>
127
- <ClCompile Include="$(MSBuildThisFileDirectory)HermesShim.cpp">
128
- <Filter>Source Files</Filter>
129
- </ClCompile>
130
115
  <ClCompile Include="$(MSBuildThisFileDirectory)Modules\HttpModule.cpp">
131
116
  <Filter>Source Files\Modules</Filter>
132
117
  </ClCompile>
@@ -163,6 +148,15 @@
163
148
  <ClCompile Include="$(MSBuildThisFileDirectory)Networking\DefaultBlobResource.cpp">
164
149
  <Filter>Source Files\Networking</Filter>
165
150
  </ClCompile>
151
+ <ClCompile Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.cpp">
152
+ <Filter>Hermes</Filter>
153
+ </ClCompile>
154
+ <ClCompile Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.cpp">
155
+ <Filter>Hermes</Filter>
156
+ </ClCompile>
157
+ <ClCompile Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.cpp" />
158
+ <ClCompile Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.cpp" />
159
+ <ClCompile Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.cpp" />
166
160
  </ItemGroup>
167
161
  <ItemGroup>
168
162
  <Filter Include="Source Files">
@@ -222,6 +216,9 @@
222
216
  <Filter Include="Source Files\Networking">
223
217
  <UniqueIdentifier>{71196e04-aca0-48d6-99e4-f418c957b50f}</UniqueIdentifier>
224
218
  </Filter>
219
+ <Filter Include="Hermes">
220
+ <UniqueIdentifier>{b32590e6-ae3d-4388-ab98-767345ce38c9}</UniqueIdentifier>
221
+ </Filter>
225
222
  </ItemGroup>
226
223
  <ItemGroup>
227
224
  <ClInclude Include="$(MSBuildThisFileDirectory)AsyncStorage\StorageFileIO.h">
@@ -287,9 +284,6 @@
287
284
  <ClInclude Include="$(MSBuildThisFileDirectory)DevSettings.h">
288
285
  <Filter>Header Files</Filter>
289
286
  </ClInclude>
290
- <ClInclude Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.h">
291
- <Filter>Header Files</Filter>
292
- </ClInclude>
293
287
  <ClInclude Include="$(MSBuildThisFileDirectory)IDevSupportManager.h">
294
288
  <Filter>Header Files</Filter>
295
289
  </ClInclude>
@@ -341,9 +335,6 @@
341
335
  <ClInclude Include="$(MSBuildThisFileDirectory)Utils.h">
342
336
  <Filter>Header Files</Filter>
343
337
  </ClInclude>
344
- <ClInclude Include="$(MSBuildThisFileDirectory)V8JSIRuntimeHolder.h">
345
- <Filter>Header Files</Filter>
346
- </ClInclude>
347
338
  <ClInclude Include="$(MSBuildThisFileDirectory)WebSocketJSExecutorFactory.h">
348
339
  <Filter>Header Files</Filter>
349
340
  </ClInclude>
@@ -399,15 +390,6 @@
399
390
  <ClInclude Include="$(MSBuildThisFileDirectory)tracing\tracing.h">
400
391
  <Filter>Header Files\tracing</Filter>
401
392
  </ClInclude>
402
- <ClInclude Include="$(MSBuildThisFileDirectory)JSI\NapiJsiV8RuntimeHolder.h">
403
- <Filter>Header Files\JSI</Filter>
404
- </ClInclude>
405
- <ClInclude Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.h">
406
- <Filter>Header Files</Filter>
407
- </ClInclude>
408
- <ClInclude Include="$(MSBuildThisFileDirectory)HermesShim.h">
409
- <Filter>Header Files</Filter>
410
- </ClInclude>
411
393
  <ClInclude Include="$(MSBuildThisFileDirectory)Modules\HttpModule.h">
412
394
  <Filter>Header Files\Modules</Filter>
413
395
  </ClInclude>
@@ -492,6 +474,17 @@
492
474
  <ClInclude Include="$(MSBuildThisFileDirectory)IBlobPersistor.h">
493
475
  <Filter>Header Files</Filter>
494
476
  </ClInclude>
477
+ <ClInclude Include="$(MSBuildThisFileDirectory)HermesRuntimeHolder.h">
478
+ <Filter>Hermes</Filter>
479
+ </ClInclude>
480
+ <ClInclude Include="$(MSBuildThisFileDirectory)HermesSamplingProfiler.h">
481
+ <Filter>Hermes</Filter>
482
+ </ClInclude>
483
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.h" />
484
+ <ClInclude Include="$(NodeApiJsiDir)src\ApiLoaders\HermesApi.inc" />
485
+ <ClInclude Include="$(MSBuildThisFileDirectory)..\Microsoft.ReactNative\JsiApi.h" />
486
+ <ClInclude Include="$(MSBuildThisFileDirectory)JSI\V8RuntimeHolder.h" />
487
+ <ClInclude Include="$(MSBuildThisFileDirectory)SafeLoadLibrary.h" />
495
488
  </ItemGroup>
496
489
  <ItemGroup>
497
490
  <None Include="$(MSBuildThisFileDirectory)tracing\rnw.wprp">