Remove some usage of unsafe code in CustomElementRegistry

This commit is contained in:
marmeladema 2019-07-27 16:09:05 +01:00
parent 0ecab7bbe0
commit 914bda9cd4

View file

@ -31,14 +31,14 @@ use crate::dom::node::{document_from_node, window_from_node, Node, ShadowIncludi
use crate::dom::promise::Promise; use crate::dom::promise::Promise;
use crate::dom::window::Window; use crate::dom::window::Window;
use crate::microtask::Microtask; use crate::microtask::Microtask;
use crate::script_runtime::JSContext as SafeJSContext; use crate::script_runtime::JSContext;
use crate::script_thread::ScriptThread; use crate::script_thread::ScriptThread;
use dom_struct::dom_struct; use dom_struct::dom_struct;
use html5ever::{LocalName, Namespace, Prefix}; use html5ever::{LocalName, Namespace, Prefix};
use js::conversions::ToJSValConvertible; use js::conversions::ToJSValConvertible;
use js::glue::UnwrapObjectStatic; use js::glue::UnwrapObjectStatic;
use js::jsapi::{HandleValueArray, Heap, IsCallable, IsConstructor}; use js::jsapi::{HandleValueArray, Heap, IsCallable, IsConstructor};
use js::jsapi::{JSAutoRealm, JSContext, JSObject}; use js::jsapi::{JSAutoRealm, JSObject};
use js::jsval::{JSVal, NullValue, ObjectValue, UndefinedValue}; use js::jsval::{JSVal, NullValue, ObjectValue, UndefinedValue};
use js::rust::wrappers::{Construct1, JS_GetProperty, SameValue}; use js::rust::wrappers::{Construct1, JS_GetProperty, SameValue};
use js::rust::{HandleObject, HandleValue, MutableHandleValue}; use js::rust::{HandleObject, HandleValue, MutableHandleValue};
@ -171,14 +171,10 @@ impl CustomElementRegistry {
// Step 4 // Step 4
Ok(LifecycleCallbacks { Ok(LifecycleCallbacks {
connected_callback: get_callback(*cx, prototype, b"connectedCallback\0")?, connected_callback: get_callback(cx, prototype, b"connectedCallback\0")?,
disconnected_callback: get_callback(*cx, prototype, b"disconnectedCallback\0")?, disconnected_callback: get_callback(cx, prototype, b"disconnectedCallback\0")?,
adopted_callback: get_callback(*cx, prototype, b"adoptedCallback\0")?, adopted_callback: get_callback(cx, prototype, b"adoptedCallback\0")?,
attribute_changed_callback: get_callback( attribute_changed_callback: get_callback(cx, prototype, b"attributeChangedCallback\0")?,
*cx,
prototype,
b"attributeChangedCallback\0",
)?,
}) })
} }
@ -221,16 +217,16 @@ impl CustomElementRegistry {
/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-define> /// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-define>
/// Step 10.4 /// Step 10.4
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn get_callback( fn get_callback(
cx: *mut JSContext, cx: JSContext,
prototype: HandleObject, prototype: HandleObject,
name: &[u8], name: &[u8],
) -> Fallible<Option<Rc<Function>>> { ) -> Fallible<Option<Rc<Function>>> {
rooted!(in(cx) let mut callback = UndefinedValue()); rooted!(in(*cx) let mut callback = UndefinedValue());
unsafe {
// Step 10.4.1 // Step 10.4.1
if !JS_GetProperty( if !JS_GetProperty(
cx, *cx,
prototype, prototype,
name.as_ptr() as *const _, name.as_ptr() as *const _,
callback.handle_mut(), callback.handle_mut(),
@ -243,13 +239,11 @@ unsafe fn get_callback(
if !callback.is_object() || !IsCallable(callback.to_object()) { if !callback.is_object() || !IsCallable(callback.to_object()) {
return Err(Error::Type("Lifecycle callback is not callable".to_owned())); return Err(Error::Type("Lifecycle callback is not callable".to_owned()));
} }
Ok(Some(Function::new( Ok(Some(Function::new(cx, callback.to_object())))
SafeJSContext::from_ptr(cx),
callback.to_object(),
)))
} else { } else {
Ok(None) Ok(None)
} }
}
} }
impl CustomElementRegistryMethods for CustomElementRegistry { impl CustomElementRegistryMethods for CustomElementRegistry {
@ -409,7 +403,7 @@ impl CustomElementRegistryMethods for CustomElementRegistry {
/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-get> /// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-get>
#[allow(unsafe_code)] #[allow(unsafe_code)]
fn Get(&self, cx: SafeJSContext, name: DOMString) -> JSVal { fn Get(&self, cx: JSContext, name: DOMString) -> JSVal {
match self.definitions.borrow().get(&LocalName::from(&*name)) { match self.definitions.borrow().get(&LocalName::from(&*name)) {
Some(definition) => unsafe { Some(definition) => unsafe {
rooted!(in(*cx) let mut constructor = UndefinedValue()); rooted!(in(*cx) let mut constructor = UndefinedValue());