Pass the interface object-related arguments to CreateInterfaceObjects2 together in an Option.

This clarifies the code and fixes our support of NoInterfaceObject interfaces.
This commit is contained in:
Ms2ger 2014-06-20 17:50:40 +02:00
parent f11e7ee0a9
commit 5acbea5199
2 changed files with 34 additions and 26 deletions

View file

@ -1880,13 +1880,6 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
getParentProto = ("let parentProto: *mut JSObject = %s;\n"
"assert!(parentProto.is_not_null());\n") % getParentProto
if self.descriptor.interface.ctor():
constructHook = CONSTRUCT_HOOK_NAME
constructArgs = methodLength(self.descriptor.interface.ctor())
else:
constructHook = "ThrowingConstructor"
constructArgs = 0
if self.descriptor.concrete:
if self.descriptor.proxy:
domClass = "&Class"
@ -1901,20 +1894,31 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
return "None"
return "Some(%s.as_slice())" % val
if needInterfaceObject:
if self.descriptor.interface.ctor():
constructHook = CONSTRUCT_HOOK_NAME
constructArgs = methodLength(self.descriptor.interface.ctor())
else:
constructHook = "ThrowingConstructor"
constructArgs = 0
constructor = 'Some((%s, "%s", %d))' % (
constructHook, self.descriptor.interface.identifier.name,
constructArgs)
else:
constructor = 'None'
call = """return CreateInterfaceObjects2(aCx, aGlobal, aReceiver, parentProto,
&PrototypeClass, %s, %d,
%s,
&PrototypeClass, %s,
%s,
%s,
%s,
%s,
%s);""" % (
"Some(%s)" % constructHook if needInterfaceObject else "None",
constructArgs,
constructor,
domClass,
arrayPtr("methods"), arrayPtr("attrs"),
arrayPtr("consts"), arrayPtr("staticMethods"),
'"' + self.descriptor.interface.identifier.name + '"' if needInterfaceObject else "ptr::null()")
arrayPtr("consts"), arrayPtr("staticMethods"))
functionBody = CGList(
[CGGeneric(getParentProto),

View file

@ -34,7 +34,7 @@ use js::jsapi::{JS_HasPropertyById, JS_GetPrototype};
use js::jsapi::{JS_GetProperty, JS_HasProperty};
use js::jsapi::{JS_DefineFunctions, JS_DefineProperty};
use js::jsapi::{JS_ValueToString, JS_GetReservedSlot, JS_SetReservedSlot};
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass, JSNative};
use js::jsapi::{JSContext, JSObject, JSBool, jsid, JSClass};
use js::jsapi::{JSFunctionSpec, JSPropertySpec};
use js::jsapi::{JS_NewGlobalObject, JS_InitStandardClasses};
use js::jsapi::{JSString};
@ -215,17 +215,18 @@ pub fn GetProtoOrIfaceArray(global: *mut JSObject) -> *mut *mut JSObject {
}
}
pub type NonNullJSNative =
unsafe extern "C" fn (arg1: *mut JSContext, arg2: c_uint, arg3: *mut JSVal) -> JSBool;
pub fn CreateInterfaceObjects2(cx: *mut JSContext, global: *mut JSObject, receiver: *mut JSObject,
protoProto: *mut JSObject,
protoClass: &'static JSClass,
constructor: JSNative,
ctorNargs: u32,
constructor: Option<(NonNullJSNative, &'static str, u32)>,
domClass: *DOMClass,
methods: Option<&'static [JSFunctionSpec]>,
properties: Option<&'static [JSPropertySpec]>,
constants: Option<&'static [ConstantSpec]>,
staticMethods: Option<&'static [JSFunctionSpec]>,
name: &str) -> *mut JSObject {
staticMethods: Option<&'static [JSFunctionSpec]>) -> *mut JSObject {
let proto = CreateInterfacePrototypeObject(cx, global, protoProto,
protoClass, methods,
properties, constants);
@ -235,25 +236,28 @@ pub fn CreateInterfaceObjects2(cx: *mut JSContext, global: *mut JSObject, receiv
PrivateValue(domClass as *libc::c_void));
}
if constructor.is_some() {
name.to_c_str().with_ref(|s| {
CreateInterfaceObject(cx, global, receiver,
constructor, ctorNargs, proto,
staticMethods, constants, s)
});
match constructor {
Some((native, name, nargs)) => {
name.to_c_str().with_ref(|s| {
CreateInterfaceObject(cx, global, receiver,
native, nargs, proto,
staticMethods, constants, s)
})
},
None => (),
}
proto
}
fn CreateInterfaceObject(cx: *mut JSContext, global: *mut JSObject, receiver: *mut JSObject,
constructorNative: JSNative,
constructorNative: NonNullJSNative,
ctorNargs: u32, proto: *mut JSObject,
staticMethods: Option<&'static [JSFunctionSpec]>,
constants: Option<&'static [ConstantSpec]>,
name: *libc::c_char) {
unsafe {
let fun = JS_NewFunction(cx, constructorNative, ctorNargs,
let fun = JS_NewFunction(cx, Some(constructorNative), ctorNargs,
JSFUN_CONSTRUCTOR, global, name);
assert!(fun.is_not_null());