Support callback interfaces with constants.

This commit is contained in:
Chris Paris 2014-10-06 22:13:14 -10:00
parent 10c68e7519
commit d2b0d5e040
3 changed files with 61 additions and 36 deletions

View file

@ -180,36 +180,38 @@ unsafe impl Sync for NativeProperties {}
pub type NonNullJSNative =
unsafe extern "C" fn (arg1: *mut JSContext, arg2: c_uint, arg3: *mut JSVal) -> JSBool;
/// Creates the *interface prototype object* and the *interface object* (if
/// needed).
/// Creates the *interface prototype object* (if a `proto_class` is given)
/// and the *interface object* (if a `constructor` is given).
/// Fails on JSAPI failure.
pub fn do_create_interface_objects(cx: *mut JSContext, global: *mut JSObject,
receiver: *mut JSObject,
proto_proto: *mut JSObject,
proto_class: &'static JSClass,
proto_class: Option<&'static JSClass>,
constructor: Option<(NonNullJSNative, &'static str, u32)>,
dom_class: *const DOMClass,
members: &'static NativeProperties)
-> *mut JSObject {
let proto = create_interface_prototype_object(cx, global, proto_proto,
proto_class, members);
unsafe {
JS_SetReservedSlot(proto, DOM_PROTO_INSTANCE_CLASS_SLOT,
PrivateValue(dom_class as *const libc::c_void));
}
let proto = match proto_class {
Some(proto_class) => {
let proto = create_interface_prototype_object(cx, global, proto_proto,
proto_class, members);
JS_SetReservedSlot(proto, DOM_PROTO_INSTANCE_CLASS_SLOT,
PrivateValue(dom_class as *const libc::c_void));
proto
},
None => ptr::null_mut()
};
match constructor {
Some((native, name, nargs)) => {
if let Some((native, name, nargs)) = constructor {
let s = CString::new(name).unwrap();
create_interface_object(cx, global, receiver,
native, nargs, proto,
members, s.as_ptr())
},
None => (),
}
}
proto
proto
}
}
/// Creates the *interface object*.