script: Impl safe from_jsval wrapper (#38149)

Implement `SafeFromJSValConvertible`, a safe wrapper for
`ToJSValConvertible`. And, replace unsafe `ToJSValConvertible` with
`SafeFromJSValConvertible` in `script/dom` to reduce the amount of
unsafe code in `script`.

This would support the implementation of `AdoptedStylesheet` where we
will need to have a setter/getter of sequence, that was implemented by
`any` types.

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

Signed-off-by: Jo Steven Novaryo <jo.steven.novaryo@huawei.com>
This commit is contained in:
Jo Steven Novaryo 2025-07-18 03:32:36 +08:00 committed by GitHub
parent dcae2dd9fd
commit 3ce95b2ba5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 79 additions and 64 deletions

View file

@ -79,6 +79,30 @@ impl ToJSValConvertible for DOMString {
}
}
/// A safe wrapper for `FromJSValConvertible`.
pub trait SafeFromJSValConvertible: Sized {
type Config;
#[allow(clippy::result_unit_err)] // Type definition depends on mozjs
fn safe_from_jsval(
cx: SafeJSContext,
value: HandleValue,
option: Self::Config,
) -> Result<ConversionResult<Self>, ()>;
}
impl<T: FromJSValConvertible> SafeFromJSValConvertible for T {
type Config = <T as FromJSValConvertible>::Config;
fn safe_from_jsval(
cx: SafeJSContext,
value: HandleValue,
option: Self::Config,
) -> Result<ConversionResult<Self>, ()> {
unsafe { T::from_jsval(*cx, value, option) }
}
}
// https://heycam.github.io/webidl/#es-DOMString
impl FromJSValConvertible for DOMString {
type Config = StringificationBehavior;