Skip to content

Commit 5ccaba4

Browse files
danbevjasnell
authored andcommitted
test: add variable arguments support for Argv
At the moment Argv only supports three arguments which fulfilled my requirements when working on #9163. This commit adds support for a variable number of arguments. There is also a no-args constructor that is the equivalent to running "node -p process.version" which is hopefully alright as a default. PR-URL: #12166 Ref: #9163 Reviewed-By: James M Snell <[email protected]>
1 parent 503342d commit 5ccaba4

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

test/cctest/node_test_fixture.h

+21-11
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,27 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
3232

3333
struct Argv {
3434
public:
35-
Argv(const char* prog, const char* arg1, const char* arg2) {
36-
int prog_len = strlen(prog) + 1;
37-
int arg1_len = strlen(arg1) + 1;
38-
int arg2_len = strlen(arg2) + 1;
39-
argv_ = static_cast<char**>(malloc(3 * sizeof(char*)));
40-
argv_[0] = static_cast<char*>(malloc(prog_len + arg1_len + arg2_len));
41-
snprintf(argv_[0], prog_len, "%s", prog);
42-
snprintf(argv_[0] + prog_len, arg1_len, "%s", arg1);
43-
snprintf(argv_[0] + prog_len + arg1_len, arg2_len, "%s", arg2);
44-
argv_[1] = argv_[0] + prog_len;
45-
argv_[2] = argv_[0] + prog_len + arg1_len;
35+
Argv() : Argv({"node", "-p", "process.version"}) {}
36+
37+
Argv(const std::initializer_list<const char*> &args) {
38+
int nrArgs = args.size();
39+
int totalLen = 0;
40+
for (auto it = args.begin(); it != args.end(); ++it) {
41+
totalLen += strlen(*it) + 1;
42+
}
43+
argv_ = static_cast<char**>(malloc(nrArgs * sizeof(char*)));
44+
argv_[0] = static_cast<char*>(malloc(totalLen));
45+
int i = 0;
46+
int offset = 0;
47+
for (auto it = args.begin(); it != args.end(); ++it, ++i) {
48+
int len = strlen(*it) + 1;
49+
snprintf(argv_[0] + offset, len, "%s", *it);
50+
// Skip argv_[0] as it points the correct location already
51+
if (i > 0) {
52+
argv_[i] = argv_[0] + offset;
53+
}
54+
offset += len;
55+
}
4656
}
4757

4858
~Argv() {

0 commit comments

Comments
 (0)