script: introduce safe wrappers for js val conversions (#38004)

Introduce a safe wrapper trait for the unsafe `ToJSValConvertible`, and
use it in `script/dom` where the default `T` implementation works.

Part of https://github.com/servo/servo/issues/37951

---------

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Gregory Terzian 2025-07-15 08:57:15 +07:00 committed by GitHub
parent 9e2ee0029a
commit 027954dbad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 149 additions and 187 deletions

View file

@ -31,10 +31,22 @@ use crate::inheritance::Castable;
use crate::num::Finite;
use crate::reflector::{DomObject, Reflector};
use crate::root::DomRoot;
use crate::script_runtime::JSContext as SafeJSContext;
use crate::str::{ByteString, DOMString, USVString};
use crate::trace::RootedTraceableBox;
use crate::utils::{DOMClass, DOMJSClass};
/// A safe wrapper for `ToJSValConvertible`.
pub trait SafeToJSValConvertible {
fn safe_to_jsval(&self, cx: SafeJSContext, rval: MutableHandleValue);
}
impl<T: ToJSValConvertible> SafeToJSValConvertible for T {
fn safe_to_jsval(&self, cx: SafeJSContext, rval: MutableHandleValue) {
unsafe { self.to_jsval(*cx, rval) };
}
}
/// A trait to check whether a given `JSObject` implements an IDL interface.
pub trait IDLInterface {
/// Returns whether the given DOM class derives that interface.