Skip to content

Commit

Permalink
Use safe JSContext when possible in interface.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
marmeladema committed Aug 8, 2019
1 parent 8b070fe commit 78034a9
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 180 deletions.
18 changes: 9 additions & 9 deletions components/script/dom/bindings/codegen/CodegenRust.py
Expand Up @@ -2651,8 +2651,8 @@ def InitUnforgeablePropertiesOnHolder(descriptor, properties):
"""
unforgeables = []

defineUnforgeableAttrs = "define_guarded_properties(*cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableMethods = "define_guarded_methods(*cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableAttrs = "define_guarded_properties(cx, unforgeable_holder.handle(), %s, global);"
defineUnforgeableMethods = "define_guarded_methods(cx, unforgeable_holder.handle(), %s, global);"

unforgeableMembers = [
(defineUnforgeableAttrs, properties.unforgeable_attrs),
Expand Down Expand Up @@ -2762,7 +2762,7 @@ def definition_body(self):
("define_guarded_methods", self.properties.methods),
("define_guarded_constants", self.properties.consts)
]
members = ["%s(*cx, obj.handle(), %s, obj.handle());" % (function, array.variableName())
members = ["%s(cx, obj.handle(), %s, obj.handle());" % (function, array.variableName())
for (function, array) in pairs if array.length() > 0]
values["members"] = "\n".join(members)

Expand All @@ -2772,7 +2772,7 @@ def definition_body(self):
rooted!(in(*cx) let mut obj = ptr::null_mut::<JSObject>());
create_global_object(
*cx,
cx,
&Class.base,
raw as *const libc::c_void,
_trace,
Expand Down Expand Up @@ -2911,7 +2911,7 @@ def definition_body(self):
rooted!(in(*cx) let proto = %(proto)s);
assert!(!proto.is_null());
rooted!(in(*cx) let mut namespace = ptr::null_mut::<JSObject>());
create_namespace_object(*cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
create_namespace_object(cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
%(methods)s, %(name)s, namespace.handle_mut());
assert!(!namespace.is_null());
assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
Expand All @@ -2924,7 +2924,7 @@ def definition_body(self):
assert not self.descriptor.interface.ctor() and self.descriptor.interface.hasConstants()
return CGGeneric("""\
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_callback_interface_object(*cx, global, sConstants, %(name)s, interface.handle_mut());
create_callback_interface_object(cx, global, sConstants, %(name)s, interface.handle_mut());
assert!(!interface.is_null());
assert!((*cache)[PrototypeList::Constructor::%(id)s as usize].is_null());
(*cache)[PrototypeList::Constructor::%(id)s as usize] = interface.get();
Expand Down Expand Up @@ -2976,7 +2976,7 @@ def definition_body(self):

code.append(CGGeneric("""
rooted!(in(*cx) let mut prototype = ptr::null_mut::<JSObject>());
create_interface_prototype_object(*cx,
create_interface_prototype_object(cx,
global.into(),
prototype_proto.handle().into(),
&PrototypeClass,
Expand Down Expand Up @@ -3011,7 +3011,7 @@ def definition_body(self):
assert!(!interface_proto.is_null());
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_noncallback_interface_object(*cx,
create_noncallback_interface_object(cx,
global.into(),
interface_proto.handle(),
&INTERFACE_OBJECT_CLASS,
Expand Down Expand Up @@ -3093,7 +3093,7 @@ def defineAliasesFor(m):
specs.append(CGGeneric("(%s as ConstructorClassHook, %s, %d)" % (hook, name, length)))
values = CGIndenter(CGList(specs, "\n"), 4)
code.append(CGWrapper(values, pre="%s = [\n" % decl, post="\n];"))
code.append(CGGeneric("create_named_constructors(*cx, global, &named_constructors, prototype.handle());"))
code.append(CGGeneric("create_named_constructors(cx, global, &named_constructors, prototype.handle());"))

if self.descriptor.hasUnforgeableMembers:
# We want to use the same JSClass and prototype as the object we'll
Expand Down
23 changes: 13 additions & 10 deletions components/script/dom/bindings/constant.rs
Expand Up @@ -4,8 +4,9 @@

//! WebIDL constants.

use crate::script_runtime::JSContext;
use js::jsapi::JSPROP_READONLY;
use js::jsapi::{JSContext, JSPROP_ENUMERATE, JSPROP_PERMANENT};
use js::jsapi::{JSPROP_ENUMERATE, JSPROP_PERMANENT};
use js::jsval::{BooleanValue, DoubleValue, Int32Value, JSVal, NullValue, UInt32Value};
use js::rust::wrappers::JS_DefineProperty;
use js::rust::HandleObject;
Expand Down Expand Up @@ -50,15 +51,17 @@ impl ConstantSpec {

/// Defines constants on `obj`.
/// Fails on JSAPI failure.
pub unsafe fn define_constants(cx: *mut JSContext, obj: HandleObject, constants: &[ConstantSpec]) {
pub fn define_constants(cx: JSContext, obj: HandleObject, constants: &[ConstantSpec]) {
for spec in constants {
rooted!(in(cx) let value = spec.get_value());
assert!(JS_DefineProperty(
cx,
obj,
spec.name.as_ptr() as *const libc::c_char,
value.handle(),
(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT) as u32
));
rooted!(in(*cx) let value = spec.get_value());
unsafe {
assert!(JS_DefineProperty(
*cx,
obj,
spec.name.as_ptr() as *const libc::c_char,
value.handle(),
(JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT) as u32
));
}
}
}
18 changes: 4 additions & 14 deletions components/script/dom/bindings/guard.rs
Expand Up @@ -6,7 +6,7 @@

use crate::dom::bindings::codegen::InterfaceObjectMap;
use crate::dom::bindings::interface::is_exposed_in;
use js::jsapi::JSContext;
use crate::script_runtime::JSContext;
use js::rust::HandleObject;
use servo_config::prefs;

Expand All @@ -28,12 +28,7 @@ impl<T: Clone + Copy> Guard<T> {
/// Expose the value if the condition is satisfied.
///
/// The passed handle is the object on which the value may be exposed.
pub unsafe fn expose(
&self,
cx: *mut JSContext,
obj: HandleObject,
global: HandleObject,
) -> Option<T> {
pub fn expose(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> Option<T> {
if self.condition.is_satisfied(cx, obj, global) {
Some(self.value)
} else {
Expand All @@ -45,7 +40,7 @@ impl<T: Clone + Copy> Guard<T> {
/// A condition to expose things.
pub enum Condition {
/// The condition is satisfied if the function returns true.
Func(unsafe fn(*mut JSContext, HandleObject) -> bool),
Func(fn(JSContext, HandleObject) -> bool),
/// The condition is satisfied if the preference is set.
Pref(&'static str),
// The condition is satisfied if the interface is exposed in the global.
Expand All @@ -55,12 +50,7 @@ pub enum Condition {
}

impl Condition {
unsafe fn is_satisfied(
&self,
cx: *mut JSContext,
obj: HandleObject,
global: HandleObject,
) -> bool {
fn is_satisfied(&self, cx: JSContext, obj: HandleObject, global: HandleObject) -> bool {
match *self {
Condition::Pref(name) => prefs::pref_map().get(name).as_bool().unwrap_or(false),
Condition::Func(f) => f(cx, obj),
Expand Down

0 comments on commit 78034a9

Please sign in to comment.