Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: fixed build on Windows (#7)
* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: fixed build on Windows

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

* test: removed useless file

Signed-off-by: Paolo Insogna <paolo@cowtech.it>

---------

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
  • Loading branch information
ShogunPanda committed Apr 11, 2026
commit 4b17d600748803e0f93ba5c3a0e7ffc4fd09ed4c
9 changes: 0 additions & 9 deletions test/ffi/fixture_library/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
'target_name': 'ffi_test_library',
'sources': ['ffi_test_library.c'],
'type': 'shared_library',
'conditions': [
[ 'OS=="win"', {
'msvs_settings': {
'VCLinkerTool': {
'ModuleDefinitionFile': 'ffi_test_library.def',
},
},
}],
],
}
]
}
127 changes: 68 additions & 59 deletions test/ffi/fixture_library/ffi_test_library.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,93 @@
#include <stdlib.h>
#include <string.h>

#ifdef _WIN32
#define FFI_EXPORT __declspec(dllexport)
#else
#define FFI_EXPORT
#endif

// Integer operations.

int8_t add_i8(int8_t a, int8_t b) {
FFI_EXPORT int8_t add_i8(int8_t a, int8_t b) {
return a + b;
}

uint8_t add_u8(uint8_t a, uint8_t b) {
FFI_EXPORT uint8_t add_u8(uint8_t a, uint8_t b) {
return a + b;
}

int16_t add_i16(int16_t a, int16_t b) {
FFI_EXPORT int16_t add_i16(int16_t a, int16_t b) {
return a + b;
}

uint16_t add_u16(uint16_t a, uint16_t b) {
FFI_EXPORT uint16_t add_u16(uint16_t a, uint16_t b) {
return a + b;
}

int32_t add_i32(int32_t a, int32_t b) {
FFI_EXPORT int32_t add_i32(int32_t a, int32_t b) {
return a + b;
}

uint32_t add_u32(uint32_t a, uint32_t b) {
FFI_EXPORT uint32_t add_u32(uint32_t a, uint32_t b) {
return a + b;
}

int64_t add_i64(int64_t a, int64_t b) {
FFI_EXPORT int64_t add_i64(int64_t a, int64_t b) {
return a + b;
}

uint64_t add_u64(uint64_t a, uint64_t b) {
FFI_EXPORT uint64_t add_u64(uint64_t a, uint64_t b) {
return a + b;
}

char identity_char(char value) {
FFI_EXPORT char identity_char(char value) {
return value;
}

int32_t char_is_signed(void) {
FFI_EXPORT int32_t char_is_signed(void) {
return ((char)-1) < 0;
}

// Floating point operations.

float add_f32(float a, float b) {
FFI_EXPORT float add_f32(float a, float b) {
return a + b;
}

float multiply_f32(float a, float b) {
FFI_EXPORT float multiply_f32(float a, float b) {
return a * b;
}

double add_f64(double a, double b) {
FFI_EXPORT double add_f64(double a, double b) {
return a + b;
}

double multiply_f64(double a, double b) {
FFI_EXPORT double multiply_f64(double a, double b) {
return a * b;
}

// Pointer operations.

void* identity_pointer(void* ptr) {
FFI_EXPORT void* identity_pointer(void* ptr) {
return ptr;
}

uint64_t pointer_to_usize(void* ptr) {
FFI_EXPORT uint64_t pointer_to_usize(void* ptr) {
return (uint64_t)(uintptr_t)ptr;
}

void* usize_to_pointer(uint64_t addr) {
FFI_EXPORT void* usize_to_pointer(uint64_t addr) {
return (void*)(uintptr_t)addr;
}

// String operations.

uint64_t string_length(const char* str) {
FFI_EXPORT uint64_t string_length(const char* str) {
return str ? strlen(str) : 0;
}

char* string_concat(const char* a, const char* b) {
FFI_EXPORT char* string_concat(const char* a, const char* b) {
if (!a || !b) {
return NULL;
}
Expand All @@ -101,7 +107,7 @@ char* string_concat(const char* a, const char* b) {
return result;
}

char* string_duplicate(const char* str) {
FFI_EXPORT char* string_duplicate(const char* str) {
if (!str) {
return NULL;
}
Expand All @@ -117,21 +123,21 @@ char* string_duplicate(const char* str) {
return result;
}

void free_string(char* str) {
FFI_EXPORT void free_string(char* str) {
free(str);
}

// Buffer/Array operations.

void fill_buffer(uint8_t* buffer, uint64_t length, uint32_t value) {
FFI_EXPORT void fill_buffer(uint8_t* buffer, uint64_t length, uint32_t value) {
if (!buffer) {
return;
}

memset(buffer, (uint8_t)value, length);
}

uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) {
FFI_EXPORT uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) {
if (!buffer) {
return 0;
}
Expand All @@ -143,7 +149,7 @@ uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) {
return sum;
}

void reverse_buffer(uint8_t* buffer, uint64_t length) {
FFI_EXPORT void reverse_buffer(uint8_t* buffer, uint64_t length) {
if (!buffer || length == 0) {
return;
}
Expand All @@ -168,53 +174,53 @@ typedef struct {
float z;
} Point3D;

Point make_point(int32_t x, int32_t y) {
FFI_EXPORT Point make_point(int32_t x, int32_t y) {
Point p = {x, y};
return p;
}

int32_t point_distance_squared(Point p1, Point p2) {
FFI_EXPORT int32_t point_distance_squared(Point p1, Point p2) {
int32_t dx = p1.x - p2.x;
int32_t dy = p1.y - p2.y;
return dx * dx + dy * dy;
}

Point3D make_point3d(float x, float y, float z) {
FFI_EXPORT Point3D make_point3d(float x, float y, float z) {
Point3D p = {x, y, z};
return p;
}

float point3d_magnitude_squared(Point3D p) {
FFI_EXPORT float point3d_magnitude_squared(Point3D p) {
return p.x * p.x + p.y * p.y + p.z * p.z;
}

// Boolean operations.

int32_t logical_and(int32_t a, int32_t b) {
FFI_EXPORT int32_t logical_and(int32_t a, int32_t b) {
return (a && b) ? 1 : 0;
}

int32_t logical_or(int32_t a, int32_t b) {
FFI_EXPORT int32_t logical_or(int32_t a, int32_t b) {
return (a || b) ? 1 : 0;
}

int32_t logical_not(int32_t a) {
FFI_EXPORT int32_t logical_not(int32_t a) {
return !a ? 1 : 0;
}

// Void operations (side effects).

static int32_t global_counter = 0;

void increment_counter(void) {
FFI_EXPORT void increment_counter(void) {
global_counter++;
}

int32_t get_counter(void) {
FFI_EXPORT int32_t get_counter(void) {
return global_counter;
}

void reset_counter(void) {
FFI_EXPORT void reset_counter(void) {
global_counter = 0;
}

Expand All @@ -227,45 +233,45 @@ typedef void (*VoidCallback)(void);
typedef void (*StringCallback)(const char*);
typedef int32_t (*BinaryIntCallback)(int32_t, int32_t);

int32_t call_int_callback(IntCallback callback, int32_t value) {
FFI_EXPORT int32_t call_int_callback(IntCallback callback, int32_t value) {
if (!callback) {
return -1;
}

return callback(value);
}

int8_t call_int8_callback(Int8Callback callback, int8_t value) {
FFI_EXPORT int8_t call_int8_callback(Int8Callback callback, int8_t value) {
if (!callback) {
return 0;
}

return callback(value);
}

int32_t call_pointer_callback_is_null(PointerCallback callback) {
FFI_EXPORT int32_t call_pointer_callback_is_null(PointerCallback callback) {
if (!callback) {
return 1;
}

return callback() == NULL;
}

void call_void_callback(VoidCallback callback) {
FFI_EXPORT void call_void_callback(VoidCallback callback) {
if (callback) {
callback();
}
}

void call_string_callback(StringCallback callback, const char* str) {
FFI_EXPORT void call_string_callback(StringCallback callback, const char* str) {
if (callback) {
callback(str);
}
}

int32_t call_binary_int_callback(BinaryIntCallback callback,
int32_t a,
int32_t b) {
FFI_EXPORT int32_t call_binary_int_callback(BinaryIntCallback callback,
int32_t a,
int32_t b) {
if (!callback) {
return -1;
}
Expand All @@ -274,7 +280,8 @@ int32_t call_binary_int_callback(BinaryIntCallback callback,
}

// Callback that calls multiple times.
void call_callback_multiple_times(IntCallback callback, int32_t times) {
FFI_EXPORT void call_callback_multiple_times(IntCallback callback,
int32_t times) {
if (!callback) {
return;
}
Expand All @@ -286,78 +293,80 @@ void call_callback_multiple_times(IntCallback callback, int32_t times) {

// Edge cases and error conditions.

int32_t divide_i32(int32_t a, int32_t b) {
FFI_EXPORT int32_t divide_i32(int32_t a, int32_t b) {
if (b == 0) {
return 0;
}

return a / b;
}

void* allocate_memory(size_t size) {
FFI_EXPORT void* allocate_memory(size_t size) {
return malloc(size);
}

void deallocate_memory(void* ptr) {
FFI_EXPORT void deallocate_memory(void* ptr) {
free(ptr);
}

// Null pointer handling.
int32_t safe_strlen(const char* str) {
FFI_EXPORT int32_t safe_strlen(const char* str) {
return str ? (int32_t)strlen(str) : -1;
}

// Multi-parameter functions.

int32_t sum_five_i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e) {
FFI_EXPORT int32_t
sum_five_i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e) {
return a + b + c + d + e;
}

double sum_five_f64(double a, double b, double c, double d, double e) {
FFI_EXPORT double sum_five_f64(
double a, double b, double c, double d, double e) {
return a + b + c + d + e;
}

// Mixed parameter types.

double mixed_operation(int32_t i, float f, double d, uint32_t u) {
FFI_EXPORT double mixed_operation(int32_t i, float f, double d, uint32_t u) {
return (double)i + (double)f + d + (double)u;
}

// Constant values for testing.

const int32_t CONSTANT_I32 = 42;
const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL;
const float CONSTANT_F32 = 3.14159f;
const double CONSTANT_F64 = 2.718281828459045;
const char* CONSTANT_STRING = "Hello from FFI addon";
FFI_EXPORT const int32_t CONSTANT_I32 = 42;
FFI_EXPORT const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL;
FFI_EXPORT const float CONSTANT_F32 = 3.14159f;
FFI_EXPORT const double CONSTANT_F64 = 2.718281828459045;
FFI_EXPORT const char* CONSTANT_STRING = "Hello from FFI addon";

// Array/pointer indexing.

int32_t array_get_i32(const int32_t* arr, size_t index) {
FFI_EXPORT int32_t array_get_i32(const int32_t* arr, size_t index) {
if (!arr) {
return 0;
}

return arr[index];
}

void array_set_i32(int32_t* arr, size_t index, int32_t value) {
FFI_EXPORT void array_set_i32(int32_t* arr, size_t index, int32_t value) {
if (!arr) {
return;
}

arr[index] = value;
}

double array_get_f64(const double* arr, size_t index) {
FFI_EXPORT double array_get_f64(const double* arr, size_t index) {
if (!arr) {
return 0.0;
}

return arr[index];
}

void array_set_f64(double* arr, size_t index, double value) {
FFI_EXPORT void array_set_f64(double* arr, size_t index, double value) {
if (!arr) {
return;
}
Expand Down
Loading