Ruby 2.7.7p221 (2022-11-24 revision 168ec2b1e5ad0e4688e963d9de019557c78feed9)
cls_double_va.c
Go to the documentation of this file.
1/* Area: ffi_call, closure_call
2 Purpose: Test doubles passed in variable argument lists.
3 Limitations: none.
4 PR: none.
5 Originator: Blake Chaffin 6/6/2007 */
6
7/* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */
8/* { dg-output "" { xfail avr32*-*-* } } */
9/* { dg-output "" { xfail mips-sgi-irix6* } } PR libffi/46660 */
10
11#include "ffitest.h"
12
13static void
14cls_double_va_fn(ffi_cif* cif __UNUSED__, void* resp,
15 void** args, void* userdata __UNUSED__)
16{
17 char* format = *(char**)args[0];
18 double doubleValue = *(double*)args[1];
19
20 *(ffi_arg*)resp = printf(format, doubleValue);
21}
22
23int main (void)
24{
25 ffi_cif cif;
26 void *code;
27 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
28 void* args[3];
29 ffi_type* arg_types[3];
30
31 char* format = "%.1f\n";
32 double doubleArg = 7;
33 ffi_arg res = 0;
34
35 arg_types[0] = &ffi_type_pointer;
36 arg_types[1] = &ffi_type_double;
37 arg_types[2] = NULL;
38
39 /* This printf call is variadic */
40 CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 2, &ffi_type_sint,
41 arg_types) == FFI_OK);
42
43 args[0] = &format;
44 args[1] = &doubleArg;
45 args[2] = NULL;
46
47 ffi_call(&cif, FFI_FN(printf), &res, args);
48 /* { dg-output "7.0" } */
49 printf("res: %d\n", (int) res);
50 /* { dg-output "\nres: 4" } */
51
52 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_double_va_fn, NULL,
53 code) == FFI_OK);
54
55 res = ((int(*)(char*, ...))(code))(format, doubleArg);
56 /* { dg-output "\n7.0" } */
57 printf("res: %d\n", (int) res);
58 /* { dg-output "\nres: 4" } */
59
60 exit(0);
61}
ffi_status ffi_prep_closure_loc(ffi_closure *closure, ffi_cif *cif, void(*fun)(ffi_cif *, void *, void **, void *), void *user_data, void *codeloc)
Definition: ffi.c:928
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
int main(void)
Definition: cls_double_va.c:23
#define CHECK(sub)
Definition: compile.c:448
#define __UNUSED__
Definition: ffitest.h:28
ffi_status ffi_prep_cif_var(ffi_cif *cif, ffi_abi abi, unsigned int nfixedargs, unsigned int ntotalargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:232
#define NULL
int int int printf(const char *__restrict__,...) __attribute__((__format__(__printf__
void exit(int __status) __attribute__((__noreturn__))
__inline__ int