Exemplo n.º 1
0
    def test_mergeDifferent(self):
        """
        Test that merging two typelibs with completely different interfaces
        produces the correctly merged typelib.
        
        """
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(xpt.Interface("IFoo"))
        t2 = xpt.Typelib()
        # add an unresolved interface
        t2.interfaces.append(xpt.Interface("IBar"))
        t1.merge(t2)

        self.assertEqual(2, len(t1.interfaces))
        # Interfaces should wind up sorted
        self.assertEqual("IBar", t1.interfaces[0].name)
        self.assertEqual("IFoo", t1.interfaces[1].name)

        # Add some IID values
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        t2 = xpt.Typelib()
        # add an unresolved interface
        t2.interfaces.append(
            xpt.Interface("IBar", iid="44332211-6655-8877-0099-aabbccddeeff"))
        t1.merge(t2)

        self.assertEqual(2, len(t1.interfaces))
        # Interfaces should wind up sorted
        self.assertEqual("IFoo", t1.interfaces[0].name)
        self.assertEqual("IBar", t1.interfaces[1].name)
Exemplo n.º 2
0
    def test_mergeUnresolvedIID(self):
        """
        Test that merging a typelib with an unresolved definition of
        an interface that's also unresolved in this typelib, but one
        has a valid IID copies the IID value to the resulting typelib.

        """
        # Unresolved in both, but t1 has an IID value
        t1 = xpt.Typelib()
        # add an unresolved interface with a valid IID
        t1.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        t2 = xpt.Typelib()
        # add an unresolved interface, no IID
        t2.interfaces.append(xpt.Interface("IFoo"))
        t1.merge(t2)

        self.assertEqual(1, len(t1.interfaces))
        self.assertEqual("IFoo", t1.interfaces[0].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t1.interfaces[0].iid)
        # Unresolved in both, but t2 has an IID value
        t1 = xpt.Typelib()
        # add an unresolved interface, no IID
        t1.interfaces.append(xpt.Interface("IFoo"))
        t2 = xpt.Typelib()
        # add an unresolved interface with a valid IID
        t2.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        t1.merge(t2)

        self.assertEqual(1, len(t1.interfaces))
        self.assertEqual("IFoo", t1.interfaces[0].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t1.interfaces[0].iid)
Exemplo n.º 3
0
    def test_mergeConflict(self):
        """
        Test that merging two typelibs with conflicting interface definitions
        raises an error.
        
        """
        # Same names, different IIDs
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        t2 = xpt.Typelib()
        # add an unresolved interface, same name different IID
        t2.interfaces.append(
            xpt.Interface("IFoo", iid="44332211-6655-8877-0099-aabbccddeeff"))
        self.assertRaises(xpt.DataError, xpt.xpt_link, [t1, t2])

        # Same IIDs, different names
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        t2 = xpt.Typelib()
        # add an unresolved interface, same IID different name
        t2.interfaces.append(
            xpt.Interface("IBar", iid="11223344-5566-7788-9900-aabbccddeeff"))
        self.assertRaises(xpt.DataError, xpt.xpt_link, [t1, t2])
Exemplo n.º 4
0
 def test_unresolvedEqual(self):
     """
     Test comparison function on xpt.Interface with equal names and IIDs.
     
     """
     i1 = xpt.Interface("ABC")
     i2 = xpt.Interface("ABC")
     self.assert_(i1 == i2)
Exemplo n.º 5
0
 def test_unresolvedName(self):
     """
     Test comparison function on xpt.Interface by name.
     
     """
     i1 = xpt.Interface("ABC")
     i2 = xpt.Interface("DEF")
     self.assert_(i1 < i2)
     self.assert_(i1 != i2)
Exemplo n.º 6
0
 def test_unresolvedIID(self):
     """
     Test comparison function on xpt.Interface with different IIDs.
     
     """
     # IIDs sort before names
     i1 = xpt.Interface("ABC", iid="22334411-5566-7788-9900-aabbccddeeff")
     i2 = xpt.Interface("DEF", iid="11223344-5566-7788-9900-aabbccddeeff")
     self.assert_(i2 < i1)
     self.assert_(i2 != i1)
