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

Tests passing and returning C unions by value. More...

#include "common/double_tap.h"
#include "types.h"
#include <infix/infix.h>
#include <math.h>
#include <string.h>
Include dependency graph for 103_unions.c:

Macros

#define DBLTAP_IMPLEMENTATION
 

Functions

int process_number_union_as_int (Number num)
 Receives a Number union and processes it as an integer.
 
float process_number_union_as_float (Number num)
 Receives a Number union and processes it as a float.
 
Number return_number_union (int selector)
 
 if (!ok(status==INFIX_SUCCESS, "infix_type for Number union created successfully"))
 
 subtest ("Passing union as argument")
 
 subtest ("Returning union by value")
 
 infix_arena_destroy (arena)
 

Variables

 TEST
 
infix_arena_tarena = infix_arena_create(4096)
 
infix_struct_membermembers
 
infix_typeunion_type = nullptr
 
infix_status status = infix_type_create_union(arena, &union_type, members, 2)
 

Detailed Description

Tests passing and returning C unions by value.

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 that the library correctly handles C union types according to the target platform's ABI. It ensures that the size and alignment are calculated correctly and that the data is placed in the appropriate registers or stack locations.

The suite covers two primary scenarios:

  1. Passing a Union: A Number union is passed to native functions that interpret its contents as either an int or a double. This implicitly verifies the ABI classification rules (e.g., on System V x64, this union is passed in an XMM register, while on Windows x64 it's passed in a GPR).
  2. Returning a Union: A native function returns a Number union by value, and the test verifies that the returned data is correct.

Macro Definition Documentation

◆ DBLTAP_IMPLEMENTATION

#define DBLTAP_IMPLEMENTATION

Function Documentation

◆ if()

if ( okstatus==INFIX_SUCCESS, "infix_type for Number union created successfully")

◆ infix_arena_destroy()

infix_arena_destroy ( arena  )

◆ process_number_union_as_float()

float process_number_union_as_float ( Number  num)

Receives a Number union and processes it as a float.

◆ process_number_union_as_int()

int process_number_union_as_int ( Number  num)

Receives a Number union and processes it as an integer.

◆ return_number_union()

Number return_number_union ( int  selector)

◆ subtest() [1/2]

subtest ( "Passing union as argument"  )

◆ subtest() [2/2]

subtest ( "Returning union by value"  )

Variable Documentation

◆ arena

◆ members

members[1]
Initial value:
=
infix_arena_t * arena
Definition 103_unions.c:65
c23_nodiscard void * infix_arena_alloc(infix_arena_t *, size_t, size_t)
Allocates a block of memory from the arena with a specific alignment.
Definition arena.c:86
Describes a single member of an aggregate type (struct or union).
Definition infix.h:219

◆ status

◆ TEST

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

◆ union_type

infix_type* union_type = nullptr