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 b9a8614
Show file tree
Hide file tree
Showing 21 changed files with 118 additions and 112 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
4 changes: 2 additions & 2 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ inline std::shared_ptr<EnvironmentOptions> Environment::options() {
return options_;
}

inline const std::vector<std::string>& Environment::argv() {
inline const std::vector<std::string_view>& Environment::argv() {
return argv_;
}

inline const std::vector<std::string>& Environment::exec_argv() {
inline const std::vector<std::string_view>& Environment::exec_argv() {
return exec_argv_;
}

Expand Down
10 changes: 5 additions & 5 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ std::unique_ptr<v8::BackingStore> Environment::release_managed_buffer(
return bs;
}

std::string Environment::GetExecPath(const std::vector<std::string>& argv) {
std::string Environment::GetExecPath(const std::vector<std::string_view>& argv) {
char exec_path_buf[2 * PATH_MAX];
size_t exec_path_len = sizeof(exec_path_buf);
std::string exec_path;
Expand Down Expand Up @@ -791,8 +791,8 @@ std::string Environment::GetExecPath(const std::vector<std::string>& argv) {

Environment::Environment(IsolateData* isolate_data,
Isolate* isolate,
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 EnvSerializeInfo* env_info,
EnvironmentFlags::Flags flags,
ThreadId thread_id)
Expand Down Expand Up @@ -910,10 +910,10 @@ Environment::Environment(IsolateData* isolate_data,
TRACING_CATEGORY_NODE1(environment)) != 0) {
auto traced_value = tracing::TracedValue::Create();
traced_value->BeginArray("args");
for (const std::string& arg : args) traced_value->AppendString(arg);
for (const auto& arg : args) traced_value->AppendString(arg.data());
traced_value->EndArray();
traced_value->BeginArray("exec_args");
for (const std::string& arg : exec_args) traced_value->AppendString(arg);
for (const auto& arg : exec_args) traced_value->AppendString(arg.data());
traced_value->EndArray();
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment),
"Environment",
Expand Down
14 changes: 7 additions & 7 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ class Environment final : public MemoryRetainer {

SET_MEMORY_INFO_NAME(Environment)

static std::string GetExecPath(const std::vector<std::string>& argv);
static std::string GetExecPath(const std::vector<std::string_view>& argv);
static std::string GetCwd(const std::string& exec_path);

inline size_t SelfSize() const override;
Expand Down Expand Up @@ -664,8 +664,8 @@ class Environment final : public MemoryRetainer {
// InitializeMainContext() to initialize a main context for it.
Environment(IsolateData* isolate_data,
v8::Isolate* isolate,
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 EnvSerializeInfo* env_info,
EnvironmentFlags::Flags flags,
ThreadId thread_id);
Expand All @@ -674,8 +674,8 @@ class Environment final : public MemoryRetainer {
~Environment() override;

void InitializeLibuv();
inline const std::vector<std::string>& exec_argv();
inline const std::vector<std::string>& argv();
inline const std::vector<std::string_view>& exec_argv();
inline const std::vector<std::string_view>& argv();
const std::string& exec_path() const;

void CleanupHandles();
Expand Down Expand Up @@ -1143,8 +1143,8 @@ class Environment final : public MemoryRetainer {
// the inspector_host_port_->port() will be the actual port being
// used.
std::shared_ptr<ExclusiveAccess<HostPort>> inspector_host_port_;
std::vector<std::string> exec_argv_;
std::vector<std::string> argv_;
std::vector<std::string_view> exec_argv_;
std::vector<std::string_view> argv_;
std::string exec_path_;

bool is_in_heapsnapshot_heap_limit_callback_ = false;
Expand Down
45 changes: 25 additions & 20 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void Environment::InitializeInspector(
inspector_path = parent_handle->url();
inspector_agent_->SetParentHandle(std::move(parent_handle));
} else {
inspector_path = argv_.size() > 1 ? argv_[1].c_str() : "";
inspector_path = argv_.size() > 1 ? argv_[1].data() : "";
}

CHECK(!inspector_agent_->IsListening());
Expand Down Expand Up @@ -710,12 +710,12 @@ 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.
std::vector<std::string> v8_args;
std::vector<std::string_view> v8_args;

Mutex::ScopedLock lock(per_process::cli_options_mutex);
options_parser::Parse(
Expand Down Expand Up @@ -771,12 +771,13 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
}
#endif

std::vector<char*> v8_args_as_char_ptr(v8_args.size());
std::vector<const char*> v8_args_as_char_ptr(v8_args.size());
if (v8_args.size() > 0) {
for (size_t i = 0; i < v8_args.size(); ++i)
v8_args_as_char_ptr[i] = v8_args[i].data();
int argc = v8_args.size();
V8::SetFlagsFromCommandLine(&argc, v8_args_as_char_ptr.data(), true);
// TODO(anonrig): enable this before landing
// V8::SetFlagsFromCommandLine(&argc, &*v8_args_as_char_ptr.data(), true);
v8_args_as_char_ptr.resize(argc);
}

Expand All @@ -790,8 +791,8 @@ static ExitCode ProcessGlobalArgsInternal(std::vector<std::string>* args,
return ExitCode::kNoFailure;
}

int ProcessGlobalArgs(std::vector<std::string>* args,
std::vector<std::string>* exec_args,
int ProcessGlobalArgs(std::vector<std::string_view>* args,
std::vector<std::string_view>* exec_args,
std::vector<std::string>* errors,
OptionEnvvarSettings settings) {
return static_cast<int>(
Expand All @@ -803,8 +804,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 +862,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 Down Expand Up @@ -975,20 +976,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 +1217,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 +1287,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 +1434,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
16 changes: 8 additions & 8 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,10 @@ class NODE_EXTERN InitializationResult {
virtual bool early_return() const = 0;

// Returns the parsed list of non-Node.js arguments.
virtual const std::vector<std::string>& args() const = 0;
virtual const std::vector<std::string_view>& args() const = 0;

// Returns the parsed list of Node.js arguments.
virtual const std::vector<std::string>& exec_args() const = 0;
virtual const std::vector<std::string_view>& exec_args() const = 0;

// Returns an array of errors. Note that these may be warnings
// whose existence does not imply a non-zero exit code.
Expand Down 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
8 changes: 4 additions & 4 deletions src/node_dotenv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ 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) {
const auto find_match = [](const std::string_view arg) {
return arg == "--" || arg == "--env-file" ||
arg.starts_with("--env-file=") || arg == "--env-file-if-exists" ||
arg.starts_with("--env-file-if-exists=");
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
8 changes: 4 additions & 4 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_view>& args() const { return args_; }
const std::vector<std::string_view>& exec_args() const { return exec_args_; }
const std::vector<std::string>& 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
4 changes: 2 additions & 2 deletions src/node_main_instance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ using v8::Locker;
NodeMainInstance::NodeMainInstance(const SnapshotData* snapshot_data,
uv_loop_t* event_loop,
MultiIsolatePlatform* platform,
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)
: args_(args),
exec_args_(exec_args),
array_buffer_allocator_(ArrayBufferAllocator::Create()),
Expand Down
Loading

0 comments on commit b9a8614

Please sign in to comment.