Exemplo n.º 7
0
 def test_simple(self):
     t = xpt.Typelib()
     # add an unresolved interface
     t.interfaces.append(xpt.Interface("IFoo"))
     self.checkRoundtrip(t)
     
     t = xpt.Typelib()
     # add an unresolved interface with an IID
     t.interfaces.append(xpt.Interface("IBar", "11223344-5566-7788-9900-aabbccddeeff"))
     self.checkRoundtrip(t)
Exemplo n.º 8
0
    def test_mergeResolvedUnresolved(self):
        """
        Test that merging two typelibs, one of which contains an unresolved
        reference to an interface, and the other of which contains a
        resolved reference to the same interface results in keeping the
        resolved reference.

        """
        # t1 has an unresolved interface, t2 has a resolved version
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(xpt.Interface("IFoo"))
        t2 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t2.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(1, len(t3.interfaces))
        self.assertEqual("IFoo", t3.interfaces[0].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[0].iid)
        self.assert_(t3.interfaces[0].resolved)
        self.assertEqual(1, len(t3.interfaces[0].methods))
        self.assertEqual("Bar", t3.interfaces[0].methods[0].name)

        # t1 has a resolved interface, t2 has an unresolved version
        t1 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t1.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t2 = xpt.Typelib()
        # add an unresolved interface
        t2.interfaces.append(xpt.Interface("IFoo"))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(1, len(t3.interfaces))
        self.assertEqual("IFoo", t3.interfaces[0].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[0].iid)
        self.assert_(t3.interfaces[0].resolved)
        self.assertEqual(1, len(t3.interfaces[0].methods))
        self.assertEqual("Bar", t3.interfaces[0].methods[0].name)
Exemplo n.º 9
0
    def test_parent(self):
        """
        Test that an interface's parent property is correctly serialized
        and deserialized.

        """
        t = xpt.Typelib()
        pi = xpt.Interface("IParent")
        t.interfaces.append(pi)
        t.interfaces.append(xpt.Interface("IChild", iid="11223344-5566-7788-9900-aabbccddeeff",
                                          parent=pi, resolved=True))
        self.checkRoundtrip(t)
Exemplo n.º 10
0
 def test_resolvedIdentical(self):
     """
     Test comparison function on xpt.Interface with interfaces with
     identical names and IIDs, both of which are resolved.
     
     """
     p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
     m = xpt.Method("Bar", p)
     i1 = xpt.Interface("ABC", iid="11223344-5566-7788-9900-aabbccddeeff",
                        methods=[m])
     i2 = xpt.Interface("ABC", iid="11223344-5566-7788-9900-aabbccddeeff",
                        methods=[m])
     self.assert_(i2 == i1)
Exemplo n.º 11
0
 def test_unresolvedResolved(self):
     """
     Test comparison function on xpt.Interface with interfaces with
     identical names and IIDs but different resolved status.
     
     """
     i1 = xpt.Interface("ABC", iid="11223344-5566-7788-9900-aabbccddeeff")
     p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
     m = xpt.Method("Bar", p)
     i2 = xpt.Interface("ABC", iid="11223344-5566-7788-9900-aabbccddeeff",
                        methods=[m])
     self.assert_(i2 < i1)
     self.assert_(i2 != i1)
