Skip to content

Commit ea943d8

Browse files
committed
src: fix to generate path from wchar_t via wstring.
take a similar approach to node_file and allow the creation of paths code point must be specified to convert from wchar_t to utf8. if we do the same for node_fs, test-fs-cp.mjs will fail
1 parent a5209a8 commit ea943d8

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/node_modules.cc

Lines changed: 36 additions & 4 deletions
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 ConvertToWideString(const std::string& str) {
335+
auto cp = GetACP();
336+
int size_needed = MultiByteToWideChar(
337+
cp, 0, &str[0], static_cast<int>(str.size()), nullptr, 0);
338+
std::wstring wstrTo(size_needed, 0);
339+
MultiByteToWideChar(
340+
cp, 0, &str[0], static_cast<int>(str.size()), &wstrTo[0], size_needed);
341+
return wstrTo;
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 = ConvertToWideString(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 = ConvertToWideString(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)