def test_windll(self): if os.name != 'nt': py.test.skip('Run only on windows') from rpython.rlib.libffi import WinDLL dll = WinDLL('Kernel32.dll') sleep = dll.getpointer('Sleep', [types.uint], types.void) chain = ArgChain() chain.arg(10) sleep.call(chain, lltype.Void, is_struct=False)
def test_windll(self): if os.name != 'nt': py.test.skip('Run only on windows') from rpython.rlib.libffi import WinDLL dll = WinDLL('Kernel32.dll') sleep = dll.getpointer('Sleep',[types.uint], types.void) chain = ArgChain() chain.arg(10) sleep.call(chain, lltype.Void, is_struct=False)
def libffi_stuff(i, j): lib = CDLL(libm_name) func = lib.getpointer("fabs", [types.double], types.double) res = 0.0 x = float(j) while i > 0: jitdriver2.jit_merge_point(i=i, res=res, func=func, x=x) promote(func) argchain = ArgChain() argchain.arg(x) res = func.call(argchain, rffi.DOUBLE) i -= 1 return res
def libffi_stuff(i, j): lib = CDLL(libm_name) func = lib.getpointer('fabs', [types.double], types.double) res = 0.0 x = float(j) v2 = Virt2(i) while v2.i > 0: jitdriver2.jit_merge_point(v2=v2, res=res, func=func, x=x) promote(func) argchain = ArgChain() argchain.arg(x) res = func.call(argchain, rffi.DOUBLE) v2.i -= 1 return res
def test_argchain(self): chain = ArgChain() assert chain.numargs == 0 chain2 = chain.arg(42) assert chain2 is chain assert chain.numargs == 1 intarg = chain.first assert chain.last is intarg assert intarg.intval == 42 chain.arg(123.45) assert chain.numargs == 2 assert chain.first is intarg assert intarg.next is chain.last floatarg = intarg.next assert floatarg.floatval == 123.45
def test_wrong_args(self): # so far the test passes but for the wrong reason :-), i.e. because # .arg() only supports integers and floats chain = ArgChain() x = lltype.malloc(lltype.GcStruct('xxx')) y = lltype.malloc(lltype.GcArray(rffi.SIGNED), 3) z = lltype.malloc(lltype.Array(rffi.SIGNED), 4, flavor='raw') py.test.raises(TypeError, "chain.arg(x)") py.test.raises(TypeError, "chain.arg(y)") py.test.raises(TypeError, "chain.arg(z)") lltype.free(z, flavor='raw')
def call(self, funcspec, args, RESULT, is_struct=False, jitif=[]): """ Call the specified function after constructing and ArgChain with the arguments in ``args``. The function is specified with ``funcspec``, which is a tuple of the form (lib, name, argtypes, restype). This method is overridden by metainterp/test/test_fficall.py in order to do the call in a loop and JIT it. The optional arguments are used only by that overridden method. """ lib, name, argtypes, restype = funcspec func = lib.getpointer(name, argtypes, restype) chain = ArgChain() for arg in args: if isinstance(arg, tuple): methname, arg = arg meth = getattr(chain, methname) meth(arg) else: chain.arg(arg) return func.call(chain, RESULT, is_struct=is_struct)
def test_by_ordinal(self): """ RPY_EXPORTED int AAA_first_ordinal_function() { return 42; } """ libfoo = self.get_libfoo() f_by_name = libfoo.getpointer('AAA_first_ordinal_function', [], types.sint) f_by_ordinal = libfoo.getpointer_by_ordinal(1, [], types.sint) print dir(f_by_name) assert f_by_name.funcsym == f_by_ordinal.funcsym chain = ArgChain() assert 42 == f_by_ordinal.call(chain, rffi.INT, is_struct=False)
def test_by_ordinal2(self): """ RPY_EXPORTED int __stdcall BBB_second_ordinal_function() { return 24; } """ from rpython.rlib.libffi import WinDLL dll = WinDLL(self.libfoo_name) # __stdcall without a DEF file decorates the name with the number of bytes # that the callee will remove from the call stack f_by_name = dll.getpointer('_BBB_second_ordinal_function@0', [], types.uint) f_by_ordinal = dll.getpointer_by_ordinal(2, [], types.uint) print dir(f_by_name) assert f_by_name.funcsym == f_by_ordinal.funcsym chain = ArgChain() assert 24 == f_by_ordinal.call(chain, lltype.Signed, is_struct=False)
def test_by_ordinal2(self): """ RPY_EXPORTED int __stdcall BBB_second_ordinal_function() { return 24; } """ from rpython.rlib.libffi import WinDLL dll = WinDLL(self.libfoo_name) # __stdcall without a DEF file decorates the name with the number of bytes # that the callee will remove from the call stack # identical to __fastcall for amd64 if IS_32_BIT: f_name = '_BBB_second_ordinal_function@0' else: f_name = 'BBB_second_ordinal_function' f_by_name = dll.getpointer(f_name, [], types.sint) f_by_ordinal = dll.getpointer_by_ordinal(2, [], types.sint) print dir(f_by_name) assert f_by_name.funcsym == f_by_ordinal.funcsym chain = ArgChain() assert 24 == f_by_ordinal.call(chain, rffi.INT, is_struct=False)