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