Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4993,8 +4993,40 @@ int Start(int argc, char** argv) {
namespace lib {

struct CmdArgs {
/**
* @brief CmdArgs builds a continuous adjacent char buffer (argv) from a vector of strings
* and stores it in the static cmd_args variable
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, it's not really a "static" variable, is it? Looks like a simple member variable to me.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, I fixed the documentation

* @param program_name the name of the executable
* @param arguments the arguments for the program
*/
CmdArgs(const std::string& program_name, const std::vector<std::string>& arguments)
: argc(0)
, argv(nullptr)
{
size_t total_size = 0;
total_size += program_name.size() + 1;
for (const auto& argument: arguments) {
total_size += argument.size() + 1;
}

// FIXME: one cannot assume that the memory in a string is the same after push_back
argument_data.reserve(total_size);
argument_pointers.push_back(const_cast<char*>(argument_data.data() + argument_data.size()));
argument_data += program_name;
argument_data += char(0x0);
for (const auto& argument: arguments) {
argument_pointers.push_back(const_cast<char*>(argument_data.data() + argument_data.size()));
argument_data += argument;
argument_data += char(0x0);
}
argc = argument_pointers.size();
argv = argument_pointers.data();
}

int argc;
char** argv;
std::string argument_data;
std::vector<char*> argument_pointers;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two don't need to be public.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

};

ArrayBufferAllocator* allocator;
Expand All @@ -5015,8 +5047,8 @@ void deleteCmdArgs() {
if (!cmd_args) {
return;
}
delete[] cmd_args->argv;
delete cmd_args;
cmd_args = nullptr;
}

int _StopEnv() {
Expand Down Expand Up @@ -5060,15 +5092,6 @@ void deinitV8() {

namespace initialize {

void generateCmdArgsFromProgramName(const std::string& program_name) {
deinitialize::deleteCmdArgs();
int argc = 1;
char* program_name_c_string = new char[program_name.length() + 1];
std::strcpy(program_name_c_string, program_name.c_str());
char** argv = new char*(program_name_c_string);
cmd_args = new CmdArgs{argc, argv};
}

void initV8() {
v8_platform.Initialize(v8_thread_pool_size, uv_default_loop());
// Enable tracing when argv has --trace-events-enabled.
Expand Down Expand Up @@ -5182,20 +5205,18 @@ void _StartEnv(int argc,

} // namespace initialize

void Initialize(const std::string& program_name) {
void Initialize(const std::string& program_name, const std::vector<std::string>& node_args) {
//////////
// Start 1
//////////
atexit([] () { uv_tty_reset_mode(); });
PlatformInit();
node::performance::performance_node_start = PERFORMANCE_NOW();

// currently we do not support additional commandline options for node, uv, or v8
// we explicitily only set the first argument to the program name
initialize::generateCmdArgsFromProgramName(program_name);
cmd_args = new CmdArgs(program_name, node_args);

// Hack around with the argv pointer. Used for process.title = "blah".
cmd_args->argv = uv_setup_args(cmd_args->argc, cmd_args->argv);
// Hack around with the argv pointer. Used for process.title = "blah --args".
uv_setup_args(cmd_args->argc, cmd_args->argv);

// This needs to run *before* V8::Initialize(). The const_cast is not
// optional, in case you're wondering.
Expand Down
2 changes: 1 addition & 1 deletion src/node_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace node { namespace lib {
Starts the Node.js engine without a concrete script file to execute.
*Important*: This requires the C++ developer to call `ProcessEvents()` periodically OR call `RunMainLoop()` to start the uv event loop.
*/
NODE_EXTERN void Initialize(const std::string& program_name = "node_lib_executable");
NODE_EXTERN void Initialize(const std::string& program_name = "node_lib_executable", const std::vector<std::string>& node_args = {});


/*
Expand Down