2 * Copyright (C) 2024, 2025 Mikulas Patocka
4 * This file is part of Ajla.
6 * Ajla is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, either version 3 of the License, or (at your option) any later
11 * Ajla is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * Ajla. If not, see <https://www.gnu.org/licenses/>.
50 t_structure : ffi_structure;
57 e_get_last_socket_error;
63 fn ffi_unsafe_get_world : world;
65 fn ffi_get_size(ft : ffi_type) : int;
66 fn ffi_get_alignment(ft : ffi_type) : int;
68 fn ffi_create_structure(elements : list(ffi_type)) : (ffi_structure, list(int));
69 fn ffi_get_structure_offset(str : ffi_structure, element : int) : int;
71 fn ffi_poke(w : world, ptr : int, ft : ffi_type, val : int) : world;
72 fn ffi_peek(w : world, ptr : int, ft : ffi_type) : (world, int);
74 fn ffi_poke_array(t : type, w : world, ptr : int, a : list(t)) : world;
75 fn ffi_peek_array(w : world, ptr : int, l : int, ft : ffi_type, t : type) : (world, list(t));
77 fn ffi_poke_zstring(w : world, destr : ffi_destructor, str : bytes) : (world, int);
78 fn ffi_peek_zstring(w : world, addr : int) : (world, bytes);
80 fn ffi_handle_to_number(w : world, desc : ffi_destructor, h : handle) : (world, int);
81 fn ffi_number_to_handle(w : world, n : int, sckt : bool) : (world, handle);
83 fn ffi_create_function(filename : bytes, funcname : bytes, err_type : ffi_error, nvarargs : int, rtype : ffi_type, args : list(ffi_type)) : ffi_function;
84 fn ffi_call_function(w : world, func : ffi_function, args : list(int)) : (world, int, int);
86 fn ffi_encode_float(f : real32) : int;
87 fn ffi_encode_double(f : real64) : int;
88 fn ffi_encode_longdouble(f : real80) : int;
89 fn ffi_decode_float(i : int) : real32;
90 fn ffi_decode_double(i : int) : real64;
91 fn ffi_decode_longdouble(i : int) : real80;
93 fn ffi_destructor_new(w : world) : (world, ffi_destructor);
94 fn ffi_destructor_destroy(w : world, fd : ffi_destructor) : world;
95 fn ffi_destructor_allocate(w : world, fd : ffi_destructor, size align : int, zero : bool) : (world, int);
96 fn ffi_destructor_free(w : world, fd : ffi_destructor, ptr : int) : world;
97 fn ffi_destructor_call(w : world, fd : ffi_destructor, func : ffi_function, args : list(int)) : world;
101 type ffi_structure := internal_type;
102 type ffi_function := internal_type;
104 fn ffi_unsafe_get_world : world
106 return unsafe_get_world;
109 fn ffi_get_size(ft : ffi_type) : int
112 pcode IO IO_FFI_Get_Size_Alignment 1 1 1 =r ft 0;
116 fn ffi_get_alignment(ft : ffi_type) : int
119 pcode IO IO_FFI_Get_Size_Alignment 1 1 1 =r ft 1;
123 fn ffi_create_structure(elements : list(ffi_type)) : (ffi_structure, list(int))
125 var r : ffi_structure;
126 var offs : list(int);
127 pcode IO IO_FFI_Create_Structure 2 1 0 =r =offs elements;
131 fn ffi_get_structure_offset(str : ffi_structure, element : int) : int
134 pcode IO IO_FFI_Structure_Offset 1 2 0 =r str element;
138 fn ffi_poke(w : world, ptr : int, ft : ffi_type, val : int) : world
141 pcode IO IO_FFI_Poke 1 4 0 =w2 w ptr ft val;
145 fn ffi_peek(w : world, ptr : int, ft : ffi_type) : (world, int)
149 pcode IO IO_FFI_Peek 2 3 0 =w2 =r w ptr ft;
153 fn ffi_poke_array(t : type, w : world, ptr : int, a : list(t)) : world
156 pcode IO IO_FFI_Poke_Array 1 3 0 =w2 w ptr a;
160 fn ffi_peek_array(w : world, ptr : int, l : int, ft : ffi_type, t : type) : (world, list(t))
164 pcode IO IO_FFI_Peek_Array 2 4 0 =w2 =r w ptr l ft;
168 fn ffi_poke_zstring(w : world, destr : ffi_destructor, str : bytes) : (world, int)
171 w, mem_str := ffi_destructor_allocate(w, destr, len(str) + 1, 1, false);
172 w := ffi_poke_array(w, mem_str, str);
173 w := ffi_poke(w, mem_str + len(str), ffi_type.t_uchar, 0);
177 fn ffi_peek_zstring(w : world, addr : int) : (world, bytes)
179 var result := empty(byte);
182 w, c := ffi_peek(w, addr, ffi_type.t_uchar);
191 fn ffi_handle_to_number(w : world, desc : ffi_destructor, h : handle) : (world, int)
195 pcode IO IO_FFI_Handle_To_Number 2 3 0 =w2 =n w desc h;
199 fn ffi_number_to_handle(w : world, n : int, sckt : bool) : (world, handle)
203 pcode IO IO_FFI_Number_To_Handle 2 3 0 =w2 =h w n sckt;
207 fn ffi_create_function(filename : bytes, funcname : bytes, err_type : ffi_error, nvarargs : int, rtype : ffi_type, args : list(ffi_type)) : ffi_function
209 var r : ffi_function;
210 pcode IO IO_FFI_Create_Function 1 6 0 =r filename funcname err_type nvarargs rtype args;
214 fn ffi_call_function(w : world, func : ffi_function, args : list(int)) : (world, int, int)
218 pcode IO IO_FFI_Call_Function 3 3 0 =w2 =r =e w func args;
222 fn ffi_encode_float(f : real32) : int
225 pcode IO IO_FFI_Encode_Real 1 1 0 =r f;
229 fn ffi_encode_double(f : real64) : int
232 pcode IO IO_FFI_Encode_Real 1 1 0 =r f;
236 fn ffi_encode_longdouble(f : real80) : int
239 pcode IO IO_FFI_Encode_Real 1 1 0 =r f;
243 fn ffi_decode_float(i : int) : real32
246 pcode IO IO_FFI_Decode_Real 1 1 0 =r i;
250 fn ffi_decode_double(i : int) : real64
253 pcode IO IO_FFI_Decode_Real 1 1 0 =r i;
257 fn ffi_decode_longdouble(i : int) : real80
260 pcode IO IO_FFI_Decode_Real 1 1 0 =r i;
264 type ffi_destructor := internal_type;
266 fn ffi_destructor_new(w : world) : (world, ffi_destructor)
268 var r : ffi_destructor;
270 pcode IO IO_FFI_Destructor_New 2 1 0 =w2 =r w;
274 fn ffi_destructor_destroy(w : world, fd : ffi_destructor) : world
277 pcode IO IO_Consume_Parameters 1 2 0 =w2 w fd;
281 fn ffi_destructor_allocate(w : world, fd : ffi_destructor, size align : int, zero : bool) : (world, int)
285 pcode IO IO_FFI_Destructor_Allocate 2 5 0 =w2 =r w fd size align zero;
289 fn ffi_destructor_free(w : world, fd : ffi_destructor, ptr : int) : world
292 pcode IO IO_FFI_Destructor_Free 1 3 0 =w2 w fd ptr;
296 fn ffi_destructor_call(w : world, fd : ffi_destructor, func : ffi_function, args : list(int)) : world
299 pcode IO IO_FFI_Destructor_Call 1 4 0 =w2 w fd func args;