diff options
author | Joel Teichroeb <joel@teichroeb.net> | 2015-05-17 23:47:15 +0000 |
---|---|---|
committer | Joel Teichroeb <joel@teichroeb.net> | 2015-06-24 10:42:53 -0700 |
commit | 012be81eaba1c6328b25a9558ef7d76f25f7439c (patch) | |
tree | 3a9e746e2a9024f22b6b1d70409b80dfca3962a0 /components/script/dom/bindings/utils.rs | |
parent | 3e90a60170ba7f46a06ea6ad2e658dcf2b24d557 (diff) | |
download | servo-012be81eaba1c6328b25a9558ef7d76f25f7439c.tar.gz servo-012be81eaba1c6328b25a9558ef7d76f25f7439c.zip |
Add support for NamedConstructor in webidls
Diffstat (limited to 'components/script/dom/bindings/utils.rs')
-rw-r--r-- | components/script/dom/bindings/utils.rs | 89 |
1 files changed, 64 insertions, 25 deletions
diff --git a/components/script/dom/bindings/utils.rs b/components/script/dom/bindings/utils.rs index c57de127dfc..0dd9d922b71 100644 --- a/components/script/dom/bindings/utils.rs +++ b/components/script/dom/bindings/utils.rs @@ -205,6 +205,7 @@ pub fn do_create_interface_objects(cx: *mut JSContext, proto_proto: HandleObject, proto_class: Option<&'static JSClass>, constructor: Option<(NonNullJSNative, &'static str, u32)>, + named_constructors: &[(NonNullJSNative, &'static str, u32)], dom_class: *const DOMClass, members: &'static NativeProperties, rval: MutableHandleObject) { @@ -226,6 +227,55 @@ pub fn do_create_interface_objects(cx: *mut JSContext, native, nargs, rval.handle(), members, s.as_ptr()) } + + for ctor in named_constructors.iter() { + let (cnative, cname, cnargs) = *ctor; + + let cs = CString::new(cname).unwrap(); + let constructor = RootedObject::new(cx, create_constructor(cx, cnative, cnargs, cs.as_ptr())); + assert!(!constructor.ptr.is_null()); + unsafe { + assert!(JS_DefineProperty1(cx, constructor.handle(), "prototype".as_ptr() as *const i8, + rval.handle(), + JSPROP_PERMANENT | JSPROP_READONLY, + None, None) != 0); + } + define_constructor(cx, receiver, cs.as_ptr(), constructor.handle()); + } + +} + +fn create_constructor(cx: *mut JSContext, + constructor_native: NonNullJSNative, + ctor_nargs: u32, + name: *const libc::c_char) -> *mut JSObject { + unsafe { + let fun = JS_NewFunction(cx, Some(constructor_native), ctor_nargs, + JSFUN_CONSTRUCTOR, name); + assert!(!fun.is_null()); + + let constructor = JS_GetFunctionObject(fun); + assert!(!constructor.is_null()); + + constructor + } +} + +fn define_constructor(cx: *mut JSContext, + receiver: HandleObject, + name: *const libc::c_char, + constructor: HandleObject) { + unsafe { + let mut already_defined = 0; + assert!(JS_AlreadyHasOwnProperty(cx, receiver, name, &mut already_defined) != 0); + + if already_defined == 0 { + assert!(JS_DefineProperty1(cx, receiver, name, + constructor, + 0, None, None) != 0); + } + + } } /// Creates the *interface object*. @@ -236,39 +286,28 @@ fn create_interface_object(cx: *mut JSContext, ctor_nargs: u32, proto: HandleObject, members: &'static NativeProperties, name: *const libc::c_char) { - unsafe { - let fun = JS_NewFunction(cx, Some(constructor_native), ctor_nargs, - JSFUN_CONSTRUCTOR, name); - assert!(!fun.is_null()); + let constructor = RootedObject::new(cx, create_constructor(cx, constructor_native, ctor_nargs, name)); + assert!(!constructor.ptr.is_null()); - let constructor = RootedObject::new(cx, JS_GetFunctionObject(fun)); - assert!(!constructor.ptr.is_null()); - - if let Some(static_methods) = members.static_methods { - define_methods(cx, constructor.handle(), static_methods); - } + if let Some(static_methods) = members.static_methods { + define_methods(cx, constructor.handle(), static_methods); + } - if let Some(static_properties) = members.static_attrs { - define_properties(cx, constructor.handle(), static_properties); - } + if let Some(static_properties) = members.static_attrs { + define_properties(cx, constructor.handle(), static_properties); + } - if let Some(constants) = members.consts { - define_constants(cx, constructor.handle(), constants); - } + if let Some(constants) = members.consts { + define_constants(cx, constructor.handle(), constants); + } + unsafe { if !proto.get().is_null() { assert!(JS_LinkConstructorAndPrototype(cx, constructor.handle(), proto) != 0); } - - let mut already_defined = 0; - assert!(JS_AlreadyHasOwnProperty(cx, receiver, name, &mut already_defined) != 0); - - if already_defined == 0 { - assert!(JS_DefineProperty1(cx, receiver, name, - constructor.handle(), - 0, None, None) != 0); - } } + + define_constructor(cx, receiver, name, constructor.handle()); } /// Defines constants on `obj`. |