Skip to content

Commit a69ab64

Browse files
committed
feat: add explicit dispose() methods to JSSandbox and LoadedJSSandbox NAPI wrappers
Add #[napi] dispose() methods that eagerly release underlying sandbox resources by calling take() on the inner Option. After disposal, all subsequent calls return ERR_CONSUMED. No-op on already-consumed instances. Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
1 parent a892a0a commit a69ab64

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/js-host-api/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,20 @@ impl JSSandboxWrapper {
801801
pub fn poisoned(&self) -> napi::Result<bool> {
802802
self.with_inner_ref(|sandbox| Ok(sandbox.poisoned()))
803803
}
804+
805+
/// Eagerly release the underlying sandbox resources.
806+
///
807+
/// After calling `dispose()`, the sandbox is consumed and all
808+
/// subsequent method calls will throw an `ERR_CONSUMED` error.
809+
/// This is useful when you want deterministic cleanup rather than
810+
/// waiting for garbage collection.
811+
///
812+
/// Calling `dispose()` on an already-consumed sandbox is a no-op.
813+
#[napi]
814+
pub fn dispose(&self) -> napi::Result<()> {
815+
let _ = self.inner.lock().map_err(|_| lock_error())?.take();
816+
Ok(())
817+
}
804818
}
805819

806820
// ── LoadedJSSandbox ──────────────────────────────────────────────────
@@ -1150,6 +1164,20 @@ impl LoadedJSSandboxWrapper {
11501164
.await
11511165
.map_err(join_error)?
11521166
}
1167+
1168+
/// Eagerly release the underlying sandbox resources.
1169+
///
1170+
/// After calling `dispose()`, the sandbox is consumed and all
1171+
/// subsequent method calls will throw an `ERR_CONSUMED` error.
1172+
/// This is useful when you want deterministic cleanup rather than
1173+
/// waiting for garbage collection.
1174+
///
1175+
/// Calling `dispose()` on an already-consumed sandbox is a no-op.
1176+
#[napi]
1177+
pub fn dispose(&self) -> napi::Result<()> {
1178+
let _ = self.inner.lock().map_err(|_| lock_error())?.take();
1179+
Ok(())
1180+
}
11531181
}
11541182

11551183
// ── CallHandlerOptions ───────────────────────────────────────────────

0 commit comments

Comments
 (0)