From 7deaa3be174339daca7f8c9995fa09ce4e050e49 Mon Sep 17 00:00:00 2001 From: himself65 Date: Fri, 3 Feb 2023 17:04:23 -0600 Subject: [PATCH 1/5] process: print versions by sort Co-authored-by: James M Snell Co-authored-by: Anna Henningsen --- src/node_process_object.cc | 31 +++++++++++++++++++++----- src/node_report.cc | 15 ++++++++++++- test/parallel/test-process-versions.js | 5 +++-- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 26137396384489..95c466f515b5fb 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -105,18 +105,37 @@ MaybeLocal CreateProcessObject(Realm* realm) { READONLY_PROPERTY( process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION)); - // process.versions Local versions = Object::New(isolate); - READONLY_PROPERTY(process, "versions", versions); + // Node.js version is always on the top + READONLY_STRING_PROPERTY( + versions, "node", per_process::metadata.versions.node); + std::vector> versions_vec; #define V(key) \ - if (!per_process::metadata.versions.key.empty()) { \ - READONLY_STRING_PROPERTY( \ - versions, #key, per_process::metadata.versions.key); \ - } + do { \ + versions_vec.emplace_back(#key, per_process::metadata.versions.key); \ + } while (0); NODE_VERSIONS_KEYS(V) #undef V + std::sort(versions_vec.begin(), versions_vec.end(), [](auto& a, auto& b) { + return a.first < b.first; + }); + + for (const auto& version : versions_vec) { + versions + ->DefineOwnProperty( + context, + OneByteString(isolate, version.first.data(), version.first.size()), + OneByteString( + isolate, version.second.data(), version.second.size()), + v8::ReadOnly) + .Check(); + } + + // process.versions + READONLY_PROPERTY(process, "versions", versions); + // process.arch READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch); diff --git a/src/node_report.cc b/src/node_report.cc index a6622b49a0e0ba..9c616e867a8776 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -793,11 +793,24 @@ static void PrintComponentVersions(JSONWriter* writer) { std::stringstream buf; writer->json_objectstart("componentVersions"); + std::vector> versions_vec; -#define V(key) writer->json_keyvalue(#key, per_process::metadata.versions.key); +#define V(key) \ + do { \ + versions_vec.emplace_back( \ + std::make_pair(#key, per_process::metadata.versions.key)); \ + } while (0); NODE_VERSIONS_KEYS(V) #undef V + std::sort(versions_vec.begin(), versions_vec.end(), [](auto& a, auto& b) { + return a.first < b.first; + }); + + for (const auto& version : versions_vec) { + writer->json_keyvalue(version.first, version.second); + } + writer->json_objectend(); } diff --git a/test/parallel/test-process-versions.js b/test/parallel/test-process-versions.js index de55405e46d341..23590d58a587d9 100644 --- a/test/parallel/test-process-versions.js +++ b/test/parallel/test-process-versions.js @@ -9,7 +9,6 @@ const expected_keys = [ 'ares', 'brotli', 'modules', - 'node', 'uv', 'v8', 'zlib', @@ -45,7 +44,9 @@ if (common.hasIntl) { } expected_keys.sort(); -const actual_keys = Object.keys(process.versions).sort(); +expected_keys.unshift('node'); + +const actual_keys = Object.keys(process.versions); assert.deepStrictEqual(actual_keys, expected_keys); From 68c03fb3a170e8adc380188091df96f558a15c93 Mon Sep 17 00:00:00 2001 From: himself65 Date: Fri, 3 Feb 2023 17:19:54 -0600 Subject: [PATCH 2/5] fixup!: code --- src/node_process_object.cc | 17 +++++++++++------ src/node_report.cc | 17 ++++++++++------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 95c466f515b5fb..03eb0256a4544a 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -109,20 +109,25 @@ MaybeLocal CreateProcessObject(Realm* realm) { // Node.js version is always on the top READONLY_STRING_PROPERTY( versions, "node", per_process::metadata.versions.node); - std::vector> versions_vec; + +#define V(key) +1 + std::pair + versions_array[NODE_VERSIONS_KEYS(V)]; +#undef V + auto* slot = &versions_array[0]; #define V(key) \ do { \ - versions_vec.emplace_back(#key, per_process::metadata.versions.key); \ + *slot++ = std::make_pair(#key, per_process::metadata.versions.key); \ } while (0); NODE_VERSIONS_KEYS(V) #undef V - std::sort(versions_vec.begin(), versions_vec.end(), [](auto& a, auto& b) { - return a.first < b.first; - }); + std::sort(&versions_array[0], + &versions_array[arraysize(versions_array)], + [](auto& a, auto& b) { return a.first < b.first; }); - for (const auto& version : versions_vec) { + for (const auto& version : versions_array) { versions ->DefineOwnProperty( context, diff --git a/src/node_report.cc b/src/node_report.cc index 9c616e867a8776..2998bd09b049fd 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -793,21 +793,24 @@ static void PrintComponentVersions(JSONWriter* writer) { std::stringstream buf; writer->json_objectstart("componentVersions"); - std::vector> versions_vec; + +#define V(key) +1 + std::pair versions_array[NODE_VERSIONS_KEYS(V)]; +#undef V + auto* slot = &versions_array[0]; #define V(key) \ do { \ - versions_vec.emplace_back( \ - std::make_pair(#key, per_process::metadata.versions.key)); \ + *slot++ = std::make_pair(#key, per_process::metadata.versions.key); \ } while (0); NODE_VERSIONS_KEYS(V) #undef V - std::sort(versions_vec.begin(), versions_vec.end(), [](auto& a, auto& b) { - return a.first < b.first; - }); + std::sort(&versions_array[0], + &versions_array[arraysize(versions_array)], + [](auto& a, auto& b) { return a.first < b.first; }); - for (const auto& version : versions_vec) { + for (const auto& version : versions_array) { writer->json_keyvalue(version.first, version.second); } From f3d87949bdda4fb1b9834e8a6272aed0a841bf5d Mon Sep 17 00:00:00 2001 From: himself65 Date: Sat, 4 Feb 2023 15:10:17 -0600 Subject: [PATCH 3/5] fixup!: code --- src/node_process_object.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 03eb0256a4544a..99b5201c1a7fd6 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -118,7 +118,7 @@ MaybeLocal CreateProcessObject(Realm* realm) { #define V(key) \ do { \ - *slot++ = std::make_pair(#key, per_process::metadata.versions.key); \ + *slot++ = std::make_pair(#key, per_process::metadata.versions.key); \ } while (0); NODE_VERSIONS_KEYS(V) #undef V From aae0ecfef871b5535ce542cd835e7b2792925ef3 Mon Sep 17 00:00:00 2001 From: himself65 Date: Sat, 4 Feb 2023 15:10:32 -0600 Subject: [PATCH 4/5] fixup!: format --- src/node_process_object.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 99b5201c1a7fd6..925643786b968d 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -118,7 +118,8 @@ MaybeLocal CreateProcessObject(Realm* realm) { #define V(key) \ do { \ - *slot++ = std::make_pair(#key, per_process::metadata.versions.key); \ + *slot++ = std::make_pair( \ + #key, per_process::metadata.versions.key); \ } while (0); NODE_VERSIONS_KEYS(V) #undef V From 73a1163802da736c9790d5fccfb255af7a912a3a Mon Sep 17 00:00:00 2001 From: himself65 Date: Sat, 4 Feb 2023 15:40:33 -0600 Subject: [PATCH 5/5] fixup!: format --- src/node_process_object.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_process_object.cc b/src/node_process_object.cc index 925643786b968d..274f1f01de8d84 100644 --- a/src/node_process_object.cc +++ b/src/node_process_object.cc @@ -118,7 +118,7 @@ MaybeLocal CreateProcessObject(Realm* realm) { #define V(key) \ do { \ - *slot++ = std::make_pair( \ + *slot++ = std::pair( \ #key, per_process::metadata.versions.key); \ } while (0); NODE_VERSIONS_KEYS(V)