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 23, 2025
1 parent 2b7400a commit cba8830
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

#include "simdjson.h"

#ifdef _WIN32
#include <windows.h>
#endif

namespace node {
namespace modules {

Expand Down Expand Up @@ -326,6 +330,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 @@ -344,8 +360,16 @@ void BindingData::GetNearestParentPackageJSON(
path_value_str.push_back(kPathSeparator);
}

auto package_json =
TraverseParent(realm, std::filesystem::path(path_value_str));
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

auto package_json = TraverseParent(realm, path);

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

auto package_json =
TraverseParent(realm, std::filesystem::path(path_value_str));
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

auto package_json = TraverseParent(realm, path);

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

0 comments on commit cba8830

Please sign in to comment.