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 addon against node
71cb0ef41Sopenharmony_ci#define JSVM_EXTERN __declspec(dllimport)
81cb0ef41Sopenharmony_ci#elif defined(__wasm32__)
91cb0ef41Sopenharmony_ci#define JSVM_EXTERN __attribute__((__import_module__("napi")))
101cb0ef41Sopenharmony_ci#endif
111cb0ef41Sopenharmony_ci#endif
121cb0ef41Sopenharmony_ci#include "jsvm.h"
131cb0ef41Sopenharmony_ci#include "jsvm_node_api_types.h"
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cistruct uv_loop_s;  // Forward declaration.
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci#ifdef _WIN32
181cb0ef41Sopenharmony_ci#define JSVM_MODULE_EXPORT __declspec(dllexport)
191cb0ef41Sopenharmony_ci#else
201cb0ef41Sopenharmony_ci#define JSVM_MODULE_EXPORT __attribute__((visibility("default")))
211cb0ef41Sopenharmony_ci#endif
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci#if defined(__GNUC__)
241cb0ef41Sopenharmony_ci#define JSVM_NO_RETURN __attribute__((noreturn))
251cb0ef41Sopenharmony_ci#elif defined(_WIN32)
261cb0ef41Sopenharmony_ci#define JSVM_NO_RETURN __declspec(noreturn)
271cb0ef41Sopenharmony_ci#else
281cb0ef41Sopenharmony_ci#define JSVM_NO_RETURN
291cb0ef41Sopenharmony_ci#endif
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_citypedef JSVM_Value(JSVM_CDECL* jsvm_addon_register_func)(JSVM_Env env,
321cb0ef41Sopenharmony_ci                                                         JSVM_Value exports);
331cb0ef41Sopenharmony_citypedef int32_t(JSVM_CDECL* node_api_addon_get_api_version_func)();
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci// Used by deprecated registration method jsvm_module_register.
361cb0ef41Sopenharmony_citypedef struct jsvm_module {
371cb0ef41Sopenharmony_ci  int nm_version;
381cb0ef41Sopenharmony_ci  unsigned int nm_flags;
391cb0ef41Sopenharmony_ci  const char* nm_filename;
401cb0ef41Sopenharmony_ci  jsvm_addon_register_func nm_register_func;
411cb0ef41Sopenharmony_ci  const char* nm_modname;
421cb0ef41Sopenharmony_ci  void* nm_priv;
431cb0ef41Sopenharmony_ci  void* reserved[4];
441cb0ef41Sopenharmony_ci} jsvm_module;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci#define JSVM_MODULE_VERSION 1
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci#define JSVM_MODULE_INITIALIZER_X(base, version)                               \
491cb0ef41Sopenharmony_ci  JSVM_MODULE_INITIALIZER_X_HELPER(base, version)
501cb0ef41Sopenharmony_ci#define JSVM_MODULE_INITIALIZER_X_HELPER(base, version) base##version
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci#ifdef __wasm32__
531cb0ef41Sopenharmony_ci#define JSVM_MODULE_INITIALIZER_BASE jsvm_register_wasm_v
541cb0ef41Sopenharmony_ci#else
551cb0ef41Sopenharmony_ci#define JSVM_MODULE_INITIALIZER_BASE jsvm_register_module_v
561cb0ef41Sopenharmony_ci#endif
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#define NODE_API_MODULE_GET_API_VERSION_BASE node_api_module_get_api_version_v
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci#define JSVM_MODULE_INITIALIZER                                                \
611cb0ef41Sopenharmony_ci  JSVM_MODULE_INITIALIZER_X(JSVM_MODULE_INITIALIZER_BASE, JSVM_MODULE_VERSION)
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci#define NODE_API_MODULE_GET_API_VERSION                                        \
641cb0ef41Sopenharmony_ci  JSVM_MODULE_INITIALIZER_X(NODE_API_MODULE_GET_API_VERSION_BASE,              \
651cb0ef41Sopenharmony_ci                            JSVM_MODULE_VERSION)
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci#define JSVM_MODULE_INIT()                                                     \
681cb0ef41Sopenharmony_ci  EXTERN_C_START                                                               \
691cb0ef41Sopenharmony_ci  JSVM_MODULE_EXPORT int32_t NODE_API_MODULE_GET_API_VERSION() {               \
701cb0ef41Sopenharmony_ci    return NAPI_VERSION;                                                       \
711cb0ef41Sopenharmony_ci  }                                                                            \
721cb0ef41Sopenharmony_ci  JSVM_MODULE_EXPORT JSVM_Value JSVM_MODULE_INITIALIZER(JSVM_Env env,          \
731cb0ef41Sopenharmony_ci                                                        JSVM_Value exports);   \
741cb0ef41Sopenharmony_ci  EXTERN_C_END                                                                 \
751cb0ef41Sopenharmony_ci  JSVM_Value JSVM_MODULE_INITIALIZER(JSVM_Env env, JSVM_Value exports)
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci#define JSVM_MODULE(modname, regfunc)                                          \
781cb0ef41Sopenharmony_ci  JSVM_MODULE_INIT() { return regfunc(env, exports); }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci// Deprecated. Use JSVM_MODULE.
811cb0ef41Sopenharmony_ci#define JSVM_MODULE_X(modname, regfunc, priv, flags)                           \
821cb0ef41Sopenharmony_ci  JSVM_MODULE(modname, regfunc)
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciEXTERN_C_START
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci// Deprecated. Replaced by symbol-based registration defined by JSVM_MODULE
871cb0ef41Sopenharmony_ci// and JSVM_MODULE_INIT macros.
881cb0ef41Sopenharmony_ci#if defined(__cplusplus) && __cplusplus >= 201402L
891cb0ef41Sopenharmony_ci[[deprecated]]
901cb0ef41Sopenharmony_ci#endif
911cb0ef41Sopenharmony_ciJSVM_EXTERN void JSVM_CDECL
921cb0ef41Sopenharmony_cijsvm_module_register(jsvm_module* mod);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_NO_RETURN void JSVM_CDECL
951cb0ef41Sopenharmony_cijsvm_fatal_error(const char* location,
961cb0ef41Sopenharmony_ci                 size_t location_len,
971cb0ef41Sopenharmony_ci                 const char* message,
981cb0ef41Sopenharmony_ci                 size_t message_len);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci// Methods for custom handling of async operations
1011cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1021cb0ef41Sopenharmony_cijsvm_async_init(JSVM_Env env,
1031cb0ef41Sopenharmony_ci                JSVM_Value async_resource,
1041cb0ef41Sopenharmony_ci                JSVM_Value async_resource_name,
1051cb0ef41Sopenharmony_ci                jsvm_async_context* result);
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1081cb0ef41Sopenharmony_cijsvm_async_destroy(JSVM_Env env, jsvm_async_context async_context);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1111cb0ef41Sopenharmony_cijsvm_make_callback(JSVM_Env env,
1121cb0ef41Sopenharmony_ci                   jsvm_async_context async_context,
1131cb0ef41Sopenharmony_ci                   JSVM_Value recv,
1141cb0ef41Sopenharmony_ci                   JSVM_Value func,
1151cb0ef41Sopenharmony_ci                   size_t argc,
1161cb0ef41Sopenharmony_ci                   const JSVM_Value* argv,
1171cb0ef41Sopenharmony_ci                   JSVM_Value* result);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci// Methods to provide node::Buffer functionality with napi types
1201cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_create_buffer(JSVM_Env env,
1211cb0ef41Sopenharmony_ci                                                      size_t length,
1221cb0ef41Sopenharmony_ci                                                      void** data,
1231cb0ef41Sopenharmony_ci                                                      JSVM_Value* result);
1241cb0ef41Sopenharmony_ci#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1251cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1261cb0ef41Sopenharmony_ciOH_JSVM_CreateExternal_buffer(JSVM_Env env,
1271cb0ef41Sopenharmony_ci                            size_t length,
1281cb0ef41Sopenharmony_ci                            void* data,
1291cb0ef41Sopenharmony_ci                            JSVM_Finalize finalizeCb,
1301cb0ef41Sopenharmony_ci                            void* finalizeHint,
1311cb0ef41Sopenharmony_ci                            JSVM_Value* result);
1321cb0ef41Sopenharmony_ci#endif  // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1331cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_create_buffer_copy(JSVM_Env env,
1341cb0ef41Sopenharmony_ci                                                           size_t length,
1351cb0ef41Sopenharmony_ci                                                           const void* data,
1361cb0ef41Sopenharmony_ci                                                           void** result_data,
1371cb0ef41Sopenharmony_ci                                                           JSVM_Value* result);
1381cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_is_buffer(JSVM_Env env,
1391cb0ef41Sopenharmony_ci                                                  JSVM_Value value,
1401cb0ef41Sopenharmony_ci                                                  bool* result);
1411cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_get_buffer_info(JSVM_Env env,
1421cb0ef41Sopenharmony_ci                                                        JSVM_Value value,
1431cb0ef41Sopenharmony_ci                                                        void** data,
1441cb0ef41Sopenharmony_ci                                                        size_t* length);
1451cb0ef41Sopenharmony_ci
1461cb0ef41Sopenharmony_ci#ifndef __wasm32__
1471cb0ef41Sopenharmony_ci// Methods to manage simple async operations
1481cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1491cb0ef41Sopenharmony_cijsvm_create_async_work(JSVM_Env env,
1501cb0ef41Sopenharmony_ci                       JSVM_Value async_resource,
1511cb0ef41Sopenharmony_ci                       JSVM_Value async_resource_name,
1521cb0ef41Sopenharmony_ci                       jsvm_async_execute_callback execute,
1531cb0ef41Sopenharmony_ci                       jsvm_async_complete_callback complete,
1541cb0ef41Sopenharmony_ci                       void* data,
1551cb0ef41Sopenharmony_ci                       jsvm_async_work* result);
1561cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_delete_async_work(JSVM_Env env,
1571cb0ef41Sopenharmony_ci                                                          jsvm_async_work work);
1581cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_queue_async_work(JSVM_Env env,
1591cb0ef41Sopenharmony_ci                                                         jsvm_async_work work);
1601cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_cancel_async_work(JSVM_Env env,
1611cb0ef41Sopenharmony_ci                                                          jsvm_async_work work);
1621cb0ef41Sopenharmony_ci#endif  // __wasm32__
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci// version management
1651cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1661cb0ef41Sopenharmony_cijsvm_get_node_version(JSVM_Env env, const jsvm_node_version** version);
1671cb0ef41Sopenharmony_ci
1681cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 2
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ci// Return the current libuv event loop for a given environment
1711cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1721cb0ef41Sopenharmony_cijsvm_get_uv_event_loop(JSVM_Env env, struct uv_loop_s** loop);
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci#endif  // NAPI_VERSION >= 2
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 3
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_fatal_exception(JSVM_Env env,
1791cb0ef41Sopenharmony_ci                                                        JSVM_Value err);
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1821cb0ef41Sopenharmony_cijsvm_add_env_cleanup_hook(JSVM_Env env, jsvm_cleanup_hook fun, void* arg);
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1851cb0ef41Sopenharmony_cijsvm_remove_env_cleanup_hook(JSVM_Env env, jsvm_cleanup_hook fun, void* arg);
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1881cb0ef41Sopenharmony_cijsvm_open_callback_scope(JSVM_Env env,
1891cb0ef41Sopenharmony_ci                         JSVM_Value resource_object,
1901cb0ef41Sopenharmony_ci                         jsvm_async_context context,
1911cb0ef41Sopenharmony_ci                         jsvm_callback_scope* result);
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
1941cb0ef41Sopenharmony_cijsvm_close_callback_scope(JSVM_Env env, jsvm_callback_scope scope);
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci#endif  // NAPI_VERSION >= 3
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 4
1991cb0ef41Sopenharmony_ci
2001cb0ef41Sopenharmony_ci#ifndef __wasm32__
2011cb0ef41Sopenharmony_ci// Calling into JS from other threads
2021cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2031cb0ef41Sopenharmony_cijsvm_create_threadsafe_function(JSVM_Env env,
2041cb0ef41Sopenharmony_ci                                JSVM_Value func,
2051cb0ef41Sopenharmony_ci                                JSVM_Value async_resource,
2061cb0ef41Sopenharmony_ci                                JSVM_Value async_resource_name,
2071cb0ef41Sopenharmony_ci                                size_t max_queue_size,
2081cb0ef41Sopenharmony_ci                                size_t initial_thread_count,
2091cb0ef41Sopenharmony_ci                                void* thread_finalize_data,
2101cb0ef41Sopenharmony_ci                                JSVM_Finalize thread_finalize_cb,
2111cb0ef41Sopenharmony_ci                                void* context,
2121cb0ef41Sopenharmony_ci                                jsvm_threadsafe_function_call_js call_js_cb,
2131cb0ef41Sopenharmony_ci                                jsvm_threadsafe_function* result);
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_get_threadsafe_function_context(
2161cb0ef41Sopenharmony_ci    jsvm_threadsafe_function func, void** result);
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2191cb0ef41Sopenharmony_cijsvm_call_threadsafe_function(jsvm_threadsafe_function func,
2201cb0ef41Sopenharmony_ci                              void* data,
2211cb0ef41Sopenharmony_ci                              jsvm_threadsafe_function_call_mode is_blocking);
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2241cb0ef41Sopenharmony_cijsvm_acquire_threadsafe_function(jsvm_threadsafe_function func);
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL jsvm_release_threadsafe_function(
2271cb0ef41Sopenharmony_ci    jsvm_threadsafe_function func, jsvm_threadsafe_function_release_mode mode);
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2301cb0ef41Sopenharmony_cijsvm_unref_threadsafe_function(JSVM_Env env, jsvm_threadsafe_function func);
2311cb0ef41Sopenharmony_ci
2321cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2331cb0ef41Sopenharmony_cijsvm_ref_threadsafe_function(JSVM_Env env, jsvm_threadsafe_function func);
2341cb0ef41Sopenharmony_ci#endif  // __wasm32__
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci#endif  // NAPI_VERSION >= 4
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 8
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2411cb0ef41Sopenharmony_cijsvm_add_async_cleanup_hook(JSVM_Env env,
2421cb0ef41Sopenharmony_ci                            jsvm_async_cleanup_hook hook,
2431cb0ef41Sopenharmony_ci                            void* arg,
2441cb0ef41Sopenharmony_ci                            jsvm_async_cleanup_hook_handle* remove_handle);
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2471cb0ef41Sopenharmony_cijsvm_remove_async_cleanup_hook(jsvm_async_cleanup_hook_handle remove_handle);
2481cb0ef41Sopenharmony_ci
2491cb0ef41Sopenharmony_ci#endif  // NAPI_VERSION >= 8
2501cb0ef41Sopenharmony_ci
2511cb0ef41Sopenharmony_ci#if NAPI_VERSION >= 9
2521cb0ef41Sopenharmony_ci
2531cb0ef41Sopenharmony_ciJSVM_EXTERN JSVM_Status JSVM_CDECL
2541cb0ef41Sopenharmony_cinode_api_get_module_file_name(JSVM_Env env, const char** result);
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_ci#endif  // NAPI_VERSION >= 9
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ciEXTERN_C_END
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci#endif  // SRC_NODE_API_H_
261