src: factor out Node.js-agnostic N-APIs
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>
This commit is contained in:
parent
451fb04b3c
commit
596bd5f1bb
3
Makefile
3
Makefile
@ -393,7 +393,8 @@ ADDONS_NAPI_BINDING_SOURCES := \
|
||||
# Implicitly depends on $(NODE_EXE), see the build-addons-napi rule for rationale.
|
||||
test/addons-napi/.buildstamp: $(ADDONS_PREREQS) \
|
||||
$(ADDONS_NAPI_BINDING_GYPS) $(ADDONS_NAPI_BINDING_SOURCES) \
|
||||
src/node_api.h src/node_api_types.h
|
||||
src/node_api.h src/node_api_types.h src/js_native_api.h \
|
||||
src/js_native_api_types.h src/js_native_api_v8.h src/js_native_api_v8_internals.h
|
||||
@$(call run_build_addons,"$$PWD/test/addons-napi",$@)
|
||||
|
||||
.PHONY: build-addons-napi
|
||||
|
@ -155,6 +155,65 @@ available to the module code.
|
||||
|
||||
\* Indicates that the N-API version was released as experimental
|
||||
|
||||
The N-APIs associated strictly with accessing ECMAScript features from native
|
||||
code can be found separately in `js_native_api.h` and `js_native_api_types.h`.
|
||||
The APIs defined in these headers are included in `node_api.h` and
|
||||
`node_api_types.h`. The headers are structured in this way in order to allow
|
||||
implementations of N-API outside of Node.js. For those implementations the
|
||||
Node.js specific APIs may not be applicable.
|
||||
|
||||
The Node.js-specific parts of an addon can be separated from the code that
|
||||
exposes the actual functionality to the JavaScript environment so that the
|
||||
latter may be used with multiple implementations of N-API. In the example below,
|
||||
`addon.c` and `addon.h` refer only to `js_native_api.h`. This ensures that
|
||||
`addon.c` can be reused to compile against either the Node.js implementation of
|
||||
N-API or any implementation of N-API outside of Node.js.
|
||||
|
||||
`addon_node.c` is a separate file that contains the Node.js specific entry point
|
||||
to the addon and which instantiates the addon by calling into `addon.c` when the
|
||||
addon is loaded into a Node.js environment.
|
||||
|
||||
```C
|
||||
// addon.h
|
||||
#ifndef _ADDON_H_
|
||||
#define _ADDON_H_
|
||||
#include <js_native_api.h>
|
||||
napi_value create_addon(napi_env env);
|
||||
#endif // _ADDON_H_
|
||||
```
|
||||
|
||||
```C
|
||||
// addon.c
|
||||
#include "addon.h"
|
||||
napi_value create_addon(napi_env env) {
|
||||
napi_value result;
|
||||
assert(napi_create_object(env, &result) == napi_ok);
|
||||
napi_value exported_function;
|
||||
assert(napi_create_function(env,
|
||||
"doSomethingUseful",
|
||||
NAPI_AUTO_LENGTH,
|
||||
DoSomethingUseful,
|
||||
NULL,
|
||||
&exported_function) == napi_ok);
|
||||
assert(napi_set_named_property(env,
|
||||
result,
|
||||
"doSomethingUseful",
|
||||
exported_function) == napi_ok);
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
||||
```C
|
||||
// addon_node.c
|
||||
#include <node_api.h>
|
||||
|
||||
static napi_value Init(napi_env env, napi_value exports) {
|
||||
return create_addon(env);
|
||||
}
|
||||
|
||||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)
|
||||
```
|
||||
|
||||
## Basic N-API Data Types
|
||||
|
||||
N-API exposes the following fundamental datatypes as abstractions that are
|
||||
|
5
node.gyp
5
node.gyp
@ -334,6 +334,11 @@
|
||||
'src/fs_event_wrap.cc',
|
||||
'src/handle_wrap.cc',
|
||||
'src/heap_utils.cc',
|
||||
'src/js_native_api.h',
|
||||
'src/js_native_api_types.h',
|
||||
'src/js_native_api_v8.cc',
|
||||
'src/js_native_api_v8.h',
|
||||
'src/js_native_api_v8_internals.h',
|
||||
'src/js_stream.cc',
|
||||
'src/module_wrap.cc',
|
||||
'src/node.cc',
|
||||
|
485
src/js_native_api.h
Normal file
485
src/js_native_api.h
Normal file
@ -0,0 +1,485 @@
|
||||
#ifndef SRC_JS_NATIVE_API_H_
|
||||
#define SRC_JS_NATIVE_API_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
#ifndef NAPI_VERSION
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
// Use INT_MAX, this should only be consumed by the pre-processor anyway.
|
||||
#define NAPI_VERSION 2147483647
|
||||
#else
|
||||
// The baseline version for N-API
|
||||
#define NAPI_VERSION 3
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// If you need __declspec(dllimport), either include <node_api.h> instead, or
|
||||
// define NAPI_EXTERN as __declspec(dllimport) on the compiler's command line.
|
||||
#ifndef NAPI_EXTERN
|
||||
#ifdef _WIN32
|
||||
#define NAPI_EXTERN __declspec(dllexport)
|
||||
#else
|
||||
#define NAPI_EXTERN /* nothing */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define NAPI_AUTO_LENGTH SIZE_MAX
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN_C_START extern "C" {
|
||||
#define EXTERN_C_END }
|
||||
#else
|
||||
#define EXTERN_C_START
|
||||
#define EXTERN_C_END
|
||||
#endif
|
||||
|
||||
EXTERN_C_START
|
||||
|
||||
NAPI_EXTERN napi_status
|
||||
napi_get_last_error_info(napi_env env,
|
||||
const napi_extended_error_info** result);
|
||||
|
||||
// Getters for defined singletons
|
||||
NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_boolean(napi_env env,
|
||||
bool value,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to create Primitive types/Objects
|
||||
NAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_double(napi_env env,
|
||||
double value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_int32(napi_env env,
|
||||
int32_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_uint32(napi_env env,
|
||||
uint32_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_int64(napi_env env,
|
||||
int64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env,
|
||||
const char* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env,
|
||||
const char* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env,
|
||||
const char16_t* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_symbol(napi_env env,
|
||||
napi_value description,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_function(napi_env env,
|
||||
const char* utf8name,
|
||||
size_t length,
|
||||
napi_callback cb,
|
||||
void* data,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_type_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to get the native napi_value from Primitive type
|
||||
NAPI_EXTERN napi_status napi_typeof(napi_env env,
|
||||
napi_value value,
|
||||
napi_valuetype* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_double(napi_env env,
|
||||
napi_value value,
|
||||
double* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_int32(napi_env env,
|
||||
napi_value value,
|
||||
int32_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_uint32(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_int64(napi_env env,
|
||||
napi_value value,
|
||||
int64_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_bool(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
|
||||
// Copies LATIN-1 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env,
|
||||
napi_value value,
|
||||
char* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Copies UTF-8 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env,
|
||||
napi_value value,
|
||||
char* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Copies UTF-16 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env,
|
||||
napi_value value,
|
||||
char16_t* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Methods to coerce values
|
||||
// These APIs may execute user scripts
|
||||
NAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_number(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_object(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_string(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to work with Objects
|
||||
NAPI_EXTERN napi_status napi_get_prototype(napi_env env,
|
||||
napi_value object,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_property_names(napi_env env,
|
||||
napi_value object,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_set_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_delete_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_has_own_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_set_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_set_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_delete_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_define_properties(napi_env env,
|
||||
napi_value object,
|
||||
size_t property_count,
|
||||
const napi_property_descriptor* properties);
|
||||
|
||||
// Methods to work with Arrays
|
||||
NAPI_EXTERN napi_status napi_is_array(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_array_length(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t* result);
|
||||
|
||||
// Methods to compare values
|
||||
NAPI_EXTERN napi_status napi_strict_equals(napi_env env,
|
||||
napi_value lhs,
|
||||
napi_value rhs,
|
||||
bool* result);
|
||||
|
||||
// Methods to work with Functions
|
||||
NAPI_EXTERN napi_status napi_call_function(napi_env env,
|
||||
napi_value recv,
|
||||
napi_value func,
|
||||
size_t argc,
|
||||
const napi_value* argv,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_new_instance(napi_env env,
|
||||
napi_value constructor,
|
||||
size_t argc,
|
||||
const napi_value* argv,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_instanceof(napi_env env,
|
||||
napi_value object,
|
||||
napi_value constructor,
|
||||
bool* result);
|
||||
|
||||
// Methods to work with napi_callbacks
|
||||
|
||||
// Gets all callback info in a single call. (Ugly, but faster.)
|
||||
NAPI_EXTERN napi_status napi_get_cb_info(
|
||||
napi_env env, // [in] NAPI environment handle
|
||||
napi_callback_info cbinfo, // [in] Opaque callback-info handle
|
||||
size_t* argc, // [in-out] Specifies the size of the provided argv array
|
||||
// and receives the actual count of args.
|
||||
napi_value* argv, // [out] Array of values
|
||||
napi_value* this_arg, // [out] Receives the JS 'this' arg for the call
|
||||
void** data); // [out] Receives the data pointer for the callback.
|
||||
|
||||
NAPI_EXTERN napi_status napi_get_new_target(napi_env env,
|
||||
napi_callback_info cbinfo,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_define_class(napi_env env,
|
||||
const char* utf8name,
|
||||
size_t length,
|
||||
napi_callback constructor,
|
||||
void* data,
|
||||
size_t property_count,
|
||||
const napi_property_descriptor* properties,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to work with external data objects
|
||||
NAPI_EXTERN napi_status napi_wrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void* native_object,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_ref* result);
|
||||
NAPI_EXTERN napi_status napi_unwrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void** result);
|
||||
NAPI_EXTERN napi_status napi_remove_wrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void** result);
|
||||
NAPI_EXTERN napi_status napi_create_external(napi_env env,
|
||||
void* data,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_external(napi_env env,
|
||||
napi_value value,
|
||||
void** result);
|
||||
|
||||
// Methods to control object lifespan
|
||||
|
||||
// Set initial_refcount to 0 for a weak reference, >0 for a strong reference.
|
||||
NAPI_EXTERN napi_status napi_create_reference(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t initial_refcount,
|
||||
napi_ref* result);
|
||||
|
||||
// Deletes a reference. The referenced value is released, and may
|
||||
// be GC'd unless there are other references to it.
|
||||
NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref);
|
||||
|
||||
// Increments the reference count, optionally returning the resulting count.
|
||||
// After this call the reference will be a strong reference because its
|
||||
// refcount is >0, and the referenced object is effectively "pinned".
|
||||
// Calling this when the refcount is 0 and the object is unavailable
|
||||
// results in an error.
|
||||
NAPI_EXTERN napi_status napi_reference_ref(napi_env env,
|
||||
napi_ref ref,
|
||||
uint32_t* result);
|
||||
|
||||
// Decrements the reference count, optionally returning the resulting count.
|
||||
// If the result is 0 the reference is now weak and the object may be GC'd
|
||||
// at any time if there are no other references. Calling this when the
|
||||
// refcount is already 0 results in an error.
|
||||
NAPI_EXTERN napi_status napi_reference_unref(napi_env env,
|
||||
napi_ref ref,
|
||||
uint32_t* result);
|
||||
|
||||
// Attempts to get a referenced value. If the reference is weak,
|
||||
// the value might no longer be available, in that case the call
|
||||
// is still successful but the result is NULL.
|
||||
NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
|
||||
napi_ref ref,
|
||||
napi_value* result);
|
||||
|
||||
NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env,
|
||||
napi_handle_scope* result);
|
||||
NAPI_EXTERN napi_status napi_close_handle_scope(napi_env env,
|
||||
napi_handle_scope scope);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_open_escapable_handle_scope(napi_env env,
|
||||
napi_escapable_handle_scope* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_close_escapable_handle_scope(napi_env env,
|
||||
napi_escapable_handle_scope scope);
|
||||
|
||||
NAPI_EXTERN napi_status napi_escape_handle(napi_env env,
|
||||
napi_escapable_handle_scope scope,
|
||||
napi_value escapee,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to support error handling
|
||||
NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error);
|
||||
NAPI_EXTERN napi_status napi_throw_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_is_error(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
|
||||
// Methods to support catching exceptions
|
||||
NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to work with array buffers and typed arrays
|
||||
NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env,
|
||||
size_t byte_length,
|
||||
void** data,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_create_external_arraybuffer(napi_env env,
|
||||
void* external_data,
|
||||
size_t byte_length,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env,
|
||||
napi_value arraybuffer,
|
||||
void** data,
|
||||
size_t* byte_length);
|
||||
NAPI_EXTERN napi_status napi_is_typedarray(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_create_typedarray(napi_env env,
|
||||
napi_typedarray_type type,
|
||||
size_t length,
|
||||
napi_value arraybuffer,
|
||||
size_t byte_offset,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env,
|
||||
napi_value typedarray,
|
||||
napi_typedarray_type* type,
|
||||
size_t* length,
|
||||
void** data,
|
||||
napi_value* arraybuffer,
|
||||
size_t* byte_offset);
|
||||
|
||||
NAPI_EXTERN napi_status napi_create_dataview(napi_env env,
|
||||
size_t length,
|
||||
napi_value arraybuffer,
|
||||
size_t byte_offset,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_is_dataview(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env,
|
||||
napi_value dataview,
|
||||
size_t* bytelength,
|
||||
void** data,
|
||||
napi_value* arraybuffer,
|
||||
size_t* byte_offset);
|
||||
|
||||
// version management
|
||||
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);
|
||||
|
||||
// Promises
|
||||
NAPI_EXTERN napi_status napi_create_promise(napi_env env,
|
||||
napi_deferred* deferred,
|
||||
napi_value* promise);
|
||||
NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env,
|
||||
napi_deferred deferred,
|
||||
napi_value resolution);
|
||||
NAPI_EXTERN napi_status napi_reject_deferred(napi_env env,
|
||||
napi_deferred deferred,
|
||||
napi_value rejection);
|
||||
NAPI_EXTERN napi_status napi_is_promise(napi_env env,
|
||||
napi_value promise,
|
||||
bool* is_promise);
|
||||
|
||||
// Running a script
|
||||
NAPI_EXTERN napi_status napi_run_script(napi_env env,
|
||||
napi_value script,
|
||||
napi_value* result);
|
||||
|
||||
// Memory management
|
||||
NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
|
||||
int64_t change_in_bytes,
|
||||
int64_t* adjusted_value);
|
||||
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
|
||||
NAPI_EXTERN napi_status napi_create_bigint_int64(napi_env env,
|
||||
int64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_bigint_uint64(napi_env env,
|
||||
uint64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_bigint_words(napi_env env,
|
||||
int sign_bit,
|
||||
size_t word_count,
|
||||
const uint64_t* words,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_int64(napi_env env,
|
||||
napi_value value,
|
||||
int64_t* result,
|
||||
bool* lossless);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_uint64(napi_env env,
|
||||
napi_value value,
|
||||
uint64_t* result,
|
||||
bool* lossless);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env,
|
||||
napi_value value,
|
||||
int* sign_bit,
|
||||
size_t* word_count,
|
||||
uint64_t* words);
|
||||
NAPI_EXTERN napi_status napi_add_finalizer(napi_env env,
|
||||
napi_value js_object,
|
||||
void* native_object,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_ref* result);
|
||||
#endif // NAPI_EXPERIMENTAL
|
||||
|
||||
EXTERN_C_END
|
||||
|
||||
#endif // SRC_JS_NATIVE_API_H_
|
108
src/js_native_api_types.h
Normal file
108
src/js_native_api_types.h
Normal file
@ -0,0 +1,108 @@
|
||||
#ifndef SRC_JS_NATIVE_API_TYPES_H_
|
||||
#define SRC_JS_NATIVE_API_TYPES_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900)
|
||||
typedef uint16_t char16_t;
|
||||
#endif
|
||||
|
||||
// JSVM API types are all opaque pointers for ABI stability
|
||||
// typedef undefined structs instead of void* for compile time type safety
|
||||
typedef struct napi_env__* napi_env;
|
||||
typedef struct napi_value__* napi_value;
|
||||
typedef struct napi_ref__* napi_ref;
|
||||
typedef struct napi_handle_scope__* napi_handle_scope;
|
||||
typedef struct napi_escapable_handle_scope__* napi_escapable_handle_scope;
|
||||
typedef struct napi_callback_info__* napi_callback_info;
|
||||
typedef struct napi_deferred__* napi_deferred;
|
||||
|
||||
typedef enum {
|
||||
napi_default = 0,
|
||||
napi_writable = 1 << 0,
|
||||
napi_enumerable = 1 << 1,
|
||||
napi_configurable = 1 << 2,
|
||||
|
||||
// Used with napi_define_class to distinguish static properties
|
||||
// from instance properties. Ignored by napi_define_properties.
|
||||
napi_static = 1 << 10,
|
||||
} napi_property_attributes;
|
||||
|
||||
typedef enum {
|
||||
// ES6 types (corresponds to typeof)
|
||||
napi_undefined,
|
||||
napi_null,
|
||||
napi_boolean,
|
||||
napi_number,
|
||||
napi_string,
|
||||
napi_symbol,
|
||||
napi_object,
|
||||
napi_function,
|
||||
napi_external,
|
||||
napi_bigint,
|
||||
} napi_valuetype;
|
||||
|
||||
typedef enum {
|
||||
napi_int8_array,
|
||||
napi_uint8_array,
|
||||
napi_uint8_clamped_array,
|
||||
napi_int16_array,
|
||||
napi_uint16_array,
|
||||
napi_int32_array,
|
||||
napi_uint32_array,
|
||||
napi_float32_array,
|
||||
napi_float64_array,
|
||||
napi_bigint64_array,
|
||||
napi_biguint64_array,
|
||||
} napi_typedarray_type;
|
||||
|
||||
typedef enum {
|
||||
napi_ok,
|
||||
napi_invalid_arg,
|
||||
napi_object_expected,
|
||||
napi_string_expected,
|
||||
napi_name_expected,
|
||||
napi_function_expected,
|
||||
napi_number_expected,
|
||||
napi_boolean_expected,
|
||||
napi_array_expected,
|
||||
napi_generic_failure,
|
||||
napi_pending_exception,
|
||||
napi_cancelled,
|
||||
napi_escape_called_twice,
|
||||
napi_handle_scope_mismatch,
|
||||
napi_callback_scope_mismatch,
|
||||
napi_queue_full,
|
||||
napi_closing,
|
||||
napi_bigint_expected,
|
||||
} napi_status;
|
||||
|
||||
typedef napi_value (*napi_callback)(napi_env env,
|
||||
napi_callback_info info);
|
||||
typedef void (*napi_finalize)(napi_env env,
|
||||
void* finalize_data,
|
||||
void* finalize_hint);
|
||||
|
||||
typedef struct {
|
||||
// One of utf8name or name should be NULL.
|
||||
const char* utf8name;
|
||||
napi_value name;
|
||||
|
||||
napi_callback method;
|
||||
napi_callback getter;
|
||||
napi_callback setter;
|
||||
napi_value value;
|
||||
|
||||
napi_property_attributes attributes;
|
||||
void* data;
|
||||
} napi_property_descriptor;
|
||||
|
||||
typedef struct {
|
||||
const char* error_message;
|
||||
void* engine_reserved;
|
||||
uint32_t engine_error_code;
|
||||
napi_status error_code;
|
||||
} napi_extended_error_info;
|
||||
|
||||
#endif // SRC_JS_NATIVE_API_TYPES_H_
|
2907
src/js_native_api_v8.cc
Normal file
2907
src/js_native_api_v8.cc
Normal file
File diff suppressed because it is too large
Load Diff
202
src/js_native_api_v8.h
Normal file
202
src/js_native_api_v8.h
Normal file
@ -0,0 +1,202 @@
|
||||
#ifndef SRC_JS_NATIVE_API_V8_H_
|
||||
#define SRC_JS_NATIVE_API_V8_H_
|
||||
|
||||
#include <string.h>
|
||||
#include "js_native_api_types.h"
|
||||
#include "js_native_api_v8_internals.h"
|
||||
|
||||
struct napi_env__ {
|
||||
explicit napi_env__(v8::Local<v8::Context> context)
|
||||
: isolate(context->GetIsolate()),
|
||||
context_persistent(isolate, context) {
|
||||
CHECK_EQ(isolate, context->GetIsolate());
|
||||
}
|
||||
v8::Isolate* const isolate; // Shortcut for context()->GetIsolate()
|
||||
v8impl::Persistent<v8::Context> context_persistent;
|
||||
|
||||
inline v8::Local<v8::Context> context() const {
|
||||
return v8impl::PersistentToLocal::Strong(context_persistent);
|
||||
}
|
||||
|
||||
inline void Ref() { refs++; }
|
||||
inline void Unref() { if ( --refs == 0) delete this; }
|
||||
|
||||
v8impl::Persistent<v8::Value> last_exception;
|
||||
napi_extended_error_info last_error;
|
||||
int open_handle_scopes = 0;
|
||||
int open_callback_scopes = 0;
|
||||
int refs = 1;
|
||||
};
|
||||
|
||||
static inline napi_status napi_clear_last_error(napi_env env) {
|
||||
env->last_error.error_code = napi_ok;
|
||||
|
||||
// TODO(boingoing): Should this be a callback?
|
||||
env->last_error.engine_error_code = 0;
|
||||
env->last_error.engine_reserved = nullptr;
|
||||
return napi_ok;
|
||||
}
|
||||
|
||||
static inline
|
||||
napi_status napi_set_last_error(napi_env env, napi_status error_code,
|
||||
uint32_t engine_error_code = 0,
|
||||
void* engine_reserved = nullptr) {
|
||||
env->last_error.error_code = error_code;
|
||||
env->last_error.engine_error_code = engine_error_code;
|
||||
env->last_error.engine_reserved = engine_reserved;
|
||||
return error_code;
|
||||
}
|
||||
|
||||
#define RETURN_STATUS_IF_FALSE(env, condition, status) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
return napi_set_last_error((env), (status)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_ENV(env) \
|
||||
do { \
|
||||
if ((env) == nullptr) { \
|
||||
return napi_invalid_arg; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_ARG(env, arg) \
|
||||
RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
|
||||
|
||||
#define CHECK_MAYBE_EMPTY(env, maybe, status) \
|
||||
RETURN_STATUS_IF_FALSE((env), !((maybe).IsEmpty()), (status))
|
||||
|
||||
// NAPI_PREAMBLE is not wrapped in do..while: try_catch must have function scope
|
||||
#define NAPI_PREAMBLE(env) \
|
||||
CHECK_ENV((env)); \
|
||||
RETURN_STATUS_IF_FALSE((env), (env)->last_exception.IsEmpty(), \
|
||||
napi_pending_exception); \
|
||||
napi_clear_last_error((env)); \
|
||||
v8impl::TryCatch try_catch((env))
|
||||
|
||||
#define CHECK_TO_TYPE(env, type, context, result, src, status) \
|
||||
do { \
|
||||
CHECK_ARG((env), (src)); \
|
||||
auto maybe = v8impl::V8LocalValueFromJsValue((src))->To##type((context)); \
|
||||
CHECK_MAYBE_EMPTY((env), maybe, (status)); \
|
||||
(result) = maybe.ToLocalChecked(); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_TO_FUNCTION(env, result, src) \
|
||||
do { \
|
||||
CHECK_ARG((env), (src)); \
|
||||
v8::Local<v8::Value> v8value = v8impl::V8LocalValueFromJsValue((src)); \
|
||||
RETURN_STATUS_IF_FALSE((env), v8value->IsFunction(), napi_invalid_arg); \
|
||||
(result) = v8value.As<v8::Function>(); \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_TO_OBJECT(env, context, result, src) \
|
||||
CHECK_TO_TYPE((env), Object, (context), (result), (src), napi_object_expected)
|
||||
|
||||
#define CHECK_TO_STRING(env, context, result, src) \
|
||||
CHECK_TO_TYPE((env), String, (context), (result), (src), napi_string_expected)
|
||||
|
||||
#define GET_RETURN_STATUS(env) \
|
||||
(!try_catch.HasCaught() ? napi_ok \
|
||||
: napi_set_last_error((env), napi_pending_exception))
|
||||
|
||||
#define THROW_RANGE_ERROR_IF_FALSE(env, condition, error, message) \
|
||||
do { \
|
||||
if (!(condition)) { \
|
||||
napi_throw_range_error((env), (error), (message)); \
|
||||
return napi_set_last_error((env), napi_generic_failure); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define NAPI_CALL_INTO_MODULE(env, call, handle_exception) \
|
||||
do { \
|
||||
int open_handle_scopes = (env)->open_handle_scopes; \
|
||||
int open_callback_scopes = (env)->open_callback_scopes; \
|
||||
napi_clear_last_error((env)); \
|
||||
call; \
|
||||
CHECK_EQ((env)->open_handle_scopes, open_handle_scopes); \
|
||||
CHECK_EQ((env)->open_callback_scopes, open_callback_scopes); \
|
||||
if (!(env)->last_exception.IsEmpty()) { \
|
||||
handle_exception( \
|
||||
v8::Local<v8::Value>::New((env)->isolate, (env)->last_exception)); \
|
||||
(env)->last_exception.Reset(); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define NAPI_CALL_INTO_MODULE_THROW(env, call) \
|
||||
NAPI_CALL_INTO_MODULE((env), call, (env)->isolate->ThrowException)
|
||||
|
||||
namespace v8impl {
|
||||
|
||||
//=== Conversion between V8 Handles and napi_value ========================
|
||||
|
||||
// This asserts v8::Local<> will always be implemented with a single
|
||||
// pointer field so that we can pass it around as a void*.
|
||||
static_assert(sizeof(v8::Local<v8::Value>) == sizeof(napi_value),
|
||||
"Cannot convert between v8::Local<v8::Value> and napi_value");
|
||||
|
||||
inline napi_value JsValueFromV8LocalValue(v8::Local<v8::Value> local) {
|
||||
return reinterpret_cast<napi_value>(*local);
|
||||
}
|
||||
|
||||
inline v8::Local<v8::Value> V8LocalValueFromJsValue(napi_value v) {
|
||||
v8::Local<v8::Value> local;
|
||||
memcpy(&local, &v, sizeof(v));
|
||||
return local;
|
||||
}
|
||||
|
||||
// Adapter for napi_finalize callbacks.
|
||||
class Finalizer {
|
||||
protected:
|
||||
Finalizer(napi_env env,
|
||||
napi_finalize finalize_callback,
|
||||
void* finalize_data,
|
||||
void* finalize_hint)
|
||||
: _env(env),
|
||||
_finalize_callback(finalize_callback),
|
||||
_finalize_data(finalize_data),
|
||||
_finalize_hint(finalize_hint) {
|
||||
}
|
||||
|
||||
~Finalizer() {
|
||||
}
|
||||
|
||||
public:
|
||||
static Finalizer* New(napi_env env,
|
||||
napi_finalize finalize_callback = nullptr,
|
||||
void* finalize_data = nullptr,
|
||||
void* finalize_hint = nullptr) {
|
||||
return new Finalizer(
|
||||
env, finalize_callback, finalize_data, finalize_hint);
|
||||
}
|
||||
|
||||
static void Delete(Finalizer* finalizer) {
|
||||
delete finalizer;
|
||||
}
|
||||
|
||||
protected:
|
||||
napi_env _env;
|
||||
napi_finalize _finalize_callback;
|
||||
void* _finalize_data;
|
||||
void* _finalize_hint;
|
||||
};
|
||||
|
||||
class TryCatch : public v8::TryCatch {
|
||||
public:
|
||||
explicit TryCatch(napi_env env)
|
||||
: v8::TryCatch(env->isolate), _env(env) {}
|
||||
|
||||
~TryCatch() {
|
||||
if (HasCaught()) {
|
||||
_env->last_exception.Reset(_env->isolate, Exception());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
napi_env _env;
|
||||
};
|
||||
|
||||
} // end of namespace v8impl
|
||||
|
||||
#endif // SRC_JS_NATIVE_API_V8_H_
|
35
src/js_native_api_v8_internals.h
Normal file
35
src/js_native_api_v8_internals.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef SRC_JS_NATIVE_API_V8_INTERNALS_H_
|
||||
#define SRC_JS_NATIVE_API_V8_INTERNALS_H_
|
||||
|
||||
// The V8 implementation of N-API, including `js_native_api_v8.h` uses certain
|
||||
// idioms which require definition here. For example, it uses a variant of
|
||||
// persistent references which need not be reset in the constructor. It is the
|
||||
// responsibility of this file to define these idioms.
|
||||
|
||||
// In the case of the Node.js implementation of N-API some of the idioms are
|
||||
// imported directly from Node.js by including `node_internals.h` below. Others
|
||||
// are bridged to remove references to the `node` namespace.
|
||||
|
||||
#include "node_version.h"
|
||||
#include "env.h"
|
||||
#include "node_internals.h"
|
||||
|
||||
#define NAPI_ARRAYSIZE(array) \
|
||||
node::arraysize((array))
|
||||
|
||||
#define NAPI_FIXED_ONE_BYTE_STRING(isolate, string) \
|
||||
node::FIXED_ONE_BYTE_STRING((isolate), (string))
|
||||
|
||||
#define NAPI_PRIVATE_KEY(context, suffix) \
|
||||
(node::Environment::GetCurrent((context))->napi_ ## suffix())
|
||||
|
||||
namespace v8impl {
|
||||
|
||||
template <typename T>
|
||||
using Persistent = node::Persistent<T>;
|
||||
|
||||
using PersistentToLocal = node::PersistentToLocal;
|
||||
|
||||
} // end of namespace v8impl
|
||||
|
||||
#endif // SRC_JS_NATIVE_API_V8_INTERNALS_H_
|
3196
src/node_api.cc
3196
src/node_api.cc
File diff suppressed because it is too large
Load Diff
519
src/node_api.h
519
src/node_api.h
@ -1,39 +1,17 @@
|
||||
#ifndef SRC_NODE_API_H_
|
||||
#define SRC_NODE_API_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.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.
|
||||
|
||||
#ifndef NAPI_VERSION
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
// Use INT_MAX, this should only be consumed by the pre-processor anyway.
|
||||
#define NAPI_VERSION 2147483647
|
||||
#else
|
||||
// The baseline version for N-API
|
||||
#define NAPI_VERSION 3
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef BUILDING_NODE_EXTENSION
|
||||
#ifdef EXTERNAL_NAPI
|
||||
// Building external N-API, or native module against external N-API
|
||||
#define NAPI_EXTERN /* nothing */
|
||||
#else
|
||||
// Building native module against node with built-in N-API
|
||||
#define NAPI_EXTERN __declspec(dllimport)
|
||||
#endif
|
||||
#else
|
||||
// Building node with built-in N-API
|
||||
#define NAPI_EXTERN __declspec(dllexport)
|
||||
#endif
|
||||
#else
|
||||
#define NAPI_EXTERN /* nothing */
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
# define NAPI_MODULE_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
@ -46,7 +24,6 @@ struct uv_loop_s; // Forward declaration.
|
||||
#define NAPI_NO_RETURN
|
||||
#endif
|
||||
|
||||
|
||||
typedef napi_value (*napi_addon_register_func)(napi_env env,
|
||||
napi_value exports);
|
||||
|
||||
@ -75,14 +52,6 @@ typedef struct {
|
||||
static void fn(void)
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN_C_START extern "C" {
|
||||
#define EXTERN_C_END }
|
||||
#else
|
||||
#define EXTERN_C_START
|
||||
#define EXTERN_C_END
|
||||
#endif
|
||||
|
||||
#define NAPI_MODULE_X(modname, regfunc, priv, flags) \
|
||||
EXTERN_C_START \
|
||||
static napi_module _module = \
|
||||
@ -122,347 +91,31 @@ typedef struct {
|
||||
napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
|
||||
napi_value exports)
|
||||
|
||||
#define NAPI_AUTO_LENGTH SIZE_MAX
|
||||
|
||||
EXTERN_C_START
|
||||
|
||||
NAPI_EXTERN void napi_module_register(napi_module* mod);
|
||||
|
||||
NAPI_EXTERN napi_status
|
||||
napi_get_last_error_info(napi_env env,
|
||||
const napi_extended_error_info** result);
|
||||
|
||||
NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
|
||||
size_t location_len,
|
||||
const char* message,
|
||||
size_t message_len);
|
||||
|
||||
// Getters for defined singletons
|
||||
NAPI_EXTERN napi_status napi_get_undefined(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_null(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_global(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_boolean(napi_env env,
|
||||
bool value,
|
||||
napi_value* result);
|
||||
// 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);
|
||||
|
||||
// Methods to create Primitive types/Objects
|
||||
NAPI_EXTERN napi_status napi_create_object(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_array(napi_env env, napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_array_with_length(napi_env env,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_double(napi_env env,
|
||||
double value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_int32(napi_env env,
|
||||
int32_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_uint32(napi_env env,
|
||||
uint32_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_int64(napi_env env,
|
||||
int64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_latin1(napi_env env,
|
||||
const char* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_utf8(napi_env env,
|
||||
const char* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_string_utf16(napi_env env,
|
||||
const char16_t* str,
|
||||
size_t length,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_symbol(napi_env env,
|
||||
napi_value description,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_function(napi_env env,
|
||||
const char* utf8name,
|
||||
size_t length,
|
||||
napi_callback cb,
|
||||
void* data,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_type_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_range_error(napi_env env,
|
||||
napi_value code,
|
||||
napi_value msg,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_async_destroy(napi_env env,
|
||||
napi_async_context async_context);
|
||||
|
||||
// Methods to get the native napi_value from Primitive type
|
||||
NAPI_EXTERN napi_status napi_typeof(napi_env env,
|
||||
napi_value value,
|
||||
napi_valuetype* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_double(napi_env env,
|
||||
napi_value value,
|
||||
double* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_int32(napi_env env,
|
||||
napi_value value,
|
||||
int32_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_uint32(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_int64(napi_env env,
|
||||
napi_value value,
|
||||
int64_t* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_bool(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
|
||||
// Copies LATIN-1 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_latin1(napi_env env,
|
||||
napi_value value,
|
||||
char* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Copies UTF-8 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_utf8(napi_env env,
|
||||
napi_value value,
|
||||
char* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Copies UTF-16 encoded bytes from a string into a buffer.
|
||||
NAPI_EXTERN napi_status napi_get_value_string_utf16(napi_env env,
|
||||
napi_value value,
|
||||
char16_t* buf,
|
||||
size_t bufsize,
|
||||
size_t* result);
|
||||
|
||||
// Methods to coerce values
|
||||
// These APIs may execute user scripts
|
||||
NAPI_EXTERN napi_status napi_coerce_to_bool(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_number(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_object(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_coerce_to_string(napi_env env,
|
||||
napi_value value,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to work with Objects
|
||||
NAPI_EXTERN napi_status napi_get_prototype(napi_env env,
|
||||
napi_value object,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_property_names(napi_env env,
|
||||
napi_value object,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_set_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_delete_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_has_own_property(napi_env env,
|
||||
napi_value object,
|
||||
napi_value key,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_set_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_named_property(napi_env env,
|
||||
napi_value object,
|
||||
const char* utf8name,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_set_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
napi_value value);
|
||||
NAPI_EXTERN napi_status napi_has_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_delete_element(napi_env env,
|
||||
napi_value object,
|
||||
uint32_t index,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_define_properties(napi_env env,
|
||||
napi_value object,
|
||||
size_t property_count,
|
||||
const napi_property_descriptor* properties);
|
||||
|
||||
// Methods to work with Arrays
|
||||
NAPI_EXTERN napi_status napi_is_array(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_array_length(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t* result);
|
||||
|
||||
// Methods to compare values
|
||||
NAPI_EXTERN napi_status napi_strict_equals(napi_env env,
|
||||
napi_value lhs,
|
||||
napi_value rhs,
|
||||
bool* result);
|
||||
|
||||
// Methods to work with Functions
|
||||
NAPI_EXTERN napi_status napi_call_function(napi_env env,
|
||||
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);
|
||||
NAPI_EXTERN napi_status napi_new_instance(napi_env env,
|
||||
napi_value constructor,
|
||||
size_t argc,
|
||||
const napi_value* argv,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_instanceof(napi_env env,
|
||||
napi_value object,
|
||||
napi_value constructor,
|
||||
bool* result);
|
||||
|
||||
// Methods to work with napi_callbacks
|
||||
|
||||
// Gets all callback info in a single call. (Ugly, but faster.)
|
||||
NAPI_EXTERN napi_status napi_get_cb_info(
|
||||
napi_env env, // [in] NAPI environment handle
|
||||
napi_callback_info cbinfo, // [in] Opaque callback-info handle
|
||||
size_t* argc, // [in-out] Specifies the size of the provided argv array
|
||||
// and receives the actual count of args.
|
||||
napi_value* argv, // [out] Array of values
|
||||
napi_value* this_arg, // [out] Receives the JS 'this' arg for the call
|
||||
void** data); // [out] Receives the data pointer for the callback.
|
||||
|
||||
NAPI_EXTERN napi_status napi_get_new_target(napi_env env,
|
||||
napi_callback_info cbinfo,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_define_class(napi_env env,
|
||||
const char* utf8name,
|
||||
size_t length,
|
||||
napi_callback constructor,
|
||||
void* data,
|
||||
size_t property_count,
|
||||
const napi_property_descriptor* properties,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to work with external data objects
|
||||
NAPI_EXTERN napi_status napi_wrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void* native_object,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_ref* result);
|
||||
NAPI_EXTERN napi_status napi_unwrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void** result);
|
||||
NAPI_EXTERN napi_status napi_remove_wrap(napi_env env,
|
||||
napi_value js_object,
|
||||
void** result);
|
||||
NAPI_EXTERN napi_status napi_create_external(napi_env env,
|
||||
void* data,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_external(napi_env env,
|
||||
napi_value value,
|
||||
void** result);
|
||||
|
||||
// Methods to control object lifespan
|
||||
|
||||
// Set initial_refcount to 0 for a weak reference, >0 for a strong reference.
|
||||
NAPI_EXTERN napi_status napi_create_reference(napi_env env,
|
||||
napi_value value,
|
||||
uint32_t initial_refcount,
|
||||
napi_ref* result);
|
||||
|
||||
// Deletes a reference. The referenced value is released, and may
|
||||
// be GC'd unless there are other references to it.
|
||||
NAPI_EXTERN napi_status napi_delete_reference(napi_env env, napi_ref ref);
|
||||
|
||||
// Increments the reference count, optionally returning the resulting count.
|
||||
// After this call the reference will be a strong reference because its
|
||||
// refcount is >0, and the referenced object is effectively "pinned".
|
||||
// Calling this when the refcount is 0 and the object is unavailable
|
||||
// results in an error.
|
||||
NAPI_EXTERN napi_status napi_reference_ref(napi_env env,
|
||||
napi_ref ref,
|
||||
uint32_t* result);
|
||||
|
||||
// Decrements the reference count, optionally returning the resulting count.
|
||||
// If the result is 0 the reference is now weak and the object may be GC'd
|
||||
// at any time if there are no other references. Calling this when the
|
||||
// refcount is already 0 results in an error.
|
||||
NAPI_EXTERN napi_status napi_reference_unref(napi_env env,
|
||||
napi_ref ref,
|
||||
uint32_t* result);
|
||||
|
||||
// Attempts to get a referenced value. If the reference is weak,
|
||||
// the value might no longer be available, in that case the call
|
||||
// is still successful but the result is NULL.
|
||||
NAPI_EXTERN napi_status napi_get_reference_value(napi_env env,
|
||||
napi_ref ref,
|
||||
napi_value* result);
|
||||
|
||||
NAPI_EXTERN napi_status napi_open_handle_scope(napi_env env,
|
||||
napi_handle_scope* result);
|
||||
NAPI_EXTERN napi_status napi_close_handle_scope(napi_env env,
|
||||
napi_handle_scope scope);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_open_escapable_handle_scope(napi_env env,
|
||||
napi_escapable_handle_scope* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_close_escapable_handle_scope(napi_env env,
|
||||
napi_escapable_handle_scope scope);
|
||||
|
||||
NAPI_EXTERN napi_status napi_escape_handle(napi_env env,
|
||||
napi_escapable_handle_scope scope,
|
||||
napi_value escapee,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to support error handling
|
||||
NAPI_EXTERN napi_status napi_throw(napi_env env, napi_value error);
|
||||
NAPI_EXTERN napi_status napi_throw_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_throw_type_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_throw_range_error(napi_env env,
|
||||
const char* code,
|
||||
const char* msg);
|
||||
NAPI_EXTERN napi_status napi_is_error(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
|
||||
// Methods to support catching exceptions
|
||||
NAPI_EXTERN napi_status napi_is_exception_pending(napi_env env, bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_and_clear_last_exception(napi_env env,
|
||||
napi_value* result);
|
||||
|
||||
// Methods to provide node::Buffer functionality with napi types
|
||||
NAPI_EXTERN napi_status napi_create_buffer(napi_env env,
|
||||
@ -488,57 +141,6 @@ NAPI_EXTERN napi_status napi_get_buffer_info(napi_env env,
|
||||
void** data,
|
||||
size_t* length);
|
||||
|
||||
// Methods to work with array buffers and typed arrays
|
||||
NAPI_EXTERN napi_status napi_is_arraybuffer(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_create_arraybuffer(napi_env env,
|
||||
size_t byte_length,
|
||||
void** data,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status
|
||||
napi_create_external_arraybuffer(napi_env env,
|
||||
void* external_data,
|
||||
size_t byte_length,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_arraybuffer_info(napi_env env,
|
||||
napi_value arraybuffer,
|
||||
void** data,
|
||||
size_t* byte_length);
|
||||
NAPI_EXTERN napi_status napi_is_typedarray(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_create_typedarray(napi_env env,
|
||||
napi_typedarray_type type,
|
||||
size_t length,
|
||||
napi_value arraybuffer,
|
||||
size_t byte_offset,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_typedarray_info(napi_env env,
|
||||
napi_value typedarray,
|
||||
napi_typedarray_type* type,
|
||||
size_t* length,
|
||||
void** data,
|
||||
napi_value* arraybuffer,
|
||||
size_t* byte_offset);
|
||||
|
||||
NAPI_EXTERN napi_status napi_create_dataview(napi_env env,
|
||||
size_t length,
|
||||
napi_value arraybuffer,
|
||||
size_t byte_offset,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_is_dataview(napi_env env,
|
||||
napi_value value,
|
||||
bool* result);
|
||||
NAPI_EXTERN napi_status napi_get_dataview_info(napi_env env,
|
||||
napi_value dataview,
|
||||
size_t* bytelength,
|
||||
void** data,
|
||||
napi_value* arraybuffer,
|
||||
size_t* byte_offset);
|
||||
|
||||
// Methods to manage simple async operations
|
||||
NAPI_EXTERN
|
||||
napi_status napi_create_async_work(napi_env env,
|
||||
@ -555,54 +157,11 @@ NAPI_EXTERN napi_status napi_queue_async_work(napi_env env,
|
||||
NAPI_EXTERN napi_status napi_cancel_async_work(napi_env env,
|
||||
napi_async_work work);
|
||||
|
||||
// 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);
|
||||
|
||||
// version management
|
||||
NAPI_EXTERN napi_status napi_get_version(napi_env env, uint32_t* result);
|
||||
|
||||
NAPI_EXTERN
|
||||
napi_status napi_get_node_version(napi_env env,
|
||||
const napi_node_version** version);
|
||||
|
||||
// Promises
|
||||
NAPI_EXTERN napi_status napi_create_promise(napi_env env,
|
||||
napi_deferred* deferred,
|
||||
napi_value* promise);
|
||||
NAPI_EXTERN napi_status napi_resolve_deferred(napi_env env,
|
||||
napi_deferred deferred,
|
||||
napi_value resolution);
|
||||
NAPI_EXTERN napi_status napi_reject_deferred(napi_env env,
|
||||
napi_deferred deferred,
|
||||
napi_value rejection);
|
||||
NAPI_EXTERN napi_status napi_is_promise(napi_env env,
|
||||
napi_value promise,
|
||||
bool* is_promise);
|
||||
|
||||
// Memory management
|
||||
NAPI_EXTERN napi_status napi_adjust_external_memory(napi_env env,
|
||||
int64_t change_in_bytes,
|
||||
int64_t* adjusted_value);
|
||||
|
||||
// Running a script
|
||||
NAPI_EXTERN napi_status napi_run_script(napi_env env,
|
||||
napi_value script,
|
||||
napi_value* result);
|
||||
|
||||
#if NAPI_VERSION >= 2
|
||||
|
||||
// Return the current libuv event loop for a given environment
|
||||
@ -613,14 +172,6 @@ NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
|
||||
|
||||
#if NAPI_VERSION >= 3
|
||||
|
||||
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);
|
||||
|
||||
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,
|
||||
@ -631,6 +182,14 @@ 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
|
||||
@ -671,36 +230,6 @@ 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);
|
||||
|
||||
NAPI_EXTERN napi_status napi_create_bigint_int64(napi_env env,
|
||||
int64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_bigint_uint64(napi_env env,
|
||||
uint64_t value,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_create_bigint_words(napi_env env,
|
||||
int sign_bit,
|
||||
size_t word_count,
|
||||
const uint64_t* words,
|
||||
napi_value* result);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_int64(napi_env env,
|
||||
napi_value value,
|
||||
int64_t* result,
|
||||
bool* lossless);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_uint64(napi_env env,
|
||||
napi_value value,
|
||||
uint64_t* result,
|
||||
bool* lossless);
|
||||
NAPI_EXTERN napi_status napi_get_value_bigint_words(napi_env env,
|
||||
napi_value value,
|
||||
int* sign_bit,
|
||||
size_t* word_count,
|
||||
uint64_t* words);
|
||||
NAPI_EXTERN napi_status napi_add_finalizer(napi_env env,
|
||||
napi_value js_object,
|
||||
void* native_object,
|
||||
napi_finalize finalize_cb,
|
||||
void* finalize_hint,
|
||||
napi_ref* result);
|
||||
#endif // NAPI_EXPERIMENTAL
|
||||
|
||||
EXTERN_C_END
|
||||
|
@ -1,89 +1,15 @@
|
||||
#ifndef SRC_NODE_API_TYPES_H_
|
||||
#define SRC_NODE_API_TYPES_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "js_native_api_types.h"
|
||||
|
||||
#if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900)
|
||||
typedef uint16_t char16_t;
|
||||
#endif
|
||||
|
||||
// JSVM API types are all opaque pointers for ABI stability
|
||||
// typedef undefined structs instead of void* for compile time type safety
|
||||
typedef struct napi_env__* napi_env;
|
||||
typedef struct napi_value__* napi_value;
|
||||
typedef struct napi_ref__* napi_ref;
|
||||
typedef struct napi_handle_scope__* napi_handle_scope;
|
||||
typedef struct napi_escapable_handle_scope__* napi_escapable_handle_scope;
|
||||
typedef struct napi_callback_scope__* napi_callback_scope;
|
||||
typedef struct napi_callback_info__* napi_callback_info;
|
||||
typedef struct napi_async_context__* napi_async_context;
|
||||
typedef struct napi_async_work__* napi_async_work;
|
||||
typedef struct napi_deferred__* napi_deferred;
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
typedef struct napi_threadsafe_function__* napi_threadsafe_function;
|
||||
#endif // NAPI_EXPERIMENTAL
|
||||
|
||||
typedef enum {
|
||||
napi_default = 0,
|
||||
napi_writable = 1 << 0,
|
||||
napi_enumerable = 1 << 1,
|
||||
napi_configurable = 1 << 2,
|
||||
|
||||
// Used with napi_define_class to distinguish static properties
|
||||
// from instance properties. Ignored by napi_define_properties.
|
||||
napi_static = 1 << 10,
|
||||
} napi_property_attributes;
|
||||
|
||||
typedef enum {
|
||||
// ES6 types (corresponds to typeof)
|
||||
napi_undefined,
|
||||
napi_null,
|
||||
napi_boolean,
|
||||
napi_number,
|
||||
napi_string,
|
||||
napi_symbol,
|
||||
napi_object,
|
||||
napi_function,
|
||||
napi_external,
|
||||
napi_bigint,
|
||||
} napi_valuetype;
|
||||
|
||||
typedef enum {
|
||||
napi_int8_array,
|
||||
napi_uint8_array,
|
||||
napi_uint8_clamped_array,
|
||||
napi_int16_array,
|
||||
napi_uint16_array,
|
||||
napi_int32_array,
|
||||
napi_uint32_array,
|
||||
napi_float32_array,
|
||||
napi_float64_array,
|
||||
napi_bigint64_array,
|
||||
napi_biguint64_array,
|
||||
} napi_typedarray_type;
|
||||
|
||||
typedef enum {
|
||||
napi_ok,
|
||||
napi_invalid_arg,
|
||||
napi_object_expected,
|
||||
napi_string_expected,
|
||||
napi_name_expected,
|
||||
napi_function_expected,
|
||||
napi_number_expected,
|
||||
napi_boolean_expected,
|
||||
napi_array_expected,
|
||||
napi_generic_failure,
|
||||
napi_pending_exception,
|
||||
napi_cancelled,
|
||||
napi_escape_called_twice,
|
||||
napi_handle_scope_mismatch,
|
||||
napi_callback_scope_mismatch,
|
||||
napi_queue_full,
|
||||
napi_closing,
|
||||
napi_bigint_expected,
|
||||
} napi_status;
|
||||
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
typedef enum {
|
||||
napi_tsfn_release,
|
||||
@ -96,17 +22,11 @@ typedef enum {
|
||||
} napi_threadsafe_function_call_mode;
|
||||
#endif // NAPI_EXPERIMENTAL
|
||||
|
||||
typedef napi_value (*napi_callback)(napi_env env,
|
||||
napi_callback_info info);
|
||||
typedef void (*napi_finalize)(napi_env env,
|
||||
void* finalize_data,
|
||||
void* finalize_hint);
|
||||
typedef void (*napi_async_execute_callback)(napi_env env,
|
||||
void* data);
|
||||
typedef void (*napi_async_complete_callback)(napi_env env,
|
||||
napi_status status,
|
||||
void* data);
|
||||
|
||||
#ifdef NAPI_EXPERIMENTAL
|
||||
typedef void (*napi_threadsafe_function_call_js)(napi_env env,
|
||||
napi_value js_callback,
|
||||
@ -114,27 +34,6 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
|
||||
void* data);
|
||||
#endif // NAPI_EXPERIMENTAL
|
||||
|
||||
typedef struct {
|
||||
// One of utf8name or name should be NULL.
|
||||
const char* utf8name;
|
||||
napi_value name;
|
||||
|
||||
napi_callback method;
|
||||
napi_callback getter;
|
||||
napi_callback setter;
|
||||
napi_value value;
|
||||
|
||||
napi_property_attributes attributes;
|
||||
void* data;
|
||||
} napi_property_descriptor;
|
||||
|
||||
typedef struct {
|
||||
const char* error_message;
|
||||
void* engine_reserved;
|
||||
uint32_t engine_error_code;
|
||||
napi_status error_code;
|
||||
} napi_extended_error_info;
|
||||
|
||||
typedef struct {
|
||||
uint32_t major;
|
||||
uint32_t minor;
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <node_api.h>
|
||||
#include "myobject.h"
|
||||
#include "../common.h"
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef TEST_ADDONS_NAPI_6_OBJECT_WRAP_MYOBJECT_H_
|
||||
#define TEST_ADDONS_NAPI_6_OBJECT_WRAP_MYOBJECT_H_
|
||||
|
||||
#include <node_api.h>
|
||||
#include <js_native_api.h>
|
||||
|
||||
class MyObject {
|
||||
public:
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <node_api.h>
|
||||
#include "myobject.h"
|
||||
#include "../common.h"
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef TEST_ADDONS_NAPI_7_FACTORY_WRAP_MYOBJECT_H_
|
||||
#define TEST_ADDONS_NAPI_7_FACTORY_WRAP_MYOBJECT_H_
|
||||
|
||||
#include <node_api.h>
|
||||
#include <js_native_api.h>
|
||||
|
||||
class MyObject {
|
||||
public:
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <node_api.h>
|
||||
#include "myobject.h"
|
||||
#include "../common.h"
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
#ifndef TEST_ADDONS_NAPI_8_PASSING_WRAPPED_MYOBJECT_H_
|
||||
#define TEST_ADDONS_NAPI_8_PASSING_WRAPPED_MYOBJECT_H_
|
||||
|
||||
#include <node_api.h>
|
||||
#include <js_native_api.h>
|
||||
|
||||
class MyObject {
|
||||
public:
|
||||
|
@ -169,6 +169,8 @@ def headers(action):
|
||||
'config.gypi',
|
||||
'src/node.h',
|
||||
'src/node_api.h',
|
||||
'src/js_native_api.h',
|
||||
'src/js_native_api_types.h',
|
||||
'src/node_api_types.h',
|
||||
'src/node_buffer.h',
|
||||
'src/node_object_wrap.h',
|
||||
|
Loading…
x
Reference in New Issue
Block a user