Beispiel #1
0
    def test_pass_around_t_object(self):
        from pypy.rpython.annlowlevel import base_ptr_lltype
        T = base_ptr_lltype()

        class X(object):
            _TYPE = T
            x = 10

        def callback(x):
            return x.x

        c_source = py.code.Source("""
        long eating_callback(void *arg, long(*call)(void*))
        {
            return call(arg);
        }
        """)

        eci = ExternalCompilationInfo(separate_module_sources=[c_source],
                                      export_symbols=['eating_callback'])

        args = [T, rffi.CCallback([T], rffi.LONG)]
        eating_callback = rffi.llexternal('eating_callback',
                                          args,
                                          rffi.LONG,
                                          compilation_info=eci)

        res = eating_callback(X(), callback)
        assert res == 10
Beispiel #2
0
    def test_qsort_callback(self):
        TP = rffi.CArrayPtr(rffi.INT)
        a = lltype.malloc(TP.TO, 5, flavor='raw')
        a[0] = rffi.r_int(5)
        a[1] = rffi.r_int(3)
        a[2] = rffi.r_int(2)
        a[3] = rffi.r_int(1)
        a[4] = rffi.r_int(4)

        def compare(a, b):
            if a[0] > b[0]:
                return rffi.r_int(1)
            else:
                return rffi.r_int(-1)

        CALLBACK = rffi.CCallback([rffi.VOIDP, rffi.VOIDP], rffi.INT)
        qsort = rffi.llexternal(
            'qsort', [rffi.VOIDP, rffi.SIZE_T, rffi.SIZE_T, CALLBACK],
            lltype.Void)

        qsort(rffi.cast(rffi.VOIDP, a), 5, rffi.sizeof(rffi.INT), compare)
        for i in range(5):
            assert a[i] == i + 1
Beispiel #3
0
    def test_qsort(self):
        TP = rffi.CArrayPtr(rffi.INT)
        a = lltype.malloc(TP.TO, 5, flavor='raw')
        a[0] = 5
        a[1] = 3
        a[2] = 2
        a[3] = 1
        a[4] = 4

        def compare(a, b):
            if a[0] > b[0]:
                return 1
            else:
                return -1

        CALLBACK = rffi.CCallback([rffi.VOIDP, rffi.VOIDP], rffi.INT)
        qsort = rffi.llexternal('qsort',
                                [rffi.VOIDP, rffi.INT, rffi.INT, CALLBACK],
                                lltype.Void)

        qsort(rffi.cast(rffi.VOIDP, a), 5, rffi.sizeof(rffi.INT), compare)
        for i in range(5):
            assert a[i] == i + 1
Beispiel #4
0
    def test_c_callback(self):
        c_source = py.code.Source("""
        int eating_callback(int arg, int(*call)(int))
        {
            return call(arg);
        }
        """)

        eci = ExternalCompilationInfo(separate_module_sources=[c_source])

        args = [rffi.INT, rffi.CCallback([rffi.INT], rffi.INT)]
        eating_callback = rffi.llexternal('eating_callback',
                                          args,
                                          rffi.INT,
                                          compilation_info=eci)

        def g(i):
            return i + 3

        def f():
            return eating_callback(3, g)

        assert f() == 6
Beispiel #5
0
    return res


# ------------------------------------------------------------------------------
# numbers
# ------------------------------------------------------------------------------

JSValueToNumber = _can_raise_wrapper('JSValueToNumber',
                                     [JSContextRef, JSValueRef], rffi.DOUBLE)

# ------------------------------------------------------------------------------
# callbacks
# ------------------------------------------------------------------------------

_JSCallback = rffi.CCallback([
    JSContextRef, JSObjectRef, JSObjectRef, rffi.INT, JSValueRefP, JSValueRefP
], JSValueRef)
# ctx, function, this, argcount, args, exc
JSObjectMakeFunctionWithCallback = external(
    'JSObjectMakeFunctionWithCallback',
    [JSContextRef, JSStringRef, _JSCallback], JSObjectRef)


def create_js_callback(callable):
    def js_callback(ctx, function, this, argcount, ll_args, exc):
        try:
            args = [ll_args[i] for i in range(argcount)]
            return callable(ctx, function, this, args)
        except:
            exc[0] = JSValueMakeString(
                ctx, JSStringCreateWithUTF8CString('python exception'))
Beispiel #6
0
FFI_BAD_TYPEDEF = cConfig.FFI_BAD_TYPEDEF
FFI_DEFAULT_ABI = rffi.cast(rffi.USHORT, cConfig.FFI_DEFAULT_ABI)
if _MS_WINDOWS:
    FFI_STDCALL = rffi.cast(rffi.USHORT, cConfig.FFI_STDCALL)
FFI_TYPE_STRUCT = rffi.cast(rffi.USHORT, cConfig.FFI_TYPE_STRUCT)
FFI_CIFP = rffi.COpaquePtr('ffi_cif', compilation_info=eci)

FFI_CLOSUREP = lltype.Ptr(cConfig.ffi_closure)

VOIDPP = rffi.CArrayPtr(rffi.VOIDP)

c_ffi_prep_cif = external('ffi_prep_cif', [FFI_CIFP, rffi.USHORT, rffi.UINT,
                                           FFI_TYPE_P, FFI_TYPE_PP], rffi.INT)
c_ffi_call = external('ffi_call', [FFI_CIFP, rffi.VOIDP, rffi.VOIDP,
                                   VOIDPP], lltype.Void)
CALLBACK_TP = rffi.CCallback([FFI_CIFP, rffi.VOIDP, rffi.VOIDPP, rffi.VOIDP],
                             lltype.Void)
c_ffi_prep_closure = external('ffi_prep_closure', [FFI_CLOSUREP, FFI_CIFP,
                                                   CALLBACK_TP, rffi.VOIDP],
                              rffi.INT)            

def make_struct_ffitype(size, aligment):
    tp = lltype.malloc(FFI_TYPE_P.TO, flavor='raw')
    tp.c_type = FFI_TYPE_STRUCT
    tp.c_size = rffi.cast(rffi.SIZE_T, size)
    tp.c_alignment = rffi.cast(rffi.USHORT, aligment)
    tp.c_elements = lltype.nullptr(FFI_TYPE_PP.TO)
    return tp

def cast_type_to_ffitype(tp):
    """ This function returns ffi representation of rpython type tp
    """