File: test_general.c

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (52 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#define NAPI_VERSION 9
// we define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED here to validate that it can
// be used as a form of test itself. It is
// not related to any of the other tests
// defined in the file
#define NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
#include <node_api.h>
#include <stdlib.h>
#include "../../js-native-api/common.h"

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

static napi_value GetFilename(napi_env env, napi_callback_info info) {
  const char* filename;
  napi_value result;

  NODE_API_CALL(env, node_api_get_module_file_name(env, &filename));
  NODE_API_CALL(env,
      napi_create_string_utf8(env, filename, NAPI_AUTO_LENGTH, &result));

  return result;
}

static napi_value Init(napi_env env, napi_value exports) {
  napi_property_descriptor descriptors[] = {
    DECLARE_NODE_API_PROPERTY("testGetNodeVersion", testGetNodeVersion),
    DECLARE_NODE_API_GETTER("filename", GetFilename),
  };

  NODE_API_CALL(env, napi_define_properties(
      env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));

  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)