use alloc::boxed::Box;
use core::{mem, ptr::NonNull};
use crate::{context::opaque::ContextOpaque, qjs, Ref};
pub struct OwnedContext {
ctx: NonNull<qjs::JSContext>,
owns_opaque: bool,
}
impl OwnedContext {
pub fn new(ctx: NonNull<qjs::JSContext>, opaque: ContextOpaque) -> Self {
debug_assert!(
unsafe { qjs::JS_GetContextOpaque(ctx.as_ptr()) }.is_null(),
"Tried to create a rust quickjs context from a context which already contained an opaque pointer"
);
let opaque = Box::into_raw(Box::new(opaque));
unsafe { qjs::JS_SetContextOpaque(ctx.as_ptr(), opaque.cast()) };
OwnedContext {
ctx,
owns_opaque: true,
}
}
pub unsafe fn from_raw(ctx: NonNull<qjs::JSContext>) -> Self {
if qjs::JS_GetContextOpaque(ctx.as_ptr()).is_null() {
Self::new(ctx, ContextOpaque::new())
} else {
OwnedContext {
ctx,
owns_opaque: false,
}
}
}
}
impl Drop for OwnedContext {
fn drop(&mut self) {
unsafe {
if self.owns_opaque {
mem::drop(Box::from_raw(
qjs::JS_GetContextOpaque(self.ctx.as_ptr()).cast::<ContextOpaque>(),
));
}
qjs::JS_FreeContext(self.ctx.as_ptr());
}
}
}
pub(crate) trait DropContext: Clone {
unsafe fn drop_context(&self, ctx: OwnedContext);
}
impl DropContext for crate::Runtime {
unsafe fn drop_context(&self, ctx: OwnedContext) {
let guard = match self.inner.try_lock() {
Some(x) => x,
None => {
#[cfg(not(feature = "parallel"))]
{
mem::drop(ctx);
return;
}
#[cfg(feature = "parallel")]
{
self.pending_free
.send(ctx)
.expect("runtime should be alive while contexts life");
return;
}
}
};
guard.update_stack_top();
mem::drop(ctx);
guard.drain_pending_free();
mem::drop(guard);
}
}
#[cfg(feature = "futures")]
impl DropContext for crate::AsyncRuntime {
unsafe fn drop_context(&self, ctx: OwnedContext) {
let guard = match self.inner.try_lock() {
Some(x) => x,
None => {
#[cfg(not(feature = "parallel"))]
{
mem::drop(ctx);
return;
}
#[cfg(feature = "parallel")]
{
self.pending_free
.send(ctx)
.expect("runtime should be alive while contexts life");
return;
}
}
};
guard.runtime.update_stack_top();
mem::drop(ctx);
guard.runtime.drain_pending_free();
mem::drop(guard);
}
}
#[derive(Clone)]
pub(crate) struct ContextOwner<R: DropContext> {
ctx: Option<Ref<OwnedContext>>,
pub(crate) rt: R,
}
impl<R: DropContext> ContextOwner<R> {
pub unsafe fn new(ctx: NonNull<qjs::JSContext>, opaque: ContextOpaque, rt: R) -> Self {
Self {
ctx: Some(Ref::new(OwnedContext::new(ctx, opaque))),
rt,
}
}
pub(crate) unsafe fn from_raw(ctx: NonNull<qjs::JSContext>, rt: R) -> Self {
Self {
ctx: Some(Ref::new(OwnedContext::from_raw(ctx))),
rt,
}
}
pub(crate) fn ctx(&self) -> NonNull<qjs::JSContext> {
self.ctx
.as_ref()
.expect("context owner is already dropped")
.ctx
}
pub(crate) fn rt(&self) -> &R {
&self.rt
}
}
#[cfg(feature = "parallel")]
unsafe impl Send for OwnedContext {}
#[cfg(feature = "parallel")]
unsafe impl Sync for OwnedContext {}
#[cfg(feature = "parallel")]
unsafe impl<R: Send + DropContext> Send for ContextOwner<R> {}
impl<R: DropContext> Drop for ContextOwner<R> {
fn drop(&mut self) {
if let Some(inner) = self.ctx.take() {
if let Some(inner) = Ref::into_inner(inner) {
unsafe { self.rt.drop_context(inner) }
}
}
}
}