pyobjc-tc.c 2.4 KB

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