• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define NAPI_EXPERIMENTAL
2 #include <node_api.h>
3 #include <stdlib.h>
4 #include "../../js-native-api/common.h"
5 
testGetNodeVersion(napi_env env,napi_callback_info info)6 static napi_value testGetNodeVersion(napi_env env, napi_callback_info info) {
7   const napi_node_version* node_version;
8   napi_value result, major, minor, patch, release;
9   NAPI_CALL(env, napi_get_node_version(env, &node_version));
10   NAPI_CALL(env, napi_create_uint32(env, node_version->major, &major));
11   NAPI_CALL(env, napi_create_uint32(env, node_version->minor, &minor));
12   NAPI_CALL(env, napi_create_uint32(env, node_version->patch, &patch));
13   NAPI_CALL(env, napi_create_string_utf8(env,
14                                          node_version->release,
15                                          NAPI_AUTO_LENGTH,
16                                          &release));
17   NAPI_CALL(env, napi_create_array_with_length(env, 4, &result));
18   NAPI_CALL(env, napi_set_element(env, result, 0, major));
19   NAPI_CALL(env, napi_set_element(env, result, 1, minor));
20   NAPI_CALL(env, napi_set_element(env, result, 2, patch));
21   NAPI_CALL(env, napi_set_element(env, result, 3, release));
22   return result;
23 }
24 
GetFilename(napi_env env,napi_callback_info info)25 static napi_value GetFilename(napi_env env, napi_callback_info info) {
26   const char* filename;
27   napi_value result;
28 
29   NAPI_CALL(env, node_api_get_module_file_name(env, &filename));
30   NAPI_CALL(env,
31       napi_create_string_utf8(env, filename, NAPI_AUTO_LENGTH, &result));
32 
33   return result;
34 }
35 
Init(napi_env env,napi_value exports)36 static napi_value Init(napi_env env, napi_value exports) {
37   napi_property_descriptor descriptors[] = {
38     DECLARE_NAPI_PROPERTY("testGetNodeVersion", testGetNodeVersion),
39     DECLARE_NAPI_GETTER("filename", GetFilename),
40   };
41 
42   NAPI_CALL(env, napi_define_properties(
43       env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
44 
45   return exports;
46 }
47 
48 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
49