Skip to content
Merged
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
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"includePath": [
"${default}",
// "${env:USERPROFILE}/AppData/Local/node-gyp/Cache/16.15.0/include/node"
"${workspaceFolder}/packages/emnapi/include"
"${workspaceFolder}/packages/emnapi/include",
"${workspaceFolder}/node_modules/node-addon-api"
],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS", "NODE_ADDON_API_ENABLE_MAYBE"],
"clPath": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.28.29910\\bin\\Hostx64\\x64\\cl.exe",
Expand Down
10 changes: 10 additions & 0 deletions packages/emnapi/include/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,16 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_add_finalizer(napi_env env,

#endif // NAPI_VERSION >= 5

#ifdef NAPI_EXPERIMENTAL

NAPI_EXTERN napi_status NAPI_CDECL
node_api_post_finalizer(napi_env env,
napi_finalize finalize_cb,
void* finalize_data,
void* finalize_hint);

#endif // NAPI_EXPERIMENTAL

#if NAPI_VERSION >= 6

// BigInt
Expand Down
2 changes: 2 additions & 0 deletions packages/emnapi/src/async_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ napi_async_init(napi_env env,
napi_value async_resource_name,
napi_async_context* result) {
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ARG(env, async_resource_name);
CHECK_ARG(env, result);

Expand All @@ -39,6 +40,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_ARG(env, async_context);

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

Expand Down Expand Up @@ -188,6 +189,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_ARG(env, work);

async_work_delete(work);
Expand Down
4 changes: 4 additions & 0 deletions packages/emnapi/src/core/async-work.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ 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()
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand All @@ -249,6 +250,7 @@ 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()
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand Down Expand Up @@ -284,6 +286,7 @@ 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()
$CHECK_ARG!(envObject, work)

emnapiAWST.remove(work)
Expand All @@ -292,6 +295,7 @@ 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()
$CHECK_ARG!(envObject, work)

const resource = emnapiAWMT.getResource(work)
Expand Down
3 changes: 3 additions & 0 deletions packages/emnapi/src/emnapi_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ napi_status _emnapi_node_make_callback(napi_env env,
double trigger_async_id,
napi_value* result);

EMNAPI_INTERNAL_EXTERN
void _emnapi_env_check_gc_access(napi_env env);

#define EMNAPI_ASYNC_RESOURCE_CTOR(env, res, name, ar) \
do { \
EMNAPI_ASSERT_CALL(napi_create_reference((env), (res), 1, &(ar)->resource_)); \
Expand Down
2 changes: 2 additions & 0 deletions packages/emnapi/src/emscripten/async-work.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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()
$CHECK_ARG!(envObject, execute)
$CHECK_ARG!(envObject, result)

Expand All @@ -24,6 +25,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()
$CHECK_ARG!(envObject, work)

emnapiAWST.remove(work)
Expand Down
6 changes: 6 additions & 0 deletions packages/emnapi/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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()
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const r = envObject.tryCatch.hasCaught()
Expand All @@ -96,6 +97,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()
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand Down Expand Up @@ -123,6 +125,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()
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -149,6 +152,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()
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -174,6 +178,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()
$CHECK_ARG!(envObject, msg)
$CHECK_ARG!(envObject, result)
const msgValue = emnapiCtx.handleStore.get(msg)!.value
Expand All @@ -199,6 +204,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()
$CHECK_ARG!(envObject, result)
$from64('result')

Expand Down
1 change: 1 addition & 0 deletions packages/emnapi/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ function napi_get_new_target (
const envObject = emnapiCtx.envStore.get(env)!
// if (!cbinfo) return envObject.setLastError(napi_status.napi_invalid_arg)
$CHECK_ARG!(envObject, result)
envObject.checkGCAccess()

$from64('result')

Expand Down
10 changes: 10 additions & 0 deletions packages/emnapi/src/life.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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()
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const scope = emnapiCtx.openScope(envObject)
Expand All @@ -12,6 +13,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()
$CHECK_ARG!(envObject, scope)
if ((envObject.openHandleScopes === 0)) {
return napi_status.napi_handle_scope_mismatch
Expand All @@ -24,6 +26,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()
$CHECK_ARG!(envObject, result)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const scope = emnapiCtx.openScope(envObject)
Expand All @@ -35,6 +38,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()
$CHECK_ARG!(envObject, scope)
if ((envObject.openHandleScopes === 0)) {
return napi_status.napi_handle_scope_mismatch
Expand All @@ -47,6 +51,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()
$CHECK_ARG!(envObject, scope)
$CHECK_ARG!(envObject, escapee)
$CHECK_ARG!(envObject, result)
Expand All @@ -72,6 +77,7 @@ function napi_create_reference (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)

Expand All @@ -94,6 +100,7 @@ function napi_delete_reference (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, ref)
emnapiCtx.refStore.get(ref)!.dispose()
return envObject.clearLastError()
Expand All @@ -106,6 +113,7 @@ function napi_reference_ref (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, ref)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const count = emnapiCtx.refStore.get(ref)!.ref()
Expand All @@ -123,6 +131,7 @@ function napi_reference_unref (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, ref)
const reference = emnapiCtx.refStore.get(ref)!
const refcount = reference.refCount()
Expand All @@ -146,6 +155,7 @@ function napi_get_reference_value (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, ref)
$CHECK_ARG!(envObject, result)
const reference = emnapiCtx.refStore.get(ref)!
Expand Down
6 changes: 6 additions & 0 deletions packages/emnapi/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ function napi_make_callback (env: napi_env, async_context: Pointer<int64_t>, rec
})
}

function __emnapi_env_check_gc_access (env: napi_env): void {
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
}

emnapiImplementInternal('_emnapi_node_emit_async_init', 'vppdp', __emnapi_node_emit_async_init)
emnapiImplementInternal('_emnapi_node_emit_async_destroy', 'vdd', __emnapi_node_emit_async_destroy)
// emnapiImplementInternal('_emnapi_node_open_callback_scope', 'vpddp', __emnapi_node_open_callback_scope)
Expand All @@ -192,6 +197,7 @@ emnapiImplementInternal('_emnapi_node_make_callback', 'ipppppddp', __emnapi_node

emnapiImplementInternal('_emnapi_async_init_js', 'ippp', __emnapi_async_init_js)
emnapiImplementInternal('_emnapi_async_destroy_js', 'ip', __emnapi_async_destroy_js)
emnapiImplementInternal('_emnapi_env_check_gc_access', 'vp', __emnapi_env_check_gc_access)

emnapiImplement('napi_open_callback_scope', 'ipppp', napi_open_callback_scope)
emnapiImplement('napi_close_callback_scope', 'ipp', napi_close_callback_scope)
Expand Down
1 change: 1 addition & 0 deletions packages/emnapi/src/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, is_promise)
const h = emnapiCtx.handleStore.get(value)!
Expand Down
3 changes: 3 additions & 0 deletions packages/emnapi/src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ var emnapiString = {
) {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$from64('length')
const autoLength = length === -1
const sizelength = length >>> 0
Expand Down Expand Up @@ -271,6 +272,8 @@ var emnapiString = {
createApi: (env: napi_env, str: number, length: size_t, result: Pointer<napi_value>) => napi_status,
stringMaker: (autoLength: boolean, sizeLength: number) => string
) {
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
const status = createApi(env, str, length, result)
if (status === napi_status.napi_ok) {
if (copied) {
Expand Down
1 change: 1 addition & 0 deletions packages/emnapi/src/threadsafe-function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ function _napi_create_threadsafe_function (
): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, async_resource_name)
$from64('max_queue_size')
$from64('initial_thread_count')
Expand Down
1 change: 1 addition & 0 deletions packages/emnapi/src/threadsafe_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ napi_create_threadsafe_function(napi_env env,
napi_threadsafe_function* result) {
#if EMNAPI_HAVE_THREADS
CHECK_ENV(env);
_emnapi_env_check_gc_access(env);
CHECK_ARG(env, async_resource_name);
RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg);
CHECK_ARG(env, result);
Expand Down
10 changes: 10 additions & 0 deletions packages/emnapi/src/value-operation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function napi_typeof (env: napi_env, value: napi_value, result: Pointer<napi_valuetype>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const v = emnapiCtx.handleStore.get(value)!
Expand Down Expand Up @@ -142,6 +143,7 @@ function napi_instanceof (env: napi_env, object: napi_value, constructor: napi_v
function napi_is_array (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand All @@ -155,6 +157,7 @@ function napi_is_array (env: napi_env, value: napi_value, result: Pointer<bool>)
function napi_is_arraybuffer (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand All @@ -168,6 +171,7 @@ function napi_is_arraybuffer (env: napi_env, value: napi_value, result: Pointer<
function napi_is_date (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand All @@ -181,6 +185,7 @@ function napi_is_date (env: napi_env, value: napi_value, result: Pointer<bool>):
function napi_is_error (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const val = emnapiCtx.handleStore.get(value)!.value
Expand All @@ -194,6 +199,7 @@ function napi_is_error (env: napi_env, value: napi_value, result: Pointer<bool>)
function napi_is_typedarray (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand All @@ -207,6 +213,7 @@ function napi_is_typedarray (env: napi_env, value: napi_value, result: Pointer<b
function napi_is_buffer (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand All @@ -220,6 +227,7 @@ function napi_is_buffer (env: napi_env, value: napi_value, result: Pointer<bool>
function napi_is_dataview (env: napi_env, value: napi_value, result: Pointer<bool>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, value)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(value)!
Expand Down Expand Up @@ -250,6 +258,7 @@ function napi_strict_equals (env: napi_env, lhs: napi_value, rhs: napi_value, re
function napi_detach_arraybuffer (env: napi_env, arraybuffer: napi_value): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
envObject.checkGCAccess()
$CHECK_ARG!(envObject, arraybuffer)
const value = emnapiCtx.handleStore.get(arraybuffer)!.value
if (!(value instanceof ArrayBuffer)) {
Expand All @@ -271,6 +280,7 @@ function napi_detach_arraybuffer (env: napi_env, arraybuffer: napi_value): napi_

function napi_is_detached_arraybuffer (env: napi_env, arraybuffer: napi_value, result: Pointer<bool>): napi_status {
return $PREAMBLE!(env, (envObject) => {
envObject.checkGCAccess()
$CHECK_ARG!(envObject, arraybuffer)
$CHECK_ARG!(envObject, result)
const h = emnapiCtx.handleStore.get(arraybuffer)!
Expand Down
Loading