1 #ifndef SRC_NODE_API_H_ 2 #define SRC_NODE_API_H_ 3 4 #ifdef BUILDING_NODE_EXTENSION 5 #ifdef _WIN32 6 // Building native module against node 7 #define NAPI_EXTERN __declspec(dllimport) 8 #elif defined(__wasm32__) 9 #define NAPI_EXTERN __attribute__((__import_module__("napi"))) 10 #endif 11 #endif 12 #include "js_native_api.h" 13 #include "node_api_types.h" 14 15 struct uv_loop_s; // Forward declaration. 16 17 #ifdef _WIN32 18 # define NAPI_MODULE_EXPORT __declspec(dllexport) 19 #else 20 # define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) 21 #endif 22 23 #if defined(__GNUC__) 24 # define NAPI_NO_RETURN __attribute__((noreturn)) 25 #elif defined(_WIN32) 26 # define NAPI_NO_RETURN __declspec(noreturn) 27 #else 28 # define NAPI_NO_RETURN 29 #endif 30 31 typedef napi_value (*napi_addon_register_func)(napi_env env, 32 napi_value exports); 33 34 typedef struct { 35 int nm_version; 36 unsigned int nm_flags; 37 const char* nm_filename; 38 napi_addon_register_func nm_register_func; 39 const char* nm_modname; 40 void* nm_priv; 41 void* reserved[4]; 42 } napi_module; 43 44 #define NAPI_MODULE_VERSION 1 45 46 #if defined(_MSC_VER) 47 #pragma section(".CRT$XCU", read) 48 #define NAPI_C_CTOR(fn) \ 49 static void __cdecl fn(void); \ 50 __declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \ 51 fn; \ 52 static void __cdecl fn(void) 53 #else 54 #define NAPI_C_CTOR(fn) \ 55 static void fn(void) __attribute__((constructor)); \ 56 static void fn(void) 57 #endif 58 59 #define NAPI_MODULE_X(modname, regfunc, priv, flags) \ 60 EXTERN_C_START \ 61 static napi_module _module = \ 62 { \ 63 NAPI_MODULE_VERSION, \ 64 flags, \ 65 __FILE__, \ 66 regfunc, \ 67 #modname, \ 68 priv, \ 69 {0}, \ 70 }; \ 71 NAPI_C_CTOR(_register_ ## modname) { \ 72 napi_module_register(&_module); \ 73 } \ 74 EXTERN_C_END 75 76 #define NAPI_MODULE_INITIALIZER_X(base, version) \ 77 NAPI_MODULE_INITIALIZER_X_HELPER(base, version) 78 #define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version 79 80 #ifdef __wasm32__ 81 #define NAPI_WASM_INITIALIZER \ 82 NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION) 83 #define NAPI_MODULE(modname, regfunc) \ 84 EXTERN_C_START \ 85 NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \ 86 napi_value exports) { \ 87 return regfunc(env, exports); \ 88 } \ 89 EXTERN_C_END 90 #else 91 #define NAPI_MODULE(modname, regfunc) \ 92 NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage) 93 #endif 94 95 #define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v 96 97 #define NAPI_MODULE_INITIALIZER \ 98 NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, \ 99 NAPI_MODULE_VERSION) 100 101 #define NAPI_MODULE_INIT() \ 102 EXTERN_C_START \ 103 NAPI_MODULE_EXPORT napi_value \ 104 NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports); \ 105 EXTERN_C_END \ 106 NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \ 107 napi_value NAPI_MODULE_INITIALIZER(napi_env env, \ 108 napi_value exports) 109 110 EXTERN_C_START 111 112 NAPI_EXTERN void napi_module_register(napi_module* mod); 113 114 NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, 115 size_t location_len, 116 const char* message, 117 size_t message_len); 118 119 // Methods for custom handling of async operations 120 NAPI_EXTERN napi_status napi_async_init(napi_env env, 121 napi_value async_resource, 122 napi_value async_resource_name, 123 napi_async_context* result); 124 125 NAPI_EXTERN napi_status napi_async_destroy(napi_env env, 126 napi_async_context async_context); 127 128 NAPI_EXTERN napi_status napi_make_callback(napi_env env, 129 napi_async_context async_context, 130 napi_value recv, 131 napi_value func, 132 size_t argc, 133 const napi_value* argv, 134 napi_value* result); 135 136 // Methods to provide node::Buffer functionality with napi types 137 NAPI_EXTERN napi_status napi_create_buffer(napi_env env, 138 size_t length, 139 void** data, 140 napi_value* result); 141 NAPI_EXTERN napi_status napi_create_external_buffer(napi_env env, 142 size_t length, 143 void* data, 144 napi_finalize finalize_cb, 145 void* finalize_hint, 146 napi_value* result); 147 NAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env, 148 size_t length, 149 const void* data, 150 void** result_data, 151 napi_value* result); 152 NAPI_EXTERN napi_status napi_is_buffer(napi_env env, 153 napi_value value, 154 bool* result); 155 NAPI_EXTERN napi_status napi_get_buffer_info(napi_env env, 156 napi_value value, 157 void** data, 158 size_t* length); 159 160 // Methods to manage simple async operations 161 NAPI_EXTERN 162 napi_status napi_create_async_work(napi_env env, 163 napi_value async_resource, 164 napi_value async_resource_name, 165 napi_async_execute_callback execute, 166 napi_async_complete_callback complete, 167 void* data, 168 napi_async_work* result); 169 NAPI_EXTERN napi_status napi_delete_async_work(napi_env env, 170 napi_async_work work); 171 NAPI_EXTERN napi_status napi_queue_async_work(napi_env env, 172 napi_async_work work); 173 NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, 174 napi_async_work work); 175 176 // version management 177 NAPI_EXTERN 178 napi_status napi_get_node_version(napi_env env, 179 const napi_node_version** version); 180 181 #if NAPI_VERSION >= 2 182 183 // Return the current libuv event loop for a given environment 184 NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, 185 struct uv_loop_s** loop); 186 187 #endif // NAPI_VERSION >= 2 188 189 #if NAPI_VERSION >= 3 190 191 NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err); 192 193 NAPI_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env, 194 void (*fun)(void* arg), 195 void* arg); 196 197 NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env, 198 void (*fun)(void* arg), 199 void* arg); 200 201 NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, 202 napi_value resource_object, 203 napi_async_context context, 204 napi_callback_scope* result); 205 206 NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, 207 napi_callback_scope scope); 208 209 #endif // NAPI_VERSION >= 3 210 211 #if NAPI_VERSION >= 4 212 213 #ifndef __wasm32__ 214 // Calling into JS from other threads 215 NAPI_EXTERN napi_status 216 napi_create_threadsafe_function(napi_env env, 217 napi_value func, 218 napi_value async_resource, 219 napi_value async_resource_name, 220 size_t max_queue_size, 221 size_t initial_thread_count, 222 void* thread_finalize_data, 223 napi_finalize thread_finalize_cb, 224 void* context, 225 napi_threadsafe_function_call_js call_js_cb, 226 napi_threadsafe_function* result); 227 228 NAPI_EXTERN napi_status 229 napi_get_threadsafe_function_context(napi_threadsafe_function func, 230 void** result); 231 232 NAPI_EXTERN napi_status 233 napi_call_threadsafe_function(napi_threadsafe_function func, 234 void* data, 235 napi_threadsafe_function_call_mode is_blocking); 236 237 NAPI_EXTERN napi_status 238 napi_acquire_threadsafe_function(napi_threadsafe_function func); 239 240 NAPI_EXTERN napi_status 241 napi_release_threadsafe_function(napi_threadsafe_function func, 242 napi_threadsafe_function_release_mode mode); 243 244 NAPI_EXTERN napi_status 245 napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func); 246 247 NAPI_EXTERN napi_status 248 napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func); 249 #endif // __wasm32__ 250 251 #endif // NAPI_VERSION >= 4 252 253 #ifdef NAPI_EXPERIMENTAL 254 255 NAPI_EXTERN napi_status napi_add_async_cleanup_hook( 256 napi_env env, 257 napi_async_cleanup_hook hook, 258 void* arg, 259 napi_async_cleanup_hook_handle* remove_handle); 260 261 NAPI_EXTERN napi_status napi_remove_async_cleanup_hook( 262 napi_async_cleanup_hook_handle remove_handle); 263 264 #endif // NAPI_EXPERIMENTAL 265 266 EXTERN_C_END 267 268 #endif // SRC_NODE_API_H_ 269