Skip to content

Commit 0697f02

Browse files
committed
src: fix to generate path from wchar_t via wstring.
1 parent aaddec2 commit 0697f02

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/node_modules.cc

+36-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#include "simdjson.h"
1818

19+
#ifdef _WIN32
20+
#include <windows.h>
21+
#endif
22+
1923
namespace node {
2024
namespace modules {
2125

@@ -326,6 +330,18 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
326330
return nullptr;
327331
}
328332

333+
#ifdef _WIN32
334+
std::wstring MaybeWCharTToWString(const std::string& input) {
335+
bool is_unicode = IsTextUnicode(input.c_str(), input.size(), nullptr);
336+
auto code_page = is_unicode ? CP_UTF8 : GetACP();
337+
int length = MultiByteToWideChar(code_page, 0, input.c_str(), -1, nullptr, 0);
338+
339+
std::wstring wide_str(length, 0);
340+
MultiByteToWideChar(code_page, 0, input.c_str(), -1, &wide_str[0], length);
341+
return wide_str;
342+
}
343+
#endif
344+
329345
void BindingData::GetNearestParentPackageJSON(
330346
const v8::FunctionCallbackInfo<v8::Value>& args) {
331347
CHECK_GE(args.Length(), 1);
@@ -344,8 +360,16 @@ void BindingData::GetNearestParentPackageJSON(
344360
path_value_str.push_back(kPathSeparator);
345361
}
346362

347-
auto package_json =
348-
TraverseParent(realm, std::filesystem::path(path_value_str));
363+
std::filesystem::path path;
364+
365+
#ifdef _WIN32
366+
std::wstring wide_path = MaybeWCharTToWString(path_value_str);
367+
path = std::filesystem::path(wide_path);
368+
#else
369+
path = std::filesystem::path(path_value_str);
370+
#endif
371+
372+
auto package_json = TraverseParent(realm, path);
349373

350374
if (package_json != nullptr) {
351375
args.GetReturnValue().Set(package_json->Serialize(realm));
@@ -370,8 +394,16 @@ void BindingData::GetNearestParentPackageJSONType(
370394
path_value_str.push_back(kPathSeparator);
371395
}
372396

373-
auto package_json =
374-
TraverseParent(realm, std::filesystem::path(path_value_str));
397+
std::filesystem::path path;
398+
399+
#ifdef _WIN32
400+
std::wstring wide_path = MaybeWCharTToWString(path_value_str);
401+
path = std::filesystem::path(wide_path);
402+
#else
403+
path = std::filesystem::path(path_value_str);
404+
#endif
405+
406+
auto package_json = TraverseParent(realm, path);
375407

376408
if (package_json == nullptr) {
377409
return;

0 commit comments

Comments
 (0)