Ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a25176072e02db9254f0e0c84c805cd)
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
10typedef struct Point {
11 float x;
12 float y;
14
15typedef struct Size {
16 float h;
17 float w;
19
20typedef struct Rect {
24
25int 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
33int 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
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}
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
unsigned long ffi_arg
Definition: ffitarget.h:30
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
unsigned int last
Definition: nkf.c:4324
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
int doit(int o, char *s, Point p, Rect r, int last)
Definition: pyobjc-tc.c:25
struct Point Point
struct Rect Rect
struct Size Size
int main(void)
Definition: pyobjc-tc.c:33
#define NULL
void * malloc(size_t) __attribute__((__malloc__)) __attribute__((__warn_unused_result__)) __attribute__((__alloc_size__(1)))
int int int printf(const char *__restrict__,...) __attribute__((__format__(__printf__
void exit(int __status) __attribute__((__noreturn__))
void abort(void) __attribute__((__noreturn__))
float x
Definition: pyobjc-tc.c:11
float y
Definition: pyobjc-tc.c:12
Definition: pyobjc-tc.c:20
Point o
Definition: pyobjc-tc.c:21
Size s
Definition: pyobjc-tc.c:22
Definition: pyobjc-tc.c:15
float w
Definition: pyobjc-tc.c:17
float h
Definition: pyobjc-tc.c:16