33#include <stdlib.h>
44#include <string.h>
55
6+ #ifdef _WIN32
7+ #define FFI_EXPORT __declspec(dllexport)
8+ #else
9+ #define FFI_EXPORT
10+ #endif
11+
612// Integer operations.
713
8- int8_t add_i8 (int8_t a , int8_t b ) {
14+ FFI_EXPORT int8_t add_i8 (int8_t a , int8_t b ) {
915 return a + b ;
1016}
1117
12- uint8_t add_u8 (uint8_t a , uint8_t b ) {
18+ FFI_EXPORT uint8_t add_u8 (uint8_t a , uint8_t b ) {
1319 return a + b ;
1420}
1521
16- int16_t add_i16 (int16_t a , int16_t b ) {
22+ FFI_EXPORT int16_t add_i16 (int16_t a , int16_t b ) {
1723 return a + b ;
1824}
1925
20- uint16_t add_u16 (uint16_t a , uint16_t b ) {
26+ FFI_EXPORT uint16_t add_u16 (uint16_t a , uint16_t b ) {
2127 return a + b ;
2228}
2329
24- int32_t add_i32 (int32_t a , int32_t b ) {
30+ FFI_EXPORT int32_t add_i32 (int32_t a , int32_t b ) {
2531 return a + b ;
2632}
2733
28- uint32_t add_u32 (uint32_t a , uint32_t b ) {
34+ FFI_EXPORT uint32_t add_u32 (uint32_t a , uint32_t b ) {
2935 return a + b ;
3036}
3137
32- int64_t add_i64 (int64_t a , int64_t b ) {
38+ FFI_EXPORT int64_t add_i64 (int64_t a , int64_t b ) {
3339 return a + b ;
3440}
3541
36- uint64_t add_u64 (uint64_t a , uint64_t b ) {
42+ FFI_EXPORT uint64_t add_u64 (uint64_t a , uint64_t b ) {
3743 return a + b ;
3844}
3945
40- char identity_char (char value ) {
46+ FFI_EXPORT char identity_char (char value ) {
4147 return value ;
4248}
4349
44- int32_t char_is_signed (void ) {
50+ FFI_EXPORT int32_t char_is_signed (void ) {
4551 return ((char )-1 ) < 0 ;
4652}
4753
4854// Floating point operations.
4955
50- float add_f32 (float a , float b ) {
56+ FFI_EXPORT float add_f32 (float a , float b ) {
5157 return a + b ;
5258}
5359
54- float multiply_f32 (float a , float b ) {
60+ FFI_EXPORT float multiply_f32 (float a , float b ) {
5561 return a * b ;
5662}
5763
58- double add_f64 (double a , double b ) {
64+ FFI_EXPORT double add_f64 (double a , double b ) {
5965 return a + b ;
6066}
6167
62- double multiply_f64 (double a , double b ) {
68+ FFI_EXPORT double multiply_f64 (double a , double b ) {
6369 return a * b ;
6470}
6571
6672// Pointer operations.
6773
68- void * identity_pointer (void * ptr ) {
74+ FFI_EXPORT void * identity_pointer (void * ptr ) {
6975 return ptr ;
7076}
7177
72- uint64_t pointer_to_usize (void * ptr ) {
78+ FFI_EXPORT uint64_t pointer_to_usize (void * ptr ) {
7379 return (uint64_t )(uintptr_t )ptr ;
7480}
7581
76- void * usize_to_pointer (uint64_t addr ) {
82+ FFI_EXPORT void * usize_to_pointer (uint64_t addr ) {
7783 return (void * )(uintptr_t )addr ;
7884}
7985
8086// String operations.
8187
82- uint64_t string_length (const char * str ) {
88+ FFI_EXPORT uint64_t string_length (const char * str ) {
8389 return str ? strlen (str ) : 0 ;
8490}
8591
86- char * string_concat (const char * a , const char * b ) {
92+ FFI_EXPORT char * string_concat (const char * a , const char * b ) {
8793 if (!a || !b ) {
8894 return NULL ;
8995 }
@@ -101,7 +107,7 @@ char* string_concat(const char* a, const char* b) {
101107 return result ;
102108}
103109
104- char * string_duplicate (const char * str ) {
110+ FFI_EXPORT char * string_duplicate (const char * str ) {
105111 if (!str ) {
106112 return NULL ;
107113 }
@@ -117,21 +123,21 @@ char* string_duplicate(const char* str) {
117123 return result ;
118124}
119125
120- void free_string (char * str ) {
126+ FFI_EXPORT void free_string (char * str ) {
121127 free (str );
122128}
123129
124130// Buffer/Array operations.
125131
126- void fill_buffer (uint8_t * buffer , uint64_t length , uint32_t value ) {
132+ FFI_EXPORT void fill_buffer (uint8_t * buffer , uint64_t length , uint32_t value ) {
127133 if (!buffer ) {
128134 return ;
129135 }
130136
131137 memset (buffer , (uint8_t )value , length );
132138}
133139
134- uint64_t sum_buffer (const uint8_t * buffer , uint64_t length ) {
140+ FFI_EXPORT uint64_t sum_buffer (const uint8_t * buffer , uint64_t length ) {
135141 if (!buffer ) {
136142 return 0 ;
137143 }
@@ -143,7 +149,7 @@ uint64_t sum_buffer(const uint8_t* buffer, uint64_t length) {
143149 return sum ;
144150}
145151
146- void reverse_buffer (uint8_t * buffer , uint64_t length ) {
152+ FFI_EXPORT void reverse_buffer (uint8_t * buffer , uint64_t length ) {
147153 if (!buffer || length == 0 ) {
148154 return ;
149155 }
@@ -168,53 +174,53 @@ typedef struct {
168174 float z ;
169175} Point3D ;
170176
171- Point make_point (int32_t x , int32_t y ) {
177+ FFI_EXPORT Point make_point (int32_t x , int32_t y ) {
172178 Point p = {x , y };
173179 return p ;
174180}
175181
176- int32_t point_distance_squared (Point p1 , Point p2 ) {
182+ FFI_EXPORT int32_t point_distance_squared (Point p1 , Point p2 ) {
177183 int32_t dx = p1 .x - p2 .x ;
178184 int32_t dy = p1 .y - p2 .y ;
179185 return dx * dx + dy * dy ;
180186}
181187
182- Point3D make_point3d (float x , float y , float z ) {
188+ FFI_EXPORT Point3D make_point3d (float x , float y , float z ) {
183189 Point3D p = {x , y , z };
184190 return p ;
185191}
186192
187- float point3d_magnitude_squared (Point3D p ) {
193+ FFI_EXPORT float point3d_magnitude_squared (Point3D p ) {
188194 return p .x * p .x + p .y * p .y + p .z * p .z ;
189195}
190196
191197// Boolean operations.
192198
193- int32_t logical_and (int32_t a , int32_t b ) {
199+ FFI_EXPORT int32_t logical_and (int32_t a , int32_t b ) {
194200 return (a && b ) ? 1 : 0 ;
195201}
196202
197- int32_t logical_or (int32_t a , int32_t b ) {
203+ FFI_EXPORT int32_t logical_or (int32_t a , int32_t b ) {
198204 return (a || b ) ? 1 : 0 ;
199205}
200206
201- int32_t logical_not (int32_t a ) {
207+ FFI_EXPORT int32_t logical_not (int32_t a ) {
202208 return !a ? 1 : 0 ;
203209}
204210
205211// Void operations (side effects).
206212
207213static int32_t global_counter = 0 ;
208214
209- void increment_counter (void ) {
215+ FFI_EXPORT void increment_counter (void ) {
210216 global_counter ++ ;
211217}
212218
213- int32_t get_counter (void ) {
219+ FFI_EXPORT int32_t get_counter (void ) {
214220 return global_counter ;
215221}
216222
217- void reset_counter (void ) {
223+ FFI_EXPORT void reset_counter (void ) {
218224 global_counter = 0 ;
219225}
220226
@@ -227,45 +233,45 @@ typedef void (*VoidCallback)(void);
227233typedef void (* StringCallback )(const char * );
228234typedef int32_t (* BinaryIntCallback )(int32_t , int32_t );
229235
230- int32_t call_int_callback (IntCallback callback , int32_t value ) {
236+ FFI_EXPORT int32_t call_int_callback (IntCallback callback , int32_t value ) {
231237 if (!callback ) {
232238 return -1 ;
233239 }
234240
235241 return callback (value );
236242}
237243
238- int8_t call_int8_callback (Int8Callback callback , int8_t value ) {
244+ FFI_EXPORT int8_t call_int8_callback (Int8Callback callback , int8_t value ) {
239245 if (!callback ) {
240246 return 0 ;
241247 }
242248
243249 return callback (value );
244250}
245251
246- int32_t call_pointer_callback_is_null (PointerCallback callback ) {
252+ FFI_EXPORT int32_t call_pointer_callback_is_null (PointerCallback callback ) {
247253 if (!callback ) {
248254 return 1 ;
249255 }
250256
251257 return callback () == NULL ;
252258}
253259
254- void call_void_callback (VoidCallback callback ) {
260+ FFI_EXPORT void call_void_callback (VoidCallback callback ) {
255261 if (callback ) {
256262 callback ();
257263 }
258264}
259265
260- void call_string_callback (StringCallback callback , const char * str ) {
266+ FFI_EXPORT void call_string_callback (StringCallback callback , const char * str ) {
261267 if (callback ) {
262268 callback (str );
263269 }
264270}
265271
266- int32_t call_binary_int_callback (BinaryIntCallback callback ,
267- int32_t a ,
268- int32_t b ) {
272+ FFI_EXPORT int32_t call_binary_int_callback (BinaryIntCallback callback ,
273+ int32_t a ,
274+ int32_t b ) {
269275 if (!callback ) {
270276 return -1 ;
271277 }
@@ -274,7 +280,8 @@ int32_t call_binary_int_callback(BinaryIntCallback callback,
274280}
275281
276282// Callback that calls multiple times.
277- void call_callback_multiple_times (IntCallback callback , int32_t times ) {
283+ FFI_EXPORT void call_callback_multiple_times (IntCallback callback ,
284+ int32_t times ) {
278285 if (!callback ) {
279286 return ;
280287 }
@@ -286,78 +293,80 @@ void call_callback_multiple_times(IntCallback callback, int32_t times) {
286293
287294// Edge cases and error conditions.
288295
289- int32_t divide_i32 (int32_t a , int32_t b ) {
296+ FFI_EXPORT int32_t divide_i32 (int32_t a , int32_t b ) {
290297 if (b == 0 ) {
291298 return 0 ;
292299 }
293300
294301 return a / b ;
295302}
296303
297- void * allocate_memory (size_t size ) {
304+ FFI_EXPORT void * allocate_memory (size_t size ) {
298305 return malloc (size );
299306}
300307
301- void deallocate_memory (void * ptr ) {
308+ FFI_EXPORT void deallocate_memory (void * ptr ) {
302309 free (ptr );
303310}
304311
305312// Null pointer handling.
306- int32_t safe_strlen (const char * str ) {
313+ FFI_EXPORT int32_t safe_strlen (const char * str ) {
307314 return str ? (int32_t )strlen (str ) : -1 ;
308315}
309316
310317// Multi-parameter functions.
311318
312- int32_t sum_five_i32 (int32_t a , int32_t b , int32_t c , int32_t d , int32_t e ) {
319+ FFI_EXPORT int32_t
320+ sum_five_i32 (int32_t a , int32_t b , int32_t c , int32_t d , int32_t e ) {
313321 return a + b + c + d + e ;
314322}
315323
316- double sum_five_f64 (double a , double b , double c , double d , double e ) {
324+ FFI_EXPORT double sum_five_f64 (
325+ double a , double b , double c , double d , double e ) {
317326 return a + b + c + d + e ;
318327}
319328
320329// Mixed parameter types.
321330
322- double mixed_operation (int32_t i , float f , double d , uint32_t u ) {
331+ FFI_EXPORT double mixed_operation (int32_t i , float f , double d , uint32_t u ) {
323332 return (double )i + (double )f + d + (double )u ;
324333}
325334
326335// Constant values for testing.
327336
328- const int32_t CONSTANT_I32 = 42 ;
329- const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL ;
330- const float CONSTANT_F32 = 3.14159f ;
331- const double CONSTANT_F64 = 2.718281828459045 ;
332- const char * CONSTANT_STRING = "Hello from FFI addon" ;
337+ FFI_EXPORT const int32_t CONSTANT_I32 = 42 ;
338+ FFI_EXPORT const uint64_t CONSTANT_U64 = 0xDEADBEEFCAFEBABEULL ;
339+ FFI_EXPORT const float CONSTANT_F32 = 3.14159f ;
340+ FFI_EXPORT const double CONSTANT_F64 = 2.718281828459045 ;
341+ FFI_EXPORT const char * CONSTANT_STRING = "Hello from FFI addon" ;
333342
334343// Array/pointer indexing.
335344
336- int32_t array_get_i32 (const int32_t * arr , size_t index ) {
345+ FFI_EXPORT int32_t array_get_i32 (const int32_t * arr , size_t index ) {
337346 if (!arr ) {
338347 return 0 ;
339348 }
340349
341350 return arr [index ];
342351}
343352
344- void array_set_i32 (int32_t * arr , size_t index , int32_t value ) {
353+ FFI_EXPORT void array_set_i32 (int32_t * arr , size_t index , int32_t value ) {
345354 if (!arr ) {
346355 return ;
347356 }
348357
349358 arr [index ] = value ;
350359}
351360
352- double array_get_f64 (const double * arr , size_t index ) {
361+ FFI_EXPORT double array_get_f64 (const double * arr , size_t index ) {
353362 if (!arr ) {
354363 return 0.0 ;
355364 }
356365
357366 return arr [index ];
358367}
359368
360- void array_set_f64 (double * arr , size_t index , double value ) {
369+ FFI_EXPORT void array_set_f64 (double * arr , size_t index , double value ) {
361370 if (!arr ) {
362371 return ;
363372 }
0 commit comments