Exemplo n.º 12
0
    def test_mergeReplaceArrayTypeParams(self):
        """
        Test that merging an interface correctly updates ArrayType
        params whose element_type is an InterfaceType on methods
        of other interfaces.

        """
        # t1 has an unresolved interface and an interface that uses the
        # unresolved interface as a type in an ArrayType in a parameter
        # of a method. t2 has a resolved version of the unresolved interface.
        t1 = xpt.Typelib()
        # add an unresolved interface
        i = xpt.Interface("IFoo")
        t1.interfaces.append(i)
        # add an interface that uses the unresolved interface
        # as a type in an ArrayType in a param value in a method.
        vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        intp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32))
        p = xpt.Param(xpt.ArrayType(xpt.InterfaceType(i), 1, 2))
        m = xpt.Method("ArrayIfaceParam", vp, params=[p, intp, intp])
        t1.interfaces.append(
            xpt.Interface("IParam",
                          iid="11111111-1111-1111-1111-111111111111",
                          methods=[m],
                          scriptable=True))
        t2 = xpt.Typelib()
        # add a resolved interface
        m = xpt.Method("Bar", vp)
        t2.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(2, len(t3.interfaces))
        self.assertEqual("IParam", t3.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t3.interfaces[0].iid)
        self.assertEqual("IFoo", t3.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[1].iid)
        self.assert_(t3.interfaces[1].resolved)
        # Ensure that IRetval's method's param type has been updated.
        self.assertEqual(1, len(t3.interfaces[0].methods))
        self.assert_(t3.interfaces[0].methods[0].params[0].type.element_type.
                     iface.resolved)
        self.assertEqual(
            t3.interfaces[1],
            t3.interfaces[0].methods[0].params[0].type.element_type.iface)
Exemplo n.º 13
0
    def test_xpt_link(self):
        """
        Test the xpt_link method.
        
        """
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(xpt.Interface("IFoo"))
        f1 = self.gettempfile()
        t1.write(f1)

        t2 = xpt.Typelib()
        # add an unresolved interface
        t2.interfaces.append(xpt.Interface("IBar"))
        f2 = self.gettempfile()
        t2.write(f2)

        f3 = self.gettempfile()
        xpt.xpt_link(f3, [f1, f2])
        t3 = xpt.Typelib.read(f3)

        self.assertEqual(2, len(t3.interfaces))
        # Interfaces should wind up sorted
        self.assertEqual("IBar", t3.interfaces[0].name)
        self.assertEqual("IFoo", t3.interfaces[1].name)

        # Add some IID values
        t1 = xpt.Typelib()
        # add an unresolved interface
        t1.interfaces.append(
            xpt.Interface("IFoo", iid="11223344-5566-7788-9900-aabbccddeeff"))
        f1 = self.gettempfile()
        t1.write(f1)

        t2 = xpt.Typelib()
        # add an unresolved interface
        t2.interfaces.append(
            xpt.Interface("IBar", iid="44332211-6655-8877-0099-aabbccddeeff"))
        f2 = self.gettempfile()
        t2.write(f2)

        f3 = self.gettempfile()
        xpt.xpt_link(f3, [f1, f2])
        t3 = xpt.Typelib.read(f3)

        self.assertEqual(2, len(t3.interfaces))
        # Interfaces should wind up sorted
        self.assertEqual("IFoo", t3.interfaces[0].name)
        self.assertEqual("IBar", t3.interfaces[1].name)
Exemplo n.º 14
0
    def get_type(type, calltype, iid_is=None, size_is=None):
        """ Return the appropriate xpt.Type object for this param """

        while isinstance(type, xpidl.Typedef):
            type = type.realtype

        if isinstance(type, xpidl.Builtin):
            if type.name == 'string' and size_is is not None:
                return xpt.StringWithSizeType(size_is, size_is)
            elif type.name == 'wstring' and size_is is not None:
                return xpt.WideStringWithSizeType(size_is, size_is)
            else:
                tag = TypeMap[type.name]
                isPtr = (tag == xpt.Type.Tags.char_ptr
                         or tag == xpt.Type.Tags.wchar_t_ptr)
                return xpt.SimpleType(tag, pointer=isPtr, reference=False)

        if isinstance(type, xpidl.Array):
            # NB: For an Array<T> we pass down the iid_is to get the type of T.
            #     This allows Arrays of InterfaceIs types to work.
            return xpt.ArrayType(
                get_type(type.type, calltype, iid_is),
                size_is,
                #XXXkhuey length_is duplicates size_is (bug 677788),
                size_is)

        if isinstance(type, xpidl.Interface) or isinstance(
                type, xpidl.Forward):
            xptiface = None
            for i in ifaces:
                if i.name == type.name:
                    xptiface = i

            if not xptiface:
                xptiface = xpt.Interface(name=type.name)
                ifaces.append(xptiface)

            return xpt.InterfaceType(xptiface)

        if isinstance(type, xpidl.Native):
            if type.specialtype:
                # XXXkhuey jsval is marked differently in the typelib and in the headers :-(
                isPtr = (type.isPtr(calltype) or type.isRef(calltype)
                         ) and not type.specialtype == 'jsval'
                isRef = type.isRef(
                    calltype) and not type.specialtype == 'jsval'
                return xpt.SimpleType(TypeMap[type.specialtype],
                                      pointer=isPtr,
                                      reference=isRef)
            elif iid_is is not None:
                return xpt.InterfaceIsType(iid_is)
            else:
                # void ptr
                return xpt.SimpleType(TypeMap['void'],
                                      pointer=True,
                                      reference=False)

        raise Exception("Unknown type!")
