Introduce ConstantSpec::get_value.

I'll need it for cross-origin wrappers as well.
This commit is contained in:
Ms2ger 2014-08-28 15:50:44 +02:00
parent 14d596c1bc
commit f925343552

View file

@ -222,6 +222,20 @@ pub struct ConstantSpec {
pub value: ConstantVal
}
impl ConstantSpec {
/// Returns a `JSVal` that represents the value of this `ConstantSpec`.
pub fn get_value(&self) -> JSVal {
match self.value {
NullVal => NullValue(),
IntVal(i) => Int32Value(i),
UintVal(u) => UInt32Value(u),
DoubleVal(d) => DoubleValue(d),
BoolVal(b) => BooleanValue(b),
VoidVal => UndefinedValue(),
}
}
}
/// Helper structure for cross-origin wrappers for DOM binding objects.
pub struct NativePropertyHooks {
/// The property arrays for this interface.
@ -349,17 +363,9 @@ fn CreateInterfaceObject(cx: *mut JSContext, global: *mut JSObject, receiver: *m
/// Fails on JSAPI failure.
fn DefineConstants(cx: *mut JSContext, obj: *mut JSObject, constants: &'static [ConstantSpec]) {
for spec in constants.iter() {
let jsval = match spec.value {
NullVal => NullValue(),
IntVal(i) => Int32Value(i),
UintVal(u) => UInt32Value(u),
DoubleVal(d) => DoubleValue(d),
BoolVal(b) => BooleanValue(b),
VoidVal => UndefinedValue(),
};
unsafe {
assert!(JS_DefineProperty(cx, obj, spec.name.as_ptr() as *const libc::c_char,
jsval, None, None,
spec.get_value(), None, None,
JSPROP_ENUMERATE | JSPROP_READONLY |
JSPROP_PERMANENT) != 0);
}