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

Tests reverse trampolines (callbacks) with primitive type signatures. More...

#include "common/double_tap.h"
#include <infix/infix.h>
#include <math.h>
#include <string.h>
Include dependency graph for 301_primitives.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

int int_callback_handler (int a, int b)
 A simple callback handler that multiplies two integers.
 
float float_callback_handler (float a, float b)
 A callback handler that adds two floats.
 
void void_callback_handler (int check_val)
 A callback handler with a void return type that checks its argument.
 
void int_closure_handler (infix_context_t *context, void *return_value, void **args)
 
void float_closure_handler (infix_context_t *context, void *return_value, void **args)
 
void void_closure_handler (infix_context_t *context, void *return_value, void **args)
 
void execute_int_callback (int(*func_ptr)(int, int), int x, int y)
 
void execute_float_callback (float(*func_ptr)(float, float), float a, float b)
 
void execute_void_callback (void(*func_ptr)(int), int val)
 
 subtest ("Callback with signature: int(int, int)")
 
 subtest ("Callback with signature: float(float, float)")
 
 subtest ("Callback with signature: void(int)")
 

Variables

 TEST
 

Detailed Description

Tests reverse trampolines (callbacks) with primitive type signatures.

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 is the cornerstone for verifying the reverse FFI (callback) functionality. It ensures that the library can correctly generate native, callable function pointers for user-defined handlers that operate on primitive C types.

For each signature, the test performs these steps:

  1. Defines a C "handler" function (e.g., int_callback_handler).
  2. Creates the infix_type definitions for the handler's public signature.
  3. Calls the appropriate infix_reverse_create_*_manual function to create a native function pointer.
  4. Defines a C "harness" function that takes a function pointer of the native type as an argument.
  5. Calls the harness, passing it the generated function pointer.
  6. Inside the harness, the generated function pointer is called. This triggers the JIT-compiled assembly stub, which marshals the arguments and invokes the original C handler via the correct path (type-safe or generic).
  7. The test asserts that the value returned by the callback is correct.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ execute_float_callback()

void execute_float_callback ( float(*)(float, float)  func_ptr,
float  a,
float  b 
)

◆ execute_int_callback()

void execute_int_callback ( int(*)(int, int)  func_ptr,
int  x,
int  y 
)

◆ execute_void_callback()

void execute_void_callback ( void(*)(int)  func_ptr,
int  val 
)

◆ float_callback_handler()

float float_callback_handler ( float  a,
float  b 
)

A callback handler that adds two floats.

◆ float_closure_handler()

void float_closure_handler ( infix_context_t context,
void *  return_value,
void **  args 
)

◆ int_callback_handler()

int int_callback_handler ( int  a,
int  b 
)

A simple callback handler that multiplies two integers.

◆ int_closure_handler()

void int_closure_handler ( infix_context_t context,
void *  return_value,
void **  args 
)

◆ subtest() [1/3]

subtest ( "Callback with signature: float(float, float)"  )

◆ subtest() [2/3]

subtest ( "Callback with signature: int(int, int)"  )

◆ subtest() [3/3]

subtest ( "Callback with signature: void(int)"  )

◆ void_callback_handler()

void void_callback_handler ( int  check_val)

A callback handler with a void return type that checks its argument.

◆ void_closure_handler()

void void_closure_handler ( infix_context_t context,
void *  return_value,
void **  args 
)

Variable Documentation

◆ TEST

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