quickjs-napi implements all 155 functions of Node-API version 10
(include/js_native_api.h and include/node_api.h, the headers of Node.js
22). This reference documents what each function does in quickjs-napi:
its contract, the statuses it returns, and where its behaviour depends on the
QuickJS implementation. The upstream
Node-API documentation
remains the normative text; differences from Node.js are collected in
compatibility.md.
| Area | File | Functions |
|---|---|---|
| Environment, versions, cleanup hooks, module registration | environment.md | 10 |
| Primitives, numbers, booleans, coercion, arrays | values.md | 24 |
| Strings, property keys, symbols | strings.md | 13 |
| Objects and properties | objects.md | 19 |
| Functions, calls, classes, scripts | functions.md | 7 |
| Errors and exceptions | errors.md | 12 |
| Handle scopes | scopes.md | 5 |
| References, externals, wrap, finalizers, type tags, instance data | lifetime.md | 16 |
| ArrayBuffer, TypedArray, DataView, BigInt, Date, Promise | binary-data.md | 25 |
Buffer, async context, napi_make_callback, callback scopes |
node-specific.md | 12 |
| Async work, thread-safe functions, the event loop | async.md | 12 |
Every function returns a napi_status. napi_ok means success; anything else
is a failure and is also recorded as the env's last error, which
napi_get_last_error_info returns with a message:
| Status | Message | Typical cause |
|---|---|---|
napi_ok |
success | |
napi_invalid_arg |
Invalid argument | a NULL pointer argument, an out-of-range enum, a value of the wrong kind where no specific status exists |
napi_object_expected |
An object was expected | the receiver is null/undefined (a TypeError is then pending), or not an object |
napi_string_expected |
A string was expected | |
napi_name_expected |
A string or symbol was expected | property keys, symbol descriptions |
napi_function_expected |
A function was expected | |
napi_number_expected |
A number was expected | getters never coerce |
napi_boolean_expected |
A boolean was expected | |
napi_array_expected |
An array was expected | |
napi_generic_failure |
Unknown failure | out of memory, or a failed engine operation without a JS exception |
napi_pending_exception |
An exception is pending | JavaScript threw during the call, or an exception was already pending |
napi_cancelled |
The async work item was cancelled | passed to complete |
napi_escape_called_twice |
napi_escape_handle already called on scope | |
napi_handle_scope_mismatch |
Invalid handle scope usage | closing a scope that is not the innermost one |
napi_callback_scope_mismatch |
Invalid callback scope usage | |
napi_queue_full |
Thread-safe function queue is full | non-blocking call on a full queue |
napi_closing |
Thread-safe function handle is closing | |
napi_bigint_expected |
A bigint was expected | |
napi_date_expected |
A date was expected | |
napi_arraybuffer_expected |
An arraybuffer was expected | |
napi_detachable_arraybuffer_expected |
A detachable arraybuffer was expected | |
napi_would_deadlock |
Main thread would deadlock | never returned (as in Node.js 22) |
napi_no_external_buffers_allowed |
External buffers are not allowed | never returned: external buffers are always allowed |
napi_cannot_run_js |
Cannot run JavaScript | the env is tearing down (modules built for Node-API ≥ 10) |
Some functions may call into JavaScript (getters, setters, valueOf,
proxies, user callbacks). These refuse to start when an exception is
already pending (napi_pending_exception) or when the env can no longer run
JavaScript (napi_cannot_run_js, or napi_pending_exception for modules
built for Node-API < 10). They are marked JS in the
function index. The other functions only touch C
bookkeeping or create values, and work even with an exception pending: this
is what lets an addon build an error message while an exception is pending.
When JavaScript throws during a call, the function returns
napi_pending_exception and the exception stays pending, exactly as in
Node.js. Returning from the native callback then propagates it to the
JavaScript caller. Use napi_is_exception_pending and
napi_get_and_clear_last_exception to handle it in C.
Functions whose first parameter is node_api_basic_env (cleanup hooks,
instance data, napi_delete_reference, napi_adjust_external_memory,
napi_get_uv_event_loop, ...) may be called from a basic finalizer
(node_api_basic_finalize). In quickjs-napi every finalizer, basic or not,
runs outside the garbage collector (from the event loop or at teardown),
so calling other functions from a basic finalizer never causes the fatal
error Node.js raises; the distinction is kept for source compatibility.
- A
napi_valueis valid until the handle scope it was created in is closed. Values created in a native callback are released when the callback returns, unless they are returned, escaped, or kept in anapi_ref. - Every function except the thread-safe function ones
(
napi_call_threadsafe_function,napi_acquire_threadsafe_function,napi_release_threadsafe_function,napi_get_threadsafe_function_context) must be called on the thread that runs the event loop.executecallbacks of async work run on the thread pool and must not call Node-API functions that take an env.
Addons are written exactly as for Node.js. A minimal C addon:
#include <node_api.h>
#define NAPI_CALL(env, call) \
do { \
if ((call) != napi_ok) { \
const napi_extended_error_info *info; \
bool pending; \
napi_get_last_error_info((env), &info); \
napi_is_exception_pending((env), &pending); \
if (!pending) \
napi_throw_error((env), NULL, info->error_message \
? info->error_message \
: "Node-API call failed"); \
return NULL; \
} \
} while (0)
static napi_value Add(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value argv[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
double a, b;
NAPI_CALL(env, napi_get_value_double(env, argv[0], &a));
NAPI_CALL(env, napi_get_value_double(env, argv[1], &b));
napi_value sum;
NAPI_CALL(env, napi_create_double(env, a + b, &sum));
return sum;
}
NAPI_MODULE_INIT(/* napi_env env, napi_value exports */) {
napi_property_descriptor desc = {"add", NULL, Add, NULL, NULL, NULL,
napi_default, NULL};
NAPI_CALL(env, napi_define_properties(env, exports, 1, &desc));
return exports;
}Build it with node-gyp, cmake-js or any toolchain that produces a Node.js addon, then:
quickjs-napi -e "console.log(require('./build/Release/addon.node').add(2, 3))"The same binary loads in Node.js. node-addon-api (C++) and napi-rs (Rust) addons work the same way; the node-addon-api 8.9.2 test suite runs in CI (testing.md).
The module API version is chosen by the addon at build time (NAPI_VERSION,
exported through node_api_module_get_api_version_v1). It is 8 when the
addon does not declare one, and changes a few behaviours: which values
napi_create_reference accepts, the status of
functions that run JavaScript during
teardown, and the thread-safe function exception policy.
JS: the function may run JavaScript and refuses to start with an exception pending (see above).
| Function | Reference | |
|---|---|---|
napi_acquire_threadsafe_function |
async.md | |
napi_add_async_cleanup_hook |
environment.md | |
napi_add_env_cleanup_hook |
environment.md | |
napi_add_finalizer |
lifetime.md | |
napi_adjust_external_memory |
lifetime.md | |
napi_async_destroy |
node-specific.md | |
napi_async_init |
node-specific.md | |
napi_call_function |
JS | functions.md |
napi_call_threadsafe_function |
async.md | |
napi_cancel_async_work |
async.md | |
napi_check_object_type_tag |
JS | lifetime.md |
napi_close_callback_scope |
node-specific.md | |
napi_close_escapable_handle_scope |
scopes.md | |
napi_close_handle_scope |
scopes.md | |
napi_coerce_to_bool |
JS | values.md |
napi_coerce_to_number |
JS | values.md |
napi_coerce_to_object |
JS | values.md |
napi_coerce_to_string |
JS | values.md |
napi_create_array |
values.md | |
napi_create_array_with_length |
values.md | |
napi_create_arraybuffer |
JS | binary-data.md |
napi_create_async_work |
async.md | |
napi_create_bigint_int64 |
binary-data.md | |
napi_create_bigint_uint64 |
binary-data.md | |
napi_create_bigint_words |
JS | binary-data.md |
napi_create_buffer |
JS | node-specific.md |
napi_create_buffer_copy |
JS | node-specific.md |
napi_create_dataview |
JS | binary-data.md |
napi_create_date |
JS | binary-data.md |
napi_create_double |
values.md | |
napi_create_error |
errors.md | |
napi_create_external |
JS | lifetime.md |
napi_create_external_arraybuffer |
JS | binary-data.md |
napi_create_external_buffer |
JS | node-specific.md |
napi_create_function |
JS | functions.md |
napi_create_int32 |
values.md | |
napi_create_int64 |
values.md | |
napi_create_object |
values.md | |
napi_create_promise |
JS | binary-data.md |
napi_create_range_error |
errors.md | |
napi_create_reference |
lifetime.md | |
napi_create_string_latin1 |
strings.md | |
napi_create_string_utf16 |
strings.md | |
napi_create_string_utf8 |
strings.md | |
napi_create_symbol |
strings.md | |
napi_create_threadsafe_function |
async.md | |
napi_create_type_error |
errors.md | |
napi_create_typedarray |
JS | binary-data.md |
napi_create_uint32 |
values.md | |
napi_define_class |
JS | functions.md |
napi_define_properties |
JS | objects.md |
napi_delete_async_work |
async.md | |
napi_delete_element |
JS | objects.md |
napi_delete_property |
JS | objects.md |
napi_delete_reference |
lifetime.md | |
napi_detach_arraybuffer |
binary-data.md | |
napi_escape_handle |
scopes.md | |
napi_fatal_error |
environment.md | |
napi_fatal_exception |
JS | node-specific.md |
napi_get_all_property_names |
JS | objects.md |
napi_get_and_clear_last_exception |
errors.md | |
napi_get_array_length |
JS | values.md |
napi_get_arraybuffer_info |
binary-data.md | |
napi_get_boolean |
values.md | |
napi_get_buffer_info |
node-specific.md | |
napi_get_cb_info |
functions.md | |
napi_get_dataview_info |
binary-data.md | |
napi_get_date_value |
JS | binary-data.md |
napi_get_element |
JS | objects.md |
napi_get_global |
values.md | |
napi_get_instance_data |
lifetime.md | |
napi_get_last_error_info |
environment.md | |
napi_get_named_property |
JS | objects.md |
napi_get_new_target |
functions.md | |
napi_get_node_version |
environment.md | |
napi_get_null |
values.md | |
napi_get_property |
JS | objects.md |
napi_get_property_names |
JS | objects.md |
napi_get_prototype |
JS | objects.md |
napi_get_reference_value |
lifetime.md | |
napi_get_threadsafe_function_context |
async.md | |
napi_get_typedarray_info |
binary-data.md | |
napi_get_undefined |
values.md | |
napi_get_uv_event_loop |
async.md | |
napi_get_value_bigint_int64 |
binary-data.md | |
napi_get_value_bigint_uint64 |
binary-data.md | |
napi_get_value_bigint_words |
binary-data.md | |
napi_get_value_bool |
values.md | |
napi_get_value_double |
values.md | |
napi_get_value_external |
lifetime.md | |
napi_get_value_int32 |
values.md | |
napi_get_value_int64 |
values.md | |
napi_get_value_string_latin1 |
strings.md | |
napi_get_value_string_utf16 |
strings.md | |
napi_get_value_string_utf8 |
strings.md | |
napi_get_value_uint32 |
values.md | |
napi_get_version |
environment.md | |
napi_has_element |
JS | objects.md |
napi_has_named_property |
JS | objects.md |
napi_has_own_property |
JS | objects.md |
napi_has_property |
JS | objects.md |
napi_instanceof |
JS | objects.md |
napi_is_array |
values.md | |
napi_is_arraybuffer |
binary-data.md | |
napi_is_buffer |
node-specific.md | |
napi_is_dataview |
binary-data.md | |
napi_is_date |
binary-data.md | |
napi_is_detached_arraybuffer |
binary-data.md | |
napi_is_error |
errors.md | |
napi_is_exception_pending |
errors.md | |
napi_is_promise |
binary-data.md | |
napi_is_typedarray |
binary-data.md | |
napi_make_callback |
JS | node-specific.md |
napi_module_register |
environment.md | |
napi_new_instance |
JS | functions.md |
napi_object_freeze |
JS | objects.md |
napi_object_seal |
JS | objects.md |
napi_open_callback_scope |
node-specific.md | |
napi_open_escapable_handle_scope |
scopes.md | |
napi_open_handle_scope |
scopes.md | |
napi_queue_async_work |
async.md | |
napi_ref_threadsafe_function |
async.md | |
napi_reference_ref |
lifetime.md | |
napi_reference_unref |
lifetime.md | |
napi_reject_deferred |
JS | binary-data.md |
napi_release_threadsafe_function |
async.md | |
napi_remove_async_cleanup_hook |
environment.md | |
napi_remove_env_cleanup_hook |
environment.md | |
napi_remove_wrap |
JS | lifetime.md |
napi_resolve_deferred |
JS | binary-data.md |
napi_run_script |
JS | functions.md |
napi_set_element |
JS | objects.md |
napi_set_instance_data |
lifetime.md | |
napi_set_named_property |
JS | objects.md |
napi_set_property |
JS | objects.md |
napi_strict_equals |
JS | values.md |
napi_throw |
JS | errors.md |
napi_throw_error |
JS | errors.md |
napi_throw_range_error |
JS | errors.md |
napi_throw_type_error |
JS | errors.md |
napi_type_tag_object |
JS | lifetime.md |
napi_typeof |
values.md | |
napi_unref_threadsafe_function |
async.md | |
napi_unwrap |
JS | lifetime.md |
napi_wrap |
JS | lifetime.md |
node_api_create_buffer_from_arraybuffer |
JS | node-specific.md |
node_api_create_external_string_latin1 |
strings.md | |
node_api_create_external_string_utf16 |
strings.md | |
node_api_create_property_key_latin1 |
strings.md | |
node_api_create_property_key_utf16 |
strings.md | |
node_api_create_property_key_utf8 |
strings.md | |
node_api_create_syntax_error |
errors.md | |
node_api_get_module_file_name |
environment.md | |
node_api_symbol_for |
strings.md | |
node_api_throw_syntax_error |
JS | errors.md |