rquickjs-core 0.15.0

High level bindings to the QuickJS JavaScript engine
Documentation
use crate::{
    class::{self},
    qjs, Ctx, Error, JsLifetime, Persistent, Value,
};

use super::{
    exotic::ExoticMethodsHolder,
    job_error::{Entry as JobErrorEntry, JobError, JobErrors},
    userdata::{UserDataGuard, UserDataMap},
    InterruptHandler, PromiseHook, PromiseHookType, RejectionTracker, UserDataError,
};
use alloc::boxed::Box;
use core::{
    any::Any,
    cell::{Cell, UnsafeCell},
    marker::PhantomData,
    ptr,
};

#[cfg(feature = "futures")]
use super::{schedular::SchedularPoll, spawner::Spawner};

#[cfg(feature = "futures")]
use core::{
    future::Future,
    task::{Context, Waker},
};

/// Opaque book keeping data for Rust.
pub(crate) struct RuntimeOpaque<'js> {
    /// Used to carry a panic if a callback triggered one.
    panic: Cell<Option<Box<dyn Any + Send + 'static>>>,

    /// The user provided promise hook, if any.
    promise_hook: UnsafeCell<Option<PromiseHook>>,

    /// The user provided rejection tracker, if any.
    rejection_tracker: UnsafeCell<Option<RejectionTracker>>,

    /// The user provided interrupt handler, if any.
    interrupt_handler: UnsafeCell<Option<InterruptHandler>>,

    /// The class id for rust classes.
    class_id: qjs::JSClassID,
    /// The class id for rust classes which can be called.
    callable_class_id: qjs::JSClassID,
    /// The class id for exotic classes
    exotic_class_id: qjs::JSClassID,

    userdata: UserDataMap,

    /// Failed-job exceptions, kept in a bounded FIFO until evicted or teardown.
    /// Every access is under the runtime lock.
    job_errors: UnsafeCell<JobErrors>,

    #[cfg(feature = "futures")]
    spawner: Option<UnsafeCell<Spawner>>,

    exotic_methods: ExoticMethodsHolder,

    _marker: PhantomData<&'js ()>,
}

impl<'js> RuntimeOpaque<'js> {
    pub fn new() -> Self {
        RuntimeOpaque {
            panic: Cell::new(None),

            promise_hook: UnsafeCell::new(None),

            rejection_tracker: UnsafeCell::new(None),

            interrupt_handler: UnsafeCell::new(None),

            class_id: qjs::JS_INVALID_CLASS_ID,
            callable_class_id: qjs::JS_INVALID_CLASS_ID,
            exotic_class_id: qjs::JS_INVALID_CLASS_ID,

            userdata: UserDataMap::default(),

            job_errors: UnsafeCell::new(JobErrors::default()),

            _marker: PhantomData,

            #[cfg(feature = "futures")]
            spawner: None,

            exotic_methods: ExoticMethodsHolder::new(),
        }
    }

    #[cfg(feature = "futures")]
    pub fn with_spawner() -> Self {
        let mut this = RuntimeOpaque::new();
        this.spawner = Some(UnsafeCell::new(Spawner::new()));
        this
    }

    pub unsafe fn initialize(&mut self, rt: *mut qjs::JSRuntime) -> Result<(), Error> {
        qjs::JS_NewClassID(rt, (&mut self.class_id) as *mut qjs::JSClassID);
        qjs::JS_NewClassID(rt, (&mut self.callable_class_id) as *mut qjs::JSClassID);
        qjs::JS_NewClassID(rt, (&mut self.exotic_class_id) as *mut qjs::JSClassID);

        let class_def = qjs::JSClassDef {
            class_name: c"RustClass".as_ptr().cast(),
            finalizer: Some(class::ffi::class_finalizer),
            gc_mark: Some(class::ffi::class_trace),
            call: None,
            exotic: ptr::null_mut(),
        };

        if 0 != qjs::JS_NewClass(rt, self.class_id, &class_def) {
            return Err(Error::Unknown);
        }

        let class_def = qjs::JSClassDef {
            class_name: c"RustFunction".as_ptr().cast(),
            finalizer: Some(class::ffi::callable_finalizer),
            gc_mark: Some(class::ffi::callable_trace),
            call: Some(class::ffi::call),
            exotic: ptr::null_mut(),
        };

        if 0 != qjs::JS_NewClass(rt, self.callable_class_id, &class_def) {
            return Err(Error::Unknown);
        }

        let class_def = qjs::JSClassDef {
            class_name: c"RustExotic".as_ptr().cast(),
            finalizer: Some(class::ffi::exotic_class_finalizer),
            gc_mark: Some(class::ffi::exotic_class_trace),
            call: None,
            exotic: self.exotic_methods.as_ptr(),
        };

        if 0 != qjs::JS_NewClass(rt, self.exotic_class_id, &class_def) {
            return Err(Error::Unknown);
        }

        Ok(())
    }

    pub unsafe fn from_runtime_ptr<'a>(rt: *mut qjs::JSRuntime) -> &'a Self {
        &*(qjs::JS_GetRuntimeOpaque(rt).cast::<Self>())
    }

