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
  • Loading branch information
pluris committed Sep 17, 2023
1 parent e9ff810 commit 2c554b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2909,11 +2909,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 @@ -2923,7 +2931,10 @@ const std::array<std::string, 10> BindingData::legacy_main_extensions = {
"/index.node",
".js",
".json",
".node"};
".node"
};

} // namespace

void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(args.Length(), 1);
Expand Down Expand Up @@ -2965,9 +2976,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

FromNamespacedPath(&initial_file_path);

for (int i = 0; i < BindingData::legacy_main_extensions_with_main_end;
for (int i = 0; i < legacy_main_extensions_with_main_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 Expand Up @@ -3000,10 +3011,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 2c554b7

Please sign in to comment.