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