Skip to content

Commit b290a31

Browse files
committed
Clean up the conversion routines
Functions returning `Root<T>` are prefixed by "root_" and the ones returning `*const T` by "native_". Functions taking `*mut JSObject` are now suffixed by "_from_object" and the ones taking `&T` by "_from_reflector".
1 parent aa105d8 commit b290a31

File tree

10 files changed

+45
-61
lines changed

10 files changed

+45
-61
lines changed

components/script/dom/bindings/callback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Base classes to work with IDL callbacks.
66
77
use dom::bindings::error::{Error, Fallible};
8-
use dom::bindings::global::global_object_for_js_object;
8+
use dom::bindings::global::global_root_from_object;
99
use dom::bindings::reflector::Reflectable;
1010
use js::jsapi::GetGlobalForObjectCrossCompartment;
1111
use js::jsapi::{Heap, MutableHandleObject, RootedObject, RootedValue};
@@ -170,7 +170,7 @@ impl CallSetup {
170170
/// Performs the setup needed to make a call.
171171
#[allow(unrooted_must_root)]
172172
pub fn new<T: CallbackContainer>(callback: &T, handling: ExceptionHandling) -> CallSetup {
173-
let global = global_object_for_js_object(callback.callback());
173+
let global = global_root_from_object(callback.callback());
174174
let cx = global.r().get_cx();
175175
unsafe { JS_BeginRequest(cx); }
176176

components/script/dom/bindings/codegen/CodegenRust.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __init__(self, descriptor, source, codeOnFailure, handletype):
119119

120120
def __str__(self):
121121
return string.Template("""\
122-
match native_from_handle${handletype}(${source}) {
122+
match root_from_handle${handletype}(${source}) {
123123
Ok(val) => val,
124124
Err(()) => {
125125
${codeOnFailure}
@@ -1996,7 +1996,7 @@ def UnionTypes(descriptors, dictionaries, callbacks, config):
19961996
'dom::bindings::conversions::FromJSValConvertible',
19971997
'dom::bindings::conversions::ToJSValConvertible',
19981998
'dom::bindings::conversions::ConversionBehavior',
1999-
'dom::bindings::conversions::native_from_handlevalue',
1999+
'dom::bindings::conversions::root_from_handlevalue',
20002000
'dom::bindings::conversions::StringificationBehavior',
20012001
'dom::bindings::error::throw_not_in_union',
20022002
'dom::bindings::js::Root',
@@ -2638,7 +2638,7 @@ def __init__(self, errorResult, arguments, argsPre, returnType,
26382638
if static:
26392639
glob = ""
26402640
else:
2641-
glob = " let global = global_object_for_js_object(this.reflector().get_jsobject().get());\n"
2641+
glob = " let global = global_root_from_reflector(this);\n"
26422642

26432643
self.cgRoot.append(CGGeneric(
26442644
"let result = match result {\n"
@@ -2845,7 +2845,7 @@ def __init__(self, descriptor, name):
28452845

28462846
def definition_body(self):
28472847
preamble = CGGeneric("""\
2848-
let global = global_object_for_js_object(JS_CALLEE(cx, vp).to_object());
2848+
let global = global_root_from_object(JS_CALLEE(cx, vp).to_object());
28492849
""")
28502850
return CGList([preamble, self.generate_code()])
28512851

@@ -4532,8 +4532,8 @@ def __init__(self, descriptor, name, returnType, args):
45324532

45334533
def definition_body_prologue(self):
45344534
return CGGeneric("""\
4535-
let this: *const %s = native_from_reflector::<%s>(obj);
4536-
""" % (self.descriptor.concreteType, self.descriptor.concreteType))
4535+
let this = private_from_object(obj) as *const %s;
4536+
""" % self.descriptor.concreteType)
45374537

45384538
def definition_body(self):
45394539
return CGList([
@@ -4593,7 +4593,7 @@ def define(self):
45934593

45944594
def definition_body(self):
45954595
preamble = CGGeneric("""\
4596-
let global = global_object_for_js_object(JS_CALLEE(cx, vp).to_object());
4596+
let global = global_root_from_object(JS_CALLEE(cx, vp).to_object());
45974597
let args = CallArgs::from_vp(vp, argc);
45984598
""")
45994599
name = self._ctor.identifier.name
@@ -4617,7 +4617,7 @@ def __init__(self, descriptor, ctor):
46174617

46184618
def definition_body(self):
46194619
preamble = CGGeneric("""\
4620-
let global = global_object_for_js_object(JS_CALLEE(cx, vp).to_object());
4620+
let global = global_root_from_object(JS_CALLEE(cx, vp).to_object());
46214621
let args = CallArgs::from_vp(vp, argc);
46224622
""")
46234623
name = self._ctor.identifier.name
@@ -5196,8 +5196,7 @@ def __init__(self, config, prefix, webIDLFile):
51965196
'js::glue::AppendToAutoIdVector',
51975197
'js::rust::GCMethods',
51985198
'dom::bindings',
5199-
'dom::bindings::global::GlobalRef',
5200-
'dom::bindings::global::global_object_for_js_object',
5199+
'dom::bindings::global::{GlobalRef, global_root_from_object, global_root_from_reflector}',
52015200
'dom::bindings::js::{JS, Root, RootedReference}',
52025201
'dom::bindings::js::{OptionalRootedReference}',
52035202
'dom::bindings::reflector::{Reflectable}',
@@ -5223,12 +5222,11 @@ def __init__(self, config, prefix, webIDLFile):
52235222
'dom::bindings::callback::{CallbackContainer,CallbackInterface,CallbackFunction}',
52245223
'dom::bindings::callback::{CallSetup,ExceptionHandling}',
52255224
'dom::bindings::callback::wrap_call_this_object',
5226-
'dom::bindings::conversions::{FromJSValConvertible, ToJSValConvertible, ConversionBehavior}',
5227-
'dom::bindings::conversions::{native_from_reflector, native_from_handlevalue, native_from_handleobject}',
5228-
'dom::bindings::conversions::DOM_OBJECT_SLOT',
5229-
'dom::bindings::conversions::IDLInterface',
5230-
'dom::bindings::conversions::jsid_to_str',
5231-
'dom::bindings::conversions::StringificationBehavior',
5225+
'dom::bindings::conversions::{ConversionBehavior, DOM_OBJECT_SLOT, IDLInterface}',
5226+
'dom::bindings::conversions::{FromJSValConvertible, StringificationBehavior}',
5227+
'dom::bindings::conversions::{ToJSValConvertible, jsid_to_str}',
5228+
'dom::bindings::conversions::{private_from_object, root_from_object}',
5229+
'dom::bindings::conversions::{root_from_handleobject, root_from_handlevalue}',
52325230
'dom::bindings::codegen::{PrototypeList, RegisterBindings, UnionTypes}',
52335231
'dom::bindings::codegen::Bindings::*',
52345232
'dom::bindings::error::{Fallible, Error, ErrorResult}',

components/script/dom/bindings/conversions.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
//! | sequences | `Vec<T>` |
3333
//! | union types | `T` |
3434
35-
use core::nonzero::NonZero;
3635
use dom::bindings::error::throw_type_error;
3736
use dom::bindings::inheritance::Castable;
3837
use dom::bindings::js::Root;
@@ -657,7 +656,7 @@ pub fn is_dom_proxy(obj: *mut JSObject) -> bool {
657656
pub const DOM_OBJECT_SLOT: u32 = 0;
658657

659658
/// Get the private pointer of a DOM object from a given reflector.
660-
unsafe fn private_from_reflector(obj: *mut JSObject) -> *const libc::c_void {
659+
pub unsafe fn private_from_object(obj: *mut JSObject) -> *const libc::c_void {
661660
let clasp = JS_GetClass(obj);
662661
let value = if is_dom_class(clasp) {
663662
JS_GetReservedSlot(obj, DOM_OBJECT_SLOT)
@@ -672,11 +671,6 @@ unsafe fn private_from_reflector(obj: *mut JSObject) -> *const libc::c_void {
672671
}
673672
}
674673

675-
/// Get the DOM object from the given reflector.
676-
pub unsafe fn native_from_reflector<T>(obj: *mut JSObject) -> *const T {
677-
private_from_reflector(obj) as *const T
678-
}
679-
680674
/// Get the `DOMClass` from `obj`, or `Err(())` if `obj` is not a DOM object.
681675
pub unsafe fn get_dom_class(obj: *mut JSObject) -> Result<&'static DOMClass, ()> {
682676
use dom::bindings::utils::DOMJSClass;
@@ -727,7 +721,7 @@ pub unsafe fn private_from_proto_check<F>(mut obj: *mut JSObject, proto_check: F
727721

728722
if proto_check(dom_class) {
729723
debug!("good prototype");
730-
Ok(private_from_reflector(obj))
724+
Ok(private_from_object(obj))
731725
} else {
732726
debug!("bad prototype");
733727
Err(())
@@ -740,28 +734,28 @@ pub unsafe fn private_from_proto_check<F>(mut obj: *mut JSObject, proto_check: F
740734
/// Returns Err(()) if `obj` is an opaque security wrapper or if the object is
741735
/// not a reflector for a DOM object of the given type (as defined by the
742736
/// proto_id and proto_depth).
743-
pub fn native_from_reflector_jsmanaged<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
737+
pub fn root_from_object<T>(obj: *mut JSObject) -> Result<Root<T>, ()>
744738
where T: Reflectable + IDLInterface
745739
{
746740
unsafe {
747-
private_from_proto_check(obj, T::derives).map(|obj| {
748-
Root::new(NonZero::new(obj as *const T))
741+
private_from_proto_check(obj, T::derives).map(|ptr| {
742+
Root::from_ref(&*(ptr as *const T))
749743
})
750744
}
751745
}
752746

753-
/// Get a Rooted<T> for a DOM object accessible from a HandleValue
754-
pub fn native_from_handlevalue<T>(v: HandleValue) -> Result<Root<T>, ()>
747+
/// Get a `Root<T>` for a DOM object accessible from a `HandleValue`.
748+
pub fn root_from_handlevalue<T>(v: HandleValue) -> Result<Root<T>, ()>
755749
where T: Reflectable + IDLInterface
756750
{
757-
native_from_reflector_jsmanaged(v.get().to_object())
751+
root_from_object(v.get().to_object())
758752
}
759753

760-
/// Get a Rooted<T> for a DOM object accessible from a HandleObject
761-
pub fn native_from_handleobject<T>(obj: HandleObject) -> Result<Root<T>, ()>
754+
/// Get a `Root<T>` for a DOM object accessible from a `HandleObject`.
755+
pub fn root_from_handleobject<T>(obj: HandleObject) -> Result<Root<T>, ()>
762756
where T: Reflectable + IDLInterface
763757
{
764-
native_from_reflector_jsmanaged(obj.get())
758+
root_from_object(obj.get())
765759
}
766760

767761
impl<T: Reflectable> ToJSValConvertible for Root<T> {

components/script/dom/bindings/global.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use devtools_traits::ScriptToDevtoolsControlMsg;
1111
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
12-
use dom::bindings::conversions::native_from_reflector_jsmanaged;
12+
use dom::bindings::conversions::root_from_object;
1313
use dom::bindings::js::{JS, Root};
1414
use dom::bindings::reflector::{Reflectable, Reflector};
1515
use dom::window::{self, ScriptHelpers};
@@ -255,23 +255,23 @@ impl GlobalField {
255255
}
256256

257257
/// Returns the global object of the realm that the given DOM object's reflector was created in.
258-
pub fn global_object_for_reflector<T: Reflectable>(reflector: &T) -> GlobalRoot {
259-
global_object_for_js_object(*reflector.reflector().get_jsobject())
258+
pub fn global_root_from_reflector<T: Reflectable>(reflector: &T) -> GlobalRoot {
259+
global_root_from_object(*reflector.reflector().get_jsobject())
260260
}
261261

262262
/// Returns the global object of the realm that the given JS object was created in.
263263
#[allow(unrooted_must_root)]
264-
pub fn global_object_for_js_object(obj: *mut JSObject) -> GlobalRoot {
264+
pub fn global_root_from_object(obj: *mut JSObject) -> GlobalRoot {
265265
unsafe {
266266
let global = GetGlobalForObjectCrossCompartment(obj);
267267
let clasp = JS_GetClass(global);
268268
assert!(((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)) != 0);
269-
match native_from_reflector_jsmanaged(global) {
269+
match root_from_object(global) {
270270
Ok(window) => return GlobalRoot::Window(window),
271271
Err(_) => (),
272272
}
273273

274-
match native_from_reflector_jsmanaged(global) {
274+
match root_from_object(global) {
275275
Ok(worker) => return GlobalRoot::Worker(worker),
276276
Err(_) => (),
277277
}

components/script/dom/bindings/utils.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
77
use dom::bindings::codegen::PrototypeList;
88
use dom::bindings::codegen::PrototypeList::MAX_PROTO_CHAIN_LENGTH;
9-
use dom::bindings::conversions::native_from_handleobject;
10-
use dom::bindings::conversions::private_from_proto_check;
11-
use dom::bindings::conversions::{is_dom_class, jsstring_to_str, DOM_OBJECT_SLOT};
9+
use dom::bindings::conversions::{DOM_OBJECT_SLOT, is_dom_class, jsstring_to_str};
10+
use dom::bindings::conversions::{private_from_proto_check, root_from_handleobject};
1211
use dom::bindings::error::{throw_invalid_this, throw_type_error};
1312
use dom::bindings::inheritance::TopTypeId;
14-
use dom::bindings::js::Root;
1513
use dom::bindings::trace::trace_object;
1614
use dom::browsercontext;
1715
use dom::window;
@@ -625,9 +623,7 @@ pub static WRAP_CALLBACKS: JSWrapObjectCallbacks = JSWrapObjectCallbacks {
625623
/// Callback to outerize windows.
626624
pub unsafe extern fn outerize_global(_cx: *mut JSContext, obj: HandleObject) -> *mut JSObject {
627625
debug!("outerizing");
628-
let win: Root<window::Window> = native_from_handleobject(obj).unwrap();
629-
// FIXME(https://github.com/rust-lang/rust/issues/23338)
630-
let win = win.r();
626+
let win = root_from_handleobject::<window::Window>(obj).unwrap();
631627
let context = win.browsing_context();
632628
context.as_ref().unwrap().window_proxy()
633629
}

components/script/dom/browsercontext.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use dom::bindings::conversions::native_from_handleobject;
6-
use dom::bindings::conversions::{ToJSValConvertible};
5+
use dom::bindings::conversions::{ToJSValConvertible, root_from_handleobject};
76
use dom::bindings::js::{JS, Root};
87
use dom::bindings::proxyhandler::{fill_property_descriptor, get_property_descriptor};
98
use dom::bindings::reflector::{Reflectable, Reflector};
@@ -113,7 +112,7 @@ unsafe fn GetSubframeWindow(cx: *mut JSContext, proxy: HandleObject, id: HandleI
113112
let index = get_array_index_from_id(cx, id);
114113
if let Some(index) = index {
115114
let target = RootedObject::new(cx, GetProxyPrivate(*proxy.ptr).to_object());
116-
let win: Root<Window> = native_from_handleobject(target.handle()).unwrap();
115+
let win = root_from_handleobject::<Window>(target.handle()).unwrap();
117116
let mut found = false;
118117
return win.IndexedGetter(index, &mut found);
119118
}

components/script/dom/eventdispatcher.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use devtools_traits::{StartedTimelineMarker, TimelineMarker, TimelineMarkerType};
66
use dom::bindings::callback::ExceptionHandling::Report;
77
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
8-
use dom::bindings::global::{GlobalRoot, global_object_for_reflector};
8+
use dom::bindings::global::{GlobalRoot, global_root_from_reflector};
99
use dom::bindings::inheritance::Castable;
1010
use dom::bindings::js::{JS, Root, RootedReference};
1111
use dom::bindings::trace::RootedVec;
@@ -49,7 +49,7 @@ fn dispatch_to_listeners(event: &Event, target: &EventTarget, chain: &[&EventTar
4949
assert!(!event.stop_propagation());
5050
assert!(!event.stop_immediate());
5151

52-
let window = match global_object_for_reflector(target) {
52+
let window = match global_root_from_reflector(target) {
5353
GlobalRoot::Window(window) => {
5454
if window.need_emit_timeline_marker(TimelineMarkerType::DOMEvent) {
5555
Some(window)

components/script/dom/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ pub fn from_untrusted_node_address(_runtime: *mut JSRuntime, candidate: Untruste
933933
if object.is_null() {
934934
panic!("Attempted to create a `JS<Node>` from an invalid pointer!")
935935
}
936-
let boxed_node: *const Node = conversions::native_from_reflector(object);
936+
let boxed_node = conversions::private_from_object(object) as *const Node;
937937
Root::from_ref(&*boxed_node)
938938
}
939939
}

components/script/dom/window.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ use dom::bindings::codegen::Bindings::FunctionBinding::Function;
1212
use dom::bindings::codegen::Bindings::WindowBinding::{ScrollBehavior, ScrollToOptions};
1313
use dom::bindings::codegen::Bindings::WindowBinding::{self, FrameRequestCallback, WindowMethods};
1414
use dom::bindings::error::{Error, Fallible, report_pending_exception};
15-
use dom::bindings::global::GlobalRef;
16-
use dom::bindings::global::global_object_for_js_object;
15+
use dom::bindings::global::{GlobalRef, global_root_from_reflector};
1716
use dom::bindings::inheritance::Castable;
1817
use dom::bindings::js::RootedReference;
1918
use dom::bindings::js::{JS, MutNullableHeap, Root};
@@ -766,8 +765,7 @@ impl<'a, T: Reflectable> ScriptHelpers for &'a T {
766765
#[allow(unsafe_code)]
767766
fn evaluate_script_on_global_with_result(self, code: &str, filename: &str,
768767
rval: MutableHandleValue) {
769-
let this = self.reflector().get_jsobject();
770-
let global = global_object_for_js_object(this.get());
768+
let global = global_root_from_reflector(self);
771769
let cx = global.r().get_cx();
772770
let _ar = JSAutoRequest::new(cx);
773771
let globalhandle = global.r().reflector().get_jsobject();

components/script/timers.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use dom::bindings::callback::ExceptionHandling::Report;
66
use dom::bindings::cell::DOMRefCell;
77
use dom::bindings::codegen::Bindings::FunctionBinding::Function;
8-
use dom::bindings::global::global_object_for_js_object;
8+
use dom::bindings::global::global_root_from_reflector;
99
use dom::bindings::reflector::Reflectable;
1010
use dom::bindings::trace::JSTraceable;
1111
use dom::window::ScriptHelpers;
@@ -285,8 +285,7 @@ impl ActiveTimers {
285285
// step 14
286286
match callback {
287287
InternalTimerCallback::StringTimerCallback(code_str) => {
288-
let proxy = this.reflector().get_jsobject();
289-
let cx = global_object_for_js_object(proxy.get()).r().get_cx();
288+
let cx = global_root_from_reflector(this).r().get_cx();
290289
let mut rval = RootedValue::new(cx, UndefinedValue());
291290

292291
this.evaluate_js_on_global_with_result(&code_str, rval.handle_mut());

0 commit comments

Comments
 (0)