summaryrefslogtreecommitdiff
path: root/demo/api.py
blob: 8cc64077ab1db6b38f1424f55ffe39bc81917590 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import cffi
from cffi import FFI

class PythonFFI(FFI):

    def __init__(self, backend=None):
        FFI.__init__(self, backend=backend)
        self._pyexports = {}

    def pyexport(self, signature):
        tp = self._typeof(signature, consider_function_as_funcptr=True)
        def decorator(func):
            name = func.__name__
            if name in self._pyexports:
                raise cffi.CDefError("duplicate pyexport'ed function %r"
                                     % (name,))
            callback_var = self.getctype(tp, name)
            self.cdef("%s;" % callback_var)
            self._pyexports[name] = _PyExport(tp, func)
        return decorator

    def verify(self, source='', **kwargs):
        extras = []
        pyexports = sorted(self._pyexports.items())
        for name, export in pyexports:
            callback_var = self.getctype(export.tp, name)
            extras.append("%s;" % callback_var)
        extras.append(source)
        source = '\n'.join(extras)
        lib = FFI.verify(self, source, **kwargs)
        for name, export in pyexports:
            cb = self.callback(export.tp, export.func)
            export.cb = cb
            setattr(lib, name, cb)
        return lib


class _PyExport(object):
    def __init__(self, tp, func):
        self.tp = tp
        self.func = func


if __name__ == '__main__':
    ffi = PythonFFI()

    @ffi.pyexport("int(int)")
    def add1(n):
        print n
        return n + 1

    ffi.cdef("""
        int f(int);
    """)

    lib = ffi.verify("""
        int f(int x) {
            return add1(add1(x));
        }
    """)

    assert lib.f(5) == 7