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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: ${{ (contains(matrix.target, 'emscripten') && matrix.target == 'wasm64-unknown-emscripten' && '20.0.0-v8-canary202302081604228b65') || '18.14.0' }}
node-version: ${{ (contains(matrix.target, 'emscripten') && matrix.target == 'wasm64-unknown-emscripten' && '20.0.0') || '18.14.0' }}
registry-url: 'https://registry.npmjs.org'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand Down
31 changes: 31 additions & 0 deletions packages/test/async_cleanup_hook/binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "uv.h"
#include "../common.h"

static int cleanup_hook_count = 0;
static void MustNotCall(napi_async_cleanup_hook_handle hook, void* arg) {
assert(0);
}
Expand All @@ -22,6 +23,7 @@ static struct AsyncData* CreateAsyncData() {
}

static void AfterCleanupHookTwo(uv_handle_t* handle) {
cleanup_hook_count++;
printf("AfterCleanupHookTwo\n");
struct AsyncData* data = (struct AsyncData*) handle->data;
napi_status status = napi_remove_async_cleanup_hook(data->handle);
Expand All @@ -30,11 +32,13 @@ static void AfterCleanupHookTwo(uv_handle_t* handle) {
}

static void AfterCleanupHookOne(uv_async_t* async) {
cleanup_hook_count++;
printf("AfterCleanupHookOne\n");
uv_close((uv_handle_t*) async, AfterCleanupHookTwo);
}

static void AsyncCleanupHook(napi_async_cleanup_hook_handle handle, void* arg) {
cleanup_hook_count++;
printf("AsyncCleanupHook\n");
struct AsyncData* data = (struct AsyncData*) arg;
uv_loop_t* loop;
Expand All @@ -48,7 +52,31 @@ static void AsyncCleanupHook(napi_async_cleanup_hook_handle handle, void* arg) {
uv_async_send(&data->async);
}

static void ObjectFinalizer(napi_env env, void* data, void* hint) {
// AsyncCleanupHook and its subsequent callbacks are called twice.
assert(cleanup_hook_count == 6);

napi_ref* ref = data;
NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref));
free(ref);
}

static void CreateObjectWrap(napi_env env) {
napi_value js_obj;
napi_ref* ref = malloc(sizeof(*ref));
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &js_obj));
NAPI_CALL_RETURN_VOID(
env, napi_wrap(env, js_obj, ref, ObjectFinalizer, NULL, ref));
// create a strong reference so that the finalizer is called at shutdown.
NAPI_CALL_RETURN_VOID(env, napi_reference_ref(env, *ref, NULL));
}

static napi_value Init(napi_env env, napi_value exports) {
// Reinitialize the static variable to be compatible with musl libc.
cleanup_hook_count = 0;
// Create object wrap before cleanup hooks.
CreateObjectWrap(env);

{
struct AsyncData* data = CreateAsyncData();
data->env = env;
Expand All @@ -68,6 +96,9 @@ static napi_value Init(napi_env env, napi_value exports) {
napi_remove_async_cleanup_hook(must_not_call_handle);
}

// Create object wrap after cleanup hooks.
CreateObjectWrap(env);

return NULL;
}

Expand Down
33 changes: 33 additions & 0 deletions packages/test/cleanup_hook/binding.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#else
#include <stddef.h>
int console_log(const char* fmt, int a);
void* malloc(size_t size);
void free(void* p);
#define assert(x) do { if (!(x)) { __builtin_trap(); } } while (0)
#endif
#include "node_api.h"
#include "../common.h"

static int cleanup_hook_count = 0;
static void cleanup(void* arg) {
cleanup_hook_count++;
#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
printf("cleanup(%d)\n", *(int*)(arg));
#else
Expand All @@ -17,11 +25,36 @@ static void cleanup(void* arg) {
static int secret = 42;
static int wrong_secret = 17;

static void ObjectFinalizer(napi_env env, void* data, void* hint) {
// cleanup is called once.
assert(cleanup_hook_count == 1);

napi_ref* ref = data;
NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, *ref));
free(ref);
}

static void CreateObjectWrap(napi_env env) {
napi_value js_obj;
napi_ref* ref = malloc(sizeof(*ref));
NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &js_obj));
NAPI_CALL_RETURN_VOID(
env, napi_wrap(env, js_obj, ref, ObjectFinalizer, NULL, ref));
// create a strong reference so that the finalizer is called at shutdown.
NAPI_CALL_RETURN_VOID(env, napi_reference_ref(env, *ref, NULL));
}

static napi_value Init(napi_env env, napi_value exports) {
// Create object wrap before cleanup hooks.
CreateObjectWrap(env);

napi_add_env_cleanup_hook(env, cleanup, &wrong_secret);
napi_add_env_cleanup_hook(env, cleanup, &secret);
napi_remove_env_cleanup_hook(env, cleanup, &wrong_secret);

// Create object wrap after cleanup hooks.
CreateObjectWrap(env);

return NULL;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/test/cleanup_hook/cleanup_hook.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ if (process.argv[2] === 'child') {
load('cleanup_hook')
} else {
module.exports = new Promise((resolve) => {
const { stdout } =
const { stdout, status, signal } =
child_process.spawnSync(process.execPath, [
'--expose-gc',
...(process.env.EMNAPI_TEST_WASI ? ['--experimental-wasi-unstable-preview1'] : []),
...(process.env.MEMORY64 ? ['--experimental-wasm-memory64'] : []),
__filename,
'child'
])
assert.strictEqual(status, 0, `process exited with status(${status}) and signal(${signal})`)
assert.strictEqual(stdout.toString().trim(), 'cleanup(42)')
resolve()
})
Expand Down