Exemplo n.º 15
0
 def __getattr__(self, attr):
     # Support constants as attributes.
     c = _constants_by_iid_map.get(self._iidobj_)
     if c is None:
         c = {}
         i = xpt.Interface(self._iidobj_)
         for c_ob in i.constants:
             c[c_ob.name] = c_ob.value
         _constants_by_iid_map[self._iidobj_] = c
     if c.has_key(attr):
         return c[attr]
     raise AttributeError, "'%s' interfaces do not define a constant '%s'" % (self.name, attr)
Exemplo n.º 16
0
    def test_ifaceFlags(self):
        """
        Test that an interface's flags are correctly serialized
        and deserialized.

        """
        t = xpt.Typelib()
        t.interfaces.append(xpt.Interface("IFlags", iid="11223344-5566-7788-9900-aabbccddeeff",
                                          resolved=True,
                                          scriptable=True,
                                          function=True))
        self.checkRoundtrip(t)
Exemplo n.º 17
0
 def test_read_file(self):
     """
     Test that a Typelib can be read/written from/to a file.
     """
     t = xpt.Typelib()
     # add an unresolved interface
     t.interfaces.append(xpt.Interface("IFoo"))
     fd, f = tempfile.mkstemp()
     os.close(fd)
     t.write(f)
     t2 = xpt.Typelib.read(f)
     os.remove(f)
     self.assert_(t2 is not None)
     self.assertEqualTypelibs(t, t2)
Exemplo n.º 18
0
 def test_constants(self):
     c = xpt.Constant("X", xpt.SimpleType(xpt.Type.Tags.uint32), 0xF000F000)
     i = xpt.Interface("IFoo",
                       iid="11223344-5566-7788-9900-aabbccddeeff",
                       constants=[c])
     t = xpt.Typelib(interfaces=[i])
     self.checkRoundtrip(t)
     # tack on some more constants
     i.constants.append(
         xpt.Constant("Y", xpt.SimpleType(xpt.Type.Tags.int16), -30000))
     i.constants.append(
         xpt.Constant("Z", xpt.SimpleType(xpt.Type.Tags.uint16), 0xB0B0))
     i.constants.append(
         xpt.Constant("A", xpt.SimpleType(xpt.Type.Tags.int32), -1000000))
     self.checkRoundtrip(t)
