aboutsummaryrefslogtreecommitdiffstats
path: root/components/script
diff options
context:
space:
mode:
authormarmeladema <xademax@gmail.com>2019-07-27 16:09:05 +0100
committermarmeladema <xademax@gmail.com>2019-08-09 00:02:08 +0100
commit914bda9cd41dd2e0724bbd758af35fcbed8ba0a5 (patch)
treed07ceb76594be7e3bbab6d5639076e3699268c43 /components/script
parent0ecab7bbe054b81b46dc227f0d5f3b303aef1920 (diff)
downloadservo-914bda9cd41dd2e0724bbd758af35fcbed8ba0a5.tar.gz
servo-914bda9cd41dd2e0724bbd758af35fcbed8ba0a5.zip
Remove some usage of unsafe code in CustomElementRegistry
Diffstat (limited to 'components/script')
-rw-r--r--components/script/dom/customelementregistry.rs62
1 files changed, 28 insertions, 34 deletions
diff --git a/components/script/dom/customelementregistry.rs b/components/script/dom/customelementregistry.rs
index 2603768ed83..318c9656adc 100644
--- a/components/script/dom/customelementregistry.rs
+++ b/components/script/dom/customelementregistry.rs
@@ -31,14 +31,14 @@ use crate::dom::node::{document_from_node, window_from_node, Node, ShadowIncludi
use crate::dom::promise::Promise;
use crate::dom::window::Window;
use crate::microtask::Microtask;
-use crate::script_runtime::JSContext as SafeJSContext;
+use crate::script_runtime::JSContext;
use crate::script_thread::ScriptThread;
use dom_struct::dom_struct;
use html5ever::{LocalName, Namespace, Prefix};
use js::conversions::ToJSValConvertible;
use js::glue::UnwrapObjectStatic;
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::rust::wrappers::{Construct1, JS_GetProperty, SameValue};
use js::rust::{HandleObject, HandleValue, MutableHandleValue};
@@ -171,14 +171,10 @@ impl CustomElementRegistry {
// Step 4
Ok(LifecycleCallbacks {
- connected_callback: get_callback(*cx, prototype, b"connectedCallback\0")?,
- disconnected_callback: get_callback(*cx, prototype, b"disconnectedCallback\0")?,
- adopted_callback: get_callback(*cx, prototype, b"adoptedCallback\0")?,
- attribute_changed_callback: get_callback(
- *cx,
- prototype,
- b"attributeChangedCallback\0",
- )?,
+ connected_callback: get_callback(cx, prototype, b"connectedCallback\0")?,
+ disconnected_callback: get_callback(cx, prototype, b"disconnectedCallback\0")?,
+ adopted_callback: get_callback(cx, prototype, b"adoptedCallback\0")?,
+ attribute_changed_callback: get_callback(cx, prototype, b"attributeChangedCallback\0")?,
})
}
@@ -221,34 +217,32 @@ impl CustomElementRegistry {
/// <https://html.spec.whatwg.org/multipage/#dom-customelementregistry-define>
/// Step 10.4
#[allow(unsafe_code)]
-unsafe fn get_callback(
- cx: *mut JSContext,
+fn get_callback(
+ cx: JSContext,
prototype: HandleObject,
name: &[u8],
) -> Fallible<Option<Rc<Function>>> {
- rooted!(in(cx) let mut callback = UndefinedValue());
-
- // Step 10.4.1
- if !JS_GetProperty(
- cx,
- prototype,
- name.as_ptr() as *const _,
- callback.handle_mut(),
- ) {
- return Err(Error::JSFailed);
- }
+ rooted!(in(*cx) let mut callback = UndefinedValue());
+ unsafe {
+ // Step 10.4.1
+ if !JS_GetProperty(
+ *cx,
+ prototype,
+ name.as_ptr() as *const _,
+ callback.handle_mut(),
+ ) {
+ return Err(Error::JSFailed);
+ }
- // Step 10.4.2
- if !callback.is_undefined() {
- if !callback.is_object() || !IsCallable(callback.to_object()) {
- return Err(Error::Type("Lifecycle callback is not callable".to_owned()));
+ // Step 10.4.2
+ if !callback.is_undefined() {
+ if !callback.is_object() || !IsCallable(callback.to_object()) {
+ 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>
#[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)) {
Some(definition) => unsafe {
rooted!(in(*cx) let mut constructor = UndefinedValue());