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