def test_byval_argument(self): """ struct Point { Signed x; Signed y; }; RPY_EXPORTED Signed sum_point(struct Point p) { return p.x + p.y; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e(0, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct sum_point = (libfoo, 'sum_point', [ffi_point], types.signed) # ARRAY = rffi.CArray(rffi.SIGNED) buf = lltype.malloc(ARRAY, 2, flavor='raw') buf[0] = 30 buf[1] = 12 adr = rffi.cast(rffi.VOIDP, buf) res = self.call(sum_point, [('arg_raw', adr)], rffi.SIGNED, jitif=["byval"]) assert res == 42 # check that we still have the ownership on the buffer assert buf[0] == 30 assert buf[1] == 12 lltype.free(buf, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')
def test_byval_result(self): """ RPY_EXPORTED struct Point make_point(Signed x, Signed y) { struct Point p; p.x = x; p.y = y; return p; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e( rffi.sizeof(rffi.SIGNED) * 2, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct libfoo = CDLL(self.libfoo_name) make_point = (libfoo, 'make_point', [types.signed, types.signed], ffi_point) # PTR = lltype.Ptr(rffi.CArray(rffi.SIGNED)) p = self.call(make_point, [12, 34], PTR, is_struct=True, jitif=["byval"]) assert p[0] == 12 assert p[1] == 34 lltype.free(p, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')
def test_byval_result(self): """ struct Point make_point(Signed x, Signed y) { struct Point p; p.x = x; p.y = y; return p; } """ libfoo = CDLL(self.libfoo_name) ffi_point_struct = make_struct_ffitype_e(0, 0, [types.signed, types.signed]) ffi_point = ffi_point_struct.ffistruct libfoo = CDLL(self.libfoo_name) make_point = (libfoo, 'make_point', [types.signed, types.signed], ffi_point) # PTR = lltype.Ptr(rffi.CArray(rffi.SIGNED)) p = self.call(make_point, [12, 34], PTR, is_struct=True, jitif=["byval"]) assert p[0] == 12 assert p[1] == 34 lltype.free(p, flavor='raw') lltype.free(ffi_point_struct, flavor='raw')