Skip to content

Commit

Permalink
factor out common code into macros (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Nov 17, 2023
1 parent 1994ef9 commit 563c3ea
Show file tree
Hide file tree
Showing 20 changed files with 151 additions and 238 deletions.
6 changes: 2 additions & 4 deletions packages/emnapi/src/async_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ napi_async_init(napi_env env,
napi_value async_resource,
napi_value async_resource_name,
napi_async_context* result) {
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, async_resource_name);
CHECK_ARG(env, result);

Expand All @@ -39,8 +38,7 @@ napi_async_init(napi_env env,

napi_status napi_async_destroy(napi_env env,
napi_async_context async_context) {
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, async_context);

napi_status status = _emnapi_async_destroy_js(async_context);
Expand Down
6 changes: 2 additions & 4 deletions packages/emnapi/src/async_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ napi_status napi_create_async_work(napi_env env,
void* data,
napi_async_work* result) {
#if EMNAPI_HAVE_THREADS
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, execute);
CHECK_ARG(env, result);

Expand Down Expand Up @@ -188,8 +187,7 @@ napi_status napi_create_async_work(napi_env env,

napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
#if EMNAPI_HAVE_THREADS
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ENV_NOT_IN_GC(env);
CHECK_ARG(env, work);

async_work_delete(work);
Expand Down
16 changes: 4 additions & 12 deletions packages/emnapi/src/core/async-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ var emnapiAWMT = {

var _napi_create_async_work = singleThreadAsyncWork
? function (env: napi_env, resource: napi_value, resource_name: napi_value, execute: number, complete: number, data: number, result: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand All @@ -251,9 +249,7 @@ var _napi_create_async_work = singleThreadAsyncWork
return envObject.clearLastError()
}
: function (env: napi_env, resource: napi_value, resource_name: napi_value, execute: number, complete: number, data: number, result: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand Down Expand Up @@ -287,18 +283,14 @@ var _napi_create_async_work = singleThreadAsyncWork

var _napi_delete_async_work = singleThreadAsyncWork
? function (env: napi_env, work: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, work)

emnapiAWST.remove(work)
return envObject.clearLastError()
}
: function (env: napi_env, work: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, work)

const resource = emnapiAWMT.getResource(work)
Expand Down
6 changes: 6 additions & 0 deletions packages/emnapi/src/emnapi_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ EXTERN_C_END
#define CHECK_ARG(env, arg) \
RETURN_STATUS_IF_FALSE((env), ((arg) != NULL), napi_invalid_arg)

#define CHECK_ENV_NOT_IN_GC(env) \
do { \
CHECK_ENV((env)); \
_emnapi_env_check_gc_access((env)); \
} while (0)

#define CHECK(expr) \
do { \
if (!(expr)) { \
Expand Down
8 changes: 2 additions & 6 deletions packages/emnapi/src/emscripten/async-work.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function _napi_create_async_work (env: napi_env, resource: napi_value, resource_name: napi_value, execute: number, complete: number, data: number, result: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand All @@ -23,9 +21,7 @@ function _napi_create_async_work (env: napi_env, resource: napi_value, resource_
}

function _napi_delete_async_work (env: napi_env, work: number): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, work)

emnapiAWST.remove(work)
Expand Down
24 changes: 6 additions & 18 deletions packages/emnapi/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ function node_api_throw_syntax_error (env: napi_env, code: const_char_p, msg: co
}

function napi_is_exception_pending (env: napi_env, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const r = envObject.tryCatch.hasCaught()
Expand All @@ -95,9 +93,7 @@ function napi_is_exception_pending (env: napi_env, result: Pointer<bool>): napi_
}

function napi_create_error (env: napi_env, code: napi_value, msg: napi_value, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -123,9 +119,7 @@ function napi_create_error (env: napi_env, code: napi_value, msg: napi_value, re
}

function napi_create_type_error (env: napi_env, code: napi_value, msg: napi_value, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -150,9 +144,7 @@ function napi_create_type_error (env: napi_env, code: napi_value, msg: napi_valu
}

function napi_create_range_error (env: napi_env, code: napi_value, msg: napi_value, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -176,9 +168,7 @@ function napi_create_range_error (env: napi_env, code: napi_value, msg: napi_val
}

function node_api_create_syntax_error (env: napi_env, code: napi_value, msg: napi_value, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -202,9 +192,7 @@ function node_api_create_syntax_error (env: napi_env, code: napi_value, msg: nap
}

function napi_get_and_clear_last_exception (env: napi_env, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, result)
$from64('result')

Expand Down
6 changes: 2 additions & 4 deletions packages/emnapi/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,9 @@ function napi_get_new_target (
cbinfo: napi_callback_info,
result: Pointer<napi_value>
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
if (!cbinfo) return envObject.setLastError(napi_status.napi_invalid_arg)
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, cbinfo)
$CHECK_ARG!(envObject, result)
envObject.checkGCAccess()

$from64('result')

Expand Down
40 changes: 10 additions & 30 deletions packages/emnapi/src/life.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function napi_open_handle_scope (env: napi_env, result: Pointer<napi_handle_scope>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const scope = emnapiCtx.openScope(envObject)
Expand All @@ -11,9 +9,7 @@ function napi_open_handle_scope (env: napi_env, result: Pointer<napi_handle_scop
}

function napi_close_handle_scope (env: napi_env, scope: napi_handle_scope): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, scope)
if ((envObject.openHandleScopes === 0)) {
return napi_status.napi_handle_scope_mismatch
Expand All @@ -24,9 +20,7 @@ function napi_close_handle_scope (env: napi_env, scope: napi_handle_scope): napi
}

function napi_open_escapable_handle_scope (env: napi_env, result: Pointer<napi_escapable_handle_scope>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const scope = emnapiCtx.openScope(envObject)
Expand All @@ -36,9 +30,7 @@ function napi_open_escapable_handle_scope (env: napi_env, result: Pointer<napi_e
}

function napi_close_escapable_handle_scope (env: napi_env, scope: napi_escapable_handle_scope): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, scope)
if ((envObject.openHandleScopes === 0)) {
return napi_status.napi_handle_scope_mismatch
Expand All @@ -49,9 +41,7 @@ function napi_close_escapable_handle_scope (env: napi_env, scope: napi_escapable
}

function napi_escape_handle (env: napi_env, scope: napi_escapable_handle_scope, escapee: napi_value, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, scope)
$CHECK_ARG!(envObject, escapee)
$CHECK_ARG!(envObject, result)
Expand All @@ -75,9 +65,7 @@ function napi_create_reference (
initial_refcount: uint32_t,
result: Pointer<napi_ref>
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)

Expand All @@ -98,9 +86,7 @@ function napi_delete_reference (
env: napi_env,
ref: napi_ref
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, ref)
emnapiCtx.refStore.get(ref)!.dispose()
return envObject.clearLastError()
Expand All @@ -111,9 +97,7 @@ function napi_reference_ref (
ref: napi_ref,
result: Pointer<uint32_t>
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, ref)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const count = emnapiCtx.refStore.get(ref)!.ref()
Expand All @@ -129,9 +113,7 @@ function napi_reference_unref (
ref: napi_ref,
result: Pointer<uint32_t>
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, ref)
const reference = emnapiCtx.refStore.get(ref)!
const refcount = reference.refCount()
Expand All @@ -153,9 +135,7 @@ function napi_get_reference_value (
ref: napi_ref,
result: Pointer<napi_value>
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, ref)
$CHECK_ARG!(envObject, result)
const reference = emnapiCtx.refStore.get(ref)!
Expand Down
27 changes: 24 additions & 3 deletions packages/emnapi/src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ function $CHECK_ARG (env: Env, arg: void_p) {

/** @macro */
function $PREAMBLE (env: napi_env, fn: (envObject: Env) => napi_status): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$RETURN_STATUS_IF_FALSE!(
envObject, envObject.tryCatch.isEmpty(), napi_status.napi_pending_exception)
$RETURN_STATUS_IF_FALSE!(
Expand All @@ -41,3 +39,26 @@ function $PREAMBLE (env: napi_env, fn: (envObject: Env) => napi_status): napi_st
return envObject.setLastError(napi_status.napi_pending_exception)
}
}

/** @macro */
function $CHECK_ENV_NOT_IN_GC (env: napi_env): any {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
}

/** @macro */
function $CHECK_NEW_STRING_ARGS (env: napi_env, str: const_char_p, length: number, result: Pointer<napi_value>): any {
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
const autoLength = length === -1
const sizelength = length >>> 0
if (length !== 0) {
$CHECK_ARG!(envObject, str)
}
$CHECK_ARG!(envObject, result)
$RETURN_STATUS_IF_FALSE!(
envObject,
autoLength || (sizelength <= 2147483647),
napi_status.napi_invalid_arg
)
}
4 changes: 1 addition & 3 deletions packages/emnapi/src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ function napi_reject_deferred (env: napi_env, deferred: napi_deferred, resolutio
}

function napi_is_promise (env: napi_env, value: napi_value, is_promise: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const envObject: Env = $CHECK_ENV_NOT_IN_GC!(env)
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, is_promise)
const h = emnapiCtx.handleStore.get(value)!
Expand Down
Loading

0 comments on commit 563c3ea

Please sign in to comment.