    #[cfg(feature = "futures")]
    fn spawner(&self) -> &UnsafeCell<Spawner> {
        self.spawner
            .as_ref()
            .expect("tried to use async function in non async runtime")
    }

    #[cfg(feature = "futures")]
    pub unsafe fn push<F>(&self, f: F)
    where
        F: Future<Output = ()>,
    {
        (*self.spawner().get()).push(f)
    }

    #[cfg(feature = "futures")]
    pub fn listen(&self, wake: Waker) {
        unsafe { (*self.spawner().get()).listen(wake) };
    }

    #[cfg(feature = "futures")]
    pub fn spawner_is_empty(&self) -> bool {
        unsafe { (*self.spawner().get()).is_empty() }
    }

    #[cfg(feature = "futures")]
    pub fn poll(&self, cx: &mut Context) -> SchedularPoll {
        unsafe { (*self.spawner().get()).poll(cx) }
    }

    pub fn insert_userdata<U>(&self, data: U) -> Result<Option<Box<U>>, UserDataError<U>>
    where
        U: JsLifetime<'js>,
        U::Changed<'static>: Any,
    {
        self.userdata.insert(data)
    }

    pub fn remove_userdata<U>(&self) -> Result<Option<Box<U>>, UserDataError<()>>
    where
        U: JsLifetime<'js>,
        U::Changed<'static>: Any,
    {
        self.userdata.remove()
    }

    pub fn get_userdata<U>(&self) -> Option<UserDataGuard<U>>
    where
        U: JsLifetime<'js>,
        U::Changed<'static>: Any,
    {
        self.userdata.get()
    }

    pub fn set_promise_hook(&self, promise_hook: Option<PromiseHook>) {
        unsafe { (*self.promise_hook.get()) = promise_hook }
    }

    pub fn run_promise_hook<'a>(
        &self,
        ctx: Ctx<'a>,
        type_: PromiseHookType,
        promise: Value<'a>,
        parent: Value<'a>,
    ) {
        unsafe { (*self.promise_hook.get()).as_mut().unwrap()(ctx, type_, promise, parent) }
    }

    pub fn set_rejection_tracker(&self, tracker: Option<RejectionTracker>) {
        unsafe { (*self.rejection_tracker.get()) = tracker }
    }

    pub fn run_rejection_tracker<'a>(
        &self,
        ctx: Ctx<'a>,
        promise: Value<'a>,
        reason: Value<'a>,
        is_handled: bool,
    ) {
        unsafe {
            (*self.rejection_tracker.get()).as_mut().unwrap()(ctx, promise, reason, is_handled)
        }
    }

    pub fn set_interrupt_handler(&self, interupt: Option<InterruptHandler>) {
        unsafe { (*self.interrupt_handler.get()) = interupt }
    }

    pub fn run_interrupt_handler(&self) -> bool {
        unsafe { (*self.interrupt_handler.get()).as_mut().unwrap()() }
    }

    #[allow(dead_code)] // not used in no_std
    pub fn set_panic(&self, panic: Box<dyn Any + Send + 'static>) {
        self.panic.set(Some(panic))
    }

    pub fn take_panic(&self) -> Option<Box<dyn Any + Send + 'static>> {
        self.panic.take()
    }

    /// Record a failed job's exception, evicting the oldest if the FIFO is full.
    ///
    /// # Safety
    /// The caller must hold the runtime lock; the queue holds live JS values.
    pub(crate) unsafe fn capture_job_error<'a>(
        &self,
        id: u64,
        ctx: &Ctx<'a>,
        value: Value<'a>,
    ) -> JobError {
        let entry = JobErrorEntry {
            id,
            value: Persistent::save(ctx, value),
        };
        let evicted = (*self.job_errors.get()).push(entry);
        // Drop after the borrow ends: a finalizer may re-enter `Opaque`.
        drop(evicted);
        JobError { id }
    }

    /// Clone a retained exception out of the runtime.
    ///
    /// # Safety
    /// The caller must hold the runtime lock; the queue holds live JS values.
    pub(crate) unsafe fn take_job_error<'a>(
        &self,
        id: u64,
        ctx: &Ctx<'a>,
    ) -> crate::Result<Value<'a>> {
        (*self.job_errors.get()).get(id, ctx)
    }

    pub fn get_class_id(&self) -> qjs::JSClassID {
        self.class_id
    }

    pub fn get_callable_id(&self) -> qjs::JSClassID {
        self.callable_class_id
    }

    pub fn get_exotic_id(&self) -> qjs::JSClassID {
        self.exotic_class_id
    }

    /// Cleans up all the internal state.
    ///
    /// Called before dropping the runtime to ensure that we drop everything before freeing the
    /// runtime.
    pub fn clear(&mut self) {
        self.rejection_tracker.get_mut().take();
        self.interrupt_handler.get_mut().take();
        self.panic.take();
        let stale = self.job_errors.get_mut().take_all();
        drop(stale);
        #[cfg(feature = "futures")]
        self.spawner.take();
        self.userdata.clear();
    }
}