Add support for NamedConstructor in webidls

This commit is contained in:
Joel Teichroeb 2015-05-17 23:47:15 +00:00
parent 3e90a60170
commit 012be81eab
2 changed files with 107 additions and 28 deletions

View file

@ -2286,11 +2286,25 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
call = """\
do_create_interface_objects(cx, receiver, parent_proto.handle(),
%s, %s,
&named_constructors,
%s,
&sNativeProperties, rval);""" % (protoClass, constructor, domClass)
createArray = """\
let named_constructors: [(NonNullJSNative, &'static str, u32); %d] = [
""" % len(self.descriptor.interface.namedConstructors)
for ctor in self.descriptor.interface.namedConstructors:
constructHook = CONSTRUCT_HOOK_NAME + "_" + ctor.identifier.name;
constructArgs = methodLength(ctor)
constructor = '(%s as NonNullJSNative, "%s", %d)' % (
constructHook, ctor.identifier.name, constructArgs)
createArray += constructor
createArray += ","
createArray += "];"
return CGList([
CGGeneric(getParentProto),
CGGeneric(createArray),
CGGeneric(call % self.properties.variableNames())
], "\n")
@ -4422,6 +4436,30 @@ let args = CallArgs::from_vp(vp, argc);
self.descriptor, self._ctor)
return CGList([preamble, callGenerator])
class CGClassNameConstructHook(CGAbstractExternMethod):
"""
JS-visible named constructor for our objects
"""
def __init__(self, descriptor, ctor):
args = [Argument('*mut JSContext', 'cx'), Argument('u32', 'argc'), Argument('*mut JSVal', 'vp')]
self._ctor = ctor
CGAbstractExternMethod.__init__(self, descriptor,
CONSTRUCT_HOOK_NAME + "_" +
self._ctor.identifier.name,
'u8', args)
def definition_body(self):
preamble = CGGeneric("""\
let global = global_object_for_js_object(JS_CALLEE(cx, vp).to_object());
let args = CallArgs::from_vp(vp, argc);
""")
name = self._ctor.identifier.name
nativeName = MakeNativeName(self.descriptor.binaryNameFor(name))
callGenerator = CGMethodCall(["global.r()"], nativeName, True,
self.descriptor, self._ctor)
return CGList([preamble, callGenerator])
class CGClassFinalizeHook(CGAbstractClassHook):
"""
A hook for finalize, used to release our native object.
@ -4568,6 +4606,8 @@ class CGDescriptor(CGThing):
if descriptor.interface.hasInterfaceObject():
cgThings.append(CGClassConstructHook(descriptor))
for ctor in descriptor.interface.namedConstructors:
cgThings.append(CGClassNameConstructHook(descriptor, ctor))
cgThings.append(CGInterfaceObjectJSClass(descriptor))
if not descriptor.interface.isCallback():