11cb0ef41Sopenharmony_ci#ifndef SRC_NODE_API_H_ 21cb0ef41Sopenharmony_ci#define SRC_NODE_API_H_ 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ci#ifdef BUILDING_NODE_EXTENSION 51cb0ef41Sopenharmony_ci #ifdef _WIN32 61cb0ef41Sopenharmony_ci // Building native module against node 71cb0ef41Sopenharmony_ci #define NAPI_EXTERN __declspec(dllimport) 81cb0ef41Sopenharmony_ci #elif defined(__wasm32__) 91cb0ef41Sopenharmony_ci #define NAPI_EXTERN __attribute__((__import_module__("napi"))) 101cb0ef41Sopenharmony_ci #endif 111cb0ef41Sopenharmony_ci#endif 121cb0ef41Sopenharmony_ci#include "js_native_api.h" 131cb0ef41Sopenharmony_ci#include "node_api_types.h" 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_cistruct uv_loop_s; // Forward declaration. 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci#ifdef _WIN32 181cb0ef41Sopenharmony_ci# define NAPI_MODULE_EXPORT __declspec(dllexport) 191cb0ef41Sopenharmony_ci#else 201cb0ef41Sopenharmony_ci# define NAPI_MODULE_EXPORT __attribute__((visibility("default"))) 211cb0ef41Sopenharmony_ci#endif 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci#if defined(__GNUC__) 241cb0ef41Sopenharmony_ci# define NAPI_NO_RETURN __attribute__((noreturn)) 251cb0ef41Sopenharmony_ci#elif defined(_WIN32) 261cb0ef41Sopenharmony_ci# define NAPI_NO_RETURN __declspec(noreturn) 271cb0ef41Sopenharmony_ci#else 281cb0ef41Sopenharmony_ci# define NAPI_NO_RETURN 291cb0ef41Sopenharmony_ci#endif 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_citypedef napi_value (*napi_addon_register_func)(napi_env env, 321cb0ef41Sopenharmony_ci napi_value exports); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_citypedef struct napi_module { 351cb0ef41Sopenharmony_ci int nm_version; 361cb0ef41Sopenharmony_ci unsigned int nm_flags; 371cb0ef41Sopenharmony_ci const char* nm_filename; 381cb0ef41Sopenharmony_ci napi_addon_register_func nm_register_func; 391cb0ef41Sopenharmony_ci const char* nm_modname; 401cb0ef41Sopenharmony_ci void* nm_priv; 411cb0ef41Sopenharmony_ci void* reserved[4]; 421cb0ef41Sopenharmony_ci} napi_module; 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci#define NAPI_MODULE_VERSION 1 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci#if defined(_MSC_VER) 471cb0ef41Sopenharmony_ci#pragma section(".CRT$XCU", read) 481cb0ef41Sopenharmony_ci#define NAPI_C_CTOR(fn) \ 491cb0ef41Sopenharmony_ci static void __cdecl fn(void); \ 501cb0ef41Sopenharmony_ci __declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \ 511cb0ef41Sopenharmony_ci fn; \ 521cb0ef41Sopenharmony_ci static void __cdecl fn(void) 531cb0ef41Sopenharmony_ci#else 541cb0ef41Sopenharmony_ci#define NAPI_C_CTOR(fn) \ 551cb0ef41Sopenharmony_ci static void fn(void) __attribute__((constructor)); \ 561cb0ef41Sopenharmony_ci static void fn(void) 571cb0ef41Sopenharmony_ci#endif 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_ci#define NAPI_MODULE_X(modname, regfunc, priv, flags) \ 601cb0ef41Sopenharmony_ci EXTERN_C_START \ 611cb0ef41Sopenharmony_ci static napi_module _module = \ 621cb0ef41Sopenharmony_ci { \ 631cb0ef41Sopenharmony_ci NAPI_MODULE_VERSION, \ 641cb0ef41Sopenharmony_ci flags, \ 651cb0ef41Sopenharmony_ci __FILE__, \ 661cb0ef41Sopenharmony_ci regfunc, \ 671cb0ef41Sopenharmony_ci #modname, \ 681cb0ef41Sopenharmony_ci priv, \ 691cb0ef41Sopenharmony_ci {0}, \ 701cb0ef41Sopenharmony_ci }; \ 711cb0ef41Sopenharmony_ci NAPI_C_CTOR(_register_ ## modname) { \ 721cb0ef41Sopenharmony_ci napi_module_register(&_module); \ 731cb0ef41Sopenharmony_ci } \ 741cb0ef41Sopenharmony_ci EXTERN_C_END 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci#define NAPI_MODULE_INITIALIZER_X(base, version) \ 771cb0ef41Sopenharmony_ci NAPI_MODULE_INITIALIZER_X_HELPER(base, version) 781cb0ef41Sopenharmony_ci#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci#ifdef __wasm32__ 811cb0ef41Sopenharmony_ci#define NAPI_WASM_INITIALIZER \ 821cb0ef41Sopenharmony_ci NAPI_MODULE_INITIALIZER_X(napi_register_wasm_v, NAPI_MODULE_VERSION) 831cb0ef41Sopenharmony_ci#define NAPI_MODULE(modname, regfunc) \ 841cb0ef41Sopenharmony_ci EXTERN_C_START \ 851cb0ef41Sopenharmony_ci NAPI_MODULE_EXPORT napi_value NAPI_WASM_INITIALIZER(napi_env env, \ 861cb0ef41Sopenharmony_ci napi_value exports) { \ 871cb0ef41Sopenharmony_ci return regfunc(env, exports); \ 881cb0ef41Sopenharmony_ci } \ 891cb0ef41Sopenharmony_ci EXTERN_C_END 901cb0ef41Sopenharmony_ci#else 911cb0ef41Sopenharmony_ci#define NAPI_MODULE(modname, regfunc) \ 921cb0ef41Sopenharmony_ci NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage) 931cb0ef41Sopenharmony_ci#endif 941cb0ef41Sopenharmony_ci 951cb0ef41Sopenharmony_ci#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ci#define NAPI_MODULE_INITIALIZER \ 981cb0ef41Sopenharmony_ci NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, \ 991cb0ef41Sopenharmony_ci NAPI_MODULE_VERSION) 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci#define NAPI_MODULE_INIT() \ 1021cb0ef41Sopenharmony_ci EXTERN_C_START \ 1031cb0ef41Sopenharmony_ci NAPI_MODULE_EXPORT napi_value \ 1041cb0ef41Sopenharmony_ci NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports); \ 1051cb0ef41Sopenharmony_ci EXTERN_C_END \ 1061cb0ef41Sopenharmony_ci NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \ 1071cb0ef41Sopenharmony_ci napi_value NAPI_MODULE_INITIALIZER(napi_env env, \ 1081cb0ef41Sopenharmony_ci napi_value exports) 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ciEXTERN_C_START 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ciNAPI_EXTERN void napi_module_register(napi_module* mod); 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ciNAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location, 1151cb0ef41Sopenharmony_ci size_t location_len, 1161cb0ef41Sopenharmony_ci const char* message, 1171cb0ef41Sopenharmony_ci size_t message_len); 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci// Methods for custom handling of async operations 1201cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_async_init(napi_env env, 1211cb0ef41Sopenharmony_ci napi_value async_resource, 1221cb0ef41Sopenharmony_ci napi_value async_resource_name, 1231cb0ef41Sopenharmony_ci napi_async_context* result); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_async_destroy(napi_env env, 1261cb0ef41Sopenharmony_ci napi_async_context async_context); 1271cb0ef41Sopenharmony_ci 1281cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_make_callback(napi_env env, 1291cb0ef41Sopenharmony_ci napi_async_context async_context, 1301cb0ef41Sopenharmony_ci napi_value recv, 1311cb0ef41Sopenharmony_ci napi_value func, 1321cb0ef41Sopenharmony_ci size_t argc, 1331cb0ef41Sopenharmony_ci const napi_value* argv, 1341cb0ef41Sopenharmony_ci napi_value* result); 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci// Methods to provide node::Buffer functionality with napi types 1371cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_buffer(napi_env env, 1381cb0ef41Sopenharmony_ci size_t length, 1391cb0ef41Sopenharmony_ci void** data, 1401cb0ef41Sopenharmony_ci napi_value* result); 1411cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_external_buffer(napi_env env, 1421cb0ef41Sopenharmony_ci size_t length, 1431cb0ef41Sopenharmony_ci void* data, 1441cb0ef41Sopenharmony_ci napi_finalize finalize_cb, 1451cb0ef41Sopenharmony_ci void* finalize_hint, 1461cb0ef41Sopenharmony_ci napi_value* result); 1471cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env, 1481cb0ef41Sopenharmony_ci size_t length, 1491cb0ef41Sopenharmony_ci const void* data, 1501cb0ef41Sopenharmony_ci void** result_data, 1511cb0ef41Sopenharmony_ci napi_value* result); 1521cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_is_buffer(napi_env env, 1531cb0ef41Sopenharmony_ci napi_value value, 1541cb0ef41Sopenharmony_ci bool* result); 1551cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_get_buffer_info(napi_env env, 1561cb0ef41Sopenharmony_ci napi_value value, 1571cb0ef41Sopenharmony_ci void** data, 1581cb0ef41Sopenharmony_ci size_t* length); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci// Methods to manage simple async operations 1611cb0ef41Sopenharmony_ciNAPI_EXTERN 1621cb0ef41Sopenharmony_cinapi_status napi_create_async_work(napi_env env, 1631cb0ef41Sopenharmony_ci napi_value async_resource, 1641cb0ef41Sopenharmony_ci napi_value async_resource_name, 1651cb0ef41Sopenharmony_ci napi_async_execute_callback execute, 1661cb0ef41Sopenharmony_ci napi_async_complete_callback complete, 1671cb0ef41Sopenharmony_ci void* data, 1681cb0ef41Sopenharmony_ci napi_async_work* result); 1691cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_delete_async_work(napi_env env, 1701cb0ef41Sopenharmony_ci napi_async_work work); 1711cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_queue_async_work(napi_env env, 1721cb0ef41Sopenharmony_ci napi_async_work work); 1731cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_cancel_async_work(napi_env env, 1741cb0ef41Sopenharmony_ci napi_async_work work); 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci// version management 1771cb0ef41Sopenharmony_ciNAPI_EXTERN 1781cb0ef41Sopenharmony_cinapi_status napi_get_node_version(napi_env env, 1791cb0ef41Sopenharmony_ci const napi_node_version** version); 1801cb0ef41Sopenharmony_ci 1811cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 2 1821cb0ef41Sopenharmony_ci 1831cb0ef41Sopenharmony_ci// Return the current libuv event loop for a given environment 1841cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env, 1851cb0ef41Sopenharmony_ci struct uv_loop_s** loop); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci#endif // NAPI_VERSION >= 2 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 3 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err); 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env, 1941cb0ef41Sopenharmony_ci void (*fun)(void* arg), 1951cb0ef41Sopenharmony_ci void* arg); 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env, 1981cb0ef41Sopenharmony_ci void (*fun)(void* arg), 1991cb0ef41Sopenharmony_ci void* arg); 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_open_callback_scope(napi_env env, 2021cb0ef41Sopenharmony_ci napi_value resource_object, 2031cb0ef41Sopenharmony_ci napi_async_context context, 2041cb0ef41Sopenharmony_ci napi_callback_scope* result); 2051cb0ef41Sopenharmony_ci 2061cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_close_callback_scope(napi_env env, 2071cb0ef41Sopenharmony_ci napi_callback_scope scope); 2081cb0ef41Sopenharmony_ci 2091cb0ef41Sopenharmony_ci#endif // NAPI_VERSION >= 3 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 4 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci#ifndef __wasm32__ 2141cb0ef41Sopenharmony_ci// Calling into JS from other threads 2151cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2161cb0ef41Sopenharmony_cinapi_create_threadsafe_function(napi_env env, 2171cb0ef41Sopenharmony_ci napi_value func, 2181cb0ef41Sopenharmony_ci napi_value async_resource, 2191cb0ef41Sopenharmony_ci napi_value async_resource_name, 2201cb0ef41Sopenharmony_ci size_t max_queue_size, 2211cb0ef41Sopenharmony_ci size_t initial_thread_count, 2221cb0ef41Sopenharmony_ci void* thread_finalize_data, 2231cb0ef41Sopenharmony_ci napi_finalize thread_finalize_cb, 2241cb0ef41Sopenharmony_ci void* context, 2251cb0ef41Sopenharmony_ci napi_threadsafe_function_call_js call_js_cb, 2261cb0ef41Sopenharmony_ci napi_threadsafe_function* result); 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2291cb0ef41Sopenharmony_cinapi_get_threadsafe_function_context(napi_threadsafe_function func, 2301cb0ef41Sopenharmony_ci void** result); 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2331cb0ef41Sopenharmony_cinapi_call_threadsafe_function(napi_threadsafe_function func, 2341cb0ef41Sopenharmony_ci void* data, 2351cb0ef41Sopenharmony_ci napi_threadsafe_function_call_mode is_blocking); 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2381cb0ef41Sopenharmony_cinapi_acquire_threadsafe_function(napi_threadsafe_function func); 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2411cb0ef41Sopenharmony_cinapi_release_threadsafe_function(napi_threadsafe_function func, 2421cb0ef41Sopenharmony_ci napi_threadsafe_function_release_mode mode); 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2451cb0ef41Sopenharmony_cinapi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func); 2461cb0ef41Sopenharmony_ci 2471cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2481cb0ef41Sopenharmony_cinapi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func); 2491cb0ef41Sopenharmony_ci#endif // __wasm32__ 2501cb0ef41Sopenharmony_ci 2511cb0ef41Sopenharmony_ci#endif // NAPI_VERSION >= 4 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 8 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_add_async_cleanup_hook( 2561cb0ef41Sopenharmony_ci napi_env env, 2571cb0ef41Sopenharmony_ci napi_async_cleanup_hook hook, 2581cb0ef41Sopenharmony_ci void* arg, 2591cb0ef41Sopenharmony_ci napi_async_cleanup_hook_handle* remove_handle); 2601cb0ef41Sopenharmony_ci 2611cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status napi_remove_async_cleanup_hook( 2621cb0ef41Sopenharmony_ci napi_async_cleanup_hook_handle remove_handle); 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_ci#endif // NAPI_VERSION >= 8 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_ci#ifdef NAPI_EXPERIMENTAL 2671cb0ef41Sopenharmony_ci 2681cb0ef41Sopenharmony_ciNAPI_EXTERN napi_status 2691cb0ef41Sopenharmony_cinode_api_get_module_file_name(napi_env env, const char** result); 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci#endif // NAPI_EXPERIMENTAL 2721cb0ef41Sopenharmony_ci 2731cb0ef41Sopenharmony_ciEXTERN_C_END 2741cb0ef41Sopenharmony_ci 2751cb0ef41Sopenharmony_ci#endif // SRC_NODE_API_H_ 276