| 1 |
import math |
| 2 |
import ctypes |
| 3 |
import sys |
| 4 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 5 |
from rpython.rlib import clibffi |
| 6 |
from rpython.rlib.rarithmetic import intmask |
| 7 |
from rpython.rlib.jit_libffi import CIF_DESCRIPTION |
| 8 |
from rpython.rlib.jit_libffi import jit_ffi_prep_cif, jit_ffi_call |
| 9 |
|
| 10 |
if sys.platform == 'win32': |
| 11 |
math_sin = intmask(ctypes.cast(ctypes.cdll.msvcrt.sin, ctypes.c_void_p).value) |
| 12 |
else: |
| 13 |
math_sin = intmask(ctypes.cast(ctypes.CDLL(None).sin, ctypes.c_void_p).value) |
| 14 |
math_sin = rffi.cast(rffi.VOIDP, math_sin) |
| 15 |
|
| 16 |
|
| 17 |
def test_jit_ffi_call(): |
| 18 |
cd = lltype.malloc(CIF_DESCRIPTION, 1, flavor='raw') |
| 19 |
cd.abi = clibffi.FFI_DEFAULT_ABI |
| 20 |
cd.nargs = 1 |
| 21 |
cd.rtype = clibffi.cast_type_to_ffitype(rffi.DOUBLE) |
| 22 |
atypes = lltype.malloc(clibffi.FFI_TYPE_PP.TO, 1, flavor='raw') |
| 23 |
atypes[0] = clibffi.cast_type_to_ffitype(rffi.DOUBLE) |
| 24 |
cd.atypes = atypes |
| 25 |
cd.exchange_size = 64 |
| 26 |
cd.exchange_result = 24 |
| 27 |
cd.exchange_args[0] = 16 |
| 28 |
|
| 29 |
jit_ffi_prep_cif(cd) |
| 30 |
|
| 31 |
assert rffi.sizeof(rffi.DOUBLE) == 8 |
| 32 |
exb = lltype.malloc(rffi.DOUBLEP.TO, 8, flavor='raw') |
| 33 |
exb[2] = 1.23 |
| 34 |
jit_ffi_call(cd, math_sin, rffi.cast(rffi.CCHARP, exb)) |
| 35 |
res = exb[3] |
| 36 |
lltype.free(exb, flavor='raw') |
| 37 |
|
| 38 |
lltype.free(atypes, flavor='raw') |
| 39 |
lltype.free(cd, flavor='raw') |
| 40 |
|
| 41 |
assert res == math.sin(1.23) |