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