-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_api_util.h
More file actions
103 lines (78 loc) · 3.53 KB
/
node_api_util.h
File metadata and controls
103 lines (78 loc) · 3.53 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef NODE_API_UTIL_H
#define NODE_API_UTIL_H
#include <dlfcn.h>
#include "js_native_api.h"
#include "js_native_tsfn.h"
inline bool napiSupportsThreadsafeFunctions(void* dl) {
return dlsym(dl, "napi_create_threadsafe_function") != NULL;
}
#define NAPI_EXPORT __attribute__((visibility("default")))
#define NAPI_PREAMBLE napi_status status;
#define NAPI_CALLBACK_BEGIN(n_args) \
NAPI_PREAMBLE \
napi_value argv[n_args]; \
size_t argc = n_args; \
napi_value jsThis; \
void* data; \
NAPI_GUARD(napi_get_cb_info(env, cbinfo, &argc, argv, &jsThis, &data)) { \
NAPI_THROW_LAST_ERROR \
return NULL; \
}
#define NAPI_ERROR_INFO \
const napi_extended_error_info* error_info = nullptr; \
napi_get_last_error_info(env, &error_info);
#define NAPI_THROW_LAST_ERROR \
NAPI_ERROR_INFO \
napi_throw_error(env, NULL, error_info->error_message);
#ifndef DEBUG
#define NAPI_GUARD(expr) \
status = expr; \
if (status != napi_ok) { \
NAPI_ERROR_INFO \
std::cout << "Node-API returned error: " << status << "\n " << #expr \
<< "\n ^\n " \
<< "at " << __FILE__ << ":" << __LINE__ << std::endl; \
} \
if (status != napi_ok)
#else
#define NAPI_GUARD(expr) \
status = expr; \
if (status != napi_ok)
#endif
#define NAPI_MODULE_REGISTER \
napi_value napi_register_module_v1(napi_env env, napi_value exports)
#define NAPI_FUNCTION(name) \
napi_value JS_##name(napi_env env, napi_callback_info cbinfo)
#define NAPI_FUNCTION_DESC(name) \
{#name, NULL, JS_##name, NULL, NULL, NULL, napi_enumerable, NULL}
namespace nativescript {
inline napi_ref make_ref(napi_env env, napi_value value,
uint32_t initialCount = 1) {
napi_ref ref;
napi_create_reference(env, value, initialCount, &ref);
return ref;
}
inline napi_value get_ref_value(napi_env env, napi_ref ref) {
napi_value value;
napi_get_reference_value(env, ref, &value);
return value;
}
inline void napi_inherits(napi_env env, napi_value ctor,
napi_value super_ctor) {
napi_value global, global_object, set_proto, ctor_proto_prop,
super_ctor_proto_prop;
napi_value argv[2];
napi_get_global(env, &global);
napi_get_named_property(env, global, "Object", &global_object);
napi_get_named_property(env, global_object, "setPrototypeOf", &set_proto);
napi_get_named_property(env, ctor, "prototype", &ctor_proto_prop);
napi_get_named_property(env, super_ctor, "prototype", &super_ctor_proto_prop);
argv[0] = ctor_proto_prop;
argv[1] = super_ctor_proto_prop;
napi_call_function(env, global, set_proto, 2, argv, NULL);
argv[0] = ctor;
argv[1] = super_ctor;
napi_call_function(env, global, set_proto, 2, argv, NULL);
}
} // namespace nativescript
#endif /* NODE_API_UTIL_H */