|
infix
A JIT-Powered FFI Library for C
|
Unit test for passing structs that contain pointer members. More...
Macros | |
| #define | DBLTAP_IMPLEMENTATION |
Functions | |
| int | process_pointer_struct (PointerStruct ps) |
| A C function that takes a struct containing pointers by value. | |
| if (!ok(infix_type_create_struct(arena, &struct_type, members, 2)==INFIX_SUCCESS, "Type created")) | |
| ok (infix_forward_create_unbound_manual(&unbound_t, return_type, &struct_type, 1, 1)==INFIX_SUCCESS, "Unbound created") | |
| unbound_cif ((void *) process_pointer_struct, &unbound_result, args) | |
| ok (unbound_result==550, "Unbound call correct") | |
| ok (infix_forward_create_manual(&bound_t, return_type, &struct_type, 1, 1,(void *) process_pointer_struct)==INFIX_SUCCESS, "Bound created") | |
| ok (bound_result==550, "Bound call correct") | |
| infix_forward_destroy (unbound_t) | |
| infix_forward_destroy (bound_t) | |
| infix_arena_destroy (arena) | |
Variables | |
| TEST | |
| infix_arena_t * | arena = infix_arena_create(4096) |
| infix_struct_member * | members |
| infix_type * | struct_type = nullptr |
| infix_type * | return_type = infix_type_create_primitive(INFIX_PRIMITIVE_SINT32) |
| int | value_to_point_to = 500 |
| const char * | string_to_point_to = "Hello Pointers" |
| PointerStruct | struct_instance = {&value_to_point_to, string_to_point_to} |
| void * | args [] = {&struct_instance} |
| infix_forward_t * | unbound_t = nullptr |
| int | unbound_result = 0 |
| infix_unbound_cif_func | unbound_cif = infix_forward_get_unbound_code(unbound_t) |
| infix_forward_t * | bound_t = nullptr |
| int | bound_result = 0 |
| infix_cif_func | bound_cif = infix_forward_get_code(bound_t) |
Unit test for passing structs that contain pointer members.
This test case is an important follow-up to 201_basic.c. It verifies that infix can correctly handle structs that are passed by value, but which themselves contain pointer members.
The ABI classifier must correctly identify the struct as an aggregate to be passed according to the platform's rules (e.g., in registers or on the stack). The JIT code must then correctly copy the entire struct, including the pointer values within it.
The test defines a PointerStruct containing an int* and a const char*. It calls a C function that takes this struct by value, dereferences the pointers inside it, and verifies their contents. This confirms that the pointer values were correctly preserved through the FFI call.
| #define DBLTAP_IMPLEMENTATION |
| if | ( | ! | okinfix_type_create_struct(arena, &struct_type, members, 2)==INFIX_SUCCESS, "Type created" | ) |
| infix_arena_destroy | ( | arena | ) |
| infix_forward_destroy | ( | bound_t | ) |
| infix_forward_destroy | ( | unbound_t | ) |
| ok | ( | bound_result | = =550, |
| "Bound call correct" | |||
| ) |
| ok | ( | infix_forward_create_manual(&,, &, 1, 1, *void | process_pointer_struct | ) |
| ok | ( | infix_forward_create_unbound_manual &,, &, 1, | 1struct_type = =INFIX_SUCCESS, |
| "Unbound created" | |||
| ) |
| ok | ( | unbound_result | = =550, |
| "Unbound call correct" | |||
| ) |
| int process_pointer_struct | ( | PointerStruct | ps | ) |
A C function that takes a struct containing pointers by value.
The ABI will pass this struct according to its size and alignment. The function then dereferences the pointers to verify they were passed correctly.
| unbound_cif | ( | (void *) | process_pointer_struct, |
| & | unbound_result, | ||
| args | |||
| ) |
| infix_arena_t* arena = infix_arena_create(4096) |
| void* args[] = {&struct_instance} |
| infix_cif_func bound_cif = infix_forward_get_code(bound_t) |
| bound_cif & bound_result = 0 |
| infix_forward_t* bound_t = nullptr |
| members[1] |
| infix_type* return_type = infix_type_create_primitive(INFIX_PRIMITIVE_SINT32) |
| const char* string_to_point_to = "Hello Pointers" |
| PointerStruct struct_instance = {&value_to_point_to, string_to_point_to} |
| infix_type* struct_type = nullptr |
| infix_unbound_cif_func unbound_cif = infix_forward_get_unbound_code(unbound_t) |
| int unbound_result = 0 |
| infix_forward_t* unbound_t = nullptr |
| int value_to_point_to = 500 |