Skip to content

Commit

Permalink
src: move const variable in node_file.h to node_file.cc
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49688
Refs: nodejs#48325
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
Jungku Lee authored and alexfernandez committed Nov 1, 2023
1 parent c0091cc commit 28d2e2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3120,11 +3120,19 @@ BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
return BindingData::FilePathIsFileReturnType::kIsNotFile;
}

namespace {

// define the final index of the algorithm resolution
// when packageConfig.main is defined.
constexpr uint8_t legacy_main_extensions_with_main_end = 7;
// define the final index of the algorithm resolution
// when packageConfig.main is NOT defined
constexpr uint8_t legacy_main_extensions_package_fallback_end = 10;
// the possible file extensions that should be tested
// 0-6: when packageConfig.main is defined
// 7-9: when packageConfig.main is NOT defined,
// or when the previous case didn't found the file
const std::array<std::string, 10> BindingData::legacy_main_extensions = {
constexpr std::array<std::string_view, 10> legacy_main_extensions = {
"",
".js",
".json",
Expand All @@ -3136,6 +3144,8 @@ const std::array<std::string, 10> BindingData::legacy_main_extensions = {
".json",
".node"};

} // namespace

void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
CHECK(args[0]->IsString());
Expand Down Expand Up @@ -3176,9 +3186,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

FromNamespacedPath(&initial_file_path);

for (int i = 0; i < BindingData::legacy_main_extensions_with_main_end;
i++) {
file_path = initial_file_path + BindingData::legacy_main_extensions[i];
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
file_path = initial_file_path + std::string(legacy_main_extensions[i]);

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down Expand Up @@ -3211,10 +3220,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

FromNamespacedPath(&initial_file_path);

for (int i = BindingData::legacy_main_extensions_with_main_end;
i < BindingData::legacy_main_extensions_package_fallback_end;
for (int i = legacy_main_extensions_with_main_end;
i < legacy_main_extensions_package_fallback_end;
i++) {
file_path = initial_file_path + BindingData::legacy_main_extensions[i];
file_path = initial_file_path + std::string(legacy_main_extensions[i]);

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down
8 changes: 0 additions & 8 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ class BindingData : public SnapshotableObject {

static FilePathIsFileReturnType FilePathIsFile(Environment* env,
const std::string& file_path);

static const std::array<std::string, 10> legacy_main_extensions;
// define the final index of the algorithm resolution
// when packageConfig.main is defined.
static const uint8_t legacy_main_extensions_with_main_end = 7;
// define the final index of the algorithm resolution
// when packageConfig.main is NOT defined
static const uint8_t legacy_main_extensions_package_fallback_end = 10;
};

// structure used to store state during a complex operation, e.g., mkdirp.
Expand Down

0 comments on commit 28d2e2b

Please sign in to comment.