Skip to content

Commit

Permalink
s/pure/nogc/g
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielschulhof committed Oct 6, 2023
1 parent d59d264 commit adc4664
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 132 deletions.
28 changes: 14 additions & 14 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
EXTERN_C_START

NAPI_EXTERN napi_status NAPI_CDECL napi_get_last_error_info(
node_api_pure_env env, const napi_extended_error_info** result);
node_api_nogc_env env, const napi_extended_error_info** result);

// Getters for defined singletons
NAPI_EXTERN napi_status NAPI_CDECL napi_get_undefined(napi_env env,
Expand Down Expand Up @@ -97,15 +97,15 @@ NAPI_EXTERN napi_status NAPI_CDECL
node_api_create_external_string_latin1(napi_env env,
char* str,
size_t length,
node_api_pure_finalize finalize_callback,
node_api_nogc_finalize finalize_callback,
void* finalize_hint,
napi_value* result,
bool* copied);
NAPI_EXTERN napi_status NAPI_CDECL
node_api_create_external_string_utf16(napi_env env,
char16_t* str,
size_t length,
node_api_pure_finalize finalize_callback,
node_api_nogc_finalize finalize_callback,
void* finalize_hint,
napi_value* result,
bool* copied);
Expand Down Expand Up @@ -289,7 +289,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_instanceof(napi_env env,

// Gets all callback info in a single call. (Ugly, but faster.)
NAPI_EXTERN napi_status NAPI_CDECL napi_get_cb_info(
node_api_pure_env env, // [in] NAPI environment handle
node_api_nogc_env env, // [in] Node-API 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.
Expand All @@ -313,7 +313,7 @@ napi_define_class(napi_env env,
NAPI_EXTERN napi_status NAPI_CDECL napi_wrap(napi_env env,
napi_value js_object,
void* native_object,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_ref* result);
NAPI_EXTERN napi_status NAPI_CDECL napi_unwrap(napi_env env,
Expand All @@ -325,7 +325,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_remove_wrap(napi_env env,
NAPI_EXTERN napi_status NAPI_CDECL
napi_create_external(napi_env env,
void* data,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_value* result);
NAPI_EXTERN napi_status NAPI_CDECL napi_get_value_external(napi_env env,
Expand Down Expand Up @@ -424,7 +424,7 @@ NAPI_EXTERN napi_status NAPI_CDECL
napi_create_external_arraybuffer(napi_env env,
void* external_data,
size_t byte_length,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_value* result);
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
Expand Down Expand Up @@ -466,7 +466,7 @@ napi_get_dataview_info(napi_env env,
size_t* byte_offset);

// version management
NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_pure_env env,
NAPI_EXTERN napi_status NAPI_CDECL napi_get_version(node_api_nogc_env env,
uint32_t* result);

// Promises
Expand All @@ -490,7 +490,7 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_run_script(napi_env env,

// Memory management
NAPI_EXTERN napi_status NAPI_CDECL napi_adjust_external_memory(
node_api_pure_env env, int64_t change_in_bytes, int64_t* adjusted_value);
node_api_nogc_env env, int64_t change_in_bytes, int64_t* adjusted_value);

#if NAPI_VERSION >= 5

Expand All @@ -512,7 +512,7 @@ NAPI_EXTERN napi_status NAPI_CDECL
napi_add_finalizer(napi_env env,
napi_value js_object,
void* finalize_data,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_ref* result);

Expand All @@ -521,7 +521,7 @@ napi_add_finalizer(napi_env env,
#ifdef NAPI_EXPERIMENTAL

NAPI_EXTERN napi_status NAPI_CDECL
node_api_post_finalizer(node_api_pure_env env,
node_api_post_finalizer(node_api_nogc_env env,
napi_finalize finalize_cb,
void* finalize_data,
void* finalize_hint);
Expand Down Expand Up @@ -566,12 +566,12 @@ napi_get_all_property_names(napi_env env,

// Instance data
NAPI_EXTERN napi_status NAPI_CDECL
napi_set_instance_data(node_api_pure_env env,
napi_set_instance_data(node_api_nogc_env env,
void* data,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint);

NAPI_EXTERN napi_status NAPI_CDECL napi_get_instance_data(node_api_pure_env env,
NAPI_EXTERN napi_status NAPI_CDECL napi_get_instance_data(node_api_nogc_env env,
void** data);
#endif // NAPI_VERSION >= 6

Expand Down
51 changes: 26 additions & 25 deletions src/js_native_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,31 @@ typedef uint16_t char16_t;
// typedef undefined structs instead of void* for compile time type safety
typedef struct napi_env__* napi_env;

// We need to mark APIs which are "pure", meaning that they do not interact
// with the JS engine, and can therefore be called synchronously from a
// finalizer that itself runs synchronously during garbage collection. Such
// "pure" APIs can receive either a `napi_env` or a `node_api_pure_env` as
// their first parameter, because we should be able to also call them during
// normal, non-garbage-collecting operations, whereas "non-pure" APIs can only
// receive a `napi_env` as their first parameter, because we must not call them
// during garbage collection. In lieu of inheritance, we use the properties of
// the const qualifier to accomplish this, because both a const and a non-const
// value can be passed to an API expecting a const value, but only a non-const
// value can be passed to an API expecting a non-const value.
// We need to mark APIs which can be called during garbage collection (GC),
// meaning that they do not affect the state of the JS engine, and can
// therefore be called synchronously from a finalizer that itself runs
// synchronously during GC. Such APIs can receive either a `napi_env` or a
// `node_api_nogc_env` as their first parameter, because we should be able to
// also call them during normal, non-garbage-collecting operations, whereas
// APIs that affect the state of the JS engine can only receive a `napi_env` as
// their first parameter, because we must not call them during GC. In lieu of
// inheritance, we use the properties of the const qualifier to accomplish
// this, because both a const and a non-const value can be passed to an API
// expecting a const value, but only a non-const value can be passed to an API
// expecting a non-const value.
//
// In conjunction with appropriate CFLAGS to warn us if we're passing a const
// (pure) environment into an API that expects a non-const (non-pure)
// environment, and the definition of pure finalizer function pointer types
// below, which receive a pure environment as their first parameter, and can
// thus only call pure APIs (unless the user explicitly casts the environment),
// we achieve the ability to ensure at compile time that we do not call non-
// pure APIs from a synchronous (pure) finalizer.
#if defined(NAPI_EXPERIMENTAL) && defined(NODE_API_EXPERIMENTAL_PURE_ENV)
typedef const struct napi_env__* node_api_pure_env;
// (nogc) environment into an API that expects a non-const environment, and the
// definition of pure finalizer function pointer types below, which receive a
// nogc environment as their first parameter, and can thus only call nogc APIs
// (unless the user explicitly casts the environment), we achieve the ability
// to ensure at compile time that we do not call APIs that affect the state of
// the JS engine from a synchronous (nogc) finalizer.
#if defined(NAPI_EXPERIMENTAL) && defined(NODE_API_EXPERIMENTAL_NOGC_ENV)
typedef const struct napi_env__* node_api_nogc_env;
#else
typedef struct napi_env__* node_api_pure_env;
#endif // NAPI_EXPERIMENTAL && NODE_API_EXPERIMENTAL_PURE_ENV
typedef struct napi_env__* node_api_nogc_env;
#endif // NAPI_EXPERIMENTAL && NODE_API_EXPERIMENTAL_NOGC_ENV

typedef struct napi_value__* napi_value;
typedef struct napi_ref__* napi_ref;
Expand Down Expand Up @@ -141,13 +142,13 @@ typedef napi_value(NAPI_CDECL* napi_callback)(napi_env env,
typedef void(NAPI_CDECL* napi_finalize)(napi_env env,
void* finalize_data,
void* finalize_hint);
#if defined(NAPI_EXPERIMENTAL) && defined(NODE_API_EXPERIMENTAL_PURE_ENV)
typedef void(NAPI_CDECL* node_api_pure_finalize)(node_api_pure_env env,
#if defined(NAPI_EXPERIMENTAL) && defined(NODE_API_EXPERIMENTAL_NOGC_ENV)
typedef void(NAPI_CDECL* node_api_nogc_finalize)(node_api_nogc_env env,
void* finalize_data,
void* finalize_hint);
#else
typedef napi_finalize node_api_pure_finalize;
#endif // NAPI_EXPERIMENTAL && NODE_API_EXPERIMENTAL_PURE_ENV
typedef napi_finalize node_api_nogc_finalize;
#endif // NAPI_EXPERIMENTAL && NODE_API_EXPERIMENTAL_NOGC_ENV

typedef struct {
// One of utf8name or name should be NULL.
Expand Down
46 changes: 23 additions & 23 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,8 @@ static const char* error_messages[] = {
};

napi_status NAPI_CDECL napi_get_last_error_info(
node_api_pure_env pure_env, const napi_extended_error_info** result) {
napi_env env = const_cast<napi_env>(pure_env);
node_api_nogc_env nogc_env, const napi_extended_error_info** result) {
napi_env env = const_cast<napi_env>(nogc_env);
CHECK_ENV(env);
CHECK_ARG(env, result);

Expand Down Expand Up @@ -1602,12 +1602,12 @@ napi_status NAPI_CDECL node_api_create_external_string_latin1(
napi_env env,
char* str,
size_t length,
node_api_pure_finalize pure_finalize_callback,
node_api_nogc_finalize nogc_finalize_callback,
void* finalize_hint,
napi_value* result,
bool* copied) {
napi_finalize finalize_callback =
reinterpret_cast<napi_finalize>(pure_finalize_callback);
reinterpret_cast<napi_finalize>(nogc_finalize_callback);
return v8impl::NewExternalString(
env,
str,
Expand All @@ -1631,12 +1631,12 @@ napi_status NAPI_CDECL node_api_create_external_string_utf16(
napi_env env,
char16_t* str,
size_t length,
node_api_pure_finalize pure_finalize_callback,
node_api_nogc_finalize nogc_finalize_callback,
void* finalize_hint,
napi_value* result,
bool* copied) {
napi_finalize finalize_callback =
reinterpret_cast<napi_finalize>(pure_finalize_callback);
reinterpret_cast<napi_finalize>(nogc_finalize_callback);
return v8impl::NewExternalString(
env,
str,
Expand Down Expand Up @@ -1993,7 +1993,7 @@ napi_status NAPI_CDECL napi_get_null(napi_env env, napi_value* result) {

// Gets all callback info in a single call. (Ugly, but faster.)
napi_status NAPI_CDECL napi_get_cb_info(
node_api_pure_env env, // [in] NAPI environment handle
node_api_nogc_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.
Expand Down Expand Up @@ -2533,10 +2533,10 @@ GEN_COERCE_FUNCTION(STRING, String, string)
napi_status NAPI_CDECL napi_wrap(napi_env env,
napi_value js_object,
void* native_object,
node_api_pure_finalize pure_finalize_cb,
node_api_nogc_finalize nogc_finalize_cb,
void* finalize_hint,
napi_ref* result) {
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(pure_finalize_cb);
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(nogc_finalize_cb);
return v8impl::Wrap(
env, js_object, native_object, finalize_cb, finalize_hint, result);
}
Expand All @@ -2556,10 +2556,10 @@ napi_status NAPI_CDECL napi_remove_wrap(napi_env env,
napi_status NAPI_CDECL
napi_create_external(napi_env env,
void* data,
node_api_pure_finalize pure_finalize_cb,
node_api_nogc_finalize nogc_finalize_cb,
void* finalize_hint,
napi_value* result) {
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(pure_finalize_cb);
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(nogc_finalize_cb);
NAPI_PREAMBLE(env);
CHECK_ARG(env, result);

Expand Down Expand Up @@ -2992,7 +2992,7 @@ napi_status NAPI_CDECL
napi_create_external_arraybuffer(napi_env env,
void* external_data,
size_t byte_length,
node_api_pure_finalize finalize_cb,
node_api_nogc_finalize finalize_cb,
void* finalize_hint,
napi_value* result) {
// The API contract here is that the cleanup function runs on the JS thread,
Expand Down Expand Up @@ -3262,7 +3262,7 @@ napi_status NAPI_CDECL napi_get_dataview_info(napi_env env,
return napi_clear_last_error(env);
}

napi_status NAPI_CDECL napi_get_version(node_api_pure_env env,
napi_status NAPI_CDECL napi_get_version(node_api_nogc_env env,
uint32_t* result) {
CHECK_ENV(env);
CHECK_ARG(env, result);
Expand Down Expand Up @@ -3387,12 +3387,12 @@ napi_status NAPI_CDECL
napi_add_finalizer(napi_env env,
napi_value js_object,
void* finalize_data,
node_api_pure_finalize pure_finalize_cb,
node_api_nogc_finalize nogc_finalize_cb,
void* finalize_hint,
napi_ref* result) {
// Omit NAPI_PREAMBLE and GET_RETURN_STATUS because V8 calls here cannot throw
// JS exceptions.
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(pure_finalize_cb);
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(nogc_finalize_cb);
CHECK_ENV(env);
env->CheckGCAccess();
CHECK_ARG(env, js_object);
Expand All @@ -3417,11 +3417,11 @@ napi_add_finalizer(napi_env env,

#ifdef NAPI_EXPERIMENTAL

napi_status NAPI_CDECL node_api_post_finalizer(node_api_pure_env pure_env,
napi_status NAPI_CDECL node_api_post_finalizer(node_api_nogc_env nogc_env,
napi_finalize finalize_cb,
void* finalize_data,
void* finalize_hint) {
napi_env env = const_cast<napi_env>(pure_env);
napi_env env = const_cast<napi_env>(nogc_env);
CHECK_ENV(env);
env->EnqueueFinalizer(v8impl::TrackedFinalizer::New(
env, finalize_cb, finalize_data, finalize_hint));
Expand All @@ -3430,7 +3430,7 @@ napi_status NAPI_CDECL node_api_post_finalizer(node_api_pure_env pure_env,

#endif

napi_status NAPI_CDECL napi_adjust_external_memory(node_api_pure_env env,
napi_status NAPI_CDECL napi_adjust_external_memory(node_api_nogc_env env,
int64_t change_in_bytes,
int64_t* adjusted_value) {
CHECK_ENV(env);
Expand All @@ -3443,12 +3443,12 @@ napi_status NAPI_CDECL napi_adjust_external_memory(node_api_pure_env env,
}

napi_status NAPI_CDECL
napi_set_instance_data(node_api_pure_env pure_env,
napi_set_instance_data(node_api_nogc_env nogc_env,
void* data,
node_api_pure_finalize pure_finalize_cb,
node_api_nogc_finalize nogc_finalize_cb,
void* finalize_hint) {
napi_env env = const_cast<napi_env>(pure_env);
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(pure_finalize_cb);
napi_env env = const_cast<napi_env>(nogc_env);
napi_finalize finalize_cb = reinterpret_cast<napi_finalize>(nogc_finalize_cb);
CHECK_ENV(env);

v8impl::RefBase* old_data = static_cast<v8impl::RefBase*>(env->instance_data);
Expand All @@ -3464,7 +3464,7 @@ napi_set_instance_data(node_api_pure_env pure_env,
return napi_clear_last_error(env);
}

napi_status NAPI_CDECL napi_get_instance_data(node_api_pure_env env,
napi_status NAPI_CDECL napi_get_instance_data(node_api_nogc_env env,
void** data) {
CHECK_ENV(env);
CHECK_ARG(env, data);
Expand Down
10 changes: 5 additions & 5 deletions src/js_native_api_v8.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "js_native_api_types.h"
#include "js_native_api_v8_internals.h"

inline napi_status napi_clear_last_error(node_api_pure_env env);
inline napi_status napi_clear_last_error(node_api_nogc_env env);

namespace v8impl {

Expand Down Expand Up @@ -172,20 +172,20 @@ struct napi_env__ {
virtual ~napi_env__() = default;
};

inline napi_status napi_clear_last_error(node_api_pure_env pure_env) {
napi_env env = const_cast<napi_env>(pure_env);
inline napi_status napi_clear_last_error(node_api_nogc_env nogc_env) {
napi_env env = const_cast<napi_env>(nogc_env);
env->last_error.error_code = napi_ok;
env->last_error.engine_error_code = 0;
env->last_error.engine_reserved = nullptr;
env->last_error.error_message = nullptr;
return napi_ok;
}

inline napi_status napi_set_last_error(node_api_pure_env pure_env,
inline napi_status napi_set_last_error(node_api_nogc_env nogc_env,
napi_status error_code,
uint32_t engine_error_code = 0,
void* engine_reserved = nullptr) {
napi_env env = const_cast<napi_env>(pure_env);
napi_env env = const_cast<napi_env>(nogc_env);
env->last_error.error_code = error_code;
env->last_error.engine_error_code = engine_error_code;
env->last_error.engine_reserved = engine_reserved;
Expand Down
Loading

0 comments on commit adc4664

Please sign in to comment.