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