Commits

Anonymous committed aa03ed9 Draft

Generate parameter variables for selected types.

Comments (0)

Files changed (3)

PCbuild/genrtwrappers.py

 
 tnames = {
     ELEMENT_TYPE_VOID: 'void',
-    ELEMENT_TYPE_BOOLEAN: 'bool',
+    ELEMENT_TYPE_BOOLEAN: 'boolean',
     ELEMENT_TYPE_CHAR: 'wchar_t',
     ELEMENT_TYPE_I1: 'char',
     ELEMENT_TYPE_U1: 'unsigned char',
     ELEMENT_TYPE_U2: 'unsigned short',
     ELEMENT_TYPE_I4: 'int',
     ELEMENT_TYPE_U4: 'unsigned int',
-    ELEMENT_TYPE_I8: 'int64_t',
-    ELEMENT_TYPE_U8: 'uint65_t',
+    ELEMENT_TYPE_I8: 'INT64',
+    ELEMENT_TYPE_U8: 'UINT64',
     ELEMENT_TYPE_R4: 'float',
     ELEMENT_TYPE_R8: 'double',
-    ELEMENT_TYPE_STRING: 'String',
+    ELEMENT_TYPE_STRING: 'HSTRING',
     ELEMENT_TYPE_I: 'int',
 }
 
     return (((b0 & 0x3f) << 24) + (s[1]<<16) +
             (s[2] << 8) + s[3]), s[4:]
 
+class BasicType:
+    def __init__(self, kind):
+        self.kind = kind
+
+    def decl_var(self, var):
+        return tnames[self.kind] + " " + var + ";"
+
+    def decl_ptr(self, var):
+        return tname[self.kind] + " *" + var + ";"
+
+builtin_types = {
+    'IUnknown':'IUnknown',
+    'Windows.Foundation.EventRegistrationToken':'EventRegistrationToken',
+    'Windows.Foundation.AsyncStatus':'AsyncStatus',
+    'Windows.Foundation.Uri':'__x_ABI_CWindows_CFoundation_CIUriRuntimeClass',
+    'System.Guid':'GUID',
+    }
+
+class NamedType:
+    def __init__(self, name, isclass):
+        self.name = name
+        try:
+            self.tname = builtin_types[name]
+        except KeyError:
+            comp = ['__x_ABI'] + list(name.split('.'))
+            # XXX tell interfaces apart
+            if isclass and not (comp[-1][0] == 'I' and comp[-1][1].isupper()):
+                comp[-1] = 'I'+comp[-1]
+            self.tname = '_C'.join(comp)
+
+    def decl_var(self, var):
+        return "/*%s*/%s %s;" % (self.name, self.tname, var)
+
+    def dec_ptr(self, var):
+        return "%s *%s;" % (self.tname, var)
+
+class SZArray:
+    def __init__(self, elemtype):
+        self.elemtype = elemtype
+
+    def decl_var(self, var):
+        return self.elemtype.decl_ptr(var)
+
 def decode_type(s):
     b0 = s[0]
     s = s[1:]
     if b0 in tnames:
-        print(tnames[b0], end=' ')
-        return s
+        return BasicType(b0), s
     if b0 in (ELEMENT_TYPE_CLASS, ELEMENT_TYPE_VALUETYPE):
         i, s = decompress_integer(s)
         table = i & 0x3
         value = i >> 2
         token = (table<<24) + value
         scope, name = mdi.GetTypeRefProps(token)
-        print("[%x]%s" % (scope, name), end=' ')
-        return s
+        return NamedType(name, b0 == ELEMENT_TYPE_CLASS), s
     if b0 == ELEMENT_TYPE_GENERICINST:
+        raise NotImplementedError
         s = decode_type(s)
         argc, s = decompress_integer(s)
         print('<', end='')
         print('>', end=' ')
         return s
     if b0 == ELEMENT_TYPE_OBJECT:
-        print('Object', end=' ')
-        return s
+        return NamedType("IUnknown", True), s
     if b0 == ELEMENT_TYPE_SZARRAY:
         b0 = s[0]
         if b0 in (ELEMENT_TYPE_CMOD_REQD, ELEMENT_TYPE_CMOD_OPT):
-            s = parse_custom_mod(s)
-        decode_type(s)
-        print('[]', end=' ')
-        return s
+            c, s = parse_custom_mod(s)
+        t, s = decode_type(s)
+        # XXX consider c
+        return t, s
     if b0 == ELEMENT_TYPE_VAR:
+        raise NotImplementedError
         param, s = decompress_integer(s)
         print('T%d' % param, end=' ')
         return s
-    assert 0, hex(b0)
+    raise NotImplementedError(hex(b0))
 
 def parse_param(s):
     b0 = s[0]
     if b0 in (ELEMENT_TYPE_CMOD_REQD, ELEMENT_TYPE_CMOD_OPT):
