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