Repository navigation
The version list should be sorted by name #45630
Description
Activity
Any easy ways to sort this macro in
cpp?#define NODE_VERSIONS_KEYS(V) \ NODE_VERSIONS_KEYS_BASE(V) \ NODE_VERSIONS_KEY_CRYPTO(V) \ NODE_VERSIONS_KEY_INTL(V) \ NODE_VERSIONS_KEY_QUIC(V)
Object.keys(process.versions).sort().reduce((r, k) => (r[k] = process.versions[k], r), {});
or
Object.fromEntries(Object.entries(process.versions).sort((a,b)=>a<b?-1:1))
```js Object.keys(process.versions).sort().reduce((r, k) => (r[k] = process.versions[k], r), {});
or
Object.fromEntries(Object.entries(process.versions).sort((a,b)=>a<b?-1:1))
Sorry. This is not JS. I was in an expectation like,
#definewill be sufficient for a programmers to understand that it's aC++code.Editing the above comment.
Any easy ways to sort this macro in cpp?
No, unfortunately. What you could do instead is:
-
write out the READONLY_STRING_PROPERTY calls in src/node_process_object.cc by hand, or
-
store the keys and values in an array and qsort/std::sort at runtime, or
-
write a j2sc-like script that spits out code that is then incorporated in the final build
(1) is kind of fragile and a maintenance drag. (2) feels meh. (3) increases total build time, possibly by a lot.
A Sufficiently Smart Compiler would do the sorting in (2) at compile time. Unlikely, though.
The runtime version string munging in src/node_metata.cc is pretty inefficient. Likely doesn't matter because we usually deserialize from snapshot but if it ran every time, that'd be a good argument for going with (3).
Reacted by Jithil P Ponnan, Alexander Mills and Alex Yang-
@MrJithil: Good point!
node/src/node_process_object.cc
Lines 112 to 118 in 71ff89f
#define V(key) \ if (!per_process::metadata.versions.key.empty()) { \ READONLY_STRING_PROPERTY( \ versions, #key, per_process::metadata.versions.key); \ } NODE_VERSIONS_KEYS(V) #undef V I think we could do here before it becomes a read-only object
Subject: [PATCH] src: sort versions --- Index: src/node_process_object.cc IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/node_process_object.cc b/src/node_process_object.cc --- a/src/node_process_object.cc (revision 2a29df64645a70bbb833298423a29206c4ec6a2e) +++ b/src/node_process_object.cc (date 1675098681432) @@ -107,7 +107,6 @@ // process.versions Local<Object> versions = Object::New(isolate); - READONLY_PROPERTY(process, "versions", versions); #define V(key) \ if (!per_process::metadata.versions.key.empty()) { \ @@ -116,6 +115,9 @@ } NODE_VERSIONS_KEYS(V) #undef V + // todo: sort the keys except node + + READONLY_PROPERTY(process, "versions", versions); // process.arch READONLY_STRING_PROPERTY(process, "arch", per_process::metadata.arch);
See #46428
- added a commit that references this issue
on Feb 18, 2023 - added 2 commits that reference this issue
on Feb 18, 2023 - added a commit that references this issue
on Apr 11, 2023
The version list should be sorted.
Current Behaviour:
Problem with the current order:
Since the current version list is not sorted, it's hard to compare with the source code (mainly the
depsfolder).Solution:
Except the
node, sort all other versions by name.Eg: