mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Remove some usage of unsafe code in CustomElementRegistry
This commit is contained in:
parent
0ecab7bbe0
commit
914bda9cd4
1 changed files with 29 additions and 35 deletions
|
@ -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,34 +217,32 @@ 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(),
|
||||||
) {
|
) {
|
||||||
return Err(Error::JSFailed);
|
return Err(Error::JSFailed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 10.4.2
|
// Step 10.4.2
|
||||||
if !callback.is_undefined() {
|
if !callback.is_undefined() {
|
||||||
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(cx, callback.to_object())))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
}
|
}
|
||||||
Ok(Some(Function::new(
|
|
||||||
SafeJSContext::from_ptr(cx),
|
|
||||||
callback.to_object(),
|
|
||||||
)))
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue