Skip to content

Commit

Permalink
src: converts args to std::span<std::string_view>
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 29, 2024
1 parent 61b077d commit d44c8f5
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/api/embed_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ std::unique_ptr<CommonEnvironmentSetup>
CommonEnvironmentSetup::CreateForSnapshotting(
MultiIsolatePlatform* platform,
std::vector<std::string>* errors,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
const SnapshotConfig& snapshot_config) {
// It's not guaranteed that a context that goes through
// v8_inspector::V8Inspector::contextCreated() is runtime-independent,
Expand Down
4 changes: 2 additions & 2 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,8 @@ struct InspectorParentHandleImpl : public InspectorParentHandle {
Environment* CreateEnvironment(
IsolateData* isolate_data,
Local<Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
EnvironmentFlags::Flags flags,
ThreadId thread_id,
std::unique_ptr<InspectorParentHandle> inspector_parent_handle) {
Expand Down
34 changes: 19 additions & 15 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,8 @@ void ResetStdio() {
#endif // __POSIX__
}

static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
std::vector<std::string>* exec_args,
static ExitCode ProcessGlobalArgsInternal(std::vector<std::string_view>* args,
std::vector<std::string_view>* exec_args,
std::vector<std::string>* errors,
OptionEnvvarSettings settings) {
// Parse a few arguments which are specific to Node.
Expand Down Expand Up @@ -803,8 +803,8 @@ static std::atomic_bool init_called{false};
// TODO(addaleax): Turn this into a wrapper around InitializeOncePerProcess()
// (with the corresponding additional flags set), then eventually remove this.
static ExitCode InitializeNodeWithArgsInternal(
std::vector<std::string>* argv,
std::vector<std::string>* exec_argv,
std::vector<std::string_view>* argv,
std::vector<std::string_view>* exec_argv,
std::vector<std::string>* errors,
ProcessInitializationFlags::Flags flags) {
// Make sure InitializeNodeWithArgs() is called only once.
Expand Down Expand Up @@ -861,16 +861,16 @@ static ExitCode InitializeNodeWithArgsInternal(
case Dotenv::ParseResult::Valid:
break;
case Dotenv::ParseResult::InvalidContent:
errors->push_back(file_data.path + ": invalid format");
errors->push_back(std::string(file_data.path) + ": invalid format");
break;
case Dotenv::ParseResult::FileError:
if (file_data.is_optional) {
fprintf(stderr,
"%s not found. Continuing without it.\n",
file_data.path.c_str());
file_data.path.data());
continue;
}
errors->push_back(file_data.path + ": not found");
errors->push_back(std::string(file_data.path) + ": not found");
break;
default:
UNREACHABLE();
Expand All @@ -885,7 +885,7 @@ static ExitCode InitializeNodeWithArgsInternal(
// NODE_OPTIONS environment variable is preferred over the file one.
if (credentials::SafeGetenv("NODE_OPTIONS", &node_options) ||
!node_options.empty()) {
std::vector<std::string> env_argv =
std::vector<std::string_view> env_argv =
ParseNodeOptionsEnvVar(node_options, errors);

if (!errors->empty()) return ExitCode::kInvalidCommandLineArgument;
Expand Down Expand Up @@ -975,20 +975,20 @@ static ExitCode InitializeNodeWithArgsInternal(
return ExitCode::kNoFailure;
}

int InitializeNodeWithArgs(std::vector<std::string>* argv,
std::vector<std::string>* exec_argv,
int InitializeNodeWithArgs(std::vector<std::string_view>* argv,
std::vector<std::string_view>* exec_argv,
std::vector<std::string>* errors,
ProcessInitializationFlags::Flags flags) {
return static_cast<int>(
InitializeNodeWithArgsInternal(argv, exec_argv, errors, flags));
}

static std::shared_ptr<InitializationResultImpl>
InitializeOncePerProcessInternal(const std::vector<std::string>& args,
InitializeOncePerProcessInternal(const std::vector<std::string_view>& args,
ProcessInitializationFlags::Flags flags =
ProcessInitializationFlags::kNoFlags) {
auto result = std::make_shared<InitializationResultImpl>();
result->args_ = args;
result->args_ = std::move(args);

if (!(flags & ProcessInitializationFlags::kNoParseGlobalDebugVariables)) {
// Initialized the enabled list for Debug() calls with system
Expand Down Expand Up @@ -1216,7 +1216,7 @@ InitializeOncePerProcessInternal(const std::vector<std::string>& args,
}

std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
const std::vector<std::string_view>& args,
ProcessInitializationFlags::Flags flags) {
return InitializeOncePerProcessInternal(args, flags);
}
Expand Down Expand Up @@ -1286,7 +1286,11 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
}
} else {
snapshot_config.builder_script_path = result->args()[1];
args_maybe_patched = result->args();
args_maybe_patched.reserve(result->args().size());
// TODO(anonrig): Avoid copying in here.
for (const auto& arg : result->args()) {
args_maybe_patched.push_back(std::string(arg));
};
}
DCHECK(snapshot_config.builder_script_path.has_value());
const std::string& builder_script =
Expand Down Expand Up @@ -1429,7 +1433,7 @@ static ExitCode StartInternal(int argc, char** argv) {

std::shared_ptr<InitializationResultImpl> result =
InitializeOncePerProcessInternal(
std::vector<std::string>(argv, argv + argc));
std::vector<std::string_view>(argv, argv + argc));
for (const std::string& error : result->errors()) {
FPrintF(stderr, "%s: %s\n", result->args().at(0), error);
}
Expand Down
12 changes: 6 additions & 6 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ NODE_DEPRECATED("Use InitializeOncePerProcess() instead",
// errors encountered during initialization, and a potential suggested
// exit code.
NODE_EXTERN std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
const std::vector<std::string_view>& args,
ProcessInitializationFlags::Flags flags =
ProcessInitializationFlags::kNoFlags);
// Undoes the initialization performed by InitializeOncePerProcess(),
Expand All @@ -359,7 +359,7 @@ NODE_EXTERN void TearDownOncePerProcess();
// Convenience overload for specifying multiple flags without having
// to worry about casts.
inline std::shared_ptr<InitializationResult> InitializeOncePerProcess(
const std::vector<std::string>& args,
const std::vector<std::string_view>& args,
std::initializer_list<ProcessInitializationFlags::Flags> list) {
uint64_t flags_accum = ProcessInitializationFlags::kNoFlags;
for (const auto flag : list) flags_accum |= static_cast<uint64_t>(flag);
Expand Down Expand Up @@ -704,8 +704,8 @@ struct InspectorParentHandle {
NODE_EXTERN Environment* CreateEnvironment(
IsolateData* isolate_data,
v8::Local<v8::Context> context,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
EnvironmentFlags::Flags flags = EnvironmentFlags::kDefaultFlags,
ThreadId thread_id = {} /* allocates a thread id automatically */,
std::unique_ptr<InspectorParentHandle> inspector_parent_handle = {});
Expand Down Expand Up @@ -923,8 +923,8 @@ class NODE_EXTERN CommonEnvironmentSetup {
static std::unique_ptr<CommonEnvironmentSetup> CreateForSnapshotting(
MultiIsolatePlatform* platform,
std::vector<std::string>* errors,
const std::vector<std::string>& args = {},
const std::vector<std::string>& exec_args = {},
const std::vector<std::string_view>& args = {},
const std::vector<std::string_view>& exec_args = {},
const SnapshotConfig& snapshot_config = {});
EmbedderSnapshotData::Pointer CreateSnapshot();

Expand Down
6 changes: 3 additions & 3 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ using v8::Object;
using v8::String;

std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
const std::vector<std::string>& args) {
const std::string_view optional_env_file_flag = "--env-file-if-exists";
const std::vector<std::string_view>& args) {
constexpr std::string_view optional_env_file_flag = "--env-file-if-exists";

const auto find_match = [](const std::string& arg) {
return arg == "--" || arg == "--env-file" ||
Expand All @@ -37,7 +37,7 @@ std::vector<Dotenv::env_file_data> Dotenv::GetDataFromArgs(
auto flag = matched_arg->substr(0, equal_char_index);
auto file_path = matched_arg->substr(equal_char_index + 1);

struct env_file_data env_file_data = {
env_file_data env_file_data = {
file_path, flag.starts_with(optional_env_file_flag)};
env_files.push_back(env_file_data);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/node_dotenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Dotenv {
public:
enum ParseResult { Valid, FileError, InvalidContent };
struct env_file_data {
std::string path;
std::string_view path;
bool is_optional;
};

Expand All @@ -32,7 +32,7 @@ class Dotenv {
v8::Local<v8::Object> ToObject(Environment* env) const;

static std::vector<env_file_data> GetDataFromArgs(
const std::vector<std::string>& args);
const std::vector<std::string_view>& args);

private:
std::map<std::string, std::string> store_;
Expand Down
10 changes: 5 additions & 5 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,14 +346,14 @@ class InitializationResultImpl final : public InitializationResult {
int exit_code() const { return static_cast<int>(exit_code_enum()); }
ExitCode exit_code_enum() const { return exit_code_; }
bool early_return() const { return early_return_; }
const std::vector<std::string>& args() const { return args_; }
const std::vector<std::string>& exec_args() const { return exec_args_; }
const std::vector<std::string>& errors() const { return errors_; }
const std::vector<std::string_view>& args() const { return args_; }
const std::vector<std::string_view>& exec_args() const { return exec_args_; }
const std::vector<std::string_view>& errors() const { return errors_; }
MultiIsolatePlatform* platform() const { return platform_; }

ExitCode exit_code_ = ExitCode::kNoFailure;
std::vector<std::string> args_;
std::vector<std::string> exec_args_;
std::vector<std::string_view> args_;
std::vector<std::string_view> exec_args_;
std::vector<std::string> errors_;
bool early_return_ = false;
MultiIsolatePlatform* platform_ = nullptr;
Expand Down
7 changes: 4 additions & 3 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class PerProcessOptions : public Options {
// TODO(addaleax): Some of these could probably be per-Environment.
std::string use_largepages = "off";
bool trace_sigint = false;
std::vector<std::string> cmdline;
std::vector<std::string_view> cmdline;

inline PerIsolateOptions* get_per_isolate_options();
void CheckOptions(std::vector<std::string>* errors,
Expand Down Expand Up @@ -567,9 +567,10 @@ class OptionsParser {
};

using StringVector = std::vector<std::string>;
using StringViewVector = std::vector<std::string_view>;
template <class OptionsType, class = Options>
void Parse(
StringVector* const args, StringVector* const exec_args,
StringViewVector* const args, StringViewVector* const exec_args,
StringVector* const v8_args, OptionsType* const options,
OptionEnvvarSettings required_env_settings, StringVector* const errors);

Expand All @@ -586,7 +587,7 @@ void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options);
void HandleEnvOptions(std::shared_ptr<EnvironmentOptions> env_options,
std::function<std::string(const char*)> opt_getter);

std::vector<std::string> ParseNodeOptionsEnvVar(
std::vector<std::string_view> ParseNodeOptionsEnvVar(
const std::string& node_options, std::vector<std::string>* errors);
} // namespace node

Expand Down
2 changes: 1 addition & 1 deletion src/node_sea.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ ExitCode GenerateSnapshotForSEA(const SeaConfig& config,
SnapshotData snapshot;
// TODO(joyeecheung): make the arguments configurable through the JSON
// config or a programmatic API.
std::vector<std::string> patched_args = {args[0], config.main_path};
std::vector patched_args = {args[0], config.main_path};
ExitCode exit_code = SnapshotBuilder::Generate(&snapshot,
patched_args,
exec_args,
Expand Down
8 changes: 4 additions & 4 deletions src/node_snapshot_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ std::optional<SnapshotConfig> ReadSnapshotConfig(const char* path);
class NODE_EXTERN_PRIVATE SnapshotBuilder {
public:
static ExitCode GenerateAsSource(const char* out_path,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
const SnapshotConfig& config,
bool use_array_literals = false);

Expand All @@ -31,8 +31,8 @@ class NODE_EXTERN_PRIVATE SnapshotBuilder {
// in case the script is already read for other purposes.
static ExitCode Generate(
SnapshotData* out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
std::optional<std::string_view> builder_script_content,
const SnapshotConfig& config);

Expand Down
18 changes: 9 additions & 9 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -936,8 +936,8 @@ std::optional<SnapshotConfig> ReadSnapshotConfig(const char* config_path) {

ExitCode BuildSnapshotWithoutCodeCache(
SnapshotData* out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
std::optional<std::string_view> builder_script_content,
const SnapshotConfig& config) {
DCHECK(builder_script_content.has_value() ==
Expand All @@ -957,7 +957,7 @@ ExitCode BuildSnapshotWithoutCodeCache(
per_process::v8_platform.Platform(), &errors, args, exec_args, config);
if (!setup) {
for (const std::string& err : errors)
fprintf(stderr, "%s: %s\n", args[0].c_str(), err.c_str());
fprintf(stderr, "%s: %s\n", args[0].data(), err.c_str());
return ExitCode::kBootstrapFailure;
}

Expand Down Expand Up @@ -999,8 +999,8 @@ ExitCode BuildSnapshotWithoutCodeCache(
}

ExitCode BuildCodeCacheFromSnapshot(SnapshotData* out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args) {
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args) {
RAIIIsolate raii_isolate(out);
Isolate* isolate = raii_isolate.get();
v8::Locker locker(isolate);
Expand Down Expand Up @@ -1039,8 +1039,8 @@ ExitCode BuildCodeCacheFromSnapshot(SnapshotData* out,

ExitCode SnapshotBuilder::Generate(
SnapshotData* out,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
std::optional<std::string_view> builder_script_content,
const SnapshotConfig& snapshot_config) {
ExitCode code = BuildSnapshotWithoutCodeCache(
Expand Down Expand Up @@ -1177,8 +1177,8 @@ ExitCode SnapshotBuilder::CreateSnapshot(SnapshotData* out,

ExitCode SnapshotBuilder::GenerateAsSource(
const char* out_path,
const std::vector<std::string>& args,
const std::vector<std::string>& exec_args,
const std::vector<std::string_view>& args,
const std::vector<std::string_view>& exec_args,
const SnapshotConfig& config,
bool use_array_literals) {
std::string builder_script_content;
Expand Down
4 changes: 2 additions & 2 deletions src/node_task_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ void RunTask(const std::shared_ptr<InitializationResultImpl>& result,
// If the "--" flag is not found, it returns an empty optional.
// Otherwise, it returns the positional arguments as a single string.
// Example: "node -- script.js arg1 arg2" returns "arg1 arg2".
PositionalArgs GetPositionalArgs(const std::vector<std::string>& args) {
PositionalArgs GetPositionalArgs(const std::vector<std::string_view>& args) {
// If the "--" flag is not found, return an empty optional
// Otherwise, return the positional arguments as a single string
if (auto dash_dash = std::find(args.begin(), args.end(), "--");
Expand All @@ -358,7 +358,7 @@ PositionalArgs GetPositionalArgs(const std::vector<std::string>& args) {
for (auto it = dash_dash + 1; it != args.end(); ++it) {
// SAFETY: The following code is safe because the lifetime of the
// arguments is guaranteed to be valid until the end of the task runner.
positional_args.emplace_back(it->c_str(), it->size());
positional_args.push_back(*it);
}
return positional_args;
}
Expand Down
2 changes: 1 addition & 1 deletion src/node_task_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ FindPackageJson(const std::filesystem::path& cwd);
void RunTask(const std::shared_ptr<InitializationResultImpl>& result,
std::string_view command_id,
const PositionalArgs& positional_args);
PositionalArgs GetPositionalArgs(const std::vector<std::string>& args);
PositionalArgs GetPositionalArgs(const std::vector<std::string_view>& args);
std::string EscapeShell(std::string_view command);

} // namespace node::task_runner
Expand Down

0 comments on commit d44c8f5

Please sign in to comment.