From 6fbfd05f8c9fe1d3bc601542eeb7a9e4df81ff4a Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 30 Sep 2024 15:53:11 -0300 Subject: [PATCH 1/3] Destroy automatically --- binding.c | 10 ++++++++++ test.js | 3 +-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/binding.c b/binding.c index eadea27..cf2faca 100644 --- a/binding.c +++ b/binding.c @@ -8,6 +8,13 @@ typedef struct { js_context_t *context; } bare_realm_t; +static void +on_realm_finalize (js_env_t *env, void *data, void *finalize_hint) { + bare_realm_t *realm = data; + + js_destroy_context(env, realm->context); +} + static js_value_t * bare_realm_create (js_env_t *env, js_callback_info_t *info) { int err; @@ -21,6 +28,9 @@ bare_realm_create (js_env_t *env, js_callback_info_t *info) { err = js_create_context(env, &realm->context); if (err < 0) return NULL; + err = js_add_finalizer(env, handle, (void *) realm, on_realm_finalize, NULL, NULL); + assert(err == 0); + return handle; } diff --git a/test.js b/test.js index 2b2d52c..ee8f007 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,8 @@ const test = require('brittle') const Realm = require('.') -test('basic', async (t) => { +test('basic', (t) => { const realm = new Realm() - t.teardown(() => realm.destroy()) const globalThat = realm.evaluate('globalThis') From f89a75a64997def1f252a0a657ccb7a2266ea8db Mon Sep 17 00:00:00 2001 From: Yasser Nascimento Date: Mon, 30 Sep 2024 18:18:25 -0300 Subject: [PATCH 2/3] Run destroy callback just once --- binding.c | 10 ++++++++-- test.js | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/binding.c b/binding.c index cf2faca..c064096 100644 --- a/binding.c +++ b/binding.c @@ -6,13 +6,17 @@ typedef struct { js_context_t *context; + bool is_destroyed; } bare_realm_t; static void on_realm_finalize (js_env_t *env, void *data, void *finalize_hint) { bare_realm_t *realm = data; - js_destroy_context(env, realm->context); + if (realm->is_destroyed) return; + + int err = js_destroy_context(env, realm->context); + if (err == 0) realm->is_destroyed = true; } static js_value_t * @@ -28,6 +32,8 @@ bare_realm_create (js_env_t *env, js_callback_info_t *info) { err = js_create_context(env, &realm->context); if (err < 0) return NULL; + realm->is_destroyed = false; + err = js_add_finalizer(env, handle, (void *) realm, on_realm_finalize, NULL, NULL); assert(err == 0); @@ -50,7 +56,7 @@ bare_realm_destroy (js_env_t *env, js_callback_info_t *info) { err = js_get_arraybuffer_info(env, argv[0], (void **) &realm, NULL); assert(err == 0); - js_destroy_context(env, realm->context); + on_realm_finalize(env, realm, NULL); return NULL; } diff --git a/test.js b/test.js index ee8f007..42afe81 100644 --- a/test.js +++ b/test.js @@ -3,6 +3,7 @@ const Realm = require('.') test('basic', (t) => { const realm = new Realm() + t.teardown(() => realm.destroy()) const globalThat = realm.evaluate('globalThis') From fa2d90467317b311f9fcac2c9e57d531a91fd7d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Isager=20Dalsgar=C3=B0?= Date: Tue, 1 Oct 2024 08:32:54 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- binding.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/binding.c b/binding.c index c064096..778a13d 100644 --- a/binding.c +++ b/binding.c @@ -10,13 +10,15 @@ typedef struct { } bare_realm_t; static void -on_realm_finalize (js_env_t *env, void *data, void *finalize_hint) { +bare_realm__on_finalize (js_env_t *env, void *data, void *finalize_hint) { bare_realm_t *realm = data; if (realm->is_destroyed) return; int err = js_destroy_context(env, realm->context); - if (err == 0) realm->is_destroyed = true; + assert(err == 0); + + realm->is_destroyed = true; } static js_value_t * @@ -34,7 +36,7 @@ bare_realm_create (js_env_t *env, js_callback_info_t *info) { realm->is_destroyed = false; - err = js_add_finalizer(env, handle, (void *) realm, on_realm_finalize, NULL, NULL); + err = js_add_finalizer(env, handle, (void *) realm, bare_realm__on_finalize, NULL, NULL); assert(err == 0); return handle; @@ -56,7 +58,7 @@ bare_realm_destroy (js_env_t *env, js_callback_info_t *info) { err = js_get_arraybuffer_info(env, argv[0], (void **) &realm, NULL); assert(err == 0); - on_realm_finalize(env, realm, NULL); + bare_realm__on_finalize(env, realm, NULL); return NULL; }