Exemplo n.º 19
0
    def test_mergeReplaceRetval(self):
        """
        Test that merging an interface correctly updates InterfaceType
        return values on methods of other interfaces.

        """
        # t1 has an unresolved interface and an interface that uses the
        # unresolved interface as a return value from a method. t2
        # has a resolved version of the unresolved interface.
        t1 = xpt.Typelib()
        # add an unresolved interface
        i = xpt.Interface("IFoo")
        t1.interfaces.append(i)
        # add an interface that uses the unresolved interface
        # as a return value in a method.
        p = xpt.Param(xpt.InterfaceType(i))
        m = xpt.Method("ReturnIface", p)
        t1.interfaces.append(
            xpt.Interface("IRetval",
                          iid="11111111-1111-1111-1111-111111111111",
                          methods=[m],
                          scriptable=True))
        t2 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t2.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(2, len(t3.interfaces))
        self.assertEqual("IRetval", t3.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t3.interfaces[0].iid)
        self.assertEqual("IFoo", t3.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[1].iid)
        self.assert_(t3.interfaces[1].resolved)
        # Ensure that IRetval's method's return value type has been updated.
        self.assertEqual(1, len(t3.interfaces[0].methods))
        self.assert_(t3.interfaces[0].methods[0].result.type.iface.resolved)
        self.assertEqual(t3.interfaces[1],
                         t3.interfaces[0].methods[0].result.type.iface)

        # t1 has a resolved interface. t2 has an unresolved version and
        # an interface that uses the unresolved interface as a return value
        # from a method.
        t1 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t1.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t2 = xpt.Typelib()
        # add an unresolved interface
        i = xpt.Interface("IFoo")
        t2.interfaces.append(i)
        # add an interface that uses the unresolved interface
        # as a return value in a method.
        p = xpt.Param(xpt.InterfaceType(i))
        m = xpt.Method("ReturnIface", p)
        t2.interfaces.append(
            xpt.Interface("IRetval",
                          iid="11111111-1111-1111-1111-111111111111",
                          methods=[m],
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(2, len(t3.interfaces))
        self.assertEqual("IRetval", t3.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t3.interfaces[0].iid)
        self.assertEqual("IFoo", t3.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[1].iid)
        self.assert_(t3.interfaces[1].resolved)
        # Ensure that IRetval's method's return value type has been updated.
        self.assertEqual(1, len(t3.interfaces[0].methods))
        self.assert_(t3.interfaces[0].methods[0].result.type.iface.resolved)
        self.assertEqual(t3.interfaces[1],
                         t3.interfaces[0].methods[0].result.type.iface)
Exemplo n.º 20
0
    def test_mergeReplaceParents(self):
        """
        Test that merging an interface results in other interfaces' parent
        member being updated properly.

        """
        # t1 has an unresolved interface, t2 has a resolved version,
        # but t1 also has another interface whose parent is the unresolved
        # interface.
        t1 = xpt.Typelib()
        # add an unresolved interface
        pi = xpt.Interface("IFoo")
        t1.interfaces.append(pi)
        # add a child of the unresolved interface
        t1.interfaces.append(
            xpt.Interface("IChild",
                          iid="11111111-1111-1111-1111-111111111111",
                          resolved=True,
                          parent=pi,
                          scriptable=True))
        t2 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t2.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(2, len(t3.interfaces))
        self.assertEqual("IChild", t3.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t3.interfaces[0].iid)
        self.assertEqual("IFoo", t3.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[1].iid)
        self.assert_(t3.interfaces[0].resolved)
        # Ensure that IChild's parent has been updated
        self.assertEqual(t3.interfaces[1], t3.interfaces[0].parent)
        self.assert_(t3.interfaces[0].parent.resolved)

        # t1 has a resolved interface, t2 has an unresolved version,
        # but t2 also has another interface whose parent is the unresolved
        # interface.
        t1 = xpt.Typelib()
        # add a resolved interface
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        t1.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m],
                          scriptable=True))
        t2 = xpt.Typelib()
        # add an unresolved interface
        pi = xpt.Interface("IFoo")
        t2.interfaces.append(pi)
        # add a child of the unresolved interface
        t2.interfaces.append(
            xpt.Interface("IChild",
                          iid="11111111-1111-1111-1111-111111111111",
                          resolved=True,
                          parent=pi,
                          scriptable=True))
        t3 = xpt.xpt_link([t1, t2])

        self.assertEqual(2, len(t3.interfaces))
        self.assertEqual("IChild", t3.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t3.interfaces[0].iid)
        self.assertEqual("IFoo", t3.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t3.interfaces[1].iid)
        self.assert_(t3.interfaces[0].resolved)
        # Ensure that IChild's parent has been updated
        self.assertEqual(t3.interfaces[1], t3.interfaces[0].parent)
        self.assert_(t3.interfaces[0].parent.resolved)
