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