• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define NAPI_EXPERIMENTAL
2 // we define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED here to validate that it can
3 // be used as a form of test itself. It is
4 // not related to any of the other tests
5 // defined in the file
6 #define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
7 #include <node_api.h>
8 #include <stdlib.h>
9 #include "../../js-native-api/common.h"
10 
testGetNodeVersion(napi_env env,napi_callback_info info)11 static napi_value testGetNodeVersion(napi_env env, napi_callback_info info) {
12   const napi_node_version* node_version;
13   napi_value result, major, minor, patch, release;
14   NAPI_CALL(env, napi_get_node_version(env, &node_version));
15   NAPI_CALL(env, napi_create_uint32(env, node_version->major, &major));
16   NAPI_CALL(env, napi_create_uint32(env, node_version->minor, &minor));
17   NAPI_CALL(env, napi_create_uint32(env, node_version->patch, &patch));
18   NAPI_CALL(env, napi_create_string_utf8(env,
19                                          node_version->release,
20                                          NAPI_AUTO_LENGTH,
21                                          &release));
22   NAPI_CALL(env, napi_create_array_with_length(env, 4, &result));
23   NAPI_CALL(env, napi_set_element(env, result, 0, major));
24   NAPI_CALL(env, napi_set_element(env, result, 1, minor));
25   NAPI_CALL(env, napi_set_element(env, result, 2, patch));
26   NAPI_CALL(env, napi_set_element(env, result, 3, release));
27   return result;
28 }
29 
GetFilename(napi_env env,napi_callback_info info)30 static napi_value GetFilename(napi_env env, napi_callback_info info) {
31   const char* filename;
32   napi_value result;
33 
34   NAPI_CALL(env, node_api_get_module_file_name(env, &filename));
35   NAPI_CALL(env,
36       napi_create_string_utf8(env, filename, NAPI_AUTO_LENGTH, &result));
37 
38   return result;
39 }
40 
Init(napi_env env,napi_value exports)41 static napi_value Init(napi_env env, napi_value exports) {
42   napi_property_descriptor descriptors[] = {
43     DECLARE_NAPI_PROPERTY("testGetNodeVersion", testGetNodeVersion),
44     DECLARE_NAPI_GETTER("filename", GetFilename),
45   };
46 
47   NAPI_CALL(env, napi_define_properties(
48       env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
49 
50   return exports;
51 }
52 
53 NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
54