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

Tests advanced reverse trampoline (callback) patterns. More...

#include "common/double_tap.h"
#include "types.h"
#include <infix/infix.h>
#include <string.h>
Include dependency graph for 303_advanced.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Typedefs

typedef int(* int_func_int) (int)
 A harness that receives the provider, calls it to get the real callback, and then calls that.
 
typedef int_func_int(* callback_provider) (void)
 

Functions

void pointer_modify_handler (int *p)
 Handler that dereferences a pointer and writes a new value.
 
void execute_pointer_modify_callback (void(*func_ptr)(int *), int *p)
 Harness that calls the provided function pointer, passing it an address.
 
void inner_callback_handler (int val)
 The inner callback that will be passed as an argument.
 
void execute_callback_as_arg_harness (void(*cb)(int))
 The harness function that accepts a function pointer as its argument.
 
int final_multiply_handler (int val)
 The innermost handler that will be returned and ultimately called.
 
void closure_provider_handler (infix_context_t *context, void *ret, void **args)
 A generic handler for the "provider" closure.
 
int call_returned_callback_harness (callback_provider provider, int val)
 
 subtest ("Callback modifies data via pointer")
 
 subtest ("Callback passed as an argument")
 
 subtest ("Closure returns a function pointer (via user_data)")
 

Variables

 TEST
 

Detailed Description

Tests advanced reverse trampoline (callback) patterns.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This test suite verifies the library's ability to handle complex, higher-order function patterns involving callbacks. It consolidates several previous tests to create a focused suite for advanced use cases.

The scenarios tested are:

  1. Pointer Modification: A callback receives a pointer to a variable in the caller's scope and modifies its value, a common pattern for "out" parameters.
  2. Callback as an Argument: A generated callback (reverse trampoline) is passed as a function pointer argument to a different function (called via a forward trampoline). This tests the interplay between the two FFI mechanisms.
  3. Closure Returning a Function Pointer: A closure is generated whose sole purpose is to return a different, dynamically-generated function pointer. This is achieved by storing the target function pointer in the user_data field of the reverse trampoline context, demonstrating a powerful feature for creating stateful, dynamic callback providers.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Typedef Documentation

◆ callback_provider

typedef int_func_int(* callback_provider) (void)

◆ int_func_int

typedef int(* int_func_int) (int)

A harness that receives the provider, calls it to get the real callback, and then calls that.

Function Documentation

◆ call_returned_callback_harness()

int call_returned_callback_harness ( callback_provider  provider,
int  val 
)

◆ closure_provider_handler()

void closure_provider_handler ( infix_context_t context,
void *  ret,
void **  args 
)

A generic handler for the "provider" closure.

This handler leverages user_data to return another function pointer. It retrieves the function pointer from its context and writes it to the return buffer.

◆ execute_callback_as_arg_harness()

void execute_callback_as_arg_harness ( void(*)(int)  cb)

The harness function that accepts a function pointer as its argument.

◆ execute_pointer_modify_callback()

void execute_pointer_modify_callback ( void(*)(int *)  func_ptr,
int *  p 
)

Harness that calls the provided function pointer, passing it an address.

◆ final_multiply_handler()

int final_multiply_handler ( int  val)

The innermost handler that will be returned and ultimately called.

◆ inner_callback_handler()

void inner_callback_handler ( int  val)

The inner callback that will be passed as an argument.

◆ pointer_modify_handler()

void pointer_modify_handler ( int *  p)

Handler that dereferences a pointer and writes a new value.

◆ subtest() [1/3]

subtest ( "Callback modifies data via pointer"  )

◆ subtest() [2/3]

subtest ( "Callback passed as an argument"  )

◆ subtest() [3/3]

subtest ( "Closure returns a function pointer (via user_data)"  )

Variable Documentation

◆ TEST

TEST
Initial value:
{
plan(3)
#define plan(count)
Definition double_tap.h:132