author | Claus Gittinger <cg@exept.de> |
Sat, 13 Apr 2019 06:38:59 +0200 | |
changeset 24070 | 85b7952e1260 |
parent 24068 | 3add4c42c177 |
child 24071 | d9537fdc5b9f |
permissions | -rw-r--r-- |
10609 | 1 |
" |
2 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
10610 | 3 |
All Rights Reserved |
10609 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
10607 | 13 |
|
20123 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
10613 | 16 |
ExternalFunction subclass:#ExternalFunctionCallback |
10620 | 17 |
instanceVariableNames:'returnType argumentTypes flags action' |
24070 | 18 |
classVariableNames:'CallBackRegistry Verbose' |
10607 | 19 |
poolDictionaries:'' |
20 |
category:'System-Support' |
|
21 |
! |
|
22 |
||
23 |
!ExternalFunctionCallback primitiveDefinitions! |
|
24 |
%{ |
|
14754
f2511581c8a1
class: ExternalFunctionCallback
Stefan Vogel <sv@exept.de>
parents:
14659
diff
changeset
|
25 |
#include <stdlib.h> |
10616 | 26 |
#include <stdio.h> |
27 |
||
24068 | 28 |
#define VERBOSE |
29 |
||
30 |
#ifdef VERBOSE |
|
31 |
# define DEBUGCODE_IF(flag, code) if ((flag) == true) { code } |
|
32 |
# else |
|
33 |
# define DEBUG_IF(flag, code) /* nothing */ |
|
34 |
# endif |
|
35 |
||
10607 | 36 |
#ifdef HAVE_FFI |
37 |
# include <ffi.h> |
|
38 |
# define MAX_ARGS 128 |
|
39 |
||
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
40 |
# ifdef USE_STANDARD_FFI |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
41 |
# define __get_ffi_type_sint() &ffi_type_sint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
42 |
# define __get_ffi_type_sint8() &ffi_type_sint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
43 |
# define __get_ffi_type_sint16() &ffi_type_sint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
44 |
# define __get_ffi_type_sint32() &ffi_type_sint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
45 |
# define __get_ffi_type_sint64() &ffi_type_sint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
46 |
# define __get_ffi_type_uint() &ffi_type_uint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
47 |
# define __get_ffi_type_uint8() &ffi_type_uint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
48 |
# define __get_ffi_type_uint16() &ffi_type_uint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
49 |
# define __get_ffi_type_uint32() &ffi_type_uint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
50 |
# define __get_ffi_type_uint64() &ffi_type_uint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
51 |
# define __get_ffi_type_float() &ffi_type_float |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
52 |
# define __get_ffi_type_double() &ffi_type_double |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
53 |
# define __get_ffi_type_void() &ffi_type_void |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
54 |
# define __get_ffi_type_pointer() &ffi_type_pointer |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
55 |
# else |
24068 | 56 |
extern ffi_type *__get_ffi_type_sint(); |
57 |
extern ffi_type *__get_ffi_type_sint8(); |
|
58 |
extern ffi_type *__get_ffi_type_sint16(); |
|
59 |
extern ffi_type *__get_ffi_type_sint32(); |
|
60 |
extern ffi_type *__get_ffi_type_sint64(); |
|
61 |
extern ffi_type *__get_ffi_type_uint(); |
|
62 |
extern ffi_type *__get_ffi_type_uint8(); |
|
63 |
extern ffi_type *__get_ffi_type_uint16(); |
|
64 |
extern ffi_type *__get_ffi_type_uint32(); |
|
65 |
extern ffi_type *__get_ffi_type_uint64(); |
|
66 |
extern ffi_type *__get_ffi_type_float(); |
|
67 |
extern ffi_type *__get_ffi_type_double(); |
|
68 |
extern ffi_type *__get_ffi_type_void(); |
|
69 |
extern ffi_type *__get_ffi_type_pointer(); |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
70 |
# endif |
10607 | 71 |
|
72 |
#endif |
|
73 |
||
22551 | 74 |
#ifdef __osx__ |
75 |
# define NEW_FFI |
|
76 |
#endif |
|
77 |
||
78 |
#ifndef NEW_FFI |
|
79 |
# define ffi_closure_alloc malloc |
|
80 |
# define ffi_closure_free free |
|
81 |
#endif |
|
82 |
||
10607 | 83 |
%} |
84 |
! ! |
|
85 |
||
86 |
!ExternalFunctionCallback primitiveFunctions! |
|
87 |
%{ |
|
88 |
||
10617
a9dc57b77568
Compile even if !defined(HAVE_FFI)
Stefan Vogel <sv@exept.de>
parents:
10616
diff
changeset
|
89 |
#ifdef HAVE_FFI |
10607 | 90 |
|
91 |
void |
|
10609 | 92 |
ExternalFunctionCallback__closure_wrapper_fn(ffi_cif* cif, void* resp, void** args, void* userdata) |
10607 | 93 |
{ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
94 |
INT actionIndex = (INT)userdata; |
10609 | 95 |
int i; |
96 |
OBJ st_argVector = nil; |
|
97 |
OBJ st_actionVector, st_callBack = nil, st_result; |
|
98 |
OBJFUNC code; |
|
99 |
ffi_type *retType; |
|
100 |
INT sintResult; |
|
101 |
unsigned INT uintResult; |
|
102 |
float floatResult; |
|
103 |
double doubleResult; |
|
104 |
||
105 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
106 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): actionIndex=%"_ld_" resp*=%"_lx_"\n", actionIndex, (INT)resp); |
10609 | 107 |
fflush(stderr); |
108 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): nargs=%d\n", cif->nargs); |
|
109 |
fflush(stderr); |
|
110 |
} |
|
111 |
||
112 |
st_argVector = __ARRAY_NEW_INT(cif->nargs); |
|
113 |
||
114 |
for (i=0; i<cif->nargs; i++) { |
|
115 |
ffi_type *argType; |
|
116 |
OBJ st_arg = nil; |
|
117 |
||
118 |
__PROTECT__(st_argVector); |
|
119 |
||
120 |
argType = cif->arg_types[i]; |
|
121 |
if (argType == __get_ffi_type_sint()) { |
|
122 |
st_arg = __MKINT( *(int *)(args[i]) ); |
|
22552 | 123 |
|
10609 | 124 |
} else if (argType == __get_ffi_type_uint()) { |
125 |
st_arg = __MKUINT( *(unsigned int *)(args[i]) ); |
|
22552 | 126 |
|
10609 | 127 |
} else if (argType == __get_ffi_type_uint8()) { |
128 |
st_arg = __MKSMALLINT( *(unsigned char *)(args[i]) ); |
|
22552 | 129 |
|
10609 | 130 |
} else if (argType == __get_ffi_type_sint8()) { |
131 |
st_arg = __MKSMALLINT( *(char *)(args[i]) ); |
|
22552 | 132 |
|
10609 | 133 |
} else if (argType == __get_ffi_type_uint16()) { |
134 |
st_arg = __MKSMALLINT( *(unsigned short *)(args[i]) ); |
|
22552 | 135 |
|
10609 | 136 |
} else if (argType == __get_ffi_type_sint16()) { |
137 |
st_arg = __MKSMALLINT( *(short *)(args[i]) ); |
|
22552 | 138 |
|
10609 | 139 |
} else if (argType == __get_ffi_type_uint32()) { |
140 |
st_arg = __MKUINT( *(unsigned int *)(args[i]) ); |
|
22552 | 141 |
|
10609 | 142 |
} else if (argType == __get_ffi_type_sint32()) { |
143 |
st_arg = __MKINT( *(int *)(args[i]) ); |
|
22552 | 144 |
|
145 |
} else if (argType == __get_ffi_type_uint64()) { |
|
24068 | 146 |
st_arg = __MKUINT( *(unsigned long long *)(args[i]) ); |
22552 | 147 |
|
148 |
} else if (argType == __get_ffi_type_sint64()) { |
|
24068 | 149 |
st_arg = __MKINT( *(long long *)(args[i]) ); |
22552 | 150 |
|
10609 | 151 |
} else if (argType == __get_ffi_type_float()) { |
152 |
st_arg = __MKSFLOAT( *(float *)(args[i]) ); |
|
22552 | 153 |
|
10609 | 154 |
} else if (argType == __get_ffi_type_double()) { |
155 |
st_arg = __MKFLOAT( *(double *)(args[i]) ); |
|
22552 | 156 |
|
10609 | 157 |
} else if (argType == __get_ffi_type_pointer()) { |
158 |
st_arg = __MKEXTERNALADDRESS( *(void **)(args[i]) ); |
|
22552 | 159 |
|
10609 | 160 |
} else { |
161 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
162 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid argument type %"_lx_" - arg %d\n", (INT)argType, i); |
10609 | 163 |
} |
164 |
} |
|
165 |
||
166 |
__UNPROTECT__(st_argVector); |
|
167 |
||
168 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
169 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): st-arg for %"_lx_" is %"_lx_"\n", *(unsigned INT *)(args[i]), (INT)st_arg); |
10609 | 170 |
} |
171 |
__ArrayInstPtr(st_argVector)->a_element[i] = st_arg; __STORE(st_argVector, st_arg); |
|
172 |
} |
|
173 |
||
174 |
/* the action ... */ |
|
175 |
st_actionVector = @global(ExternalFunctionCallback:CallBackRegistry); |
|
176 |
if (st_actionVector != nil) { |
|
177 |
OBJ cls = __Class(st_actionVector); |
|
178 |
||
179 |
if ((cls == Array) || (cls==WeakArray)) { |
|
180 |
actionIndex += /* nInstVars */ __intVal(__ClassInstPtr(cls)->c_ninstvars); |
|
10607 | 181 |
|
10609 | 182 |
if (__arraySize(st_actionVector) <= actionIndex) { |
183 |
st_callBack = __ArrayInstPtr(st_actionVector)->a_element[actionIndex-1]; |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
if (st_callBack == nil) { |
|
188 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
189 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): ignored nil callback\n"); |
|
190 |
} |
|
191 |
*(void **)resp = 0; |
|
192 |
return; |
|
193 |
} |
|
194 |
||
20291 | 195 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
196 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): sending value: to %"_lx_"..\n", (INT)st_callBack); |
|
197 |
} |
|
10609 | 198 |
{ |
20291 | 199 |
static struct inlineCache value_snd = __DUMMYILC1((@line+1)); |
10613 | 200 |
st_result = _SEND1(st_callBack, @symbol(callFromCWith:), nil, &value_snd, st_argVector); |
20291 | 201 |
} |
202 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
203 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): result is %"_lx_"\n", (INT)st_result); |
|
10609 | 204 |
} |
205 |
||
206 |
retType = cif->rtype; |
|
207 |
||
208 |
if (st_result == true) { |
|
209 |
sintResult = uintResult = 1; |
|
210 |
} else if (st_result == false) { |
|
211 |
sintResult = uintResult = 0; |
|
212 |
} else if (st_result == nil) { |
|
213 |
sintResult = uintResult = 0; |
|
214 |
} else { |
|
215 |
sintResult = __signedLongIntVal(st_result); |
|
216 |
uintResult = __unsignedLongIntVal(st_result); |
|
217 |
} |
|
218 |
||
219 |
if (retType == __get_ffi_type_sint()) { |
|
220 |
*(int *)resp = sintResult; |
|
22552 | 221 |
|
10609 | 222 |
} else if (retType == __get_ffi_type_uint()) { |
223 |
*(int *)resp = uintResult; |
|
22552 | 224 |
|
10609 | 225 |
} else if (retType == __get_ffi_type_uint8()) { |
226 |
*(unsigned char *)resp = uintResult; |
|
22552 | 227 |
|
10609 | 228 |
} else if (retType == __get_ffi_type_sint8()) { |
229 |
*(char *)resp = sintResult; |
|
22552 | 230 |
|
10609 | 231 |
} else if (retType == __get_ffi_type_uint16()) { |
232 |
*(unsigned short *)resp = uintResult; |
|
22552 | 233 |
|
10609 | 234 |
} else if (retType == __get_ffi_type_sint16()) { |
235 |
*(short *)resp = sintResult; |
|
22552 | 236 |
|
10609 | 237 |
} else if (retType == __get_ffi_type_uint32()) { |
238 |
*(int *)resp = uintResult; |
|
22552 | 239 |
|
10609 | 240 |
} else if (retType == __get_ffi_type_sint32()) { |
241 |
*(int *)resp = sintResult; |
|
22552 | 242 |
|
10609 | 243 |
} else if (retType == __get_ffi_type_float()) { |
244 |
if (__isFloat(st_result)) { |
|
245 |
*(float *)resp = (float)__floatVal(st_result); |
|
246 |
} else { |
|
247 |
if (__isShortFloat(st_result)) { |
|
248 |
*(float *)resp = __shortFloatVal(st_result); |
|
249 |
} else { |
|
250 |
*(float *)resp = (float)sintResult; |
|
251 |
} |
|
252 |
} |
|
253 |
} else if (retType == __get_ffi_type_double()) { |
|
254 |
if (__isFloat(st_result)) { |
|
255 |
*(double *)resp = __floatVal(st_result); |
|
256 |
} else { |
|
257 |
if (__isShortFloat(st_result)) { |
|
258 |
*(double *)resp = (double)__shortFloatVal(st_result); |
|
259 |
} else { |
|
260 |
*(double *)resp = (double)sintResult; |
|
261 |
} |
|
262 |
} |
|
22552 | 263 |
|
10609 | 264 |
} else if (retType == __get_ffi_type_pointer()) { |
265 |
*(void **)resp = (void *)__externalAddressVal( st_result ); |
|
22552 | 266 |
|
10609 | 267 |
} else { |
268 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
269 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid result type %"_ld_"\n", (INT)retType); |
10609 | 270 |
} |
271 |
*(void **)resp = 0; |
|
272 |
} |
|
10607 | 273 |
} |
274 |
||
275 |
void |
|
20307 | 276 |
ExternalFunctionCallback__test_call_closure(INTLFUNC f) |
10607 | 277 |
{ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
278 |
INT result = 0; |
10607 | 279 |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
280 |
printf("doCall_closure: calling closure %"_lx_"(123)...\n", (INT)f); |
10607 | 281 |
result = (*f)(123); |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
282 |
printf("doCall_closure: back; result is %"_lx_"...\n", (INT)result); |
10607 | 283 |
} |
10617
a9dc57b77568
Compile even if !defined(HAVE_FFI)
Stefan Vogel <sv@exept.de>
parents:
10616
diff
changeset
|
284 |
#endif /* HAVE_FFI */ |
10607 | 285 |
|
286 |
%} |
|
287 |
! ! |
|
288 |
||
10609 | 289 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
290 |
||
291 |
copyright |
|
292 |
" |
|
293 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
10610 | 294 |
All Rights Reserved |
10609 | 295 |
|
296 |
This software is furnished under a license and may be used |
|
297 |
only in accordance with the terms of that license and with the |
|
298 |
inclusion of the above copyright notice. This software may not |
|
299 |
be provided or otherwise made available to, or used by, any |
|
300 |
other person. No title to or ownership of the software is |
|
301 |
hereby transferred. |
|
302 |
" |
|
303 |
! |
|
304 |
||
305 |
documentation |
|
306 |
" |
|
307 |
an ExternalFunctionCallback wraps a block into a C-callable function; |
|
308 |
i.e. it creates a closure, which as seen from C-code looks like an ordinary |
|
309 |
function pointer, but when invoked evaluates a smalltalk block. |
|
310 |
||
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
311 |
A callback is created with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
312 |
cb := ExternalFunctionCallback new. |
10613 | 313 |
the arguments (as passed from the C-caller into ST) |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
314 |
and the returnValue (from ST to the C-caller) are specified with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
315 |
cb returnType:#bool argumentTypes:#(uint). |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
316 |
Then, the code is generated with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
317 |
cb generateClosure. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
318 |
|
20123 | 319 |
After that, the callBack-functions address can be acquired with: |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
320 |
cb address. 'can be passed to C'. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
321 |
and handed out to C. (you can also hand out the callBack directly - as it is a subclass of |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
322 |
ExternalBytes. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
323 |
The actual action of the callback can be changed (at any time later) with: |
24068 | 324 |
cb action:[:args | Transcript showCR:args. true]. |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
325 |
|
11849 | 326 |
Eventually, the callback MUST be released: |
24068 | 327 |
cb release. |
22551 | 328 |
|
329 |
[supported returnTypes:] |
|
24068 | 330 |
int |
331 |
uint |
|
332 |
uint8 |
|
333 |
uint16 |
|
334 |
uint32 |
|
335 |
uint64 |
|
336 |
sint |
|
337 |
sint8 |
|
338 |
sint16 |
|
339 |
sint32 |
|
340 |
sint64 |
|
341 |
long system dependent sint32 or sint64 |
|
342 |
ulong system dependent uint32 or uint64 |
|
22551 | 343 |
|
24068 | 344 |
bool |
345 |
float |
|
346 |
double |
|
347 |
void |
|
22551 | 348 |
|
24068 | 349 |
pointer, |
350 |
handle, |
|
351 |
charPointer, |
|
352 |
bytePointer, |
|
353 |
floatPointer |
|
354 |
doublePointer |
|
355 |
intPointer |
|
356 |
shortPointer |
|
357 |
wcharPointer |
|
22551 | 358 |
|
24068 | 359 |
<name of subclass of ExternalAddress> |
22551 | 360 |
|
361 |
[supported argumentTypes:] |
|
24068 | 362 |
handle |
363 |
pointer |
|
364 |
voidPointer |
|
22551 | 365 |
|
24068 | 366 |
long, ulong - system dependent |
22551 | 367 |
|
24068 | 368 |
int, |
369 |
uint8, sint8, uint16, sint16, |
|
370 |
uint32, sint32, |
|
371 |
float, double |
|
372 |
void |
|
22551 | 373 |
|
24068 | 374 |
charPointer, floatPointer, doublePointer, |
22551 | 375 |
|
24068 | 376 |
<name of subclass of ExternalAddress> |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
377 |
|
10609 | 378 |
[author:] |
24068 | 379 |
Claus Gittinger |
10609 | 380 |
" |
381 |
! |
|
10607 | 382 |
|
383 |
examples |
|
384 |
" |
|
385 |
|cb| |
|
386 |
||
387 |
cb := ExternalFunctionCallback new. |
|
388 |
cb returnType:#bool argumentTypes:#(uint). |
|
10620 | 389 |
cb beCallTypeWINAPI. |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
390 |
cb generateClosure. |
10609 | 391 |
cb action:[:args | Transcript showCR:args. true]. |
22547 | 392 |
cb code. 'address can be passed to C'. |
393 |
Transcript showCR:cb code. |
|
10609 | 394 |
|
395 |
ExternalFunctionCallback testCall:cb withArgument:123. |
|
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
396 |
|
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
397 |
cb action:[:args | Transcript show:'hello '; showCR:args. true]. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
398 |
|
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
399 |
ExternalFunctionCallback testCall:cb withArgument:123. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
400 |
|
10609 | 401 |
cb release |
10607 | 402 |
" |
403 |
! ! |
|
404 |
||
13867 | 405 |
!ExternalFunctionCallback class methodsFor:'instance creation'! |
11849 | 406 |
|
11851 | 407 |
callbackFor:aBlock returnType:returnType argumentTypes:argumentTypes |
11849 | 408 |
"generate a callback for the ErrorCallbackProc signature: |
13574 | 409 |
ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText) |
11849 | 410 |
which, can be given to an external API call and which invokes the |
411 |
three arg block when clled. |
|
412 |
Do not forget to eventually release the callback to avoid a memory leak." |
|
413 |
||
414 |
|cb| |
|
415 |
||
11851 | 416 |
self assert:(aBlock numArgs == argumentTypes size). |
417 |
||
11849 | 418 |
cb := ExternalFunctionCallback new. |
11851 | 419 |
cb returnType:returnType argumentTypes:argumentTypes. |
11849 | 420 |
cb beCallTypeWINAPI. |
421 |
cb generateClosure. |
|
11851 | 422 |
cb action:aBlock. |
11849 | 423 |
"/ ^ cb code. 'can be passed to C'. |
424 |
^ cb |
|
425 |
||
426 |
" |
|
427 |
|cb| |
|
428 |
||
429 |
cb := self errorCallbackProcFor:[:a1 :a2 :a3 | Transcript showCR:('%1 %2 %3' bindWith:a1 with:a2 with:a3)]. |
|
430 |
ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello'). |
|
431 |
cb release |
|
432 |
" |
|
11851 | 433 |
! |
434 |
||
435 |
errorCallbackProcFor:aThreeArgBlock |
|
436 |
"generate a callback for the ErrorCallbackProc signature: |
|
13574 | 437 |
ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText) |
11851 | 438 |
which, can be given to an external API call and which invokes the |
439 |
three arg block when clled. |
|
440 |
Do not forget to eventually release the callback to avoid a memory leak." |
|
441 |
||
13574 | 442 |
^ self |
443 |
callbackFor:aThreeArgBlock |
|
444 |
returnType:#long |
|
445 |
argumentTypes:#(handle int charPointer) |
|
11851 | 446 |
|
447 |
" |
|
448 |
|cb| |
|
449 |
||
450 |
cb := self errorCallbackProcFor:[:a1 :a2 :a3 | Transcript showCR:('%1 %2 %3' bindWith:a1 with:a2 with:a3)]. |
|
451 |
ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello'). |
|
452 |
cb release |
|
453 |
" |
|
11849 | 454 |
! ! |
455 |
||
10620 | 456 |
!ExternalFunctionCallback class methodsFor:'constants'! |
457 |
||
458 |
callTypeAPI |
|
459 |
^ ExternalLibraryFunction callTypeAPI |
|
460 |
! |
|
461 |
||
462 |
callTypeC |
|
463 |
^ ExternalLibraryFunction callTypeC |
|
464 |
! |
|
465 |
||
466 |
callTypeCDecl |
|
467 |
^ ExternalLibraryFunction callTypeCDecl |
|
468 |
! |
|
469 |
||
470 |
callTypeMASK |
|
471 |
^ ExternalLibraryFunction callTypeMASK |
|
472 |
! |
|
473 |
||
474 |
callTypeOLE |
|
475 |
^ ExternalLibraryFunction callTypeOLE |
|
22545 | 476 |
! |
477 |
||
478 |
callTypeUNIX64 |
|
479 |
^ ExternalLibraryFunction callTypeUNIX64 |
|
10620 | 480 |
! ! |
481 |
||
10609 | 482 |
!ExternalFunctionCallback class methodsFor:'helpers'! |
483 |
||
484 |
closureIndexFor:aCallBack |
|
485 |
CallBackRegistry isNil ifTrue:[ |
|
486 |
CallBackRegistry := WeakArray with:aCallBack. |
|
487 |
] ifFalse:[ |
|
488 |
CallBackRegistry := CallBackRegistry copyWith:aCallBack. |
|
489 |
]. |
|
490 |
^ CallBackRegistry size. |
|
491 |
! |
|
492 |
||
493 |
testCall:aCallback withArgument:arg |
|
10613 | 494 |
"a simple test, if I can be called" |
11849 | 495 |
|
496 |
self testCall:aCallback withArguments:(Array with:arg) |
|
497 |
! |
|
498 |
||
499 |
testCall:aCallback withArguments:args |
|
500 |
"a simple test, if I can be called" |
|
10609 | 501 |
%{ |
13574 | 502 |
# define MAX_CALLBACK_ARGS 5 |
20307 | 503 |
INTLFUNC f = __externalAddressVal(aCallback); |
10609 | 504 |
INT result; |
11849 | 505 |
int i; |
13574 | 506 |
void *c_args[MAX_CALLBACK_ARGS]; |
11849 | 507 |
|
21623 | 508 |
if (! __isArrayLike(args)) |
13574 | 509 |
goto badArg; |
510 |
if (__arraySize(args) > MAX_CALLBACK_ARGS) |
|
511 |
goto badArg; |
|
11849 | 512 |
|
513 |
for (i=0; i < __arraySize(args); i++) { |
|
13574 | 514 |
OBJ arg = __ArrayInstPtr(args)->a_element[i]; |
10609 | 515 |
|
13574 | 516 |
if (__isSmallInteger(arg)) { |
517 |
c_args[i] = (void *)(__intVal(arg)); |
|
518 |
} else if (arg == true) { |
|
519 |
c_args[i] = (void *)1; |
|
520 |
} else if (arg == false) { |
|
521 |
c_args[i] = (void *)0; |
|
522 |
} else if (__isStringLike(arg)) { |
|
523 |
c_args[i] = (void *)__stringVal(arg); |
|
524 |
} else if (__isByteArrayLike(arg)) { |
|
525 |
c_args[i] = (void *)__byteArrayVal(arg); |
|
526 |
} else |
|
527 |
goto badArg; |
|
10613 | 528 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
529 |
fprintf(stderr, "ExternalFunctionCallback: calling callBack %"_lx_"(%"_lx_", %"_lx_")\n", (INT)f, (INT)(c_args[0]), (INT)(c_args[1])); |
11849 | 530 |
result = (*f)(c_args[0], c_args[1], c_args[2], c_args[3], c_args[4]); |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
531 |
fprintf(stderr, "ExternalFunctionCallback: result from callBack is %"_lx_"\n", (INT)result); |
11849 | 532 |
RETURN(true); |
533 |
||
534 |
badArg: ; |
|
535 |
%}. |
|
536 |
self error:'bad argument' |
|
10609 | 537 |
! ! |
538 |
||
10607 | 539 |
!ExternalFunctionCallback methodsFor:'accessing'! |
540 |
||
10613 | 541 |
action:aBlock |
542 |
"set the action-block, to be evaluated when C calls me. |
|
543 |
The C-arguments will be passed as arguments to the block. |
|
544 |
The value returned by the block will be returned to the C-caller." |
|
545 |
||
546 |
action := aBlock. |
|
10620 | 547 |
! |
548 |
||
549 |
beCallTypeAPI |
|
550 |
flags := (flags ? 0) bitOr: (self class callTypeAPI). |
|
551 |
||
552 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
553 |
! |
|
554 |
||
555 |
beCallTypeC |
|
556 |
flags := (flags ? 0) bitOr: (self class callTypeC). |
|
557 |
||
558 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
559 |
! |
|
560 |
||
561 |
beCallTypeOLE |
|
562 |
flags := (flags ? 0) bitOr: (self class callTypeOLE). |
|
563 |
||
564 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
565 |
! |
|
566 |
||
22545 | 567 |
beCallTypeUNIX64 |
568 |
flags := (flags ? 0) bitOr: (self class callTypeUNIX64). |
|
569 |
! |
|
570 |
||
10620 | 571 |
beCallTypeWINAPI |
572 |
self beCallTypeAPI |
|
573 |
||
574 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
575 |
! |
|
576 |
||
577 |
callTypeNumber |
|
578 |
^ (flags ? 0) bitAnd: (self class callTypeMASK) |
|
579 |
||
580 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
581 |
! |
|
582 |
||
583 |
isCallTypeAPI |
|
584 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeAPI) |
|
585 |
||
586 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
587 |
! |
|
588 |
||
589 |
isCallTypeC |
|
590 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeC) |
|
591 |
! |
|
592 |
||
593 |
isCallTypeOLE |
|
594 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeOLE) |
|
10609 | 595 |
! ! |
596 |
||
597 |
!ExternalFunctionCallback methodsFor:'callback'! |
|
598 |
||
10613 | 599 |
callFromCWith:argList |
600 |
"invoked by the C-code, to which we have given out the code-ptr. |
|
601 |
Because this is evaluated from C, we probably should not block or abort or do |
|
10609 | 602 |
any other things which confuse C |
603 |
(its probably a good idea to write something into a queue here)" |
|
604 |
||
605 |
action notNil ifTrue:[ |
|
10613 | 606 |
^ action valueWithArguments:argList |
10609 | 607 |
]. |
608 |
^ nil |
|
10607 | 609 |
! ! |
610 |
||
611 |
!ExternalFunctionCallback methodsFor:'generation'! |
|
612 |
||
10613 | 613 |
code |
614 |
self hasCode ifFalse:[ |
|
14626 | 615 |
self generateClosure |
10607 | 616 |
]. |
10613 | 617 |
^ super code |
10607 | 618 |
|
619 |
"Created: / 11-06-2007 / 15:53:00 / cg" |
|
22545 | 620 |
! |
621 |
||
622 |
getCode |
|
623 |
^ super code |
|
624 |
||
625 |
"Created: / 03-03-2017 / 13:55:00 / cg" |
|
10607 | 626 |
! ! |
627 |
||
628 |
!ExternalFunctionCallback methodsFor:'private-accessing'! |
|
629 |
||
630 |
returnType:aReturnType argumentTypes:argTypes |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
631 |
"see generateClosure for valid return types" |
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
632 |
|
10607 | 633 |
returnType := aReturnType. |
634 |
argumentTypes := argTypes. |
|
635 |
||
636 |
"Created: / 11-06-2007 / 15:52:01 / cg" |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
637 |
"Modified: / 19-09-2007 / 18:14:59 / cg" |
10607 | 638 |
! ! |
639 |
||
640 |
!ExternalFunctionCallback methodsFor:'private-generation'! |
|
641 |
||
642 |
generateClosure |
|
10609 | 643 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo |
644 |
callTypeNumber returnValueClass argValueClass callBackIndex| |
|
10607 | 645 |
|
646 |
argTypeSymbols := argumentTypes. |
|
647 |
returnTypeSymbol := returnType. |
|
10609 | 648 |
callBackIndex := self class closureIndexFor:self. |
10621 | 649 |
callTypeNumber := self callTypeNumber. |
10609 | 650 |
%{ |
10607 | 651 |
#ifdef HAVE_FFI |
10609 | 652 |
ffi_cif *pcif; |
10607 | 653 |
ffi_type *__returnType = NULL; |
654 |
static int null = 0; |
|
655 |
int i; |
|
656 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
657 |
int __numArgsWanted; |
|
22551 | 658 |
|
10609 | 659 |
struct closurePlusCIF { |
22552 | 660 |
ffi_closure closure; |
661 |
ffi_cif cif; |
|
662 |
ffi_type *argTypes[MAX_ARGS]; |
|
10609 | 663 |
} *closurePlusCIFp; |
10607 | 664 |
ffi_closure *pcl; |
10609 | 665 |
ffi_cif *cif; |
666 |
ffi_type **argTypePtrs; |
|
22551 | 667 |
|
22548 | 668 |
void* codePtr; |
10609 | 669 |
|
24068 | 670 |
# ifndef NEW_FFI |
10609 | 671 |
closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF)); |
22548 | 672 |
codePtr = &(closurePlusCIFp->closure); |
24068 | 673 |
DEBUGCODE_IF( @global(Verbose), { |
674 |
printf("old ffi\n"); |
|
675 |
}) |
|
676 |
# else |
|
22548 | 677 |
closurePlusCIFp = (struct closurePlusCIF *) ffi_closure_alloc(sizeof(struct closurePlusCIF), &codePtr); |
24068 | 678 |
DEBUGCODE_IF( @global(Verbose), { |
679 |
printf("new ffi\n"); |
|
680 |
}) |
|
681 |
# endif |
|
22548 | 682 |
pcl = &(closurePlusCIFp->closure); |
683 |
cif = &(closurePlusCIFp->cif); |
|
684 |
argTypePtrs = closurePlusCIFp->argTypes; |
|
685 |
||
22551 | 686 |
# define __FAIL__(fcode) \ |
22552 | 687 |
{ \ |
688 |
failureCode = fcode; ffi_closure_free(closurePlusCIFp); goto getOutOfHere; \ |
|
689 |
} |
|
22551 | 690 |
|
10607 | 691 |
if (argTypeSymbols == nil) { |
22552 | 692 |
__numArgsWanted = 0; |
21623 | 693 |
} else if (__isArrayLike(argTypeSymbols)) { |
22552 | 694 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10607 | 695 |
} else { |
22552 | 696 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10607 | 697 |
} |
698 |
||
699 |
if (__numArgsWanted > MAX_ARGS) { |
|
22552 | 700 |
__FAIL__(@symbol(TooManyArguments)) |
10607 | 701 |
} |
702 |
||
703 |
/* |
|
704 |
* validate the return type |
|
705 |
*/ |
|
706 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
|
22552 | 707 |
returnTypeSymbol = @symbol(handle); |
10607 | 708 |
} |
709 |
||
710 |
if (returnTypeSymbol == @symbol(int)) { |
|
22552 | 711 |
__returnType = __get_ffi_type_sint(); |
10607 | 712 |
} else if (returnTypeSymbol == @symbol(uint)) { |
22552 | 713 |
__returnType = __get_ffi_type_uint(); |
10607 | 714 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
22552 | 715 |
__returnType = __get_ffi_type_uint8(); |
10607 | 716 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
22552 | 717 |
__returnType = __get_ffi_type_uint16(); |
10607 | 718 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
22552 | 719 |
__returnType = __get_ffi_type_uint32(); |
10607 | 720 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
22552 | 721 |
__returnType = __get_ffi_type_uint64(); |
10607 | 722 |
|
723 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
22552 | 724 |
__returnType = __get_ffi_type_sint(); |
10607 | 725 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
22552 | 726 |
__returnType = __get_ffi_type_sint8(); |
10607 | 727 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
22552 | 728 |
__returnType = __get_ffi_type_sint16(); |
10607 | 729 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
22552 | 730 |
__returnType = __get_ffi_type_sint32(); |
10607 | 731 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
22552 | 732 |
__returnType = __get_ffi_type_sint64(); |
10607 | 733 |
|
734 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
22552 | 735 |
if (sizeof(long) == 4) { |
736 |
returnTypeSymbol = @symbol(sint32); |
|
737 |
__returnType = __get_ffi_type_sint32(); |
|
738 |
} else if (sizeof(long) == 8) { |
|
739 |
returnTypeSymbol = @symbol(sint64); |
|
740 |
__returnType = __get_ffi_type_sint64(); |
|
741 |
} else { |
|
742 |
__FAIL__(@symbol(UnknownReturnType)) |
|
743 |
} |
|
10607 | 744 |
|
745 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
22552 | 746 |
if (sizeof(long) == 4) { |
747 |
returnTypeSymbol = @symbol(uint32); |
|
748 |
__returnType = __get_ffi_type_uint32(); |
|
749 |
}else if (sizeof(long) == 8) { |
|
750 |
returnTypeSymbol = @symbol(uint64); |
|
751 |
__returnType = __get_ffi_type_uint64(); |
|
752 |
} else { |
|
753 |
__FAIL__(@symbol(UnknownReturnType)) |
|
754 |
} |
|
10607 | 755 |
|
756 |
} else if (returnTypeSymbol == @symbol(bool)) { |
|
22552 | 757 |
__returnType = __get_ffi_type_uint(); |
10607 | 758 |
|
759 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
22552 | 760 |
__returnType = __get_ffi_type_float(); |
10607 | 761 |
} else if (returnTypeSymbol == @symbol(double)) { |
22552 | 762 |
__returnType = __get_ffi_type_double(); |
10607 | 763 |
|
764 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
22552 | 765 |
__returnType = __get_ffi_type_void(); |
10607 | 766 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
22552 | 767 |
|| (returnTypeSymbol == @symbol(handle)) |
768 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
769 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
770 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
771 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
772 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
773 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
774 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
775 |
__returnType = __get_ffi_type_pointer(); |
|
10607 | 776 |
} else { |
22552 | 777 |
if (__isSymbol(returnTypeSymbol) |
778 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
779 |
if (! __isBehaviorLike(returnValueClass)) { |
|
780 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
781 |
} |
|
782 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
783 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
784 |
} |
|
785 |
__returnType = __get_ffi_type_pointer(); |
|
786 |
returnTypeSymbol = @symbol(pointer); |
|
787 |
} else { |
|
788 |
__FAIL__(@symbol(UnknownReturnType)) |
|
789 |
} |
|
10607 | 790 |
} |
24068 | 791 |
DEBUGCODE_IF( @global(Verbose), { |
792 |
printf("__returnType: %p\n", __returnType); |
|
793 |
}) |
|
10607 | 794 |
|
795 |
/* |
|
796 |
* setup arg-buffers |
|
797 |
*/ |
|
798 |
for (i=0; i<__numArgsWanted; i++) { |
|
24068 | 799 |
ffi_type *thisType; |
800 |
void *argValuePtr; |
|
801 |
OBJ typeSymbol; |
|
10607 | 802 |
|
24068 | 803 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
10607 | 804 |
|
24068 | 805 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
10607 | 806 |
|
24068 | 807 |
if (typeSymbol == @symbol(handle)) { |
808 |
typeSymbol = @symbol(pointer); |
|
809 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
810 |
typeSymbol = @symbol(pointer); |
|
811 |
} |
|
10607 | 812 |
|
24068 | 813 |
if (typeSymbol == @symbol(long)) { |
814 |
if (sizeof(long) == sizeof(int)) { |
|
815 |
typeSymbol = @symbol(sint); |
|
816 |
} else { |
|
817 |
if (sizeof(long) == 4) { |
|
818 |
typeSymbol = @symbol(sint32); |
|
819 |
} else if (sizeof(long) == 8) { |
|
820 |
typeSymbol = @symbol(sint64); |
|
821 |
} |
|
822 |
} |
|
823 |
} |
|
824 |
if (typeSymbol == @symbol(ulong)) { |
|
825 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
826 |
typeSymbol = @symbol(uint); |
|
827 |
} else { |
|
828 |
if (sizeof(long) == 4) { |
|
829 |
typeSymbol = @symbol(uint32); |
|
830 |
} else if (sizeof(long) == 8) { |
|
831 |
typeSymbol = @symbol(uint64); |
|
832 |
} |
|
833 |
} |
|
834 |
} |
|
10607 | 835 |
|
24068 | 836 |
if (typeSymbol == @symbol(int)) { |
837 |
thisType = __get_ffi_type_sint(); |
|
838 |
} else if (typeSymbol == @symbol(uint)) { |
|
839 |
thisType = __get_ffi_type_uint(); |
|
840 |
} else if (typeSymbol == @symbol(uint8)) { |
|
841 |
thisType = __get_ffi_type_uint8(); |
|
842 |
} else if (typeSymbol == @symbol(sint8)) { |
|
843 |
thisType = __get_ffi_type_sint8(); |
|
844 |
} else if (typeSymbol == @symbol(uint16)) { |
|
845 |
thisType = __get_ffi_type_uint16(); |
|
846 |
} else if (typeSymbol == @symbol(sint16)) { |
|
847 |
thisType = __get_ffi_type_sint16(); |
|
848 |
} else if (typeSymbol == @symbol(uint32)) { |
|
849 |
thisType = __get_ffi_type_uint32(); |
|
850 |
} else if (typeSymbol == @symbol(sint32)) { |
|
851 |
thisType = __get_ffi_type_sint32(); |
|
852 |
} else if (typeSymbol == @symbol(uint64)) { |
|
853 |
thisType = __get_ffi_type_uint64(); |
|
854 |
} else if (typeSymbol == @symbol(sint64)) { |
|
855 |
thisType = __get_ffi_type_sint64(); |
|
22551 | 856 |
|
24068 | 857 |
} else if (typeSymbol == @symbol(float)) { |
858 |
thisType = __get_ffi_type_float(); |
|
859 |
} else if (typeSymbol == @symbol(double)) { |
|
860 |
thisType = __get_ffi_type_double(); |
|
861 |
} else if (typeSymbol == @symbol(void)) { |
|
862 |
thisType = __get_ffi_type_void(); |
|
863 |
} else if (typeSymbol == @symbol(charPointer)) { |
|
864 |
thisType = __get_ffi_type_pointer(); |
|
865 |
} else if (typeSymbol == @symbol(floatPointer)) { |
|
866 |
thisType = __get_ffi_type_pointer(); |
|
867 |
} else if (typeSymbol == @symbol(doublePointer)) { |
|
868 |
thisType = __get_ffi_type_pointer(); |
|
869 |
} else if (typeSymbol == @symbol(intPointer)) { |
|
870 |
thisType = __get_ffi_type_pointer(); |
|
871 |
} else if (typeSymbol == @symbol(bytePointer)) { |
|
872 |
thisType = __get_ffi_type_pointer(); |
|
873 |
} else if (typeSymbol == @symbol(shortPointer)) { |
|
874 |
thisType = __get_ffi_type_pointer(); |
|
875 |
} else if (typeSymbol == @symbol(wcharPointer)) { |
|
876 |
thisType = __get_ffi_type_pointer(); |
|
877 |
} else if (typeSymbol == @symbol(pointer)) { |
|
10607 | 878 |
commonPointerTypeArg: ; |
22552 | 879 |
thisType = __get_ffi_type_pointer(); |
880 |
} else if (typeSymbol == @symbol(bool)) { |
|
881 |
thisType = __get_ffi_type_uint(); |
|
882 |
} else if (__isSymbol(typeSymbol) |
|
883 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
884 |
if (! __isBehaviorLike(argValueClass)) { |
|
885 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
886 |
} |
|
887 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
888 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
889 |
} |
|
890 |
goto commonPointerTypeArg; /* sorry */ |
|
891 |
} else { |
|
892 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
893 |
} |
|
10607 | 894 |
|
22552 | 895 |
argTypePtrs[i] = thisType; |
10607 | 896 |
} |
897 |
failureInfo = nil; |
|
898 |
||
24068 | 899 |
DEBUGCODE_IF( @global(Verbose), { |
900 |
printf("got argTypes\n"); |
|
901 |
}) |
|
902 |
||
10607 | 903 |
__callType = FFI_DEFAULT_ABI; |
22546 | 904 |
#ifndef __osx__ |
10607 | 905 |
if (callTypeNumber != nil) { |
22545 | 906 |
# ifdef CALLTYPE_FFI_STDCALL |
22552 | 907 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) { |
908 |
__callType = CALLTYPE_FFI_STDCALL; |
|
909 |
} |
|
22545 | 910 |
# endif |
911 |
# ifdef CALLTYPE_FFI_V8 |
|
22552 | 912 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) { |
913 |
__callType = CALLTYPE_FFI_V8; |
|
914 |
} |
|
22545 | 915 |
# endif |
916 |
# ifdef CALLTYPE_FFI_V9 |
|
22552 | 917 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) { |
918 |
__callType = CALLTYPE_FFI_V9; |
|
919 |
} |
|
22545 | 920 |
# endif |
921 |
# ifdef CALLTYPE_FFI_UNIX64 |
|
22552 | 922 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) { |
923 |
__callType = CALLTYPE_FFI_UNIX64; |
|
924 |
} |
|
22545 | 925 |
# endif |
926 |
} |
|
10607 | 927 |
#endif |
22545 | 928 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
24068 | 929 |
printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (void*)cif); |
22545 | 930 |
} |
931 |
||
932 |
if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) { |
|
22552 | 933 |
__FAIL__(@symbol(FFIPrepareFailed)) |
10607 | 934 |
} |
935 |
||
10609 | 936 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22552 | 937 |
printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure)); |
938 |
printf("index is %"_ld_"\n", __intVal(callBackIndex)); |
|
10609 | 939 |
} |
22551 | 940 |
#ifndef NEW_FFI |
10609 | 941 |
if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) { |
22552 | 942 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
10607 | 943 |
} |
22548 | 944 |
#else |
22551 | 945 |
if (ffi_prep_closure_loc(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)), codePtr) != FFI_OK) { |
22552 | 946 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
22548 | 947 |
} |
948 |
#endif |
|
10609 | 949 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22552 | 950 |
printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif)); |
951 |
printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun)); |
|
952 |
printf("pcl code at %"_lx_" is:\n", (INT)pcl); |
|
953 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]); |
|
954 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]); |
|
955 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]); |
|
956 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]); |
|
10609 | 957 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
958 |
__INST(code_) = (OBJ)pcl; |
10609 | 959 |
|
960 |
#if 0 |
|
20307 | 961 |
ExternalFunctionCallback__test_call_closure((INTLFUNC)pcl); |
10607 | 962 |
#endif |
963 |
||
964 |
#else /* no FFI support */ |
|
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10617
diff
changeset
|
965 |
failureCode = @symbol(FFINotSupported); |
10607 | 966 |
#endif /* HAVE_FFI */ |
967 |
getOutOfHere: ; |
|
968 |
%}. |
|
969 |
failureCode notNil ifTrue:[ |
|
22552 | 970 |
self primitiveFailed:(failureCode->failureInfo). "see failureCode and failureInfo for details" |
971 |
^ nil |
|
10607 | 972 |
]. |
973 |
||
974 |
"Created: / 11-06-2007 / 21:53:02 / cg" |
|
24068 | 975 |
"Modified: / 12-04-2019 / 11:26:53 / Claus Gittinger" |
10607 | 976 |
! ! |
10609 | 977 |
|
978 |
!ExternalFunctionCallback methodsFor:'private-releasing'! |
|
979 |
||
980 |
release |
|
981 |
|idx| |
|
982 |
||
983 |
idx := CallBackRegistry identityIndexOf:self. |
|
984 |
CallBackRegistry at:idx put:nil. |
|
985 |
%{ |
|
10613 | 986 |
void *pcl = (void *)__INST(code_); |
10609 | 987 |
|
10613 | 988 |
__INST(code_) = 0; |
10609 | 989 |
if (pcl) { |
22551 | 990 |
ffi_closure_free(pcl); |
13574 | 991 |
RETURN(self); |
10609 | 992 |
} |
993 |
%}. |
|
994 |
self invalidateReference. |
|
995 |
! ! |
|
996 |
||
997 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
|
998 |
||
999 |
version |
|
20123 | 1000 |
^ '$Header$' |
12461
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1001 |
! |
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1002 |
|
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1003 |
version_CVS |
20123 | 1004 |
^ '$Header$' |
10609 | 1005 |
! ! |
24070 | 1006 |