Skip to content

Commit 45139e5

Browse files
mhdawsonjasnell
authored andcommitted
n-api: add napi_get_version
Add napi_get_version function so that addons can query the level of N-API supported. PR-URL: #13207 Fixes: nodejs/abi-stable-node#231 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jason Ginchereau <[email protected]>
1 parent e7d098c commit 45139e5

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

doc/api/n-api.md

+29
Original file line numberDiff line numberDiff line change
@@ -2946,6 +2946,35 @@ the `complete` callback will be invoked with a status value of
29462946
`napi_cancelled`. The work should not be deleted before the `complete`
29472947
callback invocation, even if it has been successfully cancelled.
29482948

2949+
## Version Management
2950+
2951+
### napi_get_version
2952+
<!-- YAML
2953+
added: REPLACEME
2954+
-->
2955+
```C
2956+
NAPI_EXTERN napi_status napi_get_version(napi_env env,
2957+
uint32_t* result);
2958+
```
2959+
2960+
- `[in] env`: The environment that the API is invoked under.
2961+
- `[out] result`: The highest version of N-API supported.
2962+
2963+
Returns `napi_ok` if the API succeeded.
2964+
2965+
This API returns the highest N-API version supported by the
2966+
Node.js runtime. N-API is planned to be additive such that
2967+
newer releases of Node.js may support additional API functions.
2968+
In order to allow an addon to use a newer function when running with
2969+
versions of Node.js that support it, while providing
2970+
fallback behavior when running with Node.js versions that don't
2971+
support it:
2972+
2973+
* Call `napi_get_version()` to determine if the API is available.
2974+
* If available, dynamically load a pointer to the function using `uv_dlsym()`.
2975+
* Use the dynamically loaded pointer to invoke the function.
2976+
* If the function is not available, provide an alternate implementation
2977+
that does not use the function.
29492978
29502979
[Aynchronous Operations]: #n_api_asynchronous_operations
29512980
[Basic N-API Data Types]: #n_api_basic_n_api_data_types

src/node_api.cc

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "uv.h"
1919
#include "node_api.h"
2020

21+
#define NAPI_VERSION 1
22+
2123
static
2224
napi_status napi_set_last_error(napi_env env, napi_status error_code,
2325
uint32_t engine_error_code = 0,
@@ -2713,6 +2715,13 @@ napi_status napi_get_typedarray_info(napi_env env,
27132715
return napi_clear_last_error(env);
27142716
}
27152717

2718+
napi_status napi_get_version(napi_env env, uint32_t* result) {
2719+
CHECK_ENV(env);
2720+
CHECK_ARG(env, result);
2721+
*result = NAPI_VERSION;
2722+
return napi_clear_last_error(env);
2723+
}
2724+
27162725
namespace uvimpl {
27172726

27182727
static napi_status ConvertUVErrorCode(int code) {

src/node_api.h

+4
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,10 @@ NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
478478
NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
479479
napi_async_work work);
480480

481+
482+
// version management
483+
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);
484+
481485
EXTERN_C_END
482486

483487
#endif // SRC_NODE_API_H_

test/addons-napi/test_general/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ assert.strictEqual(test_general.testGetPrototype(extendedObject),
3030
assert.ok(test_general.testGetPrototype(baseObject) !==
3131
test_general.testGetPrototype(extendedObject),
3232
'Prototypes for base and extended should be different');
33+
34+
// test version management funcitons
35+
// expected version is currently 1
36+
assert.strictEqual(test_general.testGetVersion(), 1);

test/addons-napi/test_general/test_general.c

+9
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ napi_value testGetPrototype(napi_env env, napi_callback_info info) {
2525
return result;
2626
}
2727

28+
napi_value testGetVersion(napi_env env, napi_callback_info info) {
29+
uint32_t version;
30+
napi_value result;
31+
NAPI_CALL(env, napi_get_version(env, &version));
32+
NAPI_CALL(env ,napi_create_number(env, version, &result));
33+
return result;
34+
}
35+
2836
void Init(napi_env env, napi_value exports, napi_value module, void* priv) {
2937
napi_property_descriptor descriptors[] = {
3038
DECLARE_NAPI_PROPERTY("testStrictEquals", testStrictEquals),
3139
DECLARE_NAPI_PROPERTY("testGetPrototype", testGetPrototype),
40+
DECLARE_NAPI_PROPERTY("testGetVersion", testGetVersion),
3241
};
3342

3443
NAPI_CALL_RETURN_VOID(env, napi_define_properties(

0 commit comments

Comments
 (0)