author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 10722 | ab6fbe61f116 |
child 17728 | bbc5fa73dfab |
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: |
10613 | 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 |
|
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
265 |
Eventually, the callback should be released: |
10613 | 266 |
cb release. |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
267 |
|
10609 | 268 |
[author:] |
10613 | 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 |
||
10620 | 294 |
!ExternalFunctionCallback class methodsFor:'constants'! |
295 |
||
296 |
callTypeAPI |
|
297 |
^ ExternalLibraryFunction callTypeAPI |
|
298 |
! |
|
299 |
||
300 |
callTypeC |
|
301 |
^ ExternalLibraryFunction callTypeC |
|
302 |
! |
|
303 |
||
304 |
callTypeCDecl |
|
305 |
^ ExternalLibraryFunction callTypeCDecl |
|
306 |
! |
|
307 |
||
308 |
callTypeMASK |
|
309 |
^ ExternalLibraryFunction callTypeMASK |
|
310 |
! |
|
311 |
||
312 |
callTypeOLE |
|
313 |
^ ExternalLibraryFunction callTypeOLE |
|
314 |
! ! |
|
315 |
||
10609 | 316 |
!ExternalFunctionCallback class methodsFor:'helpers'! |
317 |
||
318 |
closureIndexFor:aCallBack |
|
319 |
CallBackRegistry isNil ifTrue:[ |
|
320 |
CallBackRegistry := WeakArray with:aCallBack. |
|
321 |
] ifFalse:[ |
|
322 |
CallBackRegistry := CallBackRegistry copyWith:aCallBack. |
|
323 |
]. |
|
324 |
^ CallBackRegistry size. |
|
325 |
! |
|
326 |
||
327 |
testCall:aCallback withArgument:arg |
|
10613 | 328 |
"a simple test, if I can be called" |
10609 | 329 |
%{ |
330 |
INTFUNC f = __externalAddressVal(aCallback); |
|
331 |
INT result; |
|
10613 | 332 |
void *c_arg = 0; |
10609 | 333 |
|
10613 | 334 |
if (__isSmallInteger(arg)) { |
335 |
c_arg = (void *)(__intVal(arg)); |
|
336 |
} else { |
|
337 |
if (arg == true) { |
|
338 |
c_arg = (void *)1; |
|
339 |
} else { |
|
340 |
if (__isString(arg)) { |
|
341 |
c_arg = (void *)__stringVal(arg); |
|
342 |
} |
|
343 |
} |
|
344 |
} |
|
345 |
fprintf(stderr, "ExternalFunctionCallback: calling callBack %x(%x)\n", f, c_arg); |
|
346 |
result = (*f)(c_arg); |
|
10610 | 347 |
fprintf(stderr, "ExternalFunctionCallback: result from callBack is %x\n", result); |
10609 | 348 |
%} |
349 |
! ! |
|
350 |
||
10607 | 351 |
!ExternalFunctionCallback methodsFor:'accessing'! |
352 |
||
10613 | 353 |
action:aBlock |
354 |
"set the action-block, to be evaluated when C calls me. |
|
355 |
The C-arguments will be passed as arguments to the block. |
|
356 |
The value returned by the block will be returned to the C-caller." |
|
357 |
||
358 |
action := aBlock. |
|
10620 | 359 |
! |
360 |
||
361 |
beCallTypeAPI |
|
362 |
flags := (flags ? 0) bitOr: (self class callTypeAPI). |
|
363 |
||
364 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
365 |
! |
|
366 |
||
367 |
beCallTypeC |
|
368 |
flags := (flags ? 0) bitOr: (self class callTypeC). |
|
369 |
||
370 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
371 |
! |
|
372 |
||
373 |
beCallTypeOLE |
|
374 |
flags := (flags ? 0) bitOr: (self class callTypeOLE). |
|
375 |
||
376 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
377 |
! |
|
378 |
||
379 |
beCallTypeWINAPI |
|
380 |
self beCallTypeAPI |
|
381 |
||
382 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
383 |
! |
|
384 |
||
385 |
callTypeNumber |
|
386 |
^ (flags ? 0) bitAnd: (self class callTypeMASK) |
|
387 |
||
388 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
389 |
! |
|
390 |
||
391 |
isCallTypeAPI |
|
392 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeAPI) |
|
393 |
||
394 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
395 |
! |
|
396 |
||
397 |
isCallTypeC |
|
398 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeC) |
|
399 |
! |
|
400 |
||
401 |
isCallTypeOLE |
|
402 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeOLE) |
|
10609 | 403 |
! ! |
404 |
||
405 |
!ExternalFunctionCallback methodsFor:'callback'! |
|
406 |
||
10613 | 407 |
callFromCWith:argList |
408 |
"invoked by the C-code, to which we have given out the code-ptr. |
|
409 |
Because this is evaluated from C, we probably should not block or abort or do |
|
10609 | 410 |
any other things which confuse C |
411 |
(its probably a good idea to write something into a queue here)" |
|
412 |
||
413 |
action notNil ifTrue:[ |
|
10613 | 414 |
^ action valueWithArguments:argList |
10609 | 415 |
]. |
416 |
^ nil |
|
10607 | 417 |
! ! |
418 |
||
419 |
!ExternalFunctionCallback methodsFor:'generation'! |
|
420 |
||
10613 | 421 |
code |
422 |
self hasCode ifFalse:[ |
|
10607 | 423 |
self generate |
424 |
]. |
|
10613 | 425 |
^ super code |
10607 | 426 |
|
427 |
"Created: / 11-06-2007 / 15:53:00 / cg" |
|
428 |
! ! |
|
429 |
||
430 |
!ExternalFunctionCallback methodsFor:'private-accessing'! |
|
431 |
||
432 |
returnType:aReturnType argumentTypes:argTypes |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
433 |
"see generateClosure for valid return types" |
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
434 |
|
10607 | 435 |
returnType := aReturnType. |
436 |
argumentTypes := argTypes. |
|
437 |
||
438 |
"Created: / 11-06-2007 / 15:52:01 / cg" |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
439 |
"Modified: / 19-09-2007 / 18:14:59 / cg" |
10607 | 440 |
! ! |
441 |
||
442 |
!ExternalFunctionCallback methodsFor:'private-generation'! |
|
443 |
||
444 |
generateClosure |
|
10609 | 445 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo |
446 |
callTypeNumber returnValueClass argValueClass callBackIndex| |
|
10607 | 447 |
|
448 |
argTypeSymbols := argumentTypes. |
|
449 |
returnTypeSymbol := returnType. |
|
10609 | 450 |
callBackIndex := self class closureIndexFor:self. |
10621 | 451 |
callTypeNumber := self callTypeNumber. |
10609 | 452 |
%{ |
10607 | 453 |
#ifdef HAVE_FFI |
10609 | 454 |
ffi_cif *pcif; |
10607 | 455 |
ffi_type *__returnType = NULL; |
456 |
static int null = 0; |
|
457 |
int i; |
|
458 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
459 |
int __numArgsWanted; |
|
10609 | 460 |
struct closurePlusCIF { |
461 |
ffi_closure closure; |
|
462 |
ffi_cif cif; |
|
463 |
ffi_type *argTypes[MAX_ARGS]; |
|
464 |
} *closurePlusCIFp; |
|
10607 | 465 |
ffi_closure *pcl; |
10609 | 466 |
ffi_cif *cif; |
467 |
ffi_type **argTypePtrs; |
|
468 |
||
469 |
closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF)); |
|
470 |
cif = &(closurePlusCIFp->cif); |
|
471 |
argTypePtrs = closurePlusCIFp->argTypes; |
|
472 |
pcl = &(closurePlusCIFp->closure); |
|
10607 | 473 |
|
474 |
#define __FAIL__(fcode) \ |
|
475 |
{ \ |
|
10609 | 476 |
failureCode = fcode; free(closurePlusCIFp); goto getOutOfHere; \ |
10607 | 477 |
} |
478 |
||
479 |
if (argTypeSymbols == nil) { |
|
480 |
__numArgsWanted = 0; |
|
481 |
} else if (__isArray(argTypeSymbols)) { |
|
482 |
__numArgsWanted = __arraySize(argTypeSymbols); |
|
483 |
} else { |
|
484 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
|
485 |
} |
|
486 |
||
487 |
if (__numArgsWanted > MAX_ARGS) { |
|
488 |
__FAIL__(@symbol(TooManyArguments)) |
|
489 |
} |
|
490 |
||
491 |
/* |
|
492 |
* validate the return type |
|
493 |
*/ |
|
494 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
|
495 |
returnTypeSymbol = @symbol(handle); |
|
496 |
} |
|
497 |
||
498 |
if (returnTypeSymbol == @symbol(int)) { |
|
499 |
__returnType = __get_ffi_type_sint(); |
|
500 |
} else if (returnTypeSymbol == @symbol(uint)) { |
|
501 |
__returnType = __get_ffi_type_uint(); |
|
502 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
|
503 |
__returnType = __get_ffi_type_uint8(); |
|
504 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
|
505 |
__returnType = __get_ffi_type_uint16(); |
|
506 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
|
507 |
__returnType = __get_ffi_type_uint32(); |
|
508 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
|
509 |
__returnType = __get_ffi_type_uint64(); |
|
510 |
||
511 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
512 |
__returnType = __get_ffi_type_sint(); |
|
513 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
|
514 |
__returnType = __get_ffi_type_sint8(); |
|
515 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
|
516 |
__returnType = __get_ffi_type_sint16(); |
|
517 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
|
518 |
__returnType = __get_ffi_type_sint32(); |
|
519 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
|
520 |
__returnType = __get_ffi_type_sint64(); |
|
521 |
||
522 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
523 |
if (sizeof(long) == 4) { |
|
524 |
returnTypeSymbol = @symbol(sint32); |
|
525 |
__returnType = __get_ffi_type_sint32(); |
|
526 |
} else if (sizeof(long) == 8) { |
|
527 |
returnTypeSymbol = @symbol(sint64); |
|
528 |
__returnType = __get_ffi_type_sint64(); |
|
529 |
} else { |
|
530 |
__FAIL__(@symbol(UnknownReturnType)) |
|
531 |
} |
|
532 |
||
533 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
534 |
if (sizeof(long) == 4) { |
|
535 |
returnTypeSymbol = @symbol(uint32); |
|
536 |
__returnType = __get_ffi_type_uint32(); |
|
537 |
}else if (sizeof(long) == 8) { |
|
538 |
returnTypeSymbol = @symbol(uint64); |
|
539 |
__returnType = __get_ffi_type_uint64(); |
|
540 |
} else { |
|
541 |
__FAIL__(@symbol(UnknownReturnType)) |
|
542 |
} |
|
543 |
||
544 |
} else if (returnTypeSymbol == @symbol(bool)) { |
|
545 |
__returnType = __get_ffi_type_uint(); |
|
546 |
||
547 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
548 |
__returnType = __get_ffi_type_float(); |
|
549 |
} else if (returnTypeSymbol == @symbol(double)) { |
|
550 |
__returnType = __get_ffi_type_double(); |
|
551 |
||
552 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
553 |
__returnType = __get_ffi_type_void(); |
|
554 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
|
555 |
|| (returnTypeSymbol == @symbol(handle)) |
|
556 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
557 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
558 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
559 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
560 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
561 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
562 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
563 |
__returnType = __get_ffi_type_pointer(); |
|
564 |
} else { |
|
565 |
if (__isSymbol(returnTypeSymbol) |
|
566 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
567 |
if (! __isBehaviorLike(returnValueClass)) { |
|
568 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
569 |
} |
|
570 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
571 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
572 |
} |
|
573 |
__returnType = __get_ffi_type_pointer(); |
|
574 |
returnTypeSymbol = @symbol(pointer); |
|
575 |
} else { |
|
576 |
__FAIL__(@symbol(UnknownReturnType)) |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
/* |
|
581 |
* setup arg-buffers |
|
582 |
*/ |
|
583 |
for (i=0; i<__numArgsWanted; i++) { |
|
584 |
ffi_type *thisType; |
|
585 |
void *argValuePtr; |
|
586 |
OBJ typeSymbol; |
|
587 |
||
588 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
|
589 |
||
590 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
|
591 |
||
592 |
if (typeSymbol == @symbol(handle)) { |
|
593 |
typeSymbol = @symbol(pointer); |
|
594 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
595 |
typeSymbol = @symbol(pointer); |
|
596 |
} |
|
597 |
||
598 |
if (typeSymbol == @symbol(long)) { |
|
599 |
if (sizeof(long) == sizeof(int)) { |
|
600 |
typeSymbol = @symbol(sint); |
|
601 |
} else { |
|
602 |
if (sizeof(long) == 4) { |
|
603 |
typeSymbol = @symbol(sint32); |
|
604 |
} else if (sizeof(long) == 8) { |
|
605 |
typeSymbol = @symbol(sint64); |
|
606 |
} |
|
607 |
} |
|
608 |
} |
|
609 |
if (typeSymbol == @symbol(ulong)) { |
|
610 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
611 |
typeSymbol = @symbol(uint); |
|
612 |
} else { |
|
613 |
if (sizeof(long) == 4) { |
|
614 |
typeSymbol = @symbol(uint32); |
|
615 |
} else if (sizeof(long) == 8) { |
|
616 |
typeSymbol = @symbol(uint64); |
|
617 |
} |
|
618 |
} |
|
619 |
} |
|
620 |
||
621 |
if (typeSymbol == @symbol(int)) { |
|
622 |
thisType = __get_ffi_type_sint(); |
|
623 |
} else if (typeSymbol == @symbol(uint)) { |
|
624 |
thisType = __get_ffi_type_uint(); |
|
625 |
} else if (typeSymbol == @symbol(uint8)) { |
|
626 |
thisType = __get_ffi_type_uint8(); |
|
627 |
} else if (typeSymbol == @symbol(sint8)) { |
|
628 |
thisType = __get_ffi_type_sint8(); |
|
629 |
} else if (typeSymbol == @symbol(uint16)) { |
|
630 |
thisType = __get_ffi_type_uint16(); |
|
631 |
} else if (typeSymbol == @symbol(sint16)) { |
|
632 |
thisType = __get_ffi_type_sint16(); |
|
633 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
|
634 |
thisType = __get_ffi_type_uint32(); |
|
635 |
} else if (typeSymbol == @symbol(float)) { |
|
636 |
thisType = __get_ffi_type_float(); |
|
637 |
} else if (typeSymbol == @symbol(double)) { |
|
638 |
thisType = __get_ffi_type_double(); |
|
639 |
} else if (typeSymbol == @symbol(void)) { |
|
640 |
thisType = __get_ffi_type_void(); |
|
641 |
} else if (typeSymbol == @symbol(charPointer)) { |
|
642 |
thisType = __get_ffi_type_pointer(); |
|
643 |
} else if (typeSymbol == @symbol(floatPointer)) { |
|
644 |
thisType = __get_ffi_type_pointer(); |
|
645 |
} else if (typeSymbol == @symbol(doublePointer)) { |
|
646 |
thisType = __get_ffi_type_pointer(); |
|
647 |
} else if (typeSymbol == @symbol(pointer)) { |
|
648 |
commonPointerTypeArg: ; |
|
649 |
thisType = __get_ffi_type_pointer(); |
|
650 |
} else if (typeSymbol == @symbol(bool)) { |
|
651 |
thisType = __get_ffi_type_uint(); |
|
652 |
} else if (__isSymbol(typeSymbol) |
|
653 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
654 |
if (! __isBehaviorLike(argValueClass)) { |
|
655 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
656 |
} |
|
657 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
658 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
659 |
} |
|
660 |
goto commonPointerTypeArg; /* sorry */ |
|
661 |
} else { |
|
662 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
663 |
} |
|
664 |
||
10609 | 665 |
closurePlusCIFp->argTypes[i] = thisType; |
10607 | 666 |
} |
667 |
failureInfo = nil; |
|
668 |
||
669 |
__callType = FFI_DEFAULT_ABI; |
|
670 |
||
671 |
if (callTypeNumber != nil) { |
|
672 |
#ifdef CALLTYPE_FFI_STDCALL |
|
673 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) { |
|
674 |
__callType = CALLTYPE_FFI_STDCALL; |
|
675 |
} |
|
676 |
#endif |
|
677 |
#ifdef CALLTYPE_FFI_V8 |
|
678 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) { |
|
679 |
__callType = CALLTYPE_FFI_V8; |
|
680 |
} |
|
681 |
#endif |
|
682 |
#ifdef CALLTYPE_FFI_V9 |
|
683 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) { |
|
684 |
__callType = CALLTYPE_FFI_V9; |
|
685 |
} |
|
686 |
#endif |
|
687 |
#ifdef CALLTYPE_FFI_UNIX64 |
|
688 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) { |
|
689 |
__callType = CALLTYPE_FFI_UNIX64; |
|
690 |
} |
|
691 |
#endif |
|
692 |
} |
|
693 |
||
10609 | 694 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
695 |
printf("prep_cif cif-ptr=%x\n", cif); |
|
696 |
} |
|
697 |
||
698 |
if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) { |
|
10607 | 699 |
__FAIL__(@symbol(FFIPrepareFailed)) |
700 |
} |
|
10609 | 701 |
|
702 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
703 |
printf("closure is 0x%x (%d bytes)\n", pcl, sizeof(ffi_closure)); |
|
704 |
printf("index is %d\n", __intVal(callBackIndex)); |
|
705 |
} |
|
706 |
if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) { |
|
10607 | 707 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
708 |
} |
|
10609 | 709 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
710 |
printf("pcl->cif is 0x%x\n", pcl->cif); |
|
711 |
printf("pcl->fun is 0x%x\n", pcl->fun); |
|
712 |
printf("pcl code at %x is:\n", pcl); |
|
713 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]); |
|
714 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]); |
|
715 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]); |
|
716 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]); |
|
717 |
} |
|
10613 | 718 |
__INST(code_) = pcl; |
10609 | 719 |
|
720 |
#if 0 |
|
721 |
ExternalFunctionCallback__test_call_closure((INTFUNC)pcl); |
|
10607 | 722 |
#endif |
723 |
||
724 |
#else /* no FFI support */ |
|
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10617
diff
changeset
|
725 |
failureCode = @symbol(FFINotSupported); |
10607 | 726 |
#endif /* HAVE_FFI */ |
727 |
getOutOfHere: ; |
|
728 |
%}. |
|
729 |
failureCode notNil ifTrue:[ |
|
730 |
self primitiveFailed:(failureCode->failureInfo). "see failureCode and failureInfo for details" |
|
731 |
^ nil |
|
732 |
]. |
|
733 |
||
734 |
"Created: / 11-06-2007 / 21:53:02 / cg" |
|
735 |
! ! |
|
10609 | 736 |
|
737 |
!ExternalFunctionCallback methodsFor:'private-releasing'! |
|
738 |
||
739 |
release |
|
740 |
|idx| |
|
741 |
||
742 |
idx := CallBackRegistry identityIndexOf:self. |
|
743 |
CallBackRegistry at:idx put:nil. |
|
744 |
%{ |
|
10613 | 745 |
void *pcl = (void *)__INST(code_); |
10609 | 746 |
|
10613 | 747 |
__INST(code_) = 0; |
10609 | 748 |
if (pcl) { |
749 |
free(pcl); |
|
750 |
} |
|
751 |
%}. |
|
752 |
self invalidateReference. |
|
753 |
||
754 |
! ! |
|
755 |
||
756 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
|
757 |
||
758 |
version |
|
17711 | 759 |
^ '$Id: ExternalFunctionCallback.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
10609 | 760 |
! ! |