Skip to content

Commit

Permalink
src: fix to generate path from wchar_t via wstring.
Browse files Browse the repository at this point in the history
  • Loading branch information
yamachu committed Jan 22, 2025
1 parent c25be11 commit 2c318e8
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,18 @@ const BindingData::PackageConfig* BindingData::TraverseParent(
return nullptr;
}

#ifdef _WIN32
std::wstring MaybeWCharTToWSTring(const std::string& input) {
bool is_unicode = IsTextUnicode(input.c_str(), input.size(), nullptr);
auto code_page = is_unicode ? CP_UTF8 : GetACP();
int length = MultiByteToWideChar(code_page, 0, input.c_str(), -1, nullptr, 0);

std::wstring wide_str(length, 0);
MultiByteToWideChar(code_page, 0, input.c_str(), -1, &wide_str[0], length);
return wide_str;
}
#endif

void BindingData::GetNearestParentPackageJSON(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK_GE(args.Length(), 1);
Expand All @@ -340,12 +352,20 @@ void BindingData::GetNearestParentPackageJSON(
ToNamespacedPath(realm->env(), &path_value);

std::string path_value_str = path_value.ToString();
std::filesystem::path path;

#ifdef _WIN32
std::wstring wide_path = MaybeWCharTToWSTring(path_value_str);
path = std::filesystem::path(wide_path);
#else
path = std::filesystem::path(path_value_str);
#endif

if (slashCheck) {
path_value_str.push_back(kPathSeparator);
path /= std::filesystem::path(std::string(1, kPathSeparator));
}

auto package_json =
TraverseParent(realm, std::filesystem::u8path(path_value_str));
auto package_json = TraverseParent(realm, path);

if (package_json != nullptr) {
args.GetReturnValue().Set(package_json->Serialize(realm));
Expand All @@ -366,12 +386,20 @@ void BindingData::GetNearestParentPackageJSONType(
ToNamespacedPath(realm->env(), &path_value);

std::string path_value_str = path_value.ToString();
std::filesystem::path path;

#ifdef _WIN32
std::wstring wide_path = MaybeWCharTToWSTring(path_value_str);
path = std::filesystem::path(wide_path);
#else
path = std::filesystem::path(path_value_str);
#endif

if (slashCheck) {
path_value_str.push_back(kPathSeparator);
path /= std::filesystem::path(std::string(1, kPathSeparator));
}

auto package_json =
TraverseParent(realm, std::filesystem::u8path(path_value_str));
auto package_json = TraverseParent(realm, path);

if (package_json == nullptr) {
return;
Expand Down

0 comments on commit 2c318e8

Please sign in to comment.