Skip to content

Commit

Permalink
process: print versions by sort
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 committed Jan 30, 2023
1 parent 2a29df6 commit affc744
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/node_process_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,37 @@ MaybeLocal<Object> CreateProcessObject(Realm* realm) {
READONLY_PROPERTY(
process, "version", FIXED_ONE_BYTE_STRING(isolate, NODE_VERSION));

// process.versions
Local<Object> 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<std::pair<const char*, std::string>> 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.push_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 strcmp(a.first, b.first) < 0;
});

for (size_t i = 0; i < versions_vec.size(); ++i) {
const auto version = versions_vec[i];
versions
->DefineOwnProperty(context,
OneByteString(isolate, version.first),
ToV8Value(context, version.second).ToLocalChecked(),
v8::ReadOnly)
.Check();
}

// process.versions
READONLY_PROPERTY(process, "versions", versions);

// process.arch
READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);

Expand Down

0 comments on commit affc744

Please sign in to comment.