Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3285,6 +3285,35 @@ callback invocation, even if it has been successfully cancelled.

## Version Management

### napi_get_node_version
<!-- YAML
added: REPLACEME
-->

```C
typedef struct {
uint32_t major;
uint32_t minor;
uint32_t patch;
const char* release;
} napi_node_version;
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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. :)


NAPI_EXTERN
napi_status napi_get_node_version(napi_env env,
const napi_node_version** version);
```

- `[in] env`: The environment that the API is invoked under.
- `[out] version`: A pointer to version information for Node itself.

Returns `napi_ok` if the API succeeded.

This function fills the `version` struct with the major, minor and patch version
of Node that is currently running, and the `release` field with the
value of [`process.release.name`][`process.release`].

The returned buffer is statically allocated and does not need to be freed.

### napi_get_version
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -3368,3 +3397,5 @@ support it:
[`napi_throw_type_error`]: #n_api_napi_throw_type_error
[`napi_unwrap`]: #n_api_napi_unwrap
[`napi_wrap`]: #n_api_napi_wrap

[`process.release`]: process.html#process_process_release
3 changes: 2 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3266,7 +3266,8 @@ void SetupProcessObject(Environment* env,
// process.release
Local<Object> release = Object::New(env->isolate());
READONLY_PROPERTY(process, "release", release);
READONLY_PROPERTY(release, "name", OneByteString(env->isolate(), "node"));
READONLY_PROPERTY(release, "name",
OneByteString(env->isolate(), NODE_RELEASE));

// if this is a release build and no explicit base has been set
// substitute the standard release download URL
Expand Down
14 changes: 14 additions & 0 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3119,6 +3119,20 @@ 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,
const napi_node_version** result) {
CHECK_ENV(env);
CHECK_ARG(env, result);
static const napi_node_version version = {
NODE_MAJOR_VERSION,
NODE_MINOR_VERSION,
NODE_PATCH_VERSION,
NODE_RELEASE
};
*result = &version;
Copy link
Contributor

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.

Copy link
Member Author

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?

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.

Copy link
Contributor

@kfarnung kfarnung Aug 8, 2017

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.

return napi_clear_last_error(env);
}

namespace uvimpl {

static napi_status ConvertUVErrorCode(int code) {
Expand Down
4 changes: 4 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
// version management
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);

NAPI_EXTERN
napi_status napi_get_node_version(napi_env env,
const napi_node_version** version);

EXTERN_C_END

#endif // SRC_NODE_API_H_
7 changes: 7 additions & 0 deletions src/node_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ typedef struct {
napi_status error_code;
} napi_extended_error_info;

typedef struct {
uint32_t major;
uint32_t minor;
uint32_t patch;
const char* release;
} napi_node_version;

#endif // SRC_NODE_API_TYPES_H_
4 changes: 4 additions & 0 deletions src/node_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#define NODE_STRINGIFY_HELPER(n) #n
#endif

#ifndef NODE_RELEASE
#define NODE_RELEASE "node"
#endif

#ifndef NODE_TAG
# if NODE_VERSION_IS_RELEASE
# define NODE_TAG ""
Expand Down
5 changes: 5 additions & 0 deletions test/addons-napi/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ assert.ok(test_general.testGetPrototype(baseObject) !==
// expected version is currently 1
assert.strictEqual(test_general.testGetVersion(), 1);

const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
`v${major}.${minor}.${patch}`);
assert.strictEqual(release, process.release.name);

[
123,
'test string',
Expand Down
20 changes: 20 additions & 0 deletions test/addons-napi/test_general/test_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ napi_value testGetVersion(napi_env env, napi_callback_info info) {
return result;
}

napi_value testGetNodeVersion(napi_env env, napi_callback_info info) {
const napi_node_version* node_version;
napi_value result, major, minor, patch, release;
NAPI_CALL(env, napi_get_node_version(env, &node_version));
NAPI_CALL(env, napi_create_uint32(env, node_version->major, &major));
NAPI_CALL(env, napi_create_uint32(env, node_version->minor, &minor));
NAPI_CALL(env, napi_create_uint32(env, node_version->patch, &patch));
NAPI_CALL(env, napi_create_string_utf8(env,
node_version->release,
(size_t)-1,
&release));
NAPI_CALL(env, napi_create_array_with_length(env, 4, &result));
NAPI_CALL(env, napi_set_element(env, result, 0, major));
NAPI_CALL(env, napi_set_element(env, result, 1, minor));
NAPI_CALL(env, napi_set_element(env, result, 2, patch));
NAPI_CALL(env, napi_set_element(env, result, 3, release));
return result;
}

napi_value doInstanceOf(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
Expand Down Expand Up @@ -142,6 +161,7 @@ void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype),
DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion),
DECLARE_NAPI_PROPERTY("testGetNodeVersion", testGetNodeVersion),
DECLARE_NAPI_PROPERTY("doInstanceOf", doInstanceOf),
DECLARE_NAPI_PROPERTY("getUndefined", getUndefined),
DECLARE_NAPI_PROPERTY("getNull", getNull),
Expand Down