Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v8.x-backport] n-api: mark thread-safe function as stable #25648

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 169 additions & 11 deletions doc/api/n-api.md

Large diffs are not rendered by default.

60 changes: 41 additions & 19 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

#include <stddef.h>
#include <stdbool.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 4
#endif
#endif

#include "node_api_types.h"

struct uv_loop_s; // Forward declaration.
Expand Down Expand Up @@ -99,19 +110,10 @@ EXTERN_C_START

NAPI_EXTERN void napi_module_register(napi_module* mod);

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_get_last_error_info(napi_env env,
const napi_extended_error_info** result);

NAPI_EXTERN napi_status napi_fatal_exception(napi_env env, napi_value err);

NAPI_EXTERN NAPI_NO_RETURN void napi_fatal_error(const char* location,
size_t location_len,
const char* message,
Expand Down Expand Up @@ -424,14 +426,6 @@ NAPI_EXTERN napi_status napi_escape_handle(napi_env env,
napi_value escapee,
napi_value* result);

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);

// 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,
Expand Down Expand Up @@ -591,11 +585,38 @@ 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
NAPI_EXTERN napi_status napi_get_uv_event_loop(napi_env env,
struct uv_loop_s** loop);

#ifdef NAPI_EXPERIMENTAL
#endif // NAPI_VERSION >= 2

#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,
void (*fun)(void* arg),
void* arg);

NAPI_EXTERN napi_status napi_remove_env_cleanup_hook(napi_env env,
void (*fun)(void* arg),
void* arg);

#endif // NAPI_VERSION >= 3

#if NAPI_VERSION >= 4

// Calling into JS from other threads
gabrielschulhof marked this conversation as resolved.
Show resolved Hide resolved
NAPI_EXTERN napi_status
napi_create_threadsafe_function(napi_env env,
Expand Down Expand Up @@ -632,7 +653,8 @@ 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
#endif // NAPI_VERSION >= 4

EXTERN_C_END

#endif // SRC_NODE_API_H_
16 changes: 8 additions & 8 deletions src/node_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ 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
#if NAPI_VERSION >= 4
typedef struct napi_threadsafe_function__* napi_threadsafe_function;
#endif // NAPI_EXPERIMENTAL
#endif // NAPI_VERSION >= 4

typedef enum {
napi_default = 0,
Expand Down Expand Up @@ -76,13 +76,13 @@ typedef enum {
napi_escape_called_twice,
napi_handle_scope_mismatch,
napi_callback_scope_mismatch,
#ifdef NAPI_EXPERIMENTAL
#if NAPI_VERSION >= 4
napi_queue_full,
napi_closing,
#endif // NAPI_EXPERIMENTAL
#endif // NAPI_VERSION >= 4
} napi_status;

#ifdef NAPI_EXPERIMENTAL
#if NAPI_VERSION >= 4
typedef enum {
napi_tsfn_release,
napi_tsfn_abort
Expand All @@ -92,7 +92,7 @@ typedef enum {
napi_tsfn_nonblocking,
napi_tsfn_blocking
} napi_threadsafe_function_call_mode;
#endif // NAPI_EXPERIMENTAL
#endif // NAPI_VERSION >= 4

typedef napi_value (*napi_callback)(napi_env env,
napi_callback_info info);
Expand All @@ -105,12 +105,12 @@ typedef void (*napi_async_complete_callback)(napi_env env,
napi_status status,
void* data);

#ifdef NAPI_EXPERIMENTAL
#if NAPI_VERSION >= 4
typedef void (*napi_threadsafe_function_call_js)(napi_env env,
napi_value js_callback,
void* context,
void* data);
#endif // NAPI_EXPERIMENTAL
#endif // NAPI_VERSION >= 4

typedef struct {
// One of utf8name or name should be NULL.
Expand Down
2 changes: 1 addition & 1 deletion src/node_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@
#define NODE_MODULE_VERSION 57

// the NAPI_VERSION provided by this version of the runtime
#define NAPI_VERSION 3
#define NAPI_VERSION 4

#endif // SRC_NODE_VERSION_H_
4 changes: 2 additions & 2 deletions test/addons-napi/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ assert.notStrictEqual(test_general.testGetPrototype(baseObject),
test_general.testGetPrototype(extendedObject));

// test version management functions
// expected version is currently 3
assert.strictEqual(test_general.testGetVersion(), 3);
// expected version is currently 4
assert.strictEqual(test_general.testGetVersion(), 4);

const [ major, minor, patch, release ] = test_general.testGetNodeVersion();
assert.strictEqual(process.version.split('-')[0],
Expand Down
1 change: 0 additions & 1 deletion test/addons-napi/test_threadsafe_function/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// which, in turn, may affect the ABI stability of the project despite its use
// of N-API.
#include <uv.h>
#define NAPI_EXPERIMENTAL
#include <node_api.h>
#include "../common.h"

Expand Down
4 changes: 4 additions & 0 deletions tools/doc/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ function extractAndParseYAML(text) {
meta.added = arrify(meta.added);
}

if (meta.napiVersion) {
meta.napiVersion = arrify(meta.napiVersion);
}

if (meta.deprecated) {
// Treat deprecated like added for consistency.
meta.deprecated = arrify(meta.deprecated);
Expand Down
4 changes: 4 additions & 0 deletions tools/doc/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ function parseYAML(text) {
html.push(`${added.description}${deprecated.description}`);
}

if (meta.napiVersion) {
html.push(`<span>N-API version: ${meta.napiVersion.join(', ')}</span>\n`);
}

html.push('</div>');
return html.join('\n');
}
Expand Down