File: common-inl.h

package info (click to toggle)
nodejs 18.20.4%2Bdfsg-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 416,132 kB
  • sloc: perl: 1,766,352; cpp: 1,167,027; ansic: 634,245; asm: 576,575; javascript: 536,356; python: 76,986; sh: 8,334; makefile: 2,929; f90: 641; lisp: 480; xml: 36; awk: 18; ruby: 14; sed: 6
file content (56 lines) | stat: -rw-r--r-- 1,803 bytes parent folder | download | duplicates (3)
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
53
54
55
56
#ifndef JS_NATIVE_API_COMMON_INL_H_
#define JS_NATIVE_API_COMMON_INL_H_

#include <js_native_api.h>
#include "common.h"

#include <stdio.h>

inline void add_returned_status(napi_env env,
                                const char* key,
                                napi_value object,
                                char* expected_message,
                                napi_status expected_status,
                                napi_status actual_status) {
  char napi_message_string[100] = "";
  napi_value prop_value;

  if (actual_status != expected_status) {
    snprintf(napi_message_string,
             sizeof(napi_message_string),
             "Invalid status [%d]",
             actual_status);
  }

  NODE_API_CALL_RETURN_VOID(
      env,
      napi_create_string_utf8(
          env,
          (actual_status == expected_status ? expected_message
                                            : napi_message_string),
          NAPI_AUTO_LENGTH,
          &prop_value));
  NODE_API_CALL_RETURN_VOID(
      env, napi_set_named_property(env, object, key, prop_value));
}

inline void add_last_status(napi_env env,
                            const char* key,
                            napi_value return_value) {
  napi_value prop_value;
  const napi_extended_error_info* p_last_error;
  NODE_API_CALL_RETURN_VOID(env, napi_get_last_error_info(env, &p_last_error));

  NODE_API_CALL_RETURN_VOID(
      env,
      napi_create_string_utf8(
          env,
          (p_last_error->error_message == NULL ? "napi_ok"
                                               : p_last_error->error_message),
          NAPI_AUTO_LENGTH,
          &prop_value));
  NODE_API_CALL_RETURN_VOID(
      env, napi_set_named_property(env, return_value, key, prop_value));
}

#endif  // JS_NATIVE_API_COMMON_INL_H_