+        raise NotImplementedError
         s = parse_custom_mod(s)
         return parse_param(s)
     if b0 == ELEMENT_TYPE_BYREF:
+        raise NotImplementedError
         print('BYREF', end=' ')
         return decode_type(s[1:])
     elif b0 == ELEMENT_TYPE_TYPEDBYREF:
+        raise NotImplementedError
         print('TYPEDBYREF', end=' ')
     elif b0 == ELEMENT_TYPE_VOID:
+        raise NotImplementedError
         print('void', end=' ')
     else:
         return decode_type(s)
     #    print("EXPLICITTHIS", end=' ')
     #callconv = ('DEFAULT', 'C', 'STDCALL', 'THISCALL', 'FASTCALL', 'VARARG')
     #print(callconv[s0 & 7], end=' ')
+    nparams, s = decompress_integer(s)
+    try:
+        rtype, s = decode_type(s)
+        params = []
+        for i in range(nparams):
+            t, s = parse_param(s)
+            params.append(t)
+    except NotImplementedError:
+        params = None
     outfile.write('static PyObject*\n%s_%s(PyObject *_this, PyObject *args)\n{\n' %
                   (cname, name))
-    outfile.write('  PyErr_SetString(PyExc_NotImplementedError, "signature is unsupported");\n')
-    outfile.write('  return NULL;\n')
+    if params is None:
+        outfile.write('  PyErr_SetString(PyExc_NotImplementedError, "signature is unsupported");\n')
+        outfile.write('  return NULL;\n')
+    else:
+        for i, p in enumerate(params):
+            outfile.write("  %s\n" % p.decl_var("param%d" % i))
     outfile.write('}\n\n')
-    return
-    nparams, s = decompress_integer(s)
-    s = decode_type(s)
-    print(name, '(', sep='', end=' ')
-    for i in range(nparams):
-        s = parse_param(s)
-        if i < nparams-1:
-            print(',', end=' ')
-    print(')')
 
+# No header file for these namespaces
+noheader = {
+    "Windows.Data",
+    "Windows.Devices",
+    "Windows.Graphics",
+    "Windows.Management",
+    "Windows.Security"
+    }
+    
+included = set()
+def include(h):
+    if h in included:
+        return
+    outfile.write('#include <%s.h>\n' % h)
+    included.add(h)
 
 def namespaces(n, files):
+    if n not in noheader:
+        include(n)
     mdfiles, subns = winrt.RoResolveNamespace(n)
     files.extend(mdfiles)
     for sub in subns:
         namespaces(n+"."+sub, files)
 
+outfile = open('rtwrapper.c', 'w')
+outfile.write('#include "rtsupport.c"\n')
 
 files = []
 namespaces('Windows', files)
 
 dispenser = winrt.newdispenser()
 
-outfile = open('rtwrapper.c', 'w')
-outfile.write('#include "rtsupport.c"\n')
-
 def print_type():
     outfile.write("static PyMethodDef %s_methods[] = {\n" % cname)
     for m in methods:
     outfile.write('  %s_slots,\n' % cname)
     outfile.write('};\n\n')
 
+# skip for now
+skipped_types = {
+    'Windows.Devices.Enumeration.DeviceThumbnail',
+    }
+skipped_namespaces = {
+    'Windows.Foundation.Metadata', # no header file
+    'Windows.Graphics.Printing.OptionDetails', # inconsistent type names
+    'Windows.UI.Xaml.Documents', # missing types
+    'Windows.UI.Xaml.Controls', # vector types
+    'Windows.UI.Xaml.Controls.Primitives', # vector types
+    'Windows.UI.Xaml', # vector types
+    'Windows.UI.Xaml.Media', # vector types
+    }
+
 for f in files:
+    outfile.write("/********* %s ************/\n" % f)
     mdi = dispenser.OpenScope(f)
     for typedef in mdi.EnumTypeDefs(None, 1000):
         tname, flags, base = mdi.GetTypeDefProps(typedef)
+        namespace = tname.rsplit('.', 1)[0]
+        if namespace in skipped_namespaces:
+            continue
+        include(namespace)
+    for typedef in mdi.EnumTypeDefs(None, 1000):
+        tname, flags, base = mdi.GetTypeDefProps(typedef)
         if '`' in tname:
             # XXX generics
             continue
+        if tname in skipped_types:
+            continue
+        namespace = tname.rsplit('.', 1)[0]
+        if namespace in skipped_namespaces:
+            continue
         outfile.write("/******* %s ********/\n" % tname)
         cname = tname.replace('.', '_')
         methods = []

PCbuild/rtsupport.c

 #include "Python.h"
 
-struct RTObject {
+typedef struct RTObject {
     PyObject_HEAD
     void *_com;
-};
+} RTObject;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.