infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
304_reverse_call_types.c File Reference

Unit test to explicitly compare the "Callback" vs. "Closure" reverse trampoline APIs. More...

#include "common/double_tap.h"
#include <infix/infix.h>
#include <string.h>
Include dependency graph for 304_reverse_call_types.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

void harness (int(*func_ptr)(int, int), int a, int b, int expected)
 
int handler_callback (int a, int b)
 
void handler_closure (infix_context_t *ctx, void *ret, void **args)
 
 subtest ("Reverse Trampoline API: Callback vs. Closure")
 

Variables

 TEST
 

Detailed Description

Unit test to explicitly compare the "Callback" vs. "Closure" reverse trampoline APIs.

This test serves as a clear, side-by-side demonstration of the two main models for creating reverse trampolines in infix. Both models are functionally equivalent from the perspective of the C code that calls the generated function pointer, but they offer different interfaces to the developer using the infix library.

  1. **infix_reverse_create_callback**:
    • Handler: A native, type-safe C function (e.g., int handler(int, int)).
    • Pros: Easy to use from C/C++, compile-time type checking, potentially higher performance.
    • Cons: Less flexible, stateless by default.
  2. **infix_reverse_create_closure**:
    • Handler: A generic function (infix_closure_handler_fn) that receives arguments as a void** array.
    • Pros: Highly flexible, ideal for language bindings, supports stateful callbacks via user_data.
    • Cons: Requires manual argument unpacking, lacks compile-time type safety.

This test creates a JIT-compiled function pointer using both APIs for the same signature ((int, int) -> int) and verifies that both can be called from a C harness function and produce the correct result.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ handler_callback()

int handler_callback ( int  a,
int  b 
)

◆ handler_closure()

void handler_closure ( infix_context_t ctx,
void *  ret,
void **  args 
)

◆ harness()

void harness ( int(*)(int, int)  func_ptr,
int  a,
int  b,
int  expected 
)

◆ subtest()

subtest ( "Reverse Trampoline API: Callback vs. Closure"  )

Variable Documentation

◆ TEST

TEST
Initial value:
{
plan(1)
#define plan(count)
Definition double_tap.h:154