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