diff --git a/components/script/dom/bindings/error.rs b/components/script/dom/bindings/error.rs index 01edba3bfb4..1fe3b187298 100644 --- a/components/script/dom/bindings/error.rs +++ b/components/script/dom/bindings/error.rs @@ -16,13 +16,12 @@ use js::jsval::UndefinedValue; use js::rust::wrappers::{JS_ErrorFromException, JS_GetPendingException, JS_SetPendingException}; use js::rust::{HandleObject, HandleValue, MutableHandleValue}; use libc::c_uint; +use script_bindings::conversions::SafeToJSValConvertible; pub(crate) use script_bindings::error::*; #[cfg(feature = "js_backtrace")] use crate::dom::bindings::cell::DomRefCell; -use crate::dom::bindings::conversions::{ - ConversionResult, FromJSValConvertible, ToJSValConvertible, root_from_object, -}; +use crate::dom::bindings::conversions::{ConversionResult, FromJSValConvertible, root_from_object}; use crate::dom::bindings::str::USVString; use crate::dom::domexception::{DOMErrorName, DOMException}; use crate::dom::globalscope::GlobalScope; @@ -80,7 +79,7 @@ pub(crate) fn throw_dom_exception( can_gc, ); rooted!(in(*cx) let mut thrown = UndefinedValue()); - exception.to_jsval(*cx, thrown.handle_mut()); + exception.safe_to_jsval(cx, thrown.handle_mut()); JS_SetPendingException(*cx, thrown.handle(), ExceptionStackBehavior::Capture); return; }, @@ -119,7 +118,7 @@ pub(crate) fn throw_dom_exception( assert!(!JS_IsExceptionPending(*cx)); let exception = DOMException::new(global, code, can_gc); rooted!(in(*cx) let mut thrown = UndefinedValue()); - exception.to_jsval(*cx, thrown.handle_mut()); + exception.safe_to_jsval(cx, thrown.handle_mut()); JS_SetPendingException(*cx, thrown.handle(), ExceptionStackBehavior::Capture); } } diff --git a/components/script/dom/bindings/structuredclone.rs b/components/script/dom/bindings/structuredclone.rs index 929209f7432..6047c783189 100644 --- a/components/script/dom/bindings/structuredclone.rs +++ b/components/script/dom/bindings/structuredclone.rs @@ -32,10 +32,10 @@ use js::jsapi::{ use js::jsval::UndefinedValue; use js::rust::wrappers::{JS_ReadStructuredClone, JS_WriteStructuredClone}; use js::rust::{CustomAutoRooterGuard, HandleValue, MutableHandleValue}; -use script_bindings::conversions::IDLInterface; +use script_bindings::conversions::{IDLInterface, SafeToJSValConvertible}; use strum::IntoEnumIterator; -use crate::dom::bindings::conversions::{ToJSValConvertible, root_from_object}; +use crate::dom::bindings::conversions::root_from_object; use crate::dom::bindings::error::{Error, Fallible}; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::serializable::{Serializable, StorageKey}; @@ -594,7 +594,7 @@ pub(crate) fn write( unsafe { rooted!(in(*cx) let mut val = UndefinedValue()); if let Some(transfer) = transfer { - transfer.to_jsval(*cx, val.handle_mut()); + transfer.safe_to_jsval(cx, val.handle_mut()); } let mut sc_writer = StructuredDataWriter::default(); let sc_writer_ptr = &mut sc_writer as *mut _; diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index ba7db745832..cbccc314720 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -13,6 +13,7 @@ use js::jsapi::{ CallArgs, DOMCallbacks, HandleObject as RawHandleObject, JS_FreezeObject, JSContext, JSObject, }; use js::rust::{HandleObject, MutableHandleValue, get_object_class, is_dom_class}; +use script_bindings::conversions::SafeToJSValConvertible; use script_bindings::interfaces::{DomHelpers, Interface}; use script_bindings::settings_stack::StackEntry; @@ -59,7 +60,7 @@ pub(crate) fn to_frozen_array( mut rval: MutableHandleValue, _can_gc: CanGc, ) { - unsafe { convertibles.to_jsval(*cx, rval.reborrow()) }; + convertibles.safe_to_jsval(cx, rval.reborrow()); rooted!(in(*cx) let obj = rval.to_object()); unsafe { JS_FreezeObject(*cx, RawHandleObject::from(obj.handle())) }; diff --git a/components/script/dom/idbrequest.rs b/components/script/dom/idbrequest.rs index 3bb3038ec00..b2f2e3fe08f 100644 --- a/components/script/dom/idbrequest.rs +++ b/components/script/dom/idbrequest.rs @@ -8,7 +8,6 @@ use std::iter::repeat; use constellation_traits::StructuredSerializedData; use dom_struct::dom_struct; use ipc_channel::router::ROUTER; -use js::conversions::ToJSValConvertible; use js::gc::MutableHandle; use js::jsapi::Heap; use js::jsval::{DoubleValue, JSVal, UndefinedValue}; @@ -18,6 +17,7 @@ use net_traits::indexeddb_thread::{ AsyncOperation, IdbResult, IndexedDBKeyType, IndexedDBThreadMsg, IndexedDBTxnMode, }; use profile_traits::ipc; +use script_bindings::conversions::SafeToJSValConvertible; use stylo_atoms::Atom; use crate::dom::bindings::codegen::Bindings::IDBRequestBinding::{ @@ -48,22 +48,22 @@ struct RequestListener { fn key_type_to_jsval(cx: SafeJSContext, key: &IndexedDBKeyType, mut result: MutableHandleValue) { match key { IndexedDBKeyType::Number(n) => result.set(DoubleValue(*n)), - IndexedDBKeyType::String(s) => unsafe { s.to_jsval(*cx, result) }, - IndexedDBKeyType::Binary(b) => unsafe { b.to_jsval(*cx, result) }, + IndexedDBKeyType::String(s) => s.safe_to_jsval(cx, result), + IndexedDBKeyType::Binary(b) => b.safe_to_jsval(cx, result), IndexedDBKeyType::Date(_d) => { // TODO: implement this when Date's representation is finalized. result.set(UndefinedValue()); }, - IndexedDBKeyType::Array(a) => unsafe { + IndexedDBKeyType::Array(a) => { rooted_vec!(let mut values <- repeat(UndefinedValue()).take(a.len())); - for (key, value) in a.iter().zip( + for (key, value) in a.iter().zip(unsafe { values .iter_mut() - .map(|v| MutableHandle::from_marked_location(v)), - ) { + .map(|v| MutableHandle::from_marked_location(v)) + }) { key_type_to_jsval(cx, key, value); } - values.to_jsval(*cx, result); + values.safe_to_jsval(cx, result); }, } } diff --git a/components/script/dom/messageport.rs b/components/script/dom/messageport.rs index a002b5d7d35..fa7556fb9ef 100644 --- a/components/script/dom/messageport.rs +++ b/components/script/dom/messageport.rs @@ -13,6 +13,7 @@ use dom_struct::dom_struct; use js::jsapi::{Heap, JS_NewObject, JSObject}; use js::jsval::UndefinedValue; use js::rust::{CustomAutoRooter, CustomAutoRooterGuard, HandleValue}; +use script_bindings::conversions::SafeToJSValConvertible; use crate::dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull; use crate::dom::bindings::codegen::Bindings::MessagePortBinding::{ @@ -29,7 +30,6 @@ use crate::dom::bindings::transferable::Transferable; use crate::dom::bindings::utils::set_dictionary_property; use crate::dom::eventtarget::EventTarget; use crate::dom::globalscope::GlobalScope; -use crate::js::conversions::ToJSValConvertible; use crate::script_runtime::{CanGc, JSContext as SafeJSContext}; #[dom_struct] @@ -219,9 +219,7 @@ impl MessagePort { // Let message be OrdinaryObjectCreate(null). rooted!(in(*cx) let mut message = unsafe { JS_NewObject(*cx, ptr::null()) }); rooted!(in(*cx) let mut type_string = UndefinedValue()); - unsafe { - type_.to_jsval(*cx, type_string.handle_mut()); - } + type_.safe_to_jsval(cx, type_string.handle_mut()); // Perform ! CreateDataProperty(message, "type", type). unsafe { @@ -244,9 +242,7 @@ impl MessagePort { // Run the message port post message steps providing targetPort, message, and options. rooted!(in(*cx) let mut message_val = UndefinedValue()); - unsafe { - message.to_jsval(*cx, message_val.handle_mut()); - } + message.safe_to_jsval(cx, message_val.handle_mut()); self.post_message_impl(cx, message_val.handle(), transfer) } } diff --git a/components/script/dom/trustedtypepolicyfactory.rs b/components/script/dom/trustedtypepolicyfactory.rs index 8b385efcc8d..35890aadf3b 100644 --- a/components/script/dom/trustedtypepolicyfactory.rs +++ b/components/script/dom/trustedtypepolicyfactory.rs @@ -7,6 +7,7 @@ use dom_struct::dom_struct; use html5ever::{LocalName, Namespace, QualName, local_name, ns}; use js::jsval::NullValue; use js::rust::HandleValue; +use script_bindings::conversions::SafeToJSValConvertible; use crate::dom::bindings::codegen::Bindings::TrustedTypePolicyFactoryBinding::{ TrustedTypePolicyFactoryMethods, TrustedTypePolicyOptions, @@ -22,7 +23,6 @@ use crate::dom::trustedhtml::TrustedHTML; use crate::dom::trustedscript::TrustedScript; use crate::dom::trustedscripturl::TrustedScriptURL; use crate::dom::trustedtypepolicy::{TrustedType, TrustedTypePolicy}; -use crate::js::conversions::ToJSValConvertible; use crate::script_runtime::{CanGc, JSContext}; #[dom_struct] @@ -137,7 +137,6 @@ impl TrustedTypePolicyFactory { data } /// - #[allow(unsafe_code)] pub(crate) fn process_value_with_default_policy( expected_type: TrustedType, global: &GlobalScope, @@ -155,15 +154,11 @@ impl TrustedTypePolicyFactory { // Step 2: Let policyValue be the result of executing Get Trusted Type policy value, // with the following arguments: rooted!(in(*cx) let mut trusted_type_name_value = NullValue()); - unsafe { - let trusted_type_name: &'static str = expected_type.clone().into(); - trusted_type_name.to_jsval(*cx, trusted_type_name_value.handle_mut()); - } + let trusted_type_name: &'static str = expected_type.clone().into(); + trusted_type_name.safe_to_jsval(cx, trusted_type_name_value.handle_mut()); rooted!(in(*cx) let mut sink_value = NullValue()); - unsafe { - sink.to_jsval(*cx, sink_value.handle_mut()); - } + sink.safe_to_jsval(cx, sink_value.handle_mut()); let arguments = vec![trusted_type_name_value.handle(), sink_value.handle()]; let policy_value = default_policy.get_trusted_type_policy_value( diff --git a/components/script/dom/webgl2renderingcontext.rs b/components/script/dom/webgl2renderingcontext.rs index 5e3811e1e7e..db3d7dc5223 100644 --- a/components/script/dom/webgl2renderingcontext.rs +++ b/components/script/dom/webgl2renderingcontext.rs @@ -22,6 +22,7 @@ use js::jsval::{BooleanValue, DoubleValue, Int32Value, NullValue, ObjectValue, U use js::rust::{CustomAutoRooterGuard, HandleObject, MutableHandleValue}; use js::typedarray::{ArrayBufferView, CreateWith, Float32, Int32Array, Uint32, Uint32Array}; use pixels::{Alpha, Snapshot}; +use script_bindings::conversions::SafeToJSValConvertible; use script_bindings::interfaces::WebGL2RenderingContextHelpers; use servo_config::pref; use url::Host; @@ -72,7 +73,6 @@ use crate::dom::webgltransformfeedback::WebGLTransformFeedback; use crate::dom::webgluniformlocation::WebGLUniformLocation; use crate::dom::webglvertexarrayobject::WebGLVertexArrayObject; use crate::dom::window::Window; -use crate::js::conversions::ToJSValConvertible; use crate::script_runtime::{CanGc, JSContext}; #[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)] @@ -647,7 +647,6 @@ impl WebGL2RenderingContext { Ok(()) } - #[allow(unsafe_code)] fn get_specific_fb_attachment_param( &self, cx: JSContext, @@ -692,11 +691,11 @@ impl WebGL2RenderingContext { if pname == constants::FRAMEBUFFER_ATTACHMENT_OBJECT_NAME { match fb.attachment(attachment) { - Some(Renderbuffer(rb)) => unsafe { - rb.to_jsval(*cx, rval); + Some(Renderbuffer(rb)) => { + rb.safe_to_jsval(cx, rval); }, - Some(Texture(texture)) => unsafe { - texture.to_jsval(*cx, rval); + Some(Texture(texture)) => { + texture.safe_to_jsval(cx, rval); }, _ => rval.set(NullValue()), } @@ -1033,16 +1032,15 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont self.base.get_buffer_param(buffer, parameter, retval) } - #[allow(unsafe_code)] /// fn GetParameter(&self, cx: JSContext, parameter: u32, mut rval: MutableHandleValue) { match parameter { - constants::VERSION => unsafe { - "WebGL 2.0".to_jsval(*cx, rval); + constants::VERSION => { + "WebGL 2.0".safe_to_jsval(cx, rval); return; }, - constants::SHADING_LANGUAGE_VERSION => unsafe { - "WebGL GLSL ES 3.00".to_jsval(*cx, rval); + constants::SHADING_LANGUAGE_VERSION => { + "WebGL GLSL ES 3.00".safe_to_jsval(cx, rval); return; }, constants::MAX_CLIENT_WAIT_TIMEOUT_WEBGL => { @@ -1057,60 +1055,62 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont )); return; }, - constants::SAMPLER_BINDING => unsafe { + constants::SAMPLER_BINDING => { let idx = (self.base.textures().active_unit_enum() - constants::TEXTURE0) as usize; assert!(idx < self.samplers.len()); let sampler = self.samplers[idx].get(); - sampler.to_jsval(*cx, rval); + sampler.safe_to_jsval(cx, rval); return; }, - constants::COPY_READ_BUFFER_BINDING => unsafe { - self.bound_copy_read_buffer.get().to_jsval(*cx, rval); + constants::COPY_READ_BUFFER_BINDING => { + self.bound_copy_read_buffer.get().safe_to_jsval(cx, rval); return; }, - constants::COPY_WRITE_BUFFER_BINDING => unsafe { - self.bound_copy_write_buffer.get().to_jsval(*cx, rval); + constants::COPY_WRITE_BUFFER_BINDING => { + self.bound_copy_write_buffer.get().safe_to_jsval(cx, rval); return; }, - constants::PIXEL_PACK_BUFFER_BINDING => unsafe { - self.bound_pixel_pack_buffer.get().to_jsval(*cx, rval); + constants::PIXEL_PACK_BUFFER_BINDING => { + self.bound_pixel_pack_buffer.get().safe_to_jsval(cx, rval); return; }, - constants::PIXEL_UNPACK_BUFFER_BINDING => unsafe { - self.bound_pixel_unpack_buffer.get().to_jsval(*cx, rval); + constants::PIXEL_UNPACK_BUFFER_BINDING => { + self.bound_pixel_unpack_buffer.get().safe_to_jsval(cx, rval); return; }, - constants::TRANSFORM_FEEDBACK_BUFFER_BINDING => unsafe { + constants::TRANSFORM_FEEDBACK_BUFFER_BINDING => { self.bound_transform_feedback_buffer .get() - .to_jsval(*cx, rval); + .safe_to_jsval(cx, rval); return; }, - constants::UNIFORM_BUFFER_BINDING => unsafe { - self.bound_uniform_buffer.get().to_jsval(*cx, rval); + constants::UNIFORM_BUFFER_BINDING => { + self.bound_uniform_buffer.get().safe_to_jsval(cx, rval); return; }, - constants::TRANSFORM_FEEDBACK_BINDING => unsafe { - self.current_transform_feedback.get().to_jsval(*cx, rval); + constants::TRANSFORM_FEEDBACK_BINDING => { + self.current_transform_feedback + .get() + .safe_to_jsval(cx, rval); return; }, - constants::ELEMENT_ARRAY_BUFFER_BINDING => unsafe { + constants::ELEMENT_ARRAY_BUFFER_BINDING => { let buffer = self.current_vao().element_array_buffer().get(); - buffer.to_jsval(*cx, rval); + buffer.safe_to_jsval(cx, rval); return; }, - constants::VERTEX_ARRAY_BINDING => unsafe { + constants::VERTEX_ARRAY_BINDING => { let vao = self.current_vao(); let vao = vao.id().map(|_| &*vao); - vao.to_jsval(*cx, rval); + vao.safe_to_jsval(cx, rval); return; }, // NOTE: DRAW_FRAMEBUFFER_BINDING is the same as FRAMEBUFFER_BINDING, handled on the WebGL1 side - constants::READ_FRAMEBUFFER_BINDING => unsafe { + constants::READ_FRAMEBUFFER_BINDING => { self.base .get_read_framebuffer_slot() .get() - .to_jsval(*cx, rval); + .safe_to_jsval(cx, rval); return; }, constants::READ_BUFFER => { @@ -2034,7 +2034,6 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont } /// - #[allow(unsafe_code)] fn GetIndexedParameter( &self, cx: JSContext, @@ -2066,8 +2065,8 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont }; match target { - constants::TRANSFORM_FEEDBACK_BUFFER_BINDING | constants::UNIFORM_BUFFER_BINDING => unsafe { - binding.buffer.get().to_jsval(*cx, retval) + constants::TRANSFORM_FEEDBACK_BUFFER_BINDING | constants::UNIFORM_BUFFER_BINDING => { + binding.buffer.get().safe_to_jsval(cx, retval) }, constants::TRANSFORM_FEEDBACK_BUFFER_START | constants::UNIFORM_BUFFER_START => { retval.set(Int32Value(binding.start.get() as _)) @@ -4494,7 +4493,6 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont } /// - #[allow(unsafe_code)] fn GetActiveUniforms( &self, cx: JSContext, @@ -4520,12 +4518,12 @@ impl WebGL2RenderingContextMethods for WebGL2RenderingCont constants::UNIFORM_BLOCK_INDEX | constants::UNIFORM_OFFSET | constants::UNIFORM_ARRAY_STRIDE | - constants::UNIFORM_MATRIX_STRIDE => unsafe { - values.to_jsval(*cx, rval); + constants::UNIFORM_MATRIX_STRIDE => { + values.safe_to_jsval(cx, rval); }, - constants::UNIFORM_IS_ROW_MAJOR => unsafe { + constants::UNIFORM_IS_ROW_MAJOR => { let values = values.iter().map(|&v| v != 0).collect::>(); - values.to_jsval(*cx, rval); + values.safe_to_jsval(cx, rval); }, _ => unreachable!(), } diff --git a/components/script/dom/webglrenderingcontext.rs b/components/script/dom/webglrenderingcontext.rs index c65e8d87cbb..5702737ae1d 100644 --- a/components/script/dom/webglrenderingcontext.rs +++ b/components/script/dom/webglrenderingcontext.rs @@ -30,6 +30,7 @@ use js::typedarray::{ TypedArrayElementCreator, Uint32Array, }; use pixels::{self, Alpha, PixelFormat, Snapshot, SnapshotPixelFormat}; +use script_bindings::conversions::SafeToJSValConvertible; use serde::{Deserialize, Serialize}; use servo_config::pref; use webrender_api::ImageKey; @@ -48,7 +49,7 @@ use crate::dom::bindings::codegen::UnionTypes::{ ArrayBufferViewOrArrayBuffer, Float32ArrayOrUnrestrictedFloatSequence, HTMLCanvasElementOrOffscreenCanvas, Int32ArrayOrLongSequence, }; -use crate::dom::bindings::conversions::{DerivedFrom, ToJSValConvertible}; +use crate::dom::bindings::conversions::DerivedFrom; use crate::dom::bindings::error::{Error, ErrorResult, Fallible}; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::reflector::{DomGlobal, DomObject, Reflector, reflect_dom_object}; @@ -2128,37 +2129,37 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex } match parameter { - constants::ARRAY_BUFFER_BINDING => unsafe { - self.bound_buffer_array.get().to_jsval(*cx, retval); + constants::ARRAY_BUFFER_BINDING => { + self.bound_buffer_array.get().safe_to_jsval(cx, retval); return; }, - constants::CURRENT_PROGRAM => unsafe { - self.current_program.get().to_jsval(*cx, retval); + constants::CURRENT_PROGRAM => { + self.current_program.get().safe_to_jsval(cx, retval); return; }, - constants::ELEMENT_ARRAY_BUFFER_BINDING => unsafe { + constants::ELEMENT_ARRAY_BUFFER_BINDING => { let buffer = self.current_vao().element_array_buffer().get(); - buffer.to_jsval(*cx, retval); + buffer.safe_to_jsval(cx, retval); return; }, - constants::FRAMEBUFFER_BINDING => unsafe { - self.bound_draw_framebuffer.get().to_jsval(*cx, retval); + constants::FRAMEBUFFER_BINDING => { + self.bound_draw_framebuffer.get().safe_to_jsval(cx, retval); return; }, - constants::RENDERBUFFER_BINDING => unsafe { - self.bound_renderbuffer.get().to_jsval(*cx, retval); + constants::RENDERBUFFER_BINDING => { + self.bound_renderbuffer.get().safe_to_jsval(cx, retval); return; }, - constants::TEXTURE_BINDING_2D => unsafe { + constants::TEXTURE_BINDING_2D => { let texture = self .textures .active_texture_slot(constants::TEXTURE_2D, self.webgl_version()) .unwrap() .get(); - texture.to_jsval(*cx, retval); + texture.safe_to_jsval(cx, retval); return; }, - WebGL2RenderingContextConstants::TEXTURE_BINDING_2D_ARRAY => unsafe { + WebGL2RenderingContextConstants::TEXTURE_BINDING_2D_ARRAY => { let texture = self .textures .active_texture_slot( @@ -2167,10 +2168,10 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex ) .unwrap() .get(); - texture.to_jsval(*cx, retval); + texture.safe_to_jsval(cx, retval); return; }, - WebGL2RenderingContextConstants::TEXTURE_BINDING_3D => unsafe { + WebGL2RenderingContextConstants::TEXTURE_BINDING_3D => { let texture = self .textures .active_texture_slot( @@ -2179,21 +2180,21 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex ) .unwrap() .get(); - texture.to_jsval(*cx, retval); + texture.safe_to_jsval(cx, retval); return; }, - constants::TEXTURE_BINDING_CUBE_MAP => unsafe { + constants::TEXTURE_BINDING_CUBE_MAP => { let texture = self .textures .active_texture_slot(constants::TEXTURE_CUBE_MAP, self.webgl_version()) .unwrap() .get(); - texture.to_jsval(*cx, retval); + texture.safe_to_jsval(cx, retval); return; }, - OESVertexArrayObjectConstants::VERTEX_ARRAY_BINDING_OES => unsafe { + OESVertexArrayObjectConstants::VERTEX_ARRAY_BINDING_OES => { let vao = self.current_vao.get().filter(|vao| vao.id().is_some()); - vao.to_jsval(*cx, retval); + vao.safe_to_jsval(cx, retval); return; }, // In readPixels we currently support RGBA/UBYTE only. If @@ -2223,16 +2224,16 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex .unwrap(); return retval.set(ObjectValue(rval.get())); }, - constants::VERSION => unsafe { - "WebGL 1.0".to_jsval(*cx, retval); + constants::VERSION => { + "WebGL 1.0".safe_to_jsval(cx, retval); return; }, - constants::RENDERER | constants::VENDOR => unsafe { - "Mozilla/Servo".to_jsval(*cx, retval); + constants::RENDERER | constants::VENDOR => { + "Mozilla/Servo".safe_to_jsval(cx, retval); return; }, - constants::SHADING_LANGUAGE_VERSION => unsafe { - "WebGL GLSL ES 1.0".to_jsval(*cx, retval); + constants::SHADING_LANGUAGE_VERSION => { + "WebGL GLSL ES 1.0".safe_to_jsval(cx, retval); return; }, constants::UNPACK_FLIP_Y_WEBGL => { @@ -2310,10 +2311,10 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex self.send_command(WebGLCommand::GetParameterBool(param, sender)); retval.set(BooleanValue(receiver.recv().unwrap())) }, - Parameter::Bool4(param) => unsafe { + Parameter::Bool4(param) => { let (sender, receiver) = webgl_channel().unwrap(); self.send_command(WebGLCommand::GetParameterBool4(param, sender)); - receiver.recv().unwrap().to_jsval(*cx, retval); + receiver.recv().unwrap().safe_to_jsval(cx, retval); }, Parameter::Int(param) => { let (sender, receiver) = webgl_channel().unwrap(); @@ -3255,7 +3256,6 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex handle_potential_webgl_error!(self, program.get_attrib_location(name), -1) } - #[allow(unsafe_code)] // https://www.khronos.org/registry/webgl/specs/latest/1.0/#5.14.6 fn GetFramebufferAttachmentParameter( &self, @@ -3352,12 +3352,12 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex let fb = self.bound_draw_framebuffer.get().unwrap(); if let Some(webgl_attachment) = fb.attachment(attachment) { match webgl_attachment { - WebGLFramebufferAttachmentRoot::Renderbuffer(rb) => unsafe { - rb.to_jsval(*cx, retval); + WebGLFramebufferAttachmentRoot::Renderbuffer(rb) => { + rb.safe_to_jsval(cx, retval); return; }, - WebGLFramebufferAttachmentRoot::Texture(texture) => unsafe { - texture.to_jsval(*cx, retval); + WebGLFramebufferAttachmentRoot::Texture(texture) => { + texture.safe_to_jsval(cx, retval); return; }, } @@ -3640,9 +3640,9 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex retval.set(BooleanValue(data.normalized)) }, constants::VERTEX_ATTRIB_ARRAY_STRIDE => retval.set(Int32Value(data.stride as i32)), - constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING => unsafe { + constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING => { if let Some(buffer) = data.buffer() { - buffer.to_jsval(*cx, retval.reborrow()); + buffer.safe_to_jsval(cx, retval.reborrow()); } else { retval.set(NullValue()); } @@ -4264,14 +4264,14 @@ impl WebGLRenderingContextMethods for WebGLRenderingContex triple, WebGLCommand::GetUniformBool, ))), - constants::BOOL_VEC2 => unsafe { - uniform_get(triple, WebGLCommand::GetUniformBool2).to_jsval(*cx, rval); + constants::BOOL_VEC2 => { + uniform_get(triple, WebGLCommand::GetUniformBool2).safe_to_jsval(cx, rval) }, - constants::BOOL_VEC3 => unsafe { - uniform_get(triple, WebGLCommand::GetUniformBool3).to_jsval(*cx, rval); + constants::BOOL_VEC3 => { + uniform_get(triple, WebGLCommand::GetUniformBool3).safe_to_jsval(cx, rval) }, - constants::BOOL_VEC4 => unsafe { - uniform_get(triple, WebGLCommand::GetUniformBool4).to_jsval(*cx, rval); + constants::BOOL_VEC4 => { + uniform_get(triple, WebGLCommand::GetUniformBool4).safe_to_jsval(cx, rval) }, constants::INT | constants::SAMPLER_2D | diff --git a/components/script/dom/xmlhttprequest.rs b/components/script/dom/xmlhttprequest.rs index 05b839c5cea..96008f3a3d3 100644 --- a/components/script/dom/xmlhttprequest.rs +++ b/components/script/dom/xmlhttprequest.rs @@ -32,6 +32,7 @@ use net_traits::{ FetchMetadata, FetchResponseListener, FilteredMetadata, NetworkError, ReferrerPolicy, ResourceFetchTiming, ResourceTimingType, trim_http_whitespace, }; +use script_bindings::conversions::SafeToJSValConvertible; use script_bindings::num::Finite; use script_traits::DocumentActivity; use servo_url::ServoUrl; @@ -47,7 +48,6 @@ use crate::dom::bindings::codegen::Bindings::XMLHttpRequestBinding::{ XMLHttpRequestMethods, XMLHttpRequestResponseType, }; use crate::dom::bindings::codegen::UnionTypes::DocumentOrBlobOrArrayBufferViewOrArrayBufferOrFormDataOrStringOrURLSearchParams as DocumentOrXMLHttpRequestBodyInit; -use crate::dom::bindings::conversions::ToJSValConvertible; use crate::dom::bindings::error::{Error, ErrorResult, Fallible}; use crate::dom::bindings::inheritance::Castable; use crate::dom::bindings::refcounted::Trusted; @@ -919,20 +919,19 @@ impl XMLHttpRequestMethods for XMLHttpRequest { } } - #[allow(unsafe_code)] /// fn Response(&self, cx: JSContext, can_gc: CanGc, mut rval: MutableHandleValue) { match self.response_type.get() { - XMLHttpRequestResponseType::_empty | XMLHttpRequestResponseType::Text => unsafe { + XMLHttpRequestResponseType::_empty | XMLHttpRequestResponseType::Text => { let ready_state = self.ready_state.get(); // Step 2 if ready_state == XMLHttpRequestState::Done || ready_state == XMLHttpRequestState::Loading { - self.text_response().to_jsval(*cx, rval); + self.text_response().safe_to_jsval(cx, rval); } else { // Step 1 - "".to_jsval(*cx, rval); + "".safe_to_jsval(cx, rval); } }, // Step 1 @@ -940,16 +939,14 @@ impl XMLHttpRequestMethods for XMLHttpRequest { rval.set(NullValue()); }, // Step 2 - XMLHttpRequestResponseType::Document => unsafe { - self.document_response(can_gc).to_jsval(*cx, rval); + XMLHttpRequestResponseType::Document => { + self.document_response(can_gc).safe_to_jsval(cx, rval) }, XMLHttpRequestResponseType::Json => self.json_response(cx, rval), - XMLHttpRequestResponseType::Blob => unsafe { - self.blob_response(can_gc).to_jsval(*cx, rval); - }, + XMLHttpRequestResponseType::Blob => self.blob_response(can_gc).safe_to_jsval(cx, rval), XMLHttpRequestResponseType::Arraybuffer => { match self.arraybuffer_response(cx, can_gc) { - Some(array_buffer) => unsafe { array_buffer.to_jsval(*cx, rval) }, + Some(array_buffer) => array_buffer.safe_to_jsval(cx, rval), None => rval.set(NullValue()), } }, diff --git a/components/script_bindings/conversions.rs b/components/script_bindings/conversions.rs index 96fdc2040f3..4c02775ff1f 100644 --- a/components/script_bindings/conversions.rs +++ b/components/script_bindings/conversions.rs @@ -41,7 +41,7 @@ pub trait SafeToJSValConvertible { fn safe_to_jsval(&self, cx: SafeJSContext, rval: MutableHandleValue); } -impl SafeToJSValConvertible for T { +impl SafeToJSValConvertible for T { fn safe_to_jsval(&self, cx: SafeJSContext, rval: MutableHandleValue) { unsafe { self.to_jsval(*cx, rval) }; }