-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
n-api: add napi_get_node_version #14696
Conversation
doc/api/n-api.md
Outdated
|
||
- `[in] env`: The environment that the API is invoked under. | ||
- `[out] versions`: A pointer to an array of length 3. | ||
- `[out] release`: Optional A string identical to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Period after Optional and after process.release.name
.
src/node_api.cc
Outdated
versions[0] = NODE_MAJOR_VERSION; | ||
versions[1] = NODE_MINOR_VERSION; | ||
versions[2] = NODE_PATCH_VERSION; | ||
if (release != nullptr) *release = NODE_RELEASE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test where release
is null.
src/node_api.cc
Outdated
@@ -3088,6 +3088,18 @@ napi_status napi_get_version(napi_env env, uint32_t* result) { | |||
return napi_clear_last_error(env); | |||
} | |||
|
|||
napi_status napi_get_node_version(napi_env env, | |||
uint32_t* versions, | |||
const char** release) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having a string out parameter is a little bit risky since there needs to be a clear chain of ownership. In this case it will always be a constant string, but as a developer it may not be clear what the scope of the string is and whether it needs to be freed by the caller.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but as a developer it may not be clear what the scope of the string is and whether it needs to be freed by the caller.
I can add a bit to the docs if you have a suggestion for wording, but since this refers to a const char pointer, there isn’t really any ambiguity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @addaleax, you're right about the const char *
being a good indicator, I hadn't really considered that the object would not be a valid argument to free
with the const
modifier. It does seem like all usages of this function would at least yield a global string value, so I think my original concern is invalid. I'll take a look at the documentation and suggest any specific changes there.
src/node_api.cc
Outdated
@@ -3088,6 +3088,18 @@ napi_status napi_get_version(napi_env env, uint32_t* result) { | |||
return napi_clear_last_error(env); | |||
} | |||
|
|||
napi_status napi_get_node_version(napi_env env, | |||
uint32_t* versions, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to use a struct here? I see two benefits:
- No need for the developer to study the documentation to determine the size of the array required
- Easier to access the parts of the version by member name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, that makes sense. I’ll push again in a couple minutes.
b41455a
to
b2e5ccf
Compare
@kfarnung I’ve updated this anyway with the |
NODE_PATCH_VERSION, | ||
NODE_RELEASE | ||
}; | ||
*result = &version; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're returning a pointer to this static struct, should the result
be a const napi_node_version**
to prevent modification by the callers?
I think the other option would be to use a single indirection to a non-const napi_node_version
and then just fill in the values. I'm fine either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're returning a pointer to this static struct, should the
result
be aconst napi_node_version**
to prevent modification by the callers?
Yes. :)
I think the other option would be to use a single indirection to a non-const
napi_node_version
and then just fill in the values. I'm fine either way.
I thought about that, but this approach has the advantage of being extensible on the N-API side in a backwards-compatible fashion, if we or somebody else (Node forkers?) ever need to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed on the current approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
FWIW, libuv has two separate functions for this, one that returns a version string and one that returns Aside: the |
All the
Maybe for this particular case, items 2 and 3 will never be an issue. But still it would be strange if this API did not follow the pattern of all the others that have |
CI: https://ci.nodejs.org/job/node-test-commit/11696/ This should be ready. |
uint32_t minor; | ||
uint32_t patch; | ||
const char* release; | ||
} napi_node_version; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense for this struct to also include the dependency version details equivalent to process.versions
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t like it when N-API exposes things that are already doable by just calling into JS, using, well N-API :) This particular function might be required to check whether some of that calling-into-JS works the way you want it to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, eventually one of the goals should be for Node.js itself to use N-API internally, in which case this method would be the way to get at this information from JS :-) .. but that's down the road.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, eventually one of the goals should be for Node.js itself to use N-API internally
I think N-API inherently adds too much complexity to fully replace the other APIs we use. :)
in which case this method would be the way to get at this information from JS
Adding new methods is easy, and this was implemented specifically in a way that’s easy to extend on the N-API side. :)
31e52f3
to
b23c9f3
Compare
b23c9f3
to
47ff33e
Compare
Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. PR-URL: nodejs#14696 Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
47ff33e
to
ec61a0b
Compare
Landed in 835c383 |
Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. PR-URL: #14696 Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. PR-URL: #14696 Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. PR-URL: nodejs#14696 Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
Add `napi_get_node_version`, to help with feature-detecting Node.js as an environment. Backport-PR-URL: #19447 PR-URL: #14696 Reviewed-By: Kyle Farnung <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
Add
napi_get_node_version
, to help with feature-detecting Node.js as an environment.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
N-API
/cc @nodejs/n-api