forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for cmd args #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8c1c97a
Add support for cmd args
luminosuslight 5cd57e0
Move setCmdArgs code to ctor
luminosuslight cc791b9
Fix cmd options
luminosuslight 6d08c7b
Fix visibility and comments in CmdArgs
luminosuslight cad6d94
Improve documentation
luminosuslight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
| * @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; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two don't need to be public.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| }; | ||
|
|
||
| ArrayBufferAllocator* allocator; | ||
|
|
@@ -5015,8 +5047,8 @@ void deleteCmdArgs() { | |
| if (!cmd_args) { | ||
| return; | ||
| } | ||
| delete[] cmd_args->argv; | ||
| delete cmd_args; | ||
| cmd_args = nullptr; | ||
| } | ||
|
|
||
| int _StopEnv() { | ||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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