script: Make root_from_handle_value a safe function by accepting SafeJSContext (#39193)

Change from unsafe pointer of root_from_handle_value  to SafeJSContext. 

#39131

---------

Signed-off-by: JasonHonKL <j2004nol@gmail.com>
Signed-off-by: Jason <jason@198-61-252-113-on-nets.com>
Signed-off-by: Josh Matthews <josh@joshmatthews.net>
Co-authored-by: Jason <jason@198-61-252-113-on-nets.com>
Co-authored-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
JasonHonKL 2025-09-09 12:26:02 -07:00 committed by GitHub
parent 721fc01c30
commit 839878c743
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 48 additions and 51 deletions

View file

@ -981,7 +981,7 @@ def getJSToNativeConversionInfo(type: IDLType, descriptorProvider: DescriptorPro
templateBody = fill(
"""
match ${function}($${val}, *cx) {
match ${function}($${val}, cx) {
Ok(val) => val,
Err(()) => {
$*{failureCode}
@ -991,6 +991,7 @@ def getJSToNativeConversionInfo(type: IDLType, descriptorProvider: DescriptorPro
failureCode=unwrapFailureCode + "\n",
function=conversionFunction)
declType = CGGeneric(descriptorType)
if type.nullable():
templateBody = f"Some({templateBody})"

View file

@ -229,10 +229,12 @@ impl<T: DomObject + IDLInterface> FromJSValConvertible for DomRoot<T> {
value: HandleValue,
_config: Self::Config,
) -> Result<ConversionResult<DomRoot<T>>, ()> {
Ok(match root_from_handlevalue(value, cx) {
Ok(result) => ConversionResult::Success(result),
Err(()) => ConversionResult::Failure("value is not an object".into()),
})
Ok(
match root_from_handlevalue(value, SafeJSContext::from_ptr(cx)) {
Ok(result) => ConversionResult::Success(result),
Err(()) => ConversionResult::Failure("value is not an object".into()),
},
)
}
}
@ -398,14 +400,17 @@ where
/// # Safety
/// cx must point to a valid, non-null JS context.
#[allow(clippy::result_unit_err)]
pub unsafe fn root_from_handlevalue<T>(v: HandleValue, cx: *mut JSContext) -> Result<DomRoot<T>, ()>
pub fn root_from_handlevalue<T>(v: HandleValue, cx: SafeJSContext) -> Result<DomRoot<T>, ()>
where
T: DomObject + IDLInterface,
{
if !v.get().is_object() {
return Err(());
}
root_from_object(v.get().to_object(), cx)
#[allow(unsafe_code)]
unsafe {
root_from_object(v.get().to_object(), *cx)
}
}
/// Convert `id` to a `DOMString`. Returns `None` if `id` is not a string or
@ -503,14 +508,18 @@ where
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
#[allow(clippy::result_unit_err)]
pub unsafe fn native_from_handlevalue<T>(v: HandleValue, cx: *mut JSContext) -> Result<*const T, ()>
pub fn native_from_handlevalue<T>(v: HandleValue, cx: SafeJSContext) -> Result<*const T, ()>
where
T: DomObject + IDLInterface,
{
if !v.get().is_object() {
return Err(());
}
native_from_object(v.get().to_object(), cx)
#[allow(unsafe_code)]
unsafe {
native_from_object(v.get().to_object(), *cx)
}
}
impl<T: ToJSValConvertible + JSTraceable> ToJSValConvertible for RootedTraceableBox<T> {
@ -587,7 +596,7 @@ pub unsafe fn is_array_like<D: crate::DomTypes>(cx: *mut JSContext, value: Handl
/// Caller is responsible for throwing a JS exception if needed in case of error.
pub(crate) unsafe fn windowproxy_from_handlevalue<D: crate::DomTypes>(
v: HandleValue,
_cx: *mut JSContext,
_cx: SafeJSContext,
) -> Result<DomRoot<D::WindowProxy>, ()> {
if !v.get().is_object() {
return Err(());