mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
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:
parent
dcae2dd9fd
commit
3ce95b2ba5
6 changed files with 79 additions and 64 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue