author | Claus Gittinger <cg@exept.de> |
Mon, 19 Feb 2018 14:48:01 +0100 | |
changeset 22545 | d5b6e2585870 |
parent 21623 | 0fd2de531f9a |
child 22546 | 4f467704d94e |
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]. |
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 |
|
22545 | 385 |
! |
386 |
||
387 |
callTypeUNIX64 |
|
388 |
^ ExternalLibraryFunction callTypeUNIX64 |
|
10620 | 389 |
! ! |
390 |
||
10609 | 391 |
!ExternalFunctionCallback class methodsFor:'helpers'! |
392 |
||
393 |
closureIndexFor:aCallBack |
|
394 |
CallBackRegistry isNil ifTrue:[ |
|
395 |
CallBackRegistry := WeakArray with:aCallBack. |
|
396 |
] ifFalse:[ |
|
397 |
CallBackRegistry := CallBackRegistry copyWith:aCallBack. |
|
398 |
]. |
|
399 |
^ CallBackRegistry size. |
|
400 |
! |
|
401 |
||
402 |
testCall:aCallback withArgument:arg |
|
10613 | 403 |
"a simple test, if I can be called" |
11849 | 404 |
|
405 |
self testCall:aCallback withArguments:(Array with:arg) |
|
406 |
! |
|
407 |
||
408 |
testCall:aCallback withArguments:args |
|
409 |
"a simple test, if I can be called" |
|
10609 | 410 |
%{ |
13574 | 411 |
# define MAX_CALLBACK_ARGS 5 |
20307 | 412 |
INTLFUNC f = __externalAddressVal(aCallback); |
10609 | 413 |
INT result; |
11849 | 414 |
int i; |
13574 | 415 |
void *c_args[MAX_CALLBACK_ARGS]; |
11849 | 416 |
|
21623 | 417 |
if (! __isArrayLike(args)) |
13574 | 418 |
goto badArg; |
419 |
if (__arraySize(args) > MAX_CALLBACK_ARGS) |
|
420 |
goto badArg; |
|
11849 | 421 |
|
422 |
for (i=0; i < __arraySize(args); i++) { |
|
13574 | 423 |
OBJ arg = __ArrayInstPtr(args)->a_element[i]; |
10609 | 424 |
|
13574 | 425 |
if (__isSmallInteger(arg)) { |
426 |
c_args[i] = (void *)(__intVal(arg)); |
|
427 |
} else if (arg == true) { |
|
428 |
c_args[i] = (void *)1; |
|
429 |
} else if (arg == false) { |
|
430 |
c_args[i] = (void *)0; |
|
431 |
} else if (__isStringLike(arg)) { |
|
432 |
c_args[i] = (void *)__stringVal(arg); |
|
433 |
} else if (__isByteArrayLike(arg)) { |
|
434 |
c_args[i] = (void *)__byteArrayVal(arg); |
|
435 |
} else |
|
436 |
goto badArg; |
|
10613 | 437 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
438 |
fprintf(stderr, "ExternalFunctionCallback: calling callBack %"_lx_"(%"_lx_", %"_lx_")\n", (INT)f, (INT)(c_args[0]), (INT)(c_args[1])); |
11849 | 439 |
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
|
440 |
fprintf(stderr, "ExternalFunctionCallback: result from callBack is %"_lx_"\n", (INT)result); |
11849 | 441 |
RETURN(true); |
442 |
||
443 |
badArg: ; |
|
444 |
%}. |
|
445 |
self error:'bad argument' |
|
10609 | 446 |
! ! |
447 |
||
10607 | 448 |
!ExternalFunctionCallback methodsFor:'accessing'! |
449 |
||
10613 | 450 |
action:aBlock |
451 |
"set the action-block, to be evaluated when C calls me. |
|
452 |
The C-arguments will be passed as arguments to the block. |
|
453 |
The value returned by the block will be returned to the C-caller." |
|
454 |
||
455 |
action := aBlock. |
|
10620 | 456 |
! |
457 |
||
458 |
beCallTypeAPI |
|
459 |
flags := (flags ? 0) bitOr: (self class callTypeAPI). |
|
460 |
||
461 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
462 |
! |
|
463 |
||
464 |
beCallTypeC |
|
465 |
flags := (flags ? 0) bitOr: (self class callTypeC). |
|
466 |
||
467 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
468 |
! |
|
469 |
||
470 |
beCallTypeOLE |
|
471 |
flags := (flags ? 0) bitOr: (self class callTypeOLE). |
|
472 |
||
473 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
474 |
! |
|
475 |
||
22545 | 476 |
beCallTypeUNIX64 |
477 |
flags := (flags ? 0) bitOr: (self class callTypeUNIX64). |
|
478 |
! |
|
479 |
||
10620 | 480 |
beCallTypeWINAPI |
481 |
self beCallTypeAPI |
|
482 |
||
483 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
484 |
! |
|
485 |
||
486 |
callTypeNumber |
|
487 |
^ (flags ? 0) bitAnd: (self class callTypeMASK) |
|
488 |
||
489 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
490 |
! |
|
491 |
||
492 |
isCallTypeAPI |
|
493 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeAPI) |
|
494 |
||
495 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
496 |
! |
|
497 |
||
498 |
isCallTypeC |
|
499 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeC) |
|
500 |
! |
|
501 |
||
502 |
isCallTypeOLE |
|
503 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeOLE) |
|
10609 | 504 |
! ! |
505 |
||
506 |
!ExternalFunctionCallback methodsFor:'callback'! |
|
507 |
||
10613 | 508 |
callFromCWith:argList |
509 |
"invoked by the C-code, to which we have given out the code-ptr. |
|
510 |
Because this is evaluated from C, we probably should not block or abort or do |
|
10609 | 511 |
any other things which confuse C |
512 |
(its probably a good idea to write something into a queue here)" |
|
513 |
||
514 |
action notNil ifTrue:[ |
|
10613 | 515 |
^ action valueWithArguments:argList |
10609 | 516 |
]. |
517 |
^ nil |
|
10607 | 518 |
! ! |
519 |
||
520 |
!ExternalFunctionCallback methodsFor:'generation'! |
|
521 |
||
10613 | 522 |
code |
523 |
self hasCode ifFalse:[ |
|
14626 | 524 |
self generateClosure |
10607 | 525 |
]. |
10613 | 526 |
^ super code |
10607 | 527 |
|
528 |
"Created: / 11-06-2007 / 15:53:00 / cg" |
|
22545 | 529 |
! |
530 |
||
531 |
getCode |
|
532 |
^ super code |
|
533 |
||
534 |
"Created: / 03-03-2017 / 13:55:00 / cg" |
|
10607 | 535 |
! ! |
536 |
||
537 |
!ExternalFunctionCallback methodsFor:'private-accessing'! |
|
538 |
||
539 |
returnType:aReturnType argumentTypes:argTypes |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
540 |
"see generateClosure for valid return types" |
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
541 |
|
10607 | 542 |
returnType := aReturnType. |
543 |
argumentTypes := argTypes. |
|
544 |
||
545 |
"Created: / 11-06-2007 / 15:52:01 / cg" |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
546 |
"Modified: / 19-09-2007 / 18:14:59 / cg" |
10607 | 547 |
! ! |
548 |
||
549 |
!ExternalFunctionCallback methodsFor:'private-generation'! |
|
550 |
||
551 |
generateClosure |
|
10609 | 552 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo |
553 |
callTypeNumber returnValueClass argValueClass callBackIndex| |
|
10607 | 554 |
|
555 |
argTypeSymbols := argumentTypes. |
|
556 |
returnTypeSymbol := returnType. |
|
10609 | 557 |
callBackIndex := self class closureIndexFor:self. |
10621 | 558 |
callTypeNumber := self callTypeNumber. |
10609 | 559 |
%{ |
10607 | 560 |
#ifdef HAVE_FFI |
10609 | 561 |
ffi_cif *pcif; |
10607 | 562 |
ffi_type *__returnType = NULL; |
563 |
static int null = 0; |
|
564 |
int i; |
|
565 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
566 |
int __numArgsWanted; |
|
10609 | 567 |
struct closurePlusCIF { |
22545 | 568 |
ffi_closure closure; |
569 |
ffi_cif cif; |
|
570 |
ffi_type *argTypes[MAX_ARGS]; |
|
10609 | 571 |
} *closurePlusCIFp; |
10607 | 572 |
ffi_closure *pcl; |
10609 | 573 |
ffi_cif *cif; |
574 |
ffi_type **argTypePtrs; |
|
575 |
||
576 |
closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF)); |
|
577 |
cif = &(closurePlusCIFp->cif); |
|
578 |
argTypePtrs = closurePlusCIFp->argTypes; |
|
579 |
pcl = &(closurePlusCIFp->closure); |
|
10607 | 580 |
|
21623 | 581 |
# define __FAIL__(fcode) \ |
22545 | 582 |
{ \ |
583 |
failureCode = fcode; free(closurePlusCIFp); goto getOutOfHere; \ |
|
584 |
} |
|
10607 | 585 |
|
586 |
if (argTypeSymbols == nil) { |
|
22545 | 587 |
__numArgsWanted = 0; |
21623 | 588 |
} else if (__isArrayLike(argTypeSymbols)) { |
22545 | 589 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10607 | 590 |
} else { |
22545 | 591 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10607 | 592 |
} |
593 |
||
594 |
if (__numArgsWanted > MAX_ARGS) { |
|
22545 | 595 |
__FAIL__(@symbol(TooManyArguments)) |
10607 | 596 |
} |
597 |
||
598 |
/* |
|
599 |
* validate the return type |
|
600 |
*/ |
|
601 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
|
22545 | 602 |
returnTypeSymbol = @symbol(handle); |
10607 | 603 |
} |
604 |
||
605 |
if (returnTypeSymbol == @symbol(int)) { |
|
22545 | 606 |
__returnType = __get_ffi_type_sint(); |
10607 | 607 |
} else if (returnTypeSymbol == @symbol(uint)) { |
22545 | 608 |
__returnType = __get_ffi_type_uint(); |
10607 | 609 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
22545 | 610 |
__returnType = __get_ffi_type_uint8(); |
10607 | 611 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
22545 | 612 |
__returnType = __get_ffi_type_uint16(); |
10607 | 613 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
22545 | 614 |
__returnType = __get_ffi_type_uint32(); |
10607 | 615 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
22545 | 616 |
__returnType = __get_ffi_type_uint64(); |
10607 | 617 |
|
618 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
22545 | 619 |
__returnType = __get_ffi_type_sint(); |
10607 | 620 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
22545 | 621 |
__returnType = __get_ffi_type_sint8(); |
10607 | 622 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
22545 | 623 |
__returnType = __get_ffi_type_sint16(); |
10607 | 624 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
22545 | 625 |
__returnType = __get_ffi_type_sint32(); |
10607 | 626 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
22545 | 627 |
__returnType = __get_ffi_type_sint64(); |
10607 | 628 |
|
629 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
22545 | 630 |
if (sizeof(long) == 4) { |
631 |
returnTypeSymbol = @symbol(sint32); |
|
632 |
__returnType = __get_ffi_type_sint32(); |
|
633 |
} else if (sizeof(long) == 8) { |
|
634 |
returnTypeSymbol = @symbol(sint64); |
|
635 |
__returnType = __get_ffi_type_sint64(); |
|
636 |
} else { |
|
637 |
__FAIL__(@symbol(UnknownReturnType)) |
|
638 |
} |
|
10607 | 639 |
|
640 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
22545 | 641 |
if (sizeof(long) == 4) { |
642 |
returnTypeSymbol = @symbol(uint32); |
|
643 |
__returnType = __get_ffi_type_uint32(); |
|
644 |
}else if (sizeof(long) == 8) { |
|
645 |
returnTypeSymbol = @symbol(uint64); |
|
646 |
__returnType = __get_ffi_type_uint64(); |
|
647 |
} else { |
|
648 |
__FAIL__(@symbol(UnknownReturnType)) |
|
649 |
} |
|
10607 | 650 |
|
651 |
} else if (returnTypeSymbol == @symbol(bool)) { |
|
22545 | 652 |
__returnType = __get_ffi_type_uint(); |
10607 | 653 |
|
654 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
22545 | 655 |
__returnType = __get_ffi_type_float(); |
10607 | 656 |
} else if (returnTypeSymbol == @symbol(double)) { |
22545 | 657 |
__returnType = __get_ffi_type_double(); |
10607 | 658 |
|
659 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
22545 | 660 |
__returnType = __get_ffi_type_void(); |
10607 | 661 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
22545 | 662 |
|| (returnTypeSymbol == @symbol(handle)) |
663 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
664 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
665 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
666 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
667 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
668 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
669 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
670 |
__returnType = __get_ffi_type_pointer(); |
|
10607 | 671 |
} else { |
22545 | 672 |
if (__isSymbol(returnTypeSymbol) |
673 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
674 |
if (! __isBehaviorLike(returnValueClass)) { |
|
675 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
676 |
} |
|
677 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
678 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
679 |
} |
|
680 |
__returnType = __get_ffi_type_pointer(); |
|
681 |
returnTypeSymbol = @symbol(pointer); |
|
682 |
} else { |
|
683 |
__FAIL__(@symbol(UnknownReturnType)) |
|
684 |
} |
|
10607 | 685 |
} |
686 |
||
687 |
/* |
|
688 |
* setup arg-buffers |
|
689 |
*/ |
|
690 |
for (i=0; i<__numArgsWanted; i++) { |
|
22545 | 691 |
ffi_type *thisType; |
692 |
void *argValuePtr; |
|
693 |
OBJ typeSymbol; |
|
10607 | 694 |
|
22545 | 695 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
10607 | 696 |
|
22545 | 697 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
10607 | 698 |
|
22545 | 699 |
if (typeSymbol == @symbol(handle)) { |
700 |
typeSymbol = @symbol(pointer); |
|
701 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
702 |
typeSymbol = @symbol(pointer); |
|
703 |
} |
|
10607 | 704 |
|
22545 | 705 |
if (typeSymbol == @symbol(long)) { |
706 |
if (sizeof(long) == sizeof(int)) { |
|
707 |
typeSymbol = @symbol(sint); |
|
708 |
} else { |
|
709 |
if (sizeof(long) == 4) { |
|
710 |
typeSymbol = @symbol(sint32); |
|
711 |
} else if (sizeof(long) == 8) { |
|
712 |
typeSymbol = @symbol(sint64); |
|
713 |
} |
|
714 |
} |
|
715 |
} |
|
716 |
if (typeSymbol == @symbol(ulong)) { |
|
717 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
718 |
typeSymbol = @symbol(uint); |
|
719 |
} else { |
|
720 |
if (sizeof(long) == 4) { |
|
721 |
typeSymbol = @symbol(uint32); |
|
722 |
} else if (sizeof(long) == 8) { |
|
723 |
typeSymbol = @symbol(uint64); |
|
724 |
} |
|
725 |
} |
|
726 |
} |
|
10607 | 727 |
|
22545 | 728 |
if (typeSymbol == @symbol(int)) { |
729 |
thisType = __get_ffi_type_sint(); |
|
730 |
} else if (typeSymbol == @symbol(uint)) { |
|
731 |
thisType = __get_ffi_type_uint(); |
|
732 |
} else if (typeSymbol == @symbol(uint8)) { |
|
733 |
thisType = __get_ffi_type_uint8(); |
|
734 |
} else if (typeSymbol == @symbol(sint8)) { |
|
735 |
thisType = __get_ffi_type_sint8(); |
|
736 |
} else if (typeSymbol == @symbol(uint16)) { |
|
737 |
thisType = __get_ffi_type_uint16(); |
|
738 |
} else if (typeSymbol == @symbol(sint16)) { |
|
739 |
thisType = __get_ffi_type_sint16(); |
|
740 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
|
741 |
thisType = __get_ffi_type_uint32(); |
|
742 |
} else if (typeSymbol == @symbol(float)) { |
|
743 |
thisType = __get_ffi_type_float(); |
|
744 |
} else if (typeSymbol == @symbol(double)) { |
|
745 |
thisType = __get_ffi_type_double(); |
|
746 |
} else if (typeSymbol == @symbol(void)) { |
|
747 |
thisType = __get_ffi_type_void(); |
|
748 |
} else if (typeSymbol == @symbol(charPointer)) { |
|
749 |
thisType = __get_ffi_type_pointer(); |
|
750 |
} else if (typeSymbol == @symbol(floatPointer)) { |
|
751 |
thisType = __get_ffi_type_pointer(); |
|
752 |
} else if (typeSymbol == @symbol(doublePointer)) { |
|
753 |
thisType = __get_ffi_type_pointer(); |
|
754 |
} else if (typeSymbol == @symbol(pointer)) { |
|
10607 | 755 |
commonPointerTypeArg: ; |
22545 | 756 |
thisType = __get_ffi_type_pointer(); |
757 |
} else if (typeSymbol == @symbol(bool)) { |
|
758 |
thisType = __get_ffi_type_uint(); |
|
759 |
} else if (__isSymbol(typeSymbol) |
|
760 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
761 |
if (! __isBehaviorLike(argValueClass)) { |
|
762 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
763 |
} |
|
764 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
765 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
766 |
} |
|
767 |
goto commonPointerTypeArg; /* sorry */ |
|
768 |
} else { |
|
769 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
770 |
} |
|
10607 | 771 |
|
22545 | 772 |
closurePlusCIFp->argTypes[i] = thisType; |
10607 | 773 |
} |
774 |
failureInfo = nil; |
|
775 |
||
776 |
__callType = FFI_DEFAULT_ABI; |
|
22545 | 777 |
#ifdef __osx__ |
778 |
__callType = CALLTYPE_FFI_UNIX64; |
|
779 |
#else |
|
10607 | 780 |
if (callTypeNumber != nil) { |
22545 | 781 |
# ifdef CALLTYPE_FFI_STDCALL |
782 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) { |
|
783 |
__callType = CALLTYPE_FFI_STDCALL; |
|
784 |
} |
|
785 |
# endif |
|
786 |
# ifdef CALLTYPE_FFI_V8 |
|
787 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) { |
|
788 |
__callType = CALLTYPE_FFI_V8; |
|
789 |
} |
|
790 |
# endif |
|
791 |
# ifdef CALLTYPE_FFI_V9 |
|
792 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) { |
|
793 |
__callType = CALLTYPE_FFI_V9; |
|
794 |
} |
|
795 |
# endif |
|
796 |
# ifdef CALLTYPE_FFI_UNIX64 |
|
797 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) { |
|
798 |
__callType = CALLTYPE_FFI_UNIX64; |
|
799 |
} |
|
800 |
# endif |
|
801 |
} |
|
10607 | 802 |
#endif |
22545 | 803 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
804 |
printf("prep_cif cif-ptr=%"_lx_"\n", (INT)cif); |
|
805 |
} |
|
806 |
||
807 |
if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) { |
|
808 |
__FAIL__(@symbol(FFIPrepareFailed)) |
|
10607 | 809 |
} |
810 |
||
10609 | 811 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22545 | 812 |
printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure)); |
813 |
printf("index is %"_ld_"\n", __intVal(callBackIndex)); |
|
10609 | 814 |
} |
815 |
if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) { |
|
22545 | 816 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
10607 | 817 |
} |
10609 | 818 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22545 | 819 |
printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif)); |
820 |
printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun)); |
|
821 |
printf("pcl code at %"_lx_" is:\n", (INT)pcl); |
|
822 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]); |
|
823 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]); |
|
824 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]); |
|
825 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]); |
|
10609 | 826 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
827 |
__INST(code_) = (OBJ)pcl; |
10609 | 828 |
|
829 |
#if 0 |
|
20307 | 830 |
ExternalFunctionCallback__test_call_closure((INTLFUNC)pcl); |
10607 | 831 |
#endif |
832 |
||
833 |
#else /* no FFI support */ |
|
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10617
diff
changeset
|
834 |
failureCode = @symbol(FFINotSupported); |
10607 | 835 |
#endif /* HAVE_FFI */ |
836 |
getOutOfHere: ; |
|
837 |
%}. |
|
838 |
failureCode notNil ifTrue:[ |
|
22545 | 839 |
self primitiveFailed:(failureCode->failureInfo). "see failureCode and failureInfo for details" |
840 |
^ nil |
|
10607 | 841 |
]. |
842 |
||
843 |
"Created: / 11-06-2007 / 21:53:02 / cg" |
|
844 |
! ! |
|
10609 | 845 |
|
846 |
!ExternalFunctionCallback methodsFor:'private-releasing'! |
|
847 |
||
848 |
release |
|
849 |
|idx| |
|
850 |
||
851 |
idx := CallBackRegistry identityIndexOf:self. |
|
852 |
CallBackRegistry at:idx put:nil. |
|
853 |
%{ |
|
10613 | 854 |
void *pcl = (void *)__INST(code_); |
10609 | 855 |
|
10613 | 856 |
__INST(code_) = 0; |
10609 | 857 |
if (pcl) { |
13574 | 858 |
free(pcl); |
859 |
RETURN(self); |
|
10609 | 860 |
} |
861 |
%}. |
|
862 |
self invalidateReference. |
|
863 |
! ! |
|
864 |
||
865 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
|
866 |
||
867 |
version |
|
20123 | 868 |
^ '$Header$' |
12461
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
869 |
! |
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
870 |
|
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
871 |
version_CVS |
20123 | 872 |
^ '$Header$' |
10609 | 873 |
! ! |
22545 | 874 |