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