src: iterate metadata version entries with std::array

This commit is contained in:
Chengzhong Wu 2025-08-15 10:45:30 +01:00
parent 93a414d3c1
commit 3f719bb92a
5 changed files with 37 additions and 43 deletions

View file

@ -161,6 +161,28 @@ Metadata::Versions::Versions() {
nbytes = NBYTES_VERSION;
}
std::array<std::pair<std::string_view, std::string_view>,
NODE_VERSIONS_KEY_COUNT>
Metadata::Versions::pairs() const {
std::array<std::pair<std::string_view, std::string_view>,
NODE_VERSIONS_KEY_COUNT>
versions_array;
auto slot = versions_array.begin();
#define V(key) \
do { \
*slot++ = std::pair<std::string_view, std::string_view>( \
#key, per_process::metadata.versions.key); \
} while (0);
NODE_VERSIONS_KEYS(V)
#undef V
std::ranges::sort(versions_array,
[](auto& a, auto& b) { return a.first < b.first; });
return versions_array;
}
Metadata::Release::Release() : name(NODE_RELEASE) {
#if NODE_VERSION_IS_LTS
lts = NODE_VERSION_LTS_CODENAME;

View file

@ -3,7 +3,9 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <array>
#include <string>
#include <utility>
#include "node_version.h"
#if HAVE_OPENSSL
@ -98,6 +100,10 @@ namespace node {
NODE_VERSIONS_KEY_QUIC(V) \
NODE_VERSIONS_KEY_SQLITE(V)
#define V(key) +1
constexpr int NODE_VERSIONS_KEY_COUNT = NODE_VERSIONS_KEYS(V);
#undef V
class Metadata {
public:
Metadata();
@ -118,6 +124,10 @@ class Metadata {
#define V(key) std::string key;
NODE_VERSIONS_KEYS(V)
#undef V
std::array<std::pair<std::string_view, std::string_view>,
NODE_VERSIONS_KEY_COUNT>
pairs() const;
};
struct Release {

View file

@ -83,24 +83,7 @@ static void SetVersions(Isolate* isolate, Local<Object> versions) {
READONLY_STRING_PROPERTY(
versions, "node", per_process::metadata.versions.node);
#define V(key) +1
std::pair<std::string_view, std::string_view>
versions_array[NODE_VERSIONS_KEYS(V)];
#undef V
auto* slot = &versions_array[0];
#define V(key) \
do { \
*slot++ = std::pair<std::string_view, std::string_view>( \
#key, per_process::metadata.versions.key); \
} while (0);
NODE_VERSIONS_KEYS(V)
#undef V
std::ranges::sort(versions_array,
[](auto& a, auto& b) { return a.first < b.first; });
for (const auto& version : versions_array) {
for (const auto& version : per_process::metadata.versions.pairs()) {
versions
->DefineOwnProperty(context,
OneByteString(isolate, version.first),

View file

@ -797,24 +797,7 @@ static void PrintComponentVersions(JSONWriter* writer) {
writer->json_objectstart("componentVersions");
#define V(key) +1
std::pair<std::string_view, std::string_view>
versions_array[NODE_VERSIONS_KEYS(V)];
#undef V
auto* slot = &versions_array[0];
#define V(key) \
do { \
*slot++ = std::pair<std::string_view, std::string_view>( \
#key, per_process::metadata.versions.key); \
} while (0);
NODE_VERSIONS_KEYS(V)
#undef V
std::ranges::sort(versions_array,
[](auto& a, auto& b) { return a.first < b.first; });
for (const auto& version : versions_array) {
for (const auto& version : per_process::metadata.versions.pairs()) {
writer->json_keyvalue(version.first, version.second);
}

View file

@ -239,13 +239,9 @@ std::unique_ptr<v8::ConvertableToTraceFormat> AsyncWrapArgs::Cast() const {
std::unique_ptr<v8::ConvertableToTraceFormat> ProcessMeta::Cast() const {
auto trace_process = tracing::TracedValue::Create();
trace_process->BeginDictionary("versions");
#define V(key) \
trace_process->SetString(#key, per_process::metadata.versions.key.c_str());
NODE_VERSIONS_KEYS(V)
#undef V
for (const auto& version : per_process::metadata.versions.pairs()) {
trace_process->SetString(version.first.data(), version.second.data());
}
trace_process->EndDictionary();
trace_process->SetString("arch", per_process::metadata.arch.c_str());