use std::default::Default;
use std::ffi::CStr;
use std::rc::Rc;
use js::context::JSContext;
use js::jsapi::{Heap, IsCallable, JSObject};
use js::jsval::{JSVal, NullValue, ObjectValue, UndefinedValue};
use js::rust::wrappers2::{EnterRealm, JS_GetProperty, JS_WrapObject, LeaveRealm};
use js::rust::{HandleObject, MutableHandleValue};
use crate::codegen::GenericBindings::WindowBinding::Window_Binding::WindowMethods;
use crate::error::{Error, Fallible};
use crate::interfaces::{DocumentHelpers, DomHelpers, GlobalScopeHelpers};
use crate::permanent_root::PermanentRoot;
use crate::realms::enter_auto_realm;
use crate::reflector::DomObject;
use crate::root::Dom;
use crate::settings_stack::{run_a_callback, run_a_script};
use crate::{DomTypes, cformat};
pub trait ThisReflector {
fn jsobject(&self) -> *mut JSObject;
}
pub trait OwnerWindow<D: DomTypes> {
fn owner_window(&self) -> Option<crate::root::DomRoot<D::Window>> {
None
}
}
impl<T: DomObject> ThisReflector for T {
fn jsobject(&self) -> *mut JSObject {
self.reflector().get_jsobject().get()
}
}
impl ThisReflector for HandleObject<'_> {
fn jsobject(&self) -> *mut JSObject {
self.get()
}
}
impl<D: DomTypes> OwnerWindow<D> for HandleObject<'_> {}
#[derive(Clone, Copy, PartialEq)]
pub enum ExceptionHandling {
Report,
Rethrow,
}
pub struct RootedCallback<T>(Rc<(T, PermanentRoot)>);
impl<D: DomTypes, T: HasCallbackHolder<D = D>> RootedCallback<T> {
pub fn to_traced(&self) -> TracedCallback<T>
where
T: for<'a> From<&'a CallbackObject<D>>,
{
let mut duplicate = Rc::new(T::from(self.callback_holder()));
match Rc::get_mut(&mut duplicate) {
Some(ref mut callback) => unsafe {
callback
.callback_holder_mut()
.init_callback(self.callback())
},
None => unreachable!(),
};
TracedCallback(duplicate)
}
}
impl<T> Clone for RootedCallback<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> std::ops::Deref for RootedCallback<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0.0
}
}
impl<T: js::conversions::ToJSValConvertible> js::conversions::ToJSValConvertible
for RootedCallback<T>
{
fn to_jsval(&self, cx: &mut JSContext, rval: MutableHandleValue<'_>) {
self.0.0.to_jsval(cx, rval)
}
}
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
#[derive(JSTraceable, MallocSizeOf, PartialEq)]
pub struct TracedCallback<T>(#[conditional_malloc_size_of] Rc<T>);
impl<T: crate::JSTraceable> js::gc::Rootable for TracedCallback<T> {}
impl<T> Clone for TracedCallback<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> std::ops::Deref for TracedCallback<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[expect(unsafe_code)]
pub(crate) unsafe fn create_callback_rooted<D: DomTypes, T: HasCallbackHolder<D = D>>(
cx: &JSContext,
obj: T,
callback: *mut JSObject,
) -> RootedCallback<T> {
let mut ret = Rc::new((obj, PermanentRoot::default()));
let (callback2, permanent_root) = Rc::get_mut(&mut ret).unwrap();
unsafe {
callback2.callback_holder_mut().init_callback(callback);
permanent_root.init(cx, callback2.callback(), c"Callback::root");
};
RootedCallback(ret)
}
impl<T: CallbackContainer + HasCallbackHolder> TracedCallback<T> {
pub fn root(&self, cx: &JSContext) -> RootedCallback<T> {
unsafe { T::new(cx, self.callback()) }
}
}
#[derive(JSTraceable, MallocSizeOf)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub struct CallbackObject<D: DomTypes> {
#[ignore_malloc_size_of = "measured by mozjs"]
callback: Heap<*mut JSObject>,
permanent_js_root: Option<PermanentRoot>,
incumbent: Option<Dom<D::GlobalScope>>,
}
impl<D: DomTypes> CallbackObject<D> {
fn new_from_existing(other: &CallbackObject<D>) -> Self {
Self {
callback: Heap::default(),
permanent_js_root: None,
incumbent: other.incumbent.clone(),
}
}
fn new_with_exterior_root() -> Self {
Self {
callback: Heap::default(),
permanent_js_root: None,
incumbent: D::GlobalScope::incumbent().map(|i| Dom::from_ref(&*i)),
}
}
pub fn get(&self) -> *mut JSObject {
self.callback.get()
}
#[expect(unsafe_code)]
unsafe fn init_callback(&mut self, callback: *mut JSObject) {
self.callback.set(callback);
}
}
impl<D: DomTypes> PartialEq for CallbackObject<D> {
fn eq(&self, other: &CallbackObject<D>) -> bool {
self.callback.get() == other.callback.get()
}
}
pub trait HasCallbackHolder {
type D: DomTypes;
fn callback_holder(&self) -> &CallbackObject<Self::D>;
fn callback_holder_mut(&mut self) -> &mut CallbackObject<Self::D>;
fn callback(&self) -> *mut JSObject {
self.callback_holder().get()
}
}
pub trait CallbackContainer {
unsafe fn new(cx: &JSContext, callback: *mut JSObject) -> RootedCallback<Self>
where
Self: Sized;
}
pub trait DeprecatedCallbackContainer {
unsafe fn new(cx: &JSContext, callback: *mut JSObject) -> Rc<Self>;
}
#[derive(JSTraceable, MallocSizeOf, PartialEq)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub struct CallbackFunction<D: DomTypes> {
object: CallbackObject<D>,
}
impl<'a, D: DomTypes> From<&'a CallbackObject<D>> for CallbackFunction<D> {
fn from(object: &'a CallbackObject<D>) -> Self {
Self {
object: CallbackObject::new_from_existing(object),
}
}
}
impl<D: DomTypes> CallbackFunction<D> {
pub(crate) fn new_with_exterior_root() -> Self {
Self {
object: CallbackObject::new_with_exterior_root(),
}
}
}
impl<D: DomTypes> HasCallbackHolder for CallbackFunction<D> {
type D = D;
fn callback_holder(&self) -> &CallbackObject<D> {
&self.object
}
fn callback_holder_mut(&mut self) -> &mut CallbackObject<D> {
&mut self.object
}
}
#[derive(JSTraceable, MallocSizeOf, PartialEq)]
#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
pub struct CallbackInterface<D: DomTypes> {
object: CallbackObject<D>,
}
impl<'a, D: DomTypes> From<&'a CallbackObject<D>> for CallbackInterface<D> {
fn from(object: &'a CallbackObject<D>) -> Self {
Self {
object: CallbackObject::new_from_existing(object),
}
}
}
impl<D: DomTypes> HasCallbackHolder for CallbackInterface<D> {
type D = D;
fn callback_holder(&self) -> &CallbackObject<D> {
&self.object
}
fn callback_holder_mut(&mut self) -> &mut CallbackObject<D> {
&mut self.object
}
}
impl<D: DomTypes> CallbackInterface<D> {
pub(crate) fn new_with_exterior_root() -> Self {
Self {
object: CallbackObject::new_with_exterior_root(),
}
}
pub fn get_callable_property(&self, cx: &mut JSContext, name: &CStr) -> Fallible<JSVal> {
rooted!(&in(cx) let mut callable = UndefinedValue());
rooted!(&in(cx) let obj = self.callback_holder().get());
unsafe {
if !JS_GetProperty(cx, obj.handle(), name.as_ptr(), callable.handle_mut()) {
return Err(Error::JSFailed);
}
if !callable.is_object() || !IsCallable(callable.to_object()) {
return Err(Error::Type(cformat!(
"The value of the {} property is not callable",
name.to_string_lossy()
)));
}
}
Ok(callable.get())
}
}
pub(crate) fn wrap_call_this_value<T: ThisReflector>(
cx: &mut JSContext,
p: &T,
mut rval: MutableHandleValue,
) -> bool {
rooted!(&in(cx) let mut obj = p.jsobject());
if obj.is_null() {
rval.set(NullValue());
return true;
}
unsafe {
if !JS_WrapObject(cx, obj.handle_mut()) {
return false;
}
}
rval.set(ObjectValue(*obj));
true
}
pub(crate) fn call_setup<D: DomTypes, T: HasCallbackHolder<D = D>, R>(
cx: &mut JSContext,
callback: &T,
owner_window: Option<&D::Window>,
handling: ExceptionHandling,
f: impl FnOnce(&mut JSContext) -> R,
) -> R {
if let Some(window) = owner_window {
window.Document().ensure_safe_to_run_script_or_layout();
}
let global = unsafe { D::GlobalScope::from_object(callback.callback()) };
let global = &global;
run_a_script::<D, R, _>(cx, global, move |cx| {
let actual_callback = || {
let old_realm = unsafe { EnterRealm(cx, callback.callback()) };
let result = f(cx);
unsafe {
LeaveRealm(cx, old_realm);
}
if handling == ExceptionHandling::Report {
let mut realm = enter_auto_realm::<D>(cx, &**global);
let cx = &mut realm.current_realm();
<D as DomHelpers<D>>::report_pending_exception(cx);
}
result
};
if let Some(incumbent_global) = callback.callback_holder().incumbent.as_deref() {
run_a_callback::<D, R>(incumbent_global, actual_callback)
} else {
actual_callback()
}
}) }