Remove utils::Prefable in favour of guard::Guard

This commit is contained in:
Anthony Ramine 2016-05-20 23:03:14 +02:00
parent a20db08f06
commit fd7c4f8149
5 changed files with 91 additions and 61 deletions

View file

@ -1335,8 +1335,8 @@ def MemberCondition(pref, func):
assert pref is None or isinstance(pref, str) assert pref is None or isinstance(pref, str)
assert func is None or isinstance(func, str) assert func is None or isinstance(func, str)
if pref: if pref:
return 'Some("%s")' % pref return 'Condition::Pref("%s")' % pref
return "None" return "Condition::Satisfied"
class PropertyDefiner: class PropertyDefiner:
@ -1380,7 +1380,7 @@ class PropertyDefiner:
PropertyDefiner.getStringAttr(interfaceMember, PropertyDefiner.getStringAttr(interfaceMember,
"Func")) "Func"))
def generatePrefableArray(self, array, name, specTemplate, specTerminator, def generateGuardedArray(self, array, name, specTemplate, specTerminator,
specType, getCondition, getDataTuple): specType, getCondition, getDataTuple):
""" """
This method generates our various arrays. This method generates our various arrays.
@ -1409,7 +1409,7 @@ class PropertyDefiner:
assert len(array) != 0 assert len(array) != 0
specs = [] specs = []
prefableSpecs = [] prefableSpecs = []
prefableTemplate = ' Prefable { pref: %s, specs: %s[%d] }' prefableTemplate = ' Guard::new(%s, %s[%d])'
for cond, members in groupby(array, lambda m: getCondition(m, self.descriptor)): for cond, members in groupby(array, lambda m: getCondition(m, self.descriptor)):
currentSpecs = [specTemplate % getDataTuple(m) for m in members] currentSpecs = [specTemplate % getDataTuple(m) for m in members]
@ -1423,7 +1423,7 @@ class PropertyDefiner:
",\n".join(specs) + "\n" + ",\n".join(specs) + "\n" +
"];\n") % (name, specType) "];\n") % (name, specType)
prefArray = ("const %s: &'static [Prefable<%s>] = &[\n" + prefArray = ("const %s: &'static [Guard<&'static [%s]>] = &[\n" +
",\n".join(prefableSpecs) + "\n" + ",\n".join(prefableSpecs) + "\n" +
"];\n") % (name, specType) "];\n") % (name, specType)
return specsArray + prefArray return specsArray + prefArray
@ -1470,7 +1470,7 @@ class MethodDefiner(PropertyDefiner):
"methodInfo": False, "methodInfo": False,
"selfHostedName": "ArrayValues", "selfHostedName": "ArrayValues",
"length": 0, "length": 0,
"condition": "None"}) "condition": "Condition::Satisfied"})
isUnforgeableInterface = bool(descriptor.interface.getExtendedAttribute("Unforgeable")) isUnforgeableInterface = bool(descriptor.interface.getExtendedAttribute("Unforgeable"))
if not static and unforgeable == isUnforgeableInterface: if not static and unforgeable == isUnforgeableInterface:
@ -1521,7 +1521,7 @@ class MethodDefiner(PropertyDefiner):
% m["name"][2:], accessor, jitinfo, m["length"], flags, selfHostedName) % m["name"][2:], accessor, jitinfo, m["length"], flags, selfHostedName)
return (str_to_const_array(m["name"]), accessor, jitinfo, m["length"], flags, selfHostedName) return (str_to_const_array(m["name"]), accessor, jitinfo, m["length"], flags, selfHostedName)
return self.generatePrefableArray( return self.generateGuardedArray(
array, name, array, name,
' JSFunctionSpec {\n' ' JSFunctionSpec {\n'
' name: %s as *const u8 as *const libc::c_char,\n' ' name: %s as *const u8 as *const libc::c_char,\n'
@ -1601,7 +1601,7 @@ class AttrDefiner(PropertyDefiner):
return (str_to_const_array(attr.identifier.name), flags, getter(attr), return (str_to_const_array(attr.identifier.name), flags, getter(attr),
setter(attr)) setter(attr))
return self.generatePrefableArray( return self.generateGuardedArray(
array, name, array, name,
' JSPropertySpec {\n' ' JSPropertySpec {\n'
' name: %s as *const u8 as *const libc::c_char,\n' ' name: %s as *const u8 as *const libc::c_char,\n'
@ -1636,7 +1636,7 @@ class ConstDefiner(PropertyDefiner):
return (str_to_const_array(const.identifier.name), return (str_to_const_array(const.identifier.name),
convertConstIDLValueToJSVal(const.value)) convertConstIDLValueToJSVal(const.value))
return self.generatePrefableArray( return self.generateGuardedArray(
array, name, array, name,
' ConstantSpec { name: %s, value: %s }', ' ConstantSpec { name: %s, value: %s }',
None, None,
@ -2297,8 +2297,8 @@ def InitUnforgeablePropertiesOnHolder(descriptor, properties):
""" """
unforgeables = [] unforgeables = []
defineUnforgeableAttrs = "define_prefable_properties(cx, unforgeable_holder.handle(), %s);" defineUnforgeableAttrs = "define_guarded_properties(cx, unforgeable_holder.handle(), %s);"
defineUnforgeableMethods = "define_prefable_methods(cx, unforgeable_holder.handle(), %s);" defineUnforgeableMethods = "define_guarded_methods(cx, unforgeable_holder.handle(), %s);"
unforgeableMembers = [ unforgeableMembers = [
(defineUnforgeableAttrs, properties.unforgeable_attrs), (defineUnforgeableAttrs, properties.unforgeable_attrs),
@ -5528,13 +5528,13 @@ class CGBindingRoot(CGThing):
'dom::bindings::interface::{InterfaceConstructorBehavior, NonCallbackInterfaceObjectClass}', 'dom::bindings::interface::{InterfaceConstructorBehavior, NonCallbackInterfaceObjectClass}',
'dom::bindings::interface::{create_callback_interface_object, create_interface_prototype_object}', 'dom::bindings::interface::{create_callback_interface_object, create_interface_prototype_object}',
'dom::bindings::interface::{create_named_constructors, create_noncallback_interface_object}', 'dom::bindings::interface::{create_named_constructors, create_noncallback_interface_object}',
'dom::bindings::interface::{define_prefable_methods, define_prefable_properties}', 'dom::bindings::interface::{define_guarded_methods, define_guarded_properties}',
'dom::bindings::interface::{ConstantSpec, NonNullJSNative}', 'dom::bindings::interface::{ConstantSpec, NonNullJSNative}',
'dom::bindings::interface::ConstantVal::{IntVal, UintVal}', 'dom::bindings::interface::ConstantVal::{IntVal, UintVal}',
'dom::bindings::js::{JS, Root, RootedReference}', 'dom::bindings::js::{JS, Root, RootedReference}',
'dom::bindings::js::{OptionalRootedReference}', 'dom::bindings::js::{OptionalRootedReference}',
'dom::bindings::reflector::{Reflectable}', 'dom::bindings::reflector::{Reflectable}',
'dom::bindings::utils::{DOMClass, DOMJSClass, Prefable}', 'dom::bindings::utils::{DOMClass, DOMJSClass}',
'dom::bindings::utils::{DOM_PROTO_UNFORGEABLE_HOLDER_SLOT, JSCLASS_DOM_GLOBAL}', 'dom::bindings::utils::{DOM_PROTO_UNFORGEABLE_HOLDER_SLOT, JSCLASS_DOM_GLOBAL}',
'dom::bindings::utils::{ProtoOrIfaceArray, create_dom_global}', 'dom::bindings::utils::{ProtoOrIfaceArray, create_dom_global}',
'dom::bindings::utils::{enumerate_global, finalize_global, find_enum_string_index}', 'dom::bindings::utils::{enumerate_global, finalize_global, find_enum_string_index}',
@ -5558,6 +5558,7 @@ class CGBindingRoot(CGThing):
'dom::bindings::error::{Fallible, Error, ErrorResult}', 'dom::bindings::error::{Fallible, Error, ErrorResult}',
'dom::bindings::error::Error::JSFailed', 'dom::bindings::error::Error::JSFailed',
'dom::bindings::error::throw_dom_exception', 'dom::bindings::error::throw_dom_exception',
'dom::bindings::guard::{Condition, Guard}',
'dom::bindings::proxyhandler', 'dom::bindings::proxyhandler',
'dom::bindings::proxyhandler::{ensure_expando_object, fill_property_descriptor}', 'dom::bindings::proxyhandler::{ensure_expando_object, fill_property_descriptor}',
'dom::bindings::proxyhandler::{get_expando_object, get_property_descriptor}', 'dom::bindings::proxyhandler::{get_expando_object, get_property_descriptor}',

View file

@ -0,0 +1,49 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Machinery to conditionally expose things.
use util::prefs::get_pref;
/// A container with a condition.
pub struct Guard<T: Clone + Copy> {
condition: Condition,
value: T,
}
impl<T: Clone + Copy> Guard<T> {
/// Construct a new guarded value.
pub const fn new(condition: Condition, value: T) -> Self {
Guard {
condition: condition,
value: value,
}
}
/// Expose the value if the condition is satisfied.
pub fn expose(&self) -> Option<T> {
if self.condition.is_satisfied() {
Some(self.value)
} else {
None
}
}
}
/// A condition to expose things.
pub enum Condition {
/// The condition is satisfied if the preference is set.
Pref(&'static str),
/// The condition is always satisfied.
Satisfied,
}
impl Condition {
fn is_satisfied(&self) -> bool {
match *self {
Condition::Pref(name) => get_pref(name).as_boolean().unwrap_or(false),
Condition::Satisfied => true,
}
}
}

View file

@ -6,7 +6,8 @@
use dom::bindings::codegen::PrototypeList; use dom::bindings::codegen::PrototypeList;
use dom::bindings::conversions::get_dom_class; use dom::bindings::conversions::get_dom_class;
use dom::bindings::utils::{get_proto_or_iface_array, Prefable}; use dom::bindings::guard::Guard;
use dom::bindings::utils::get_proto_or_iface_array;
use js::error::throw_type_error; use js::error::throw_type_error;
use js::glue::UncheckedUnwrapObject; use js::glue::UncheckedUnwrapObject;
use js::jsapi::{Class, ClassExtension, ClassSpec, GetGlobalForObjectCrossCompartment}; use js::jsapi::{Class, ClassExtension, ClassSpec, GetGlobalForObjectCrossCompartment};
@ -209,14 +210,14 @@ impl InterfaceConstructorBehavior {
pub unsafe fn create_callback_interface_object( pub unsafe fn create_callback_interface_object(
cx: *mut JSContext, cx: *mut JSContext,
receiver: HandleObject, receiver: HandleObject,
constants: &'static [Prefable<ConstantSpec>], constants: &'static [Guard<&'static [ConstantSpec]>],
name: &'static [u8], name: &'static [u8],
rval: MutableHandleObject) { rval: MutableHandleObject) {
assert!(!constants.is_empty()); assert!(!constants.is_empty());
rval.set(JS_NewObject(cx, ptr::null())); rval.set(JS_NewObject(cx, ptr::null()));
assert!(!rval.ptr.is_null()); assert!(!rval.ptr.is_null());
for prefable in constants { for guard in constants {
if let Some(specs) = prefable.specs() { if let Some(specs) = guard.expose() {
define_constants(cx, rval.handle(), specs); define_constants(cx, rval.handle(), specs);
} }
} }
@ -229,9 +230,9 @@ pub unsafe fn create_interface_prototype_object(
cx: *mut JSContext, cx: *mut JSContext,
proto: HandleObject, proto: HandleObject,
class: &'static JSClass, class: &'static JSClass,
regular_methods: &'static [Prefable<JSFunctionSpec>], regular_methods: &'static [Guard<&'static [JSFunctionSpec]>],
regular_properties: &'static [Prefable<JSPropertySpec>], regular_properties: &'static [Guard<&'static [JSPropertySpec]>],
constants: &'static [Prefable<ConstantSpec>], constants: &'static [Guard<&'static [ConstantSpec]>],
rval: MutableHandleObject) { rval: MutableHandleObject) {
create_object(cx, proto, class, regular_methods, regular_properties, constants, rval); create_object(cx, proto, class, regular_methods, regular_properties, constants, rval);
} }
@ -242,9 +243,9 @@ pub unsafe fn create_noncallback_interface_object(
receiver: HandleObject, receiver: HandleObject,
proto: HandleObject, proto: HandleObject,
class: &'static NonCallbackInterfaceObjectClass, class: &'static NonCallbackInterfaceObjectClass,
static_methods: &'static [Prefable<JSFunctionSpec>], static_methods: &'static [Guard<&'static [JSFunctionSpec]>],
static_properties: &'static [Prefable<JSPropertySpec>], static_properties: &'static [Guard<&'static [JSPropertySpec]>],
constants: &'static [Prefable<ConstantSpec>], constants: &'static [Guard<&'static [ConstantSpec]>],
interface_prototype_object: HandleObject, interface_prototype_object: HandleObject,
name: &'static [u8], name: &'static [u8],
length: u32, length: u32,
@ -356,40 +357,40 @@ unsafe fn create_object(
cx: *mut JSContext, cx: *mut JSContext,
proto: HandleObject, proto: HandleObject,
class: &'static JSClass, class: &'static JSClass,
methods: &'static [Prefable<JSFunctionSpec>], methods: &'static [Guard<&'static [JSFunctionSpec]>],
properties: &'static [Prefable<JSPropertySpec>], properties: &'static [Guard<&'static [JSPropertySpec]>],
constants: &'static [Prefable<ConstantSpec>], constants: &'static [Guard<&'static [ConstantSpec]>],
rval: MutableHandleObject) { rval: MutableHandleObject) {
rval.set(JS_NewObjectWithUniqueType(cx, class, proto)); rval.set(JS_NewObjectWithUniqueType(cx, class, proto));
assert!(!rval.ptr.is_null()); assert!(!rval.ptr.is_null());
define_prefable_methods(cx, rval.handle(), methods); define_guarded_methods(cx, rval.handle(), methods);
define_prefable_properties(cx, rval.handle(), properties); define_guarded_properties(cx, rval.handle(), properties);
for prefable in constants { for guard in constants {
if let Some(specs) = prefable.specs() { if let Some(specs) = guard.expose() {
define_constants(cx, rval.handle(), specs); define_constants(cx, rval.handle(), specs);
} }
} }
} }
/// Conditionally define methods on an object. /// Conditionally define methods on an object.
pub unsafe fn define_prefable_methods( pub unsafe fn define_guarded_methods(
cx: *mut JSContext, cx: *mut JSContext,
obj: HandleObject, obj: HandleObject,
methods: &'static [Prefable<JSFunctionSpec>]) { methods: &'static [Guard<&'static [JSFunctionSpec]>]) {
for prefable in methods { for guard in methods {
if let Some(specs) = prefable.specs() { if let Some(specs) = guard.expose() {
define_methods(cx, obj, specs).unwrap(); define_methods(cx, obj, specs).unwrap();
} }
} }
} }
/// Conditionally define properties on an object. /// Conditionally define properties on an object.
pub unsafe fn define_prefable_properties( pub unsafe fn define_guarded_properties(
cx: *mut JSContext, cx: *mut JSContext,
obj: HandleObject, obj: HandleObject,
properties: &'static [Prefable<JSPropertySpec>]) { properties: &'static [Guard<&'static [JSPropertySpec]>]) {
for prefable in properties { for guard in properties {
if let Some(specs) = prefable.specs() { if let Some(specs) = guard.expose() {
define_properties(cx, obj, specs).unwrap(); define_properties(cx, obj, specs).unwrap();
} }
} }

View file

@ -133,6 +133,7 @@ pub mod cell;
pub mod conversions; pub mod conversions;
pub mod error; pub mod error;
pub mod global; pub mod global;
pub mod guard;
pub mod inheritance; pub mod inheritance;
pub mod interface; pub mod interface;
pub mod js; pub mod js;

View file

@ -39,7 +39,6 @@ use std::ffi::CString;
use std::os::raw::c_void; use std::os::raw::c_void;
use std::ptr; use std::ptr;
use std::slice; use std::slice;
use util::prefs;
/// Proxy handler for a WindowProxy. /// Proxy handler for a WindowProxy.
pub struct WindowProxyHandler(pub *const libc::c_void); pub struct WindowProxyHandler(pub *const libc::c_void);
@ -550,24 +549,3 @@ unsafe extern "C" fn instance_class_has_proto_at_depth(clasp: *const js::jsapi::
pub const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks { pub const DOM_CALLBACKS: DOMCallbacks = DOMCallbacks {
instanceClassMatchesProto: Some(instance_class_has_proto_at_depth), instanceClassMatchesProto: Some(instance_class_has_proto_at_depth),
}; };
/// A container around JS member specifications that are conditionally enabled.
pub struct Prefable<T: 'static> {
/// If present, the name of the preference used to conditionally enable these specs.
pub pref: Option<&'static str>,
/// The underlying slice of specifications.
pub specs: &'static [T],
}
impl<T> Prefable<T> {
/// Retrieve the slice represented by this container, unless the condition
/// guarding it is false.
pub fn specs(&self) -> Option<&'static [T]> {
if let Some(pref) = self.pref {
if !prefs::get_pref(pref).as_boolean().unwrap_or(false) {
return None;
}
}
Some(self.specs)
}
}