Ruby: ext/fiddle/libffi-3.2.1/testsuite/libffi.call/pyobjc-tc.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

pyobjc-tc.c
Go to the documentation of this file.
1/* Area: ffi_call
2 Purpose: Check different structures.
3 Limitations: none.
4 PR: none.
5 Originator: Ronald Oussoren <oussoren@cistron.nl> 20030824 */
6
7/* { dg-do run } */
8#include "ffitest.h"
9
10 typedef struct Point {
11 float x;
12 float y;
13} Point;
14
15 typedef struct Size {
16 float h;
17 float w;
18} Size;
19
20 typedef struct Rect {
21 Point o;
22 Size s;
23} Rect;
24
25 int doit(int o, char* s, Point p, Rect r, int last)
26{
27 printf("CALLED WITH %d %s {%f %f} {{%f %f} {%f %f}} %d\n",
28 o, s, p.x, p.y, r.o.x, r.o.y, r.s.h, r.s.w, last);
29 return 42;
30}
31
32
33 int main(void)
34{
35 ffi_type point_type;
36 ffi_type size_type;
37 ffi_type rect_type;
38 ffi_cif cif;
39 ffi_type* arglist[6];
40 void* values[6];
41 int r;
42
43 /*
44 * First set up FFI types for the 3 struct types
45 */
46
47 point_type.size = 0; /*sizeof(Point);*/
48 point_type.alignment = 0; /*__alignof__(Point);*/
49 point_type.type = FFI_TYPE_STRUCT;
50 point_type.elements = malloc(3 * sizeof(ffi_type*));
51 point_type.elements[0] = &ffi_type_float;
52 point_type.elements[1] = &ffi_type_float;
53 point_type.elements[2] = NULL;
54
55 size_type.size = 0;/* sizeof(Size);*/
56 size_type.alignment = 0;/* __alignof__(Size);*/
57 size_type.type = FFI_TYPE_STRUCT;
58 size_type.elements = malloc(3 * sizeof(ffi_type*));
59 size_type.elements[0] = &ffi_type_float;
60 size_type.elements[1] = &ffi_type_float;
61 size_type.elements[2] = NULL;
62
63 rect_type.size = 0;/*sizeof(Rect);*/
64 rect_type.alignment =0;/* __alignof__(Rect);*/
65 rect_type.type = FFI_TYPE_STRUCT;
66 rect_type.elements = malloc(3 * sizeof(ffi_type*));
67 rect_type.elements[0] = &point_type;
68 rect_type.elements[1] = &size_type;
69 rect_type.elements[2] = NULL;
70
71 /*
72 * Create a CIF
73 */
74 arglist[0] = &ffi_type_sint;
75 arglist[1] = &ffi_type_pointer;
76 arglist[2] = &point_type;
77 arglist[3] = &rect_type;
78 arglist[4] = &ffi_type_sint;
79 arglist[5] = NULL;
80
81 r = ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
82 5, &ffi_type_sint, arglist);
83 if (r != FFI_OK) {
84 abort();
85 }
86
87
88 /* And call the function through the CIF */
89
90 {
91 Point p = { 1.0, 2.0 };
92 Rect r = { { 9.0, 10.0}, { -1.0, -2.0 } };
93 int o = 0;
94 int l = 42;
95 char* m = "myMethod";
96 ffi_arg result;
97
98 values[0] = &o;
99 values[1] = &m;
100 values[2] = &p;
101 values[3] = &r;
102 values[4] = &l;
103 values[5] = NULL;
104
105 printf("CALLING WITH %d %s {%f %f} {{%f %f} {%f %f}} %d\n",
106 o, m, p.x, p.y, r.o.x, r.o.y, r.s.h, r.s.w, l);
107
108 ffi_call(&cif, FFI_FN(doit), &result, values);
109
110 printf ("The result is %d\n", (int)result);
111
112 }
113 exit(0);
114}
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
ffi_arg
unsigned long ffi_arg
Definition: ffitarget.h:30
FFI_DEFAULT_ABI
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
ffitest.h
last
unsigned int last
Definition: nkf.c:4324
ffi_prep_cif
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:226
doit
int doit(int o, char *s, Point p, Rect r, int last)
Definition: pyobjc-tc.c:25
main
int main(void)
Definition: pyobjc-tc.c:33
NULL
#define NULL
Definition: regenc.h:69
malloc
#define malloc
Definition: st.c:170
Point
Definition: pyobjc-tc.c:10
Point::x
float x
Definition: pyobjc-tc.c:11
Point::y
float y
Definition: pyobjc-tc.c:12
Rect
Definition: pyobjc-tc.c:20
Rect::o
Point o
Definition: pyobjc-tc.c:21
Rect::s
Size s
Definition: pyobjc-tc.c:22
Size
Definition: pyobjc-tc.c:15
Size::w
float w
Definition: pyobjc-tc.c:17
Size::h
float h
Definition: pyobjc-tc.c:16