Skip to content

Commit

Permalink
Destroy automatically (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassernasc authored Oct 1, 2024
1 parent bf21363 commit 7766d05
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@

typedef struct {
js_context_t *context;
bool is_destroyed;
} bare_realm_t;

static void
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);
assert(err == 0);

realm->is_destroyed = true;
}

static js_value_t *
bare_realm_create (js_env_t *env, js_callback_info_t *info) {
int err;
Expand All @@ -21,6 +34,11 @@ 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, bare_realm__on_finalize, NULL, NULL);
assert(err == 0);

return handle;
}

Expand All @@ -40,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);

js_destroy_context(env, realm->context);
bare_realm__on_finalize(env, realm, NULL);

return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('brittle')
const Realm = require('.')

test('basic', async (t) => {
test('basic', (t) => {
const realm = new Realm()
t.teardown(() => realm.destroy())

Expand Down

0 comments on commit 7766d05

Please sign in to comment.