mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
Split the Node.js ECMAScript API (N-EAPI?) into its own header and implementation files. The motivation is that the ECMAScript API stand on its own so it might be embedded separately, implementation and all. Portions of the implementation used by both files are stored in `node_api_impl.h`. The checked boxes below indicate that the given API remains in `node_api.h`, whereas the lack of a checkbox indicates that the API was moved to `node_ecma_api.h`. * [x] NAPI_MODULE * [x] NAPI_MODULE_INIT * [x] napi_acquire_threadsafe_function * [x] napi_add_env_cleanup_hook * [x] napi_async_destroy * [x] napi_async_init * [x] napi_call_threadsafe_function * [x] napi_cancel_async_work * [x] napi_close_callback_scope * [x] napi_create_async_work * [x] napi_create_buffer * [x] napi_create_buffer_copy * [x] napi_create_external_buffer * [x] napi_create_threadsafe_function * [x] napi_delete_async_work * [x] napi_fatal_error * [x] napi_fatal_exception * [x] napi_get_buffer_info * [x] napi_get_node_version * [x] napi_get_threadsafe_function_context * [x] napi_get_uv_event_loop * [x] napi_is_buffer * [x] napi_make_callback * [x] napi_module_register * [x] napi_open_callback_scope * [x] napi_queue_async_work * [x] napi_ref_threadsafe_function * [x] napi_release_threadsafe_function * [x] napi_remove_env_cleanup_hook * [x] napi_unref_threadsafe_function * [ ] napi_add_finalizer * [ ] napi_adjust_external_memory * [ ] napi_call_function * [ ] napi_close_escapable_handle_scope * [ ] napi_close_handle_scope * [ ] napi_coerce_to_bool * [ ] napi_coerce_to_number * [ ] napi_coerce_to_object * [ ] napi_coerce_to_string * [ ] napi_create_array * [ ] napi_create_arraybuffer * [ ] napi_create_array_with_length * [ ] napi_create_bigint_int64 * [ ] napi_create_bigint_uint64 * [ ] napi_create_bigint_words * [ ] napi_create_dataview * [ ] napi_create_double * [ ] napi_create_error * [ ] napi_create_external * [ ] napi_create_external_arraybuffer * [ ] napi_create_function * [ ] napi_create_int32 * [ ] napi_create_int64 * [ ] napi_create_object * [ ] napi_create_promise * [ ] napi_create_range_error * [ ] napi_create_reference * [ ] napi_create_string_latin1 * [ ] napi_create_string_utf16 * [ ] napi_create_string_utf8 * [ ] napi_create_symbol * [ ] napi_create_typedarray * [ ] napi_create_type_error * [ ] napi_create_uint32 * [ ] napi_define_class * [ ] napi_define_properties * [ ] napi_delete_element * [ ] napi_delete_property * [ ] napi_delete_reference * [ ] napi_escape_handle * [ ] napi_get_and_clear_last_exception * [ ] napi_get_arraybuffer_info * [ ] napi_get_array_length * [ ] napi_get_boolean * [ ] napi_get_cb_info * [ ] napi_get_dataview_info * [ ] napi_get_element * [ ] napi_get_global * [ ] napi_get_last_error_info * [ ] napi_get_named_property * [ ] napi_get_new_target * [ ] napi_get_null * [ ] napi_get_property * [ ] napi_get_property_names * [ ] napi_get_prototype * [ ] napi_get_reference_value * [ ] napi_get_typedarray_info * [ ] napi_get_undefined * [ ] napi_get_value_bigint_int64 * [ ] napi_get_value_bigint_uint64 * [ ] napi_get_value_bigint_words * [ ] napi_get_value_bool * [ ] napi_get_value_double * [ ] napi_get_value_external * [ ] napi_get_value_int32 * [ ] napi_get_value_int64 * [ ] napi_get_value_string_latin1 * [ ] napi_get_value_string_utf16 * [ ] napi_get_value_string_utf8 * [ ] napi_get_value_uint32 * [ ] napi_get_version * [ ] napi_has_element * [ ] napi_has_named_property * [ ] napi_has_own_property * [ ] napi_has_property * [ ] napi_instanceof * [ ] napi_is_array * [ ] napi_is_arraybuffer * [ ] napi_is_dataview * [ ] napi_is_error * [ ] napi_is_exception_pending * [ ] napi_is_promise * [ ] napi_is_typedarray * [ ] napi_new_instance * [ ] napi_open_escapable_handle_scope * [ ] napi_open_handle_scope * [ ] napi_reference_ref * [ ] napi_reference_unref * [ ] napi_reject_deferred * [ ] napi_remove_wrap * [ ] napi_resolve_deferred * [ ] napi_run_script * [ ] napi_set_element * [ ] napi_set_named_property * [ ] napi_set_property * [ ] napi_strict_equals * [ ] napi_throw * [ ] napi_throw_error * [ ] napi_throw_range_error * [ ] napi_throw_type_error * [ ] napi_typeof * [ ] napi_unwrap * [ ] napi_wrap PR-URL: https://github.com/nodejs/node/pull/23786 Reviewed-By: Yazhong Liu <yorkiefixer@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
238 lines
10 KiB
C
238 lines
10 KiB
C
#ifndef SRC_NODE_API_H_
|
|
#define SRC_NODE_API_H_
|
|
|
|
#ifdef BUILDING_NODE_EXTENSION
|
|
#ifdef _WIN32
|
|
// Building native module against node
|
|
#define NAPI_EXTERN __declspec(dllimport)
|
|
#endif
|
|
#endif
|
|
#include "js_native_api.h"
|
|
#include "node_api_types.h"
|
|
|
|
struct uv_loop_s; // Forward declaration.
|
|
|
|
#ifdef _WIN32
|
|
# define NAPI_MODULE_EXPORT __declspec(dllexport)
|
|
#else
|
|
# define NAPI_MODULE_EXPORT __attribute__((visibility("default")))
|
|
#endif
|
|
|
|
#ifdef __GNUC__
|
|
#define NAPI_NO_RETURN __attribute__((noreturn))
|
|
#else
|
|
#define NAPI_NO_RETURN
|
|
#endif
|
|
|
|
typedef napi_value (*napi_addon_register_func)(napi_env env,
|
|
napi_value exports);
|
|
|
|
typedef struct {
|
|
int nm_version;
|
|
unsigned int nm_flags;
|
|
const char* nm_filename;
|
|
napi_addon_register_func nm_register_func;
|
|
const char* nm_modname;
|
|
void* nm_priv;
|
|
void* reserved[4];
|
|
} napi_module;
|
|
|
|
#define NAPI_MODULE_VERSION 1
|
|
|
|
#if defined(_MSC_VER)
|
|
#pragma section(".CRT$XCU", read)
|
|
#define NAPI_C_CTOR(fn) \
|
|
static void __cdecl fn(void); \
|
|
__declspec(dllexport, allocate(".CRT$XCU")) void(__cdecl * fn##_)(void) = \
|
|
fn; \
|
|
static void __cdecl fn(void)
|
|
#else
|
|
#define NAPI_C_CTOR(fn) \
|
|
static void fn(void) __attribute__((constructor)); \
|
|
static void fn(void)
|
|
#endif
|
|
|
|
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
|
|
EXTERN_C_START \
|
|
static napi_module _module = \
|
|
{ \
|
|
NAPI_MODULE_VERSION, \
|
|
flags, \
|
|
__FILE__, \
|
|
regfunc, \
|
|
#modname, \
|
|
priv, \
|
|
{0}, \
|
|
}; \
|
|
NAPI_C_CTOR(_register_ ## modname) { \
|
|
napi_module_register(&_module); \
|
|
} \
|
|
EXTERN_C_END
|
|
|
|
#define NAPI_MODULE(modname, regfunc) \
|
|
NAPI_MODULE_X(modname, regfunc, NULL, 0) // NOLINT (readability/null_usage)
|
|
|
|
#define NAPI_MODULE_INITIALIZER_BASE napi_register_module_v
|
|
|
|
#define NAPI_MODULE_INITIALIZER_X(base, version) \
|
|
NAPI_MODULE_INITIALIZER_X_HELPER(base, version)
|
|
#define NAPI_MODULE_INITIALIZER_X_HELPER(base, version) base##version
|
|
|
|
#define NAPI_MODULE_INITIALIZER \
|
|
NAPI_MODULE_INITIALIZER_X(NAPI_MODULE_INITIALIZER_BASE, \
|
|
NAPI_MODULE_VERSION)
|
|
|
|
#define NAPI_MODULE_INIT() \
|
|
EXTERN_C_START \
|
|
NAPI_MODULE_EXPORT napi_value \
|
|
NAPI_MODULE_INITIALIZER(napi_env env, napi_value exports); \
|
|
EXTERN_C_END \
|
|
NAPI_MODULE(NODE_GYP_MODULE_NAME, NAPI_MODULE_INITIALIZER) \
|
|
napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
|
|
napi_value exports)
|
|
|
|
EXTERN_C_START
|
|
|
|
NAPI_EXTERN void napi_module_register(napi_module* mod);
|
|
|
|
NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
|
|
size_t location_len,
|
|
const char* message,
|
|
size_t message_len);
|
|
|
|
// Methods for custom handling of async operations
|
|
NAPI_EXTERN napi_status napi_async_init(napi_env env,
|
|
napi_value async_resource,
|
|
napi_value async_resource_name,
|
|
napi_async_context* result);
|
|
|
|
NAPI_EXTERN napi_status napi_async_destroy(napi_env env,
|
|
napi_async_context async_context);
|
|
|
|
NAPI_EXTERN napi_status napi_make_callback(napi_env env,
|
|
napi_async_context async_context,
|
|
napi_value recv,
|
|
napi_value func,
|
|
size_t argc,
|
|
const napi_value* argv,
|
|
napi_value* result);
|
|
|
|
// Methods to provide node::Buffer functionality with napi types
|
|
NAPI_EXTERN napi_status napi_create_buffer(napi_env env,
|
|
size_t length,
|
|
void** data,
|
|
napi_value* result);
|
|
NAPI_EXTERN napi_status napi_create_external_buffer(napi_env env,
|
|
size_t length,
|
|
void* data,
|
|
napi_finalize finalize_cb,
|
|
void* finalize_hint,
|
|
napi_value* result);
|
|
NAPI_EXTERN napi_status napi_create_buffer_copy(napi_env env,
|
|
size_t length,
|
|
const void* data,
|
|
void** result_data,
|
|
napi_value* result);
|
|
NAPI_EXTERN napi_status napi_is_buffer(napi_env env,
|
|
napi_value value,
|
|
bool* result);
|
|
NAPI_EXTERN napi_status napi_get_buffer_info(napi_env env,
|
|
napi_value value,
|
|
void** data,
|
|
size_t* length);
|
|
|
|
// Methods to manage simple async operations
|
|
NAPI_EXTERN
|
|
napi_status napi_create_async_work(napi_env env,
|
|
napi_value async_resource,
|
|
napi_value async_resource_name,
|
|
napi_async_execute_callback execute,
|
|
napi_async_complete_callback complete,
|
|
void* data,
|
|
napi_async_work* result);
|
|
NAPI_EXTERN napi_status napi_delete_async_work(napi_env env,
|
|
napi_async_work work);
|
|
NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
|
|
napi_async_work work);
|
|
NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
|
|
napi_async_work work);
|
|
|
|
// version management
|
|
NAPI_EXTERN
|
|
napi_status napi_get_node_version(napi_env env,
|
|
const napi_node_version** version);
|
|
|
|
#if NAPI_VERSION >= 2
|
|
|
|
// Return the current libuv event loop for a given environment
|
|
NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
|
|
struct uv_loop_s** loop);
|
|
|
|
#endif // NAPI_VERSION >= 2
|
|
|
|
#if NAPI_VERSION >= 3
|
|
|
|
NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err);
|
|
|
|
NAPI_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
|
|
void (*fun)(void* arg),
|
|
void* arg);
|
|
|
|
NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env,
|
|
void (*fun)(void* arg),
|
|
void* arg);
|
|
|
|
NAPI_EXTERN napi_status napi_open_callback_scope(napi_env env,
|
|
napi_value resource_object,
|
|
napi_async_context context,
|
|
napi_callback_scope* result);
|
|
|
|
NAPI_EXTERN napi_status napi_close_callback_scope(napi_env env,
|
|
napi_callback_scope scope);
|
|
|
|
#endif // NAPI_VERSION >= 3
|
|
|
|
#ifdef NAPI_EXPERIMENTAL
|
|
|
|
// Calling into JS from other threads
|
|
NAPI_EXTERN napi_status
|
|
napi_create_threadsafe_function(napi_env env,
|
|
napi_value func,
|
|
napi_value async_resource,
|
|
napi_value async_resource_name,
|
|
size_t max_queue_size,
|
|
size_t initial_thread_count,
|
|
void* thread_finalize_data,
|
|
napi_finalize thread_finalize_cb,
|
|
void* context,
|
|
napi_threadsafe_function_call_js call_js_cb,
|
|
napi_threadsafe_function* result);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_get_threadsafe_function_context(napi_threadsafe_function func,
|
|
void** result);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_call_threadsafe_function(napi_threadsafe_function func,
|
|
void* data,
|
|
napi_threadsafe_function_call_mode is_blocking);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_acquire_threadsafe_function(napi_threadsafe_function func);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_release_threadsafe_function(napi_threadsafe_function func,
|
|
napi_threadsafe_function_release_mode mode);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_unref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
|
|
NAPI_EXTERN napi_status
|
|
napi_ref_threadsafe_function(napi_env env, napi_threadsafe_function func);
|
|
|
|
#endif // NAPI_EXPERIMENTAL
|
|
|
|
EXTERN_C_END
|
|
|
|
#endif // SRC_NODE_API_H_
|