GeezerVison is showing github.com — src: factor out Node.js-agnostic N-APIs · nodejs/node@af3c7ef
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: Makefile +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -393,7 +393,8 @@ ADDONS_NAPI_BINDING_SOURCES := \ | 393 | 393 | # Implicitly depends on $(NODE_EXE), see the build-addons-napi rule for rationale. | 394 | 394 | test/addons-napi/.buildstamp: $(ADDONS_PREREQS) \ | 395 | 395 | $(ADDONS_NAPI_BINDING_GYPS) $(ADDONS_NAPI_BINDING_SOURCES) \ | 396 | | - src/node_api.h src/node_api_types.h | | 396 | + src/node_api.h src/node_api_types.h src/js_native_api.h \ | | 397 | + src/js_native_api_types.h src/js_native_api_v8.h src/js_native_api_v8_internals.h | 397 | 398 | @$(call run_build_addons,"$$PWD/test/addons-napi",$@) | 398 | 399 |
| 399 | 400 | .PHONY: build-addons-napi | |
Collapse file Copy file name to clipboardExpand all lines: doc/api/n-api.md +59Lines changed: 59 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -155,6 +155,65 @@ available to the module code. | 155 | 155 |
| 156 | 156 | \* Indicates that the N-API version was released as experimental | 157 | 157 |
| | 158 | +The N-APIs associated strictly with accessing ECMAScript features from native | | 159 | +code can be found separately in `js_native_api.h` and `js_native_api_types.h`. | | 160 | +The APIs defined in these headers are included in `node_api.h` and | | 161 | +`node_api_types.h`. The headers are structured in this way in order to allow | | 162 | +implementations of N-API outside of Node.js. For those implementations the | | 163 | +Node.js specific APIs may not be applicable. | | 164 | + | | 165 | +The Node.js-specific parts of an addon can be separated from the code that | | 166 | +exposes the actual functionality to the JavaScript environment so that the | | 167 | +latter may be used with multiple implementations of N-API. In the example below, | | 168 | +`addon.c` and `addon.h` refer only to `js_native_api.h`. This ensures that | | 169 | +`addon.c` can be reused to compile against either the Node.js implementation of | | 170 | +N-API or any implementation of N-API outside of Node.js. | | 171 | + | | 172 | +`addon_node.c` is a separate file that contains the Node.js specific entry point | | 173 | +to the addon and which instantiates the addon by calling into `addon.c` when the | | 174 | +addon is loaded into a Node.js environment. | | 175 | + | | 176 | +```C | | 177 | +// addon.h | | 178 | +#ifndef _ADDON_H_ | | 179 | +#define _ADDON_H_ | | 180 | +#include <js_native_api.h> | | 181 | +napi_value create_addon(napi_env env); | | 182 | +#endif // _ADDON_H_ | | 183 | +``` | | 184 | + | | 185 | +```C | | 186 | +// addon.c | | 187 | +#include "addon.h" | | 188 | +napi_value create_addon(napi_env env) { | | 189 | + napi_value result; | | 190 | + assert(napi_create_object(env, &result) == napi_ok); | | 191 | + napi_value exported_function; | | 192 | + assert(napi_create_function(env, | | 193 | + "doSomethingUseful", | | 194 | + NAPI_AUTO_LENGTH, | | 195 | + DoSomethingUseful, | | 196 | + NULL, | | 197 | + &exported_function) == napi_ok); | | 198 | + assert(napi_set_named_property(env, | | 199 | + result, | | 200 | + "doSomethingUseful", | | 201 | + exported_function) == napi_ok); | | 202 | + return result; | | 203 | +} | | 204 | +``` | | 205 | + | | 206 | +```C | | 207 | +// addon_node.c | | 208 | +#include <node_api.h> | | 209 | + | | 210 | +static napi_value Init(napi_env env, napi_value exports) { | | 211 | + return create_addon(env); | | 212 | +} | | 213 | + | | 214 | +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) | | 215 | +``` | | 216 | + | 158 | 217 | ## Basic N-API Data Types | 159 | 218 |
| 160 | 219 | N-API exposes the following fundamental datatypes as abstractions that are | |
Collapse file Copy file name to clipboardExpand all lines: node.gyp +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 334 | 334 | 'src/fs_event_wrap.cc', | 335 | 335 | 'src/handle_wrap.cc', | 336 | 336 | 'src/heap_utils.cc', | | 337 | + 'src/js_native_api.h', | | 338 | + 'src/js_native_api_types.h', | | 339 | + 'src/js_native_api_v8.cc', | | 340 | + 'src/js_native_api_v8.h', | | 341 | + 'src/js_native_api_v8_internals.h', | 337 | 342 | 'src/js_stream.cc', | 338 | 343 | 'src/module_wrap.cc', | 339 | 344 | 'src/node.cc', | |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api.h +485Lines changed: 485 additions & 0 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboard +108Lines changed: 108 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#ifndef SRC_JS_NATIVE_API_TYPES_H_ | | 2 | +#define SRC_JS_NATIVE_API_TYPES_H_ | | 3 | + | | 4 | +#include <stddef.h> | | 5 | +#include <stdint.h> | | 6 | + | | 7 | +#if !defined __cplusplus || (defined(_MSC_VER) && _MSC_VER < 1900) | | 8 | + typedef uint16_t char16_t; | | 9 | +#endif | | 10 | + | | 11 | +// JSVM API types are all opaque pointers for ABI stability | | 12 | +// typedef undefined structs instead of void* for compile time type safety | | 13 | +typedef struct napi_env__* napi_env; | | 14 | +typedef struct napi_value__* napi_value; | | 15 | +typedef struct napi_ref__* napi_ref; | | 16 | +typedef struct napi_handle_scope__* napi_handle_scope; | | 17 | +typedef struct napi_escapable_handle_scope__* napi_escapable_handle_scope; | | 18 | +typedef struct napi_callback_info__* napi_callback_info; | | 19 | +typedef struct napi_deferred__* napi_deferred; | | 20 | + | | 21 | +typedef enum { | | 22 | + napi_default = 0, | | 23 | + napi_writable = 1 << 0, | | 24 | + napi_enumerable = 1 << 1, | | 25 | + napi_configurable = 1 << 2, | | 26 | + | | 27 | + // Used with napi_define_class to distinguish static properties | | 28 | + // from instance properties. Ignored by napi_define_properties. | | 29 | + napi_static = 1 << 10, | | 30 | +} napi_property_attributes; | | 31 | + | | 32 | +typedef enum { | | 33 | + // ES6 types (corresponds to typeof) | | 34 | + napi_undefined, | | 35 | + napi_null, | | 36 | + napi_boolean, | | 37 | + napi_number, | | 38 | + napi_string, | | 39 | + napi_symbol, | | 40 | + napi_object, | | 41 | + napi_function, | | 42 | + napi_external, | | 43 | + napi_bigint, | | 44 | +} napi_valuetype; | | 45 | + | | 46 | +typedef enum { | | 47 | + napi_int8_array, | | 48 | + napi_uint8_array, | | 49 | + napi_uint8_clamped_array, | | 50 | + napi_int16_array, | | 51 | + napi_uint16_array, | | 52 | + napi_int32_array, | | 53 | + napi_uint32_array, | | 54 | + napi_float32_array, | | 55 | + napi_float64_array, | | 56 | + napi_bigint64_array, | | 57 | + napi_biguint64_array, | | 58 | +} napi_typedarray_type; | | 59 | + | | 60 | +typedef enum { | | 61 | + napi_ok, | | 62 | + napi_invalid_arg, | | 63 | + napi_object_expected, | | 64 | + napi_string_expected, | | 65 | + napi_name_expected, | | 66 | + napi_function_expected, | | 67 | + napi_number_expected, | | 68 | + napi_boolean_expected, | | 69 | + napi_array_expected, | | 70 | + napi_generic_failure, | | 71 | + napi_pending_exception, | | 72 | + napi_cancelled, | | 73 | + napi_escape_called_twice, | | 74 | + napi_handle_scope_mismatch, | | 75 | + napi_callback_scope_mismatch, | | 76 | + napi_queue_full, | | 77 | + napi_closing, | | 78 | + napi_bigint_expected, | | 79 | +} napi_status; | | 80 | + | | 81 | +typedef napi_value (*napi_callback)(napi_env env, | | 82 | + napi_callback_info info); | | 83 | +typedef void (*napi_finalize)(napi_env env, | | 84 | + void* finalize_data, | | 85 | + void* finalize_hint); | | 86 | + | | 87 | +typedef struct { | | 88 | + // One of utf8name or name should be NULL. | | 89 | + const char* utf8name; | | 90 | + napi_value name; | | 91 | + | | 92 | + napi_callback method; | | 93 | + napi_callback getter; | | 94 | + napi_callback setter; | | 95 | + napi_value value; | | 96 | + | | 97 | + napi_property_attributes attributes; | | 98 | + void* data; | | 99 | +} napi_property_descriptor; | | 100 | + | | 101 | +typedef struct { | | 102 | + const char* error_message; | | 103 | + void* engine_reserved; | | 104 | + uint32_t engine_error_code; | | 105 | + napi_status error_code; | | 106 | +} napi_extended_error_info; | | 107 | + | | 108 | +#endif // SRC_JS_NATIVE_API_TYPES_H_ |
|