diff --git a/components/script/dom/trustedtypepolicyfactory.rs b/components/script/dom/trustedtypepolicyfactory.rs index db7d77b3560..2bcb493e956 100644 --- a/components/script/dom/trustedtypepolicyfactory.rs +++ b/components/script/dom/trustedtypepolicyfactory.rs @@ -342,7 +342,7 @@ impl TrustedTypePolicyFactory { cx: JSContext, value: HandleValue, ) -> Result, ()> { - unsafe { root_from_handlevalue::(value, *cx) } + root_from_handlevalue::(value, cx) } } @@ -359,7 +359,7 @@ impl TrustedTypePolicyFactoryMethods for TrustedTypePolicy /// #[allow(unsafe_code)] fn IsHTML(&self, cx: JSContext, value: HandleValue) -> bool { - unsafe { root_from_handlevalue::(value, *cx).is_ok() } + root_from_handlevalue::(value, cx).is_ok() } /// #[allow(unsafe_code)] @@ -369,7 +369,7 @@ impl TrustedTypePolicyFactoryMethods for TrustedTypePolicy /// #[allow(unsafe_code)] fn IsScriptURL(&self, cx: JSContext, value: HandleValue) -> bool { - unsafe { root_from_handlevalue::(value, *cx).is_ok() } + root_from_handlevalue::(value, cx).is_ok() } /// fn EmptyHTML(&self, can_gc: CanGc) -> DomRoot { diff --git a/components/script/indexed_db.rs b/components/script/indexed_db.rs index 3f737c5319f..864f7e7dc0e 100644 --- a/components/script/indexed_db.rs +++ b/components/script/indexed_db.rs @@ -295,70 +295,57 @@ pub(crate) fn evaluate_key_path_on_value( // If value is a Blob and identifier is "size" if identifier == "size" { - unsafe { - if let Ok(blob) = root_from_handlevalue::(current_value.handle(), *cx) - { - // Let value be a Number equal to value’s size. - blob.Size().safe_to_jsval(cx, current_value.handle_mut()); + if let Ok(blob) = root_from_handlevalue::(current_value.handle(), cx) { + // Let value be a Number equal to value’s size. + blob.Size().safe_to_jsval(cx, current_value.handle_mut()); - continue; - } + continue; } } // If value is a Blob and identifier is "type" if identifier == "type" { - unsafe { - if let Ok(blob) = root_from_handlevalue::(current_value.handle(), *cx) - { - // Let value be a String equal to value’s type. - blob.Type().safe_to_jsval(cx, current_value.handle_mut()); + if let Ok(blob) = root_from_handlevalue::(current_value.handle(), cx) { + // Let value be a String equal to value’s type. + blob.Type().safe_to_jsval(cx, current_value.handle_mut()); - continue; - } + continue; } } // If value is a File and identifier is "name" if identifier == "name" { - unsafe { - if let Ok(file) = root_from_handlevalue::(current_value.handle(), *cx) - { - // Let value be a String equal to value’s name. - file.name().safe_to_jsval(cx, current_value.handle_mut()); + if let Ok(file) = root_from_handlevalue::(current_value.handle(), cx) { + // Let value be a String equal to value’s name. + file.name().safe_to_jsval(cx, current_value.handle_mut()); - continue; - } + continue; } } // If value is a File and identifier is "lastModified" if identifier == "lastModified" { - unsafe { - if let Ok(file) = root_from_handlevalue::(current_value.handle(), *cx) - { - // Let value be a Number equal to value’s lastModified. - file.LastModified() - .safe_to_jsval(cx, current_value.handle_mut()); + if let Ok(file) = root_from_handlevalue::(current_value.handle(), cx) { + // Let value be a Number equal to value’s lastModified. + file.LastModified() + .safe_to_jsval(cx, current_value.handle_mut()); - continue; - } + continue; } } // If value is a File and identifier is "lastModifiedDate" if identifier == "lastModifiedDate" { - unsafe { - if let Ok(file) = root_from_handlevalue::(current_value.handle(), *cx) - { - // Let value be a new Date object with [[DateValue]] internal slot equal to value’s lastModified. - let time = ClippedTime { - t: file.LastModified() as f64, - }; + if let Ok(file) = root_from_handlevalue::(current_value.handle(), cx) { + // Let value be a new Date object with [[DateValue]] internal slot equal to value’s lastModified. + let time = ClippedTime { + t: file.LastModified() as f64, + }; + unsafe { NewDateObject(*cx, time).safe_to_jsval(cx, current_value.handle_mut()); - - continue; } + + continue; } } diff --git a/components/script_bindings/codegen/codegen.py b/components/script_bindings/codegen/codegen.py index 7f5548120c1..92ac3780e55 100644 --- a/components/script_bindings/codegen/codegen.py +++ b/components/script_bindings/codegen/codegen.py @@ -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})" diff --git a/components/script_bindings/conversions.rs b/components/script_bindings/conversions.rs index a529759b351..54b56121c8a 100644 --- a/components/script_bindings/conversions.rs +++ b/components/script_bindings/conversions.rs @@ -229,10 +229,12 @@ impl FromJSValConvertible for DomRoot { value: HandleValue, _config: Self::Config, ) -> Result>, ()> { - 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(v: HandleValue, cx: *mut JSContext) -> Result, ()> +pub fn root_from_handlevalue(v: HandleValue, cx: SafeJSContext) -> Result, ()> 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(v: HandleValue, cx: *mut JSContext) -> Result<*const T, ()> +pub fn native_from_handlevalue(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 ToJSValConvertible for RootedTraceableBox { @@ -587,7 +596,7 @@ pub unsafe fn is_array_like(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( v: HandleValue, - _cx: *mut JSContext, + _cx: SafeJSContext, ) -> Result, ()> { if !v.get().is_object() { return Err(());