diff --git a/binding.c b/binding.c index eadea27..778a13d 100644 --- a/binding.c +++ b/binding.c @@ -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; @@ -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; } @@ -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; } diff --git a/test.js b/test.js index 2b2d52c..42afe81 100644 --- a/test.js +++ b/test.js @@ -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())