Exemplo n.º 21
0
def build_interface(iface, ifaces):
    def get_type(type, calltype, iid_is=None, size_is=None):
        """ Return the appropriate xpt.Type object for this param """

        while isinstance(type, xpidl.Typedef):
            type = type.realtype

        if isinstance(type, xpidl.Builtin):
            if type.name == 'string' and size_is is not None:
                return xpt.StringWithSizeType(size_is, size_is)
            elif type.name == 'wstring' and size_is is not None:
                return xpt.WideStringWithSizeType(size_is, size_is)
            else:
                tag = TypeMap[type.name]
                isPtr = (tag == xpt.Type.Tags.char_ptr
                         or tag == xpt.Type.Tags.wchar_t_ptr)
                return xpt.SimpleType(tag, pointer=isPtr, reference=False)

        if isinstance(type, xpidl.Array):
            # NB: For an Array<T> we pass down the iid_is to get the type of T.
            #     This allows Arrays of InterfaceIs types to work.
            return xpt.ArrayType(
                get_type(type.type, calltype, iid_is),
                size_is,
                #XXXkhuey length_is duplicates size_is (bug 677788),
                size_is)

        if isinstance(type, xpidl.Interface) or isinstance(
                type, xpidl.Forward):
            xptiface = None
            for i in ifaces:
                if i.name == type.name:
                    xptiface = i

            if not xptiface:
                xptiface = xpt.Interface(name=type.name)
                ifaces.append(xptiface)

            return xpt.InterfaceType(xptiface)

        if isinstance(type, xpidl.Native):
            if type.specialtype:
                # XXXkhuey jsval is marked differently in the typelib and in the headers :-(
                isPtr = (type.isPtr(calltype) or type.isRef(calltype)
                         ) and not type.specialtype == 'jsval'
                isRef = type.isRef(
                    calltype) and not type.specialtype == 'jsval'
                return xpt.SimpleType(TypeMap[type.specialtype],
                                      pointer=isPtr,
                                      reference=isRef)
            elif iid_is is not None:
                return xpt.InterfaceIsType(iid_is)
            else:
                # void ptr
                return xpt.SimpleType(TypeMap['void'],
                                      pointer=True,
                                      reference=False)

        raise Exception("Unknown type!")

    def get_nsresult():
        return xpt.SimpleType(TypeMap['nsresult'])

    def build_nsresult_param():
        return xpt.Param(get_nsresult())

    def get_result_type(m):
        if not m.notxpcom:
            return get_nsresult()

        return get_type(m.realtype, '')

    def build_result_param(m):
        return xpt.Param(get_result_type(m))

    def build_retval_param(m):
        type = get_type(m.realtype, 'out')
        if isDipperType(type.tag):
            # NB: The retval bit needs to be set here, contrary to what the
            # xpt spec says.
            return xpt.Param(type, in_=True, retval=True, dipper=True)
        return xpt.Param(type, in_=False, out=True, retval=True)

    def build_attr_param(a, getter=False, setter=False):
        if not (getter or setter):
            raise Exception(
                "Attribute param must be for a getter or a setter!")

        type = get_type(a.realtype, getter and 'out' or 'in')
        if setter:
            return xpt.Param(type)
        else:
            if isDipperType(type.tag):
                # NB: The retval bit needs to be set here, contrary to what the
                # xpt spec says.
                return xpt.Param(type, in_=True, retval=True, dipper=True)
            return xpt.Param(type, in_=False, out=True, retval=True)

    if iface.namemap is None:
        raise Exception("Interface was not resolved.")

    consts = []
    methods = []

    def build_const(c):
        consts.append(
            xpt.Constant(c.name, get_type(c.basetype, ''), c.getValue()))

    def build_method(m):
        params = []

        def build_param(p):
            def findattr(p, attr):
                if hasattr(p, attr) and getattr(p, attr):
                    for i, param in enumerate(m.params):
                        if param.name == getattr(p, attr):
                            return i
                    return None

            iid_is = findattr(p, 'iid_is')
            size_is = findattr(p, 'size_is')

            in_ = p.paramtype.count("in")
            out = p.paramtype.count("out")
            dipper = False
            type = get_type(p.realtype,
                            p.paramtype,
                            iid_is=iid_is,
                            size_is=size_is)
            if out and isDipperType(type.tag):
                out = False
                dipper = True

            return xpt.Param(type, in_, out, p.retval, p.shared, dipper,
                             p.optional)

        for p in m.params:
            params.append(build_param(p))

        if not m.notxpcom and m.realtype.name != 'void':
            params.append(build_retval_param(m))

        methods.append(
            xpt.Method(m.name,
                       build_result_param(m),
                       params,
                       getter=False,
                       setter=False,
                       notxpcom=m.notxpcom,
                       constructor=False,
                       hidden=m.noscript,
                       optargc=m.optional_argc,
                       implicit_jscontext=m.implicit_jscontext))

    def build_attr(a):
        # Write the getter
        methods.append(
            xpt.Method(a.name,
                       build_nsresult_param(),
                       [build_attr_param(a, getter=True)],
                       getter=True,
                       setter=False,
                       constructor=False,
                       hidden=a.noscript,
                       optargc=False,
                       implicit_jscontext=a.implicit_jscontext))

        # And maybe the setter
        if not a.readonly:
            methods.append(
                xpt.Method(a.name,
                           build_nsresult_param(),
                           [build_attr_param(a, setter=True)],
                           getter=False,
                           setter=True,
                           constructor=False,
                           hidden=a.noscript,
                           optargc=False,
                           implicit_jscontext=a.implicit_jscontext))

    for member in iface.members:
        if isinstance(member, xpidl.ConstMember):
            build_const(member)
        elif isinstance(member, xpidl.Attribute):
            build_attr(member)
        elif isinstance(member, xpidl.Method):
            build_method(member)
        elif isinstance(member, xpidl.CDATA):
            pass
        else:
            raise Exception("Unexpected interface member: %s" % member)

    parent = None
    if iface.base:
        for i in ifaces:
            if i.name == iface.base:
                parent = i
        if not parent:
            parent = xpt.Interface(name=iface.base)
            ifaces.append(parent)

    return xpt.Interface(iface.name,
                         iface.attributes.uuid,
                         methods=methods,
                         constants=consts,
                         resolved=True,
                         parent=parent,
                         scriptable=iface.attributes.scriptable,
                         function=iface.attributes.function,
                         builtinclass=iface.attributes.builtinclass,
                         main_process_scriptable_only=iface.attributes.
                         main_process_scriptable_only)
Exemplo n.º 22
0
    def test_mergeReplaceParams(self):
        """
        Test that merging an interface correctly updates InterfaceType
        params on methods of other interfaces.

        """
        # t1 has an unresolved interface and an interface that uses the
        # unresolved interface as a param value in a method. t2
        # has a resolved version of the unresolved interface.
        t1 = xpt.Typelib()
        # add an unresolved interface
        i = xpt.Interface("IFoo")
        t1.interfaces.append(i)
        # add an interface that uses the unresolved interface
        # as a param value in a method.
        vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        p = xpt.Param(xpt.InterfaceType(i))
        m = xpt.Method("IfaceParam", vp, params=[p])
        t1.interfaces.append(
            xpt.Interface("IParam",
                          iid="11111111-1111-1111-1111-111111111111",
                          methods=[m]))
        t2 = xpt.Typelib()
        # add a resolved interface
        m = xpt.Method("Bar", vp)
        t2.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m]))
        t1.merge(t2)

        self.assertEqual(2, len(t1.interfaces))
        self.assertEqual("IParam", t1.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t1.interfaces[0].iid)
        self.assertEqual("IFoo", t1.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t1.interfaces[1].iid)
        self.assert_(t1.interfaces[1].resolved)
        # Ensure that IRetval's method's param type has been updated.
        self.assertEqual(1, len(t1.interfaces[0].methods))
        self.assert_(t1.interfaces[0].methods[0].params[0].type.iface.resolved)
        self.assertEqual(t1.interfaces[1],
                         t1.interfaces[0].methods[0].params[0].type.iface)

        # t1 has a resolved interface. t2 has an unresolved version
        # and an interface that uses the unresolved interface as a
        # param value in a method.
        t1 = xpt.Typelib()
        # add a resolved interface
        m = xpt.Method("Bar", vp)
        t1.interfaces.append(
            xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m]))
        t2 = xpt.Typelib()
        # add an unresolved interface
        i = xpt.Interface("IFoo")
        t2.interfaces.append(i)
        # add an interface that uses the unresolved interface
        # as a param value in a method.
        vp = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        p = xpt.Param(xpt.InterfaceType(i))
        m = xpt.Method("IfaceParam", vp, params=[p])
        t2.interfaces.append(
            xpt.Interface("IParam",
                          iid="11111111-1111-1111-1111-111111111111",
                          methods=[m]))
        t1.merge(t2)

        self.assertEqual(2, len(t1.interfaces))
        self.assertEqual("IParam", t1.interfaces[0].name)
        self.assertEqual("11111111-1111-1111-1111-111111111111",
                         t1.interfaces[0].iid)
        self.assertEqual("IFoo", t1.interfaces[1].name)
        self.assertEqual("11223344-5566-7788-9900-aabbccddeeff",
                         t1.interfaces[1].iid)
        self.assert_(t1.interfaces[1].resolved)
        # Ensure that IRetval's method's param type has been updated.
        self.assertEqual(1, len(t1.interfaces[0].methods))
        self.assert_(t1.interfaces[0].methods[0].params[0].type.iface.resolved)
        self.assertEqual(t1.interfaces[1],
                         t1.interfaces[0].methods[0].params[0].type.iface)
Exemplo n.º 23
0
# The contents of this file are subject to the Mozilla Public License Version
Exemplo n.º 24
0
    def test_methods(self):
        p = xpt.Param(xpt.SimpleType(xpt.Type.Tags.void))
        m = xpt.Method("Bar", p)
        i = xpt.Interface("IFoo",
                          iid="11223344-5566-7788-9900-aabbccddeeff",
                          methods=[m])
        t = xpt.Typelib(interfaces=[i])
        self.checkRoundtrip(t)
        # add some more methods
        i.methods.append(
            xpt.Method("One",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                       params=[
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int64)),
                           xpt.Param(
                               xpt.SimpleType(xpt.Type.Tags.float,
                                              pointer=True))
                       ]))
        self.checkRoundtrip(t)
        # test some other types (should really be more thorough)
        i.methods.append(
            xpt.Method("Two",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                       params=[
                           xpt.Param(
                               xpt.SimpleType(xpt.Type.Tags.UTF8String,
                                              pointer=True)),
                           xpt.Param(
                               xpt.SimpleType(xpt.Type.Tags.wchar_t_ptr,
                                              pointer=True))
                       ]))
        self.checkRoundtrip(t)
        # add a method with an InterfaceType argument
        bar = xpt.Interface("IBar")
        t.interfaces.append(bar)
        i.methods.append(
            xpt.Method("IFaceMethod",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                       params=[xpt.Param(xpt.InterfaceType(bar))]))
        self.checkRoundtrip(t)

        # add a method with an InterfaceIsType argument
        i.methods.append(
            xpt.Method("IFaceIsMethod",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)),
                       params=[
                           xpt.Param(xpt.InterfaceIsType(1)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.nsIID))
                       ]))
        self.checkRoundtrip(t)

        # add a method with an ArrayType argument
        i.methods.append(
            xpt.Method("ArrayMethod",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)),
                       params=[
                           xpt.Param(
                               xpt.ArrayType(
                                   xpt.SimpleType(xpt.Type.Tags.int32), 1, 2)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                       ]))
        self.checkRoundtrip(t)

        # add a method with a StringWithSize and WideStringWithSize arguments
        i.methods.append(
            xpt.Method("StringWithSizeMethod",
                       xpt.Param(xpt.SimpleType(xpt.Type.Tags.void)),
                       params=[
                           xpt.Param(xpt.StringWithSizeType(1, 2)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                           xpt.Param(xpt.WideStringWithSizeType(4, 5)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                           xpt.Param(xpt.SimpleType(xpt.Type.Tags.int32)),
                       ]))
        self.checkRoundtrip(t)