author | ca |
Fri, 11 Aug 2006 13:20:24 +0200 | |
changeset 9525 | 120234adc94e |
parent 9524 | 2af286bbcac3 |
child 9603 | eec1a8d56cc5 |
permissions | -rw-r--r-- |
8728 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9464 | 3 |
All Rights Reserved |
8728 | 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 |
||
8533 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
15 |
ExternalFunction subclass:#ExternalLibraryFunction |
|
9465 | 16 |
instanceVariableNames:'flags moduleName returnType argumentTypes owningClass' |
9464 | 17 |
classVariableNames:'FLAG_VIRTUAL FLAG_NONVIRTUAL FLAG_ASYNC FLAG_UNLIMITEDSTACK |
9525 | 18 |
FLAG_RETVAL_IS_CONST CALLTYPE_MASK CALLTYPE_API CALLTYPE_C |
19 |
CALLTYPE_OLE CALLTYPE_V8 CALLTYPE_V9 CALLTYPE_UNIX64' |
|
9464 | 20 |
poolDictionaries:'' |
21 |
category:'System-Support' |
|
8533 | 22 |
! |
23 |
||
9322 | 24 |
!ExternalLibraryFunction primitiveDefinitions! |
25 |
%{ |
|
26 |
||
9337 | 27 |
/* |
28 |
* does this architecture support FFI ? |
|
9365 | 29 |
* NOTICE: this is now defined in the architecture-specific configuration file. |
9337 | 30 |
*/ |
9365 | 31 |
#if defined(WIN32) || defined(LINUX) |
32 |
# ifndef HAVE_FFI |
|
33 |
# define HAVE_FFI |
|
34 |
# endif |
|
9337 | 35 |
#endif |
36 |
||
37 |
||
38 |
#ifdef HAVE_FFI |
|
39 |
# include <ffi.h> |
|
9322 | 40 |
# define MAX_ARGS 128 |
9365 | 41 |
|
42 |
extern ffi_type *__get_ffi_type_sint(); |
|
43 |
extern ffi_type *__get_ffi_type_sint8(); |
|
44 |
extern ffi_type *__get_ffi_type_sint16(); |
|
45 |
extern ffi_type *__get_ffi_type_sint32(); |
|
46 |
extern ffi_type *__get_ffi_type_sint64(); |
|
47 |
extern ffi_type *__get_ffi_type_uint(); |
|
48 |
extern ffi_type *__get_ffi_type_uint8(); |
|
49 |
extern ffi_type *__get_ffi_type_uint16(); |
|
50 |
extern ffi_type *__get_ffi_type_uint32(); |
|
51 |
extern ffi_type *__get_ffi_type_uint64(); |
|
52 |
extern ffi_type *__get_ffi_type_float(); |
|
53 |
extern ffi_type *__get_ffi_type_double(); |
|
54 |
extern ffi_type *__get_ffi_type_void(); |
|
55 |
extern ffi_type *__get_ffi_type_pointer(); |
|
56 |
||
9337 | 57 |
#endif |
9322 | 58 |
|
59 |
%} |
|
60 |
! ! |
|
61 |
||
8728 | 62 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
63 |
||
64 |
copyright |
|
65 |
" |
|
66 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9464 | 67 |
All Rights Reserved |
8728 | 68 |
|
69 |
This software is furnished under a license and may be used |
|
70 |
only in accordance with the terms of that license and with the |
|
71 |
inclusion of the above copyright notice. This software may not |
|
72 |
be provided or otherwise made available to, or used by, any |
|
73 |
other person. No title to or ownership of the software is |
|
74 |
hereby transferred. |
|
75 |
" |
|
9322 | 76 |
! |
77 |
||
9331 | 78 |
documentation |
79 |
" |
|
80 |
instances of me are used to interface to external library functions (as found in a dll/shared object). |
|
81 |
When a special external-call pragma such as: |
|
9464 | 82 |
<api: bool MessageBeep(uint)> |
9331 | 83 |
|
84 |
is encountered by the parser in a method, the compiler generates a call via |
|
9464 | 85 |
<correspondingExternalLibraryFunctionObject> invokeWithArguments: argumentArray. |
9331 | 86 |
|
87 |
In the invoke method, the library is checked to be loaded (and loaded if not already), |
|
9336 | 88 |
the arguments are converted to C and pushed onto the C-stack, the function is called, |
9331 | 89 |
and finally, the return value is converted back from C to a smalltalk object. |
90 |
" |
|
91 |
! |
|
92 |
||
9322 | 93 |
example |
94 |
" |
|
9464 | 95 |
[exBegin] |
96 |
|f| |
|
9322 | 97 |
|
9464 | 98 |
f := ExternalLibraryFunction new. |
99 |
f name:'MessageBeep' |
|
100 |
module:'user32.dll' |
|
101 |
callType:#WINAPI |
|
102 |
returnType:#boolean |
|
103 |
argumentTypes:#(uint). |
|
9322 | 104 |
|
9464 | 105 |
f invokeWith:1. |
106 |
[exEnd] |
|
9322 | 107 |
" |
8728 | 108 |
! ! |
8533 | 109 |
|
8550 | 110 |
!ExternalLibraryFunction class methodsFor:'instance creation'! |
111 |
||
9465 | 112 |
name:functionName module:moduleName returnType:returnType argumentTypes:argTypes |
8550 | 113 |
^ self new |
9465 | 114 |
name:functionName module:moduleName |
115 |
returnType:returnType argumentTypes:argTypes |
|
116 |
||
117 |
"Created: / 01-08-2006 / 15:19:08 / cg" |
|
9463 | 118 |
! ! |
119 |
||
120 |
!ExternalLibraryFunction class methodsFor:'class initialization'! |
|
121 |
||
122 |
initialize |
|
9465 | 123 |
FLAG_VIRTUAL := 16r100. "/ a virtual c++ call |
124 |
FLAG_NONVIRTUAL := 16r200. "/ a non-virtual c++ call |
|
125 |
FLAG_UNLIMITEDSTACK := 16r400. "/ unlimitedstack under unix |
|
126 |
FLAG_ASYNC := 16r800. "/ async under win32 |
|
9525 | 127 |
FLAG_RETVAL_IS_CONST := 16r1000. "/ return value is not to be registered for finalization |
9463 | 128 |
|
9465 | 129 |
CALLTYPE_API := 1. "/ WINAPI-call (win32 only) |
130 |
CALLTYPE_C := 2. "/ regular C-call (the default) |
|
131 |
CALLTYPE_V8 := 3. "/ v8 call (sparc only) |
|
132 |
CALLTYPE_V9 := 4. "/ v9 call (sparc only) |
|
133 |
CALLTYPE_UNIX64 := 5. "/ unix64 call (alpha only) |
|
9463 | 134 |
|
9465 | 135 |
CALLTYPE_MASK := 16rFF. |
136 |
||
137 |
"Modified: / 01-08-2006 / 15:09:36 / cg" |
|
8550 | 138 |
! ! |
139 |
||
9321 | 140 |
!ExternalLibraryFunction class methodsFor:'constants'! |
141 |
||
142 |
callTypeAPI |
|
9463 | 143 |
^ CALLTYPE_API |
9321 | 144 |
|
9463 | 145 |
"Modified: / 01-08-2006 / 13:44:41 / cg" |
9435 | 146 |
! |
147 |
||
9321 | 148 |
callTypeC |
9463 | 149 |
^ CALLTYPE_C |
9321 | 150 |
|
9463 | 151 |
"Modified: / 01-08-2006 / 13:44:49 / cg" |
9435 | 152 |
! |
153 |
||
9321 | 154 |
callTypeCDecl |
9463 | 155 |
^ CALLTYPE_C |
9321 | 156 |
|
9463 | 157 |
"Modified: / 01-08-2006 / 13:44:52 / cg" |
9435 | 158 |
! |
159 |
||
9321 | 160 |
callTypeOLE |
9463 | 161 |
^ CALLTYPE_OLE |
9435 | 162 |
|
9463 | 163 |
"Modified: / 01-08-2006 / 13:44:57 / cg" |
9321 | 164 |
! ! |
165 |
||
166 |
!ExternalLibraryFunction methodsFor:'accessing'! |
|
167 |
||
168 |
argumentTypes |
|
169 |
^ argumentTypes |
|
9463 | 170 |
! |
171 |
||
172 |
beAsync |
|
173 |
"let this execute in a separate thread, in par with the other execution thread(s). |
|
174 |
Ignored under unix/linux (until those support multiple threads too)." |
|
175 |
||
176 |
flags := (flags ? 0) bitOr: FLAG_ASYNC. |
|
177 |
||
178 |
"Created: / 01-08-2006 / 13:42:38 / cg" |
|
179 |
! |
|
180 |
||
9465 | 181 |
beCallTypeAPI |
182 |
flags := (flags ? 0) bitOr: CALLTYPE_API. |
|
183 |
||
184 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
185 |
! |
|
186 |
||
187 |
beCallTypeC |
|
188 |
flags := (flags ? 0) bitOr: CALLTYPE_C. |
|
189 |
||
190 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
191 |
! |
|
192 |
||
193 |
beCallTypeUNIX64 |
|
194 |
flags := (flags ? 0) bitOr: CALLTYPE_UNIX64. |
|
195 |
||
196 |
"Created: / 01-08-2006 / 15:13:38 / cg" |
|
197 |
! |
|
198 |
||
199 |
beCallTypeV8 |
|
200 |
flags := (flags ? 0) bitOr: CALLTYPE_V8. |
|
201 |
||
202 |
"Created: / 01-08-2006 / 15:13:28 / cg" |
|
203 |
! |
|
204 |
||
205 |
beCallTypeV9 |
|
206 |
flags := (flags ? 0) bitOr: CALLTYPE_V9. |
|
207 |
||
208 |
"Created: / 01-08-2006 / 15:13:31 / cg" |
|
209 |
! |
|
210 |
||
211 |
beCallTypeWINAPI |
|
212 |
self beCallTypeAPI |
|
213 |
||
214 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
215 |
! |
|
216 |
||
9525 | 217 |
beConstReturnValue |
218 |
"specify that a pointer return value is not to be finalized |
|
219 |
(i.e. points to static data or data which is freed by c)" |
|
220 |
||
221 |
flags := (flags ? 0) bitOr: FLAG_RETVAL_IS_CONST. |
|
222 |
||
223 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
224 |
! |
|
225 |
||
9463 | 226 |
beNonVirtualCPP |
227 |
"specify this as a non-virtual c++-function" |
|
228 |
||
229 |
flags := (flags ? 0) bitOr: FLAG_NONVIRTUAL. |
|
230 |
||
231 |
"Created: / 01-08-2006 / 13:56:44 / cg" |
|
232 |
! |
|
233 |
||
234 |
beUnlimitedStack |
|
235 |
"let this execute on the c-stack (as opposed to the thread-stack) |
|
236 |
for unlimited auto-sized-stack under unix/linux. |
|
237 |
Ignored under windows." |
|
238 |
||
239 |
flags := (flags ? 0) bitOr: FLAG_UNLIMITEDSTACK. |
|
240 |
||
241 |
"Created: / 01-08-2006 / 13:41:54 / cg" |
|
242 |
! |
|
243 |
||
244 |
beVirtualCPP |
|
245 |
"specify this as a virtual c++-function" |
|
246 |
||
247 |
flags := (flags ? 0) bitOr: FLAG_VIRTUAL. |
|
248 |
||
249 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
250 |
! |
|
251 |
||
9465 | 252 |
callTypeNumber |
253 |
^ (flags ? 0) bitAnd: CALLTYPE_MASK. |
|
254 |
||
255 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
256 |
! |
|
257 |
||
9463 | 258 |
isAsync |
259 |
"is this executed in a separate thread, in par with the other execution thread(s) ?" |
|
260 |
||
261 |
^ (flags ? 0) bitTest: FLAG_ASYNC. |
|
262 |
||
263 |
"Created: / 01-08-2006 / 13:46:53 / cg" |
|
264 |
! |
|
265 |
||
9519 | 266 |
isCPPFunction |
267 |
"is this a virtual or non-virtual c++-function ?" |
|
268 |
||
269 |
^ (flags ? 0) bitTest: (FLAG_VIRTUAL bitOr: FLAG_NONVIRTUAL). |
|
270 |
||
271 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
272 |
! |
|
273 |
||
9465 | 274 |
isCallTypeAPI |
275 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_API. |
|
276 |
||
277 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
278 |
! |
|
279 |
||
280 |
isCallTypeC |
|
281 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_C. |
|
282 |
||
283 |
"Created: / 01-08-2006 / 15:21:23 / cg" |
|
284 |
! |
|
285 |
||
9525 | 286 |
isConstReturnValue |
287 |
"is the pointer return value not to be finalized |
|
288 |
(i.e. points to static data or data which is freed by c)" |
|
289 |
||
290 |
^ (flags ? 0) bitTest: FLAG_RETVAL_IS_CONST. |
|
291 |
||
292 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
293 |
! |
|
294 |
||
9463 | 295 |
isNonVirtualCPP |
296 |
"is this a non-virtual c++-function ?" |
|
297 |
||
298 |
^ (flags ? 0) bitTest: FLAG_NONVIRTUAL. |
|
299 |
||
300 |
"Created: / 01-08-2006 / 13:56:51 / cg" |
|
301 |
! |
|
302 |
||
303 |
isUnlimitedStack |
|
304 |
"will this execute on the c-stack (as opposed to the thread-stack) |
|
305 |
for unlimited auto-sized-stack under unix/linux. |
|
306 |
Ignored under windows." |
|
307 |
||
308 |
^ (flags ? 0) bitTest: FLAG_UNLIMITEDSTACK. |
|
309 |
||
310 |
"Created: / 01-08-2006 / 14:17:07 / cg" |
|
311 |
! |
|
312 |
||
313 |
isVirtualCPP |
|
314 |
"is this a virtual c++-function ?" |
|
315 |
||
316 |
^ (flags ? 0) bitTest: FLAG_VIRTUAL. |
|
317 |
||
318 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
9321 | 319 |
! ! |
320 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
321 |
!ExternalLibraryFunction methodsFor:'invoking'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
322 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
323 |
invoke |
9321 | 324 |
self hasCode ifFalse:[ |
9464 | 325 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
326 |
]. |
9321 | 327 |
^ self invokeFFIWithArguments:#() |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
328 |
! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
329 |
|
9347 | 330 |
invokeCPPVirtualOn:anInstance |
9341 | 331 |
self hasCode ifFalse:[ |
332 |
self prepareInvoke. |
|
333 |
]. |
|
9347 | 334 |
^ self invokeCPPVirtualFFIOn:anInstance withArguments:#() |
9341 | 335 |
! |
336 |
||
9347 | 337 |
invokeCPPVirtualOn:instance with:arg |
9341 | 338 |
self hasCode ifFalse:[ |
339 |
self prepareInvoke. |
|
340 |
]. |
|
9347 | 341 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg) |
9341 | 342 |
! |
343 |
||
9347 | 344 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 |
9341 | 345 |
self hasCode ifFalse:[ |
346 |
self prepareInvoke. |
|
347 |
]. |
|
9347 | 348 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2) |
9341 | 349 |
! |
350 |
||
9347 | 351 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 |
9341 | 352 |
self hasCode ifFalse:[ |
353 |
self prepareInvoke. |
|
354 |
]. |
|
9347 | 355 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3) |
9341 | 356 |
! |
357 |
||
9347 | 358 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 with:arg4 |
9341 | 359 |
self hasCode ifFalse:[ |
360 |
self prepareInvoke. |
|
361 |
]. |
|
9347 | 362 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
9341 | 363 |
! |
364 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
365 |
invokeWith:arg |
9321 | 366 |
self hasCode ifFalse:[ |
9464 | 367 |
self prepareInvoke. |
9321 | 368 |
]. |
369 |
^ self invokeFFIWithArguments:(Array with:arg) |
|
370 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
371 |
|
9321 | 372 |
invokeWith:arg1 with:arg2 |
373 |
self hasCode ifFalse:[ |
|
9464 | 374 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
375 |
]. |
9321 | 376 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2) |
377 |
! |
|
378 |
||
379 |
invokeWith:arg1 with:arg2 with:arg3 |
|
380 |
self hasCode ifFalse:[ |
|
9464 | 381 |
self prepareInvoke. |
9321 | 382 |
]. |
383 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
384 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
385 |
|
9321 | 386 |
invokeWith:arg1 with:arg2 with:arg3 with:arg4 |
387 |
self hasCode ifFalse:[ |
|
9464 | 388 |
self prepareInvoke. |
9321 | 389 |
]. |
390 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
391 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
392 |
|
9321 | 393 |
invokeWithArguments:argArray |
394 |
self hasCode ifFalse:[ |
|
9465 | 395 |
self prepareInvoke. |
9321 | 396 |
]. |
9465 | 397 |
^ self invokeFFIwithArguments:argArray forCPPInstance:nil |
398 |
||
399 |
"Modified: / 01-08-2006 / 16:04:08 / cg" |
|
9321 | 400 |
! ! |
401 |
||
402 |
!ExternalLibraryFunction methodsFor:'printing'! |
|
403 |
||
404 |
printOn:aStream |
|
405 |
aStream nextPutAll:'<'. |
|
9465 | 406 |
self isCallTypeAPI ifTrue:[ |
407 |
'API:' printOn:aStream. |
|
408 |
] ifFalse:[ |
|
409 |
'C:' printOn:aStream. |
|
410 |
]. |
|
9321 | 411 |
aStream nextPutAll:' '. |
412 |
name printOn:aStream. |
|
9465 | 413 |
moduleName notNil ifTrue:[ |
414 |
aStream nextPutAll:' module:'. |
|
415 |
moduleName printOn:aStream. |
|
416 |
]. |
|
9321 | 417 |
aStream nextPutAll:'>'. |
9465 | 418 |
|
419 |
"Modified: / 01-08-2006 / 15:21:42 / cg" |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
420 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
421 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
422 |
!ExternalLibraryFunction methodsFor:'private'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
423 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
424 |
linkToModule |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
425 |
"link this function to the external module. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
426 |
I.e. retrieve the module handle and the code pointer." |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
427 |
|
9465 | 428 |
|handle moduleNameUsed functionName| |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
429 |
|
9465 | 430 |
(moduleNameUsed := moduleName) isNil ifTrue:[ |
431 |
owningClass isNil ifTrue:[ |
|
432 |
self error:'Missing moduleName'. |
|
433 |
]. |
|
434 |
moduleNameUsed := owningClass theNonMetaclass libraryName asSymbol. |
|
9321 | 435 |
]. |
9336 | 436 |
moduleHandle isNil ifTrue:[ |
9465 | 437 |
handle := ObjectFileLoader loadDynamicObject:(moduleNameUsed asString). |
9340 | 438 |
handle isNil ifTrue:[ |
439 |
handle := ObjectFileLoader |
|
9465 | 440 |
loadDynamicObject:(Filename currentDirectory construct:moduleNameUsed) pathName. |
9340 | 441 |
handle isNil ifTrue:[ |
9465 | 442 |
self error:'Cannot load module: ', moduleNameUsed. |
9340 | 443 |
]. |
444 |
]. |
|
445 |
moduleHandle := handle. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
446 |
]. |
9341 | 447 |
name isNumber ifFalse:[ |
9393 | 448 |
functionName := name. |
449 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
|
9418
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
450 |
functionName := ('_', functionName) asSymbol. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
451 |
|
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
452 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
453 |
moduleHandle := nil. |
9465 | 454 |
self error:'Missing function: ', name, ' in module: ', moduleNameUsed. |
9418
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
455 |
]. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
456 |
]. |
9321 | 457 |
]. |
9392 | 458 |
|
9465 | 459 |
"Modified: / 01-08-2006 / 16:24:14 / cg" |
9321 | 460 |
! |
461 |
||
462 |
prepareInvoke |
|
463 |
self hasCode ifFalse:[ |
|
9392 | 464 |
moduleHandle isNil ifTrue:[ |
465 |
self linkToModule. |
|
466 |
]. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
467 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
468 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
469 |
|
8550 | 470 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
471 |
||
9327 | 472 |
ffiTypeSymbolForType:aType |
9340 | 473 |
|t| |
9322 | 474 |
|
475 |
"/ kludge for those who do not have the CType package... |
|
9340 | 476 |
t := aType. |
9483 | 477 |
t isSymbol ifTrue:[ |
478 |
t == #int8 ifTrue:[^ #sint8 ]. |
|
479 |
t == #int16 ifTrue:[^ #sint16 ]. |
|
480 |
t == #int32 ifTrue:[^ #sint32 ]. |
|
481 |
t == #int64 ifTrue:[^ #sint64 ]. |
|
482 |
t == #bool ifTrue:[^ #boolean ]. |
|
483 |
t == #voidPointer ifTrue:[^ #handle ]. |
|
484 |
^ t. |
|
485 |
]. |
|
486 |
||
487 |
aType isString ifFalse:[ |
|
488 |
CType isNil ifTrue:[ |
|
9340 | 489 |
self error:'unknown type'. |
490 |
]. |
|
9483 | 491 |
t := aType typeSymbol. |
492 |
]. |
|
493 |
aType isString ifTrue:[ |
|
494 |
self halt |
|
495 |
]. |
|
496 |
t isSymbol ifFalse:[ |
|
497 |
self error:'unknown type'. |
|
9340 | 498 |
]. |
9327 | 499 |
|
9340 | 500 |
^ t |
9322 | 501 |
! |
502 |
||
9466 | 503 |
name:functionNameOrVirtualIndex module:aModuleName returnType:aReturnType argumentTypes:argTypes |
504 |
name := functionNameOrVirtualIndex. |
|
505 |
functionNameOrVirtualIndex isNumber ifTrue:[ |
|
506 |
self beVirtualCPP. |
|
507 |
]. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
508 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
509 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
510 |
argumentTypes := argTypes. |
9465 | 511 |
|
512 |
"Created: / 01-08-2006 / 15:19:52 / cg" |
|
9466 | 513 |
"Modified: / 02-08-2006 / 17:20:13 / cg" |
9465 | 514 |
! |
515 |
||
516 |
owningClass:aClass |
|
517 |
owningClass := aClass. |
|
518 |
||
519 |
"Created: / 01-08-2006 / 15:22:50 / cg" |
|
8550 | 520 |
! ! |
521 |
||
9342 | 522 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
523 |
||
9347 | 524 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
9463 | 525 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance |
526 |
||
527 |
"Modified: / 01-08-2006 / 13:55:30 / cg" |
|
9342 | 528 |
! |
529 |
||
9347 | 530 |
invokeFFIWithArguments:arguments |
9463 | 531 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil |
532 |
||
533 |
"Modified: / 01-08-2006 / 13:55:35 / cg" |
|
9347 | 534 |
! |
535 |
||
9463 | 536 |
invokeFFIwithArguments:arguments forCPPInstance:aCPlusPlusObjectOrNil |
9465 | 537 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo returnValue stClass vtOffset |
9524 | 538 |
virtual async unlimitedStack callTypeNumber returnValueClass argValueClass| |
9342 | 539 |
|
540 |
argumentTypes notNil ifTrue:[ |
|
541 |
argTypeSymbols := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType]. |
|
542 |
]. |
|
543 |
returnTypeSymbol := self ffiTypeSymbolForType:returnType. |
|
9346 | 544 |
|
9463 | 545 |
virtual := self isVirtualCPP. |
9524 | 546 |
(virtual "or:[self isNonVirtualCPP]") ifTrue:[ |
9463 | 547 |
aCPlusPlusObjectOrNil isNil ifTrue:[ |
548 |
"/ must have a c++ object instance |
|
549 |
self primitiveFailed. |
|
550 |
]. |
|
551 |
||
552 |
"/ and it must be a kind of ExternalStructure !! |
|
9342 | 553 |
(aCPlusPlusObjectOrNil isKindOf:ExternalStructure) ifFalse:[ |
554 |
self primitiveFailed. |
|
555 |
]. |
|
9347 | 556 |
virtual ifTrue:[ |
557 |
vtOffset := name. |
|
558 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
559 |
self primitiveFailed. |
|
560 |
] |
|
9342 | 561 |
]. |
9463 | 562 |
] ifFalse:[ |
563 |
aCPlusPlusObjectOrNil notNil ifTrue:[ |
|
564 |
"/ must NOT have a c++ object instance |
|
565 |
self primitiveFailed. |
|
566 |
]. |
|
9342 | 567 |
]. |
9463 | 568 |
async := self isAsync. |
569 |
unlimitedStack := self isUnlimitedStack. |
|
9465 | 570 |
callTypeNumber := self callTypeNumber. |
9342 | 571 |
|
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
572 |
%{ /* STACK: 100000 */ |
9342 | 573 |
#ifdef HAVE_FFI |
574 |
ffi_cif __cif; |
|
575 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
576 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
577 |
ffi_type *__returnType = NULL; |
|
578 |
union u { |
|
579 |
int iVal; |
|
580 |
float fVal; |
|
581 |
double dVal; |
|
582 |
void *pointerVal; |
|
583 |
}; |
|
584 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
585 |
union u *__argValues = __argValuesIncludingThis; |
|
586 |
union u __returnValue; |
|
587 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
588 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
589 |
void *__returnValuePointer; |
|
590 |
int __numArgs, __numArgsIncludingThis; |
|
591 |
static int null = 0; |
|
592 |
int i; |
|
593 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
594 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
|
595 |
||
596 |
if (arguments == nil) { |
|
597 |
__numArgs = 0; |
|
598 |
if (argTypeSymbols != nil) { |
|
599 |
if (! __isArray(argTypeSymbols) |
|
600 |
|| (__arraySize(argTypeSymbols) != __numArgs)) { |
|
601 |
failureCode = @symbol(ArgumentCountMismatch); |
|
602 |
goto getOutOfHere; |
|
603 |
} |
|
604 |
} |
|
605 |
} else { |
|
606 |
if (! __isArray(arguments) |
|
607 |
|| ! __isArray(argTypeSymbols) |
|
608 |
|| (__arraySize(argTypeSymbols) != (__numArgs = __arraySize(arguments)))) { |
|
609 |
failureCode = @symbol(ArgumentCountMismatch); |
|
610 |
goto getOutOfHere; |
|
611 |
} |
|
612 |
} |
|
613 |
if (__numArgs > MAX_ARGS) { |
|
614 |
failureCode = @symbol(TooManyArguments); |
|
615 |
goto getOutOfHere; |
|
616 |
} |
|
617 |
||
618 |
/* |
|
619 |
* validate the return type |
|
620 |
*/ |
|
621 |
__returnValuePointer = &__returnValue; |
|
9479 | 622 |
|
9483 | 623 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
9479 | 624 |
returnTypeSymbol = @symbol(handle); |
9483 | 625 |
} |
9479 | 626 |
|
9342 | 627 |
if (returnTypeSymbol == @symbol(int)) { |
628 |
__returnType = __get_ffi_type_sint(); |
|
629 |
} else if (returnTypeSymbol == @symbol(uint)) { |
|
630 |
__returnType = __get_ffi_type_uint(); |
|
9479 | 631 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
632 |
__returnType = __get_ffi_type_uint8(); |
|
633 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
|
634 |
__returnType = __get_ffi_type_uint16(); |
|
635 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
|
636 |
__returnType = __get_ffi_type_uint32(); |
|
637 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
|
638 |
__returnType = __get_ffi_type_uint64(); |
|
639 |
||
640 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
641 |
__returnType = __get_ffi_type_sint(); |
|
642 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
|
643 |
__returnType = __get_ffi_type_sint8(); |
|
644 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
|
645 |
__returnType = __get_ffi_type_sint16(); |
|
646 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
|
647 |
__returnType = __get_ffi_type_sint32(); |
|
648 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
|
649 |
__returnType = __get_ffi_type_sint64(); |
|
650 |
||
9342 | 651 |
} else if (returnTypeSymbol == @symbol(long)) { |
652 |
if (sizeof(long) == 4) { |
|
9479 | 653 |
returnTypeSymbol = @symbol(sint32); |
9342 | 654 |
__returnType = __get_ffi_type_sint32(); |
655 |
} else if (sizeof(long) == 8) { |
|
9479 | 656 |
returnTypeSymbol = @symbol(sint64); |
9342 | 657 |
__returnType = __get_ffi_type_sint64(); |
658 |
} else { |
|
659 |
failureCode = @symbol(UnknownReturnType); |
|
660 |
goto getOutOfHere; |
|
661 |
} |
|
9479 | 662 |
|
9342 | 663 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
664 |
if (sizeof(long) == 4) { |
|
9479 | 665 |
returnTypeSymbol = @symbol(uint32); |
9342 | 666 |
__returnType = __get_ffi_type_uint32(); |
667 |
}else if (sizeof(long) == 8) { |
|
9479 | 668 |
returnTypeSymbol = @symbol(uint64); |
9342 | 669 |
__returnType = __get_ffi_type_uint64(); |
670 |
} else { |
|
671 |
failureCode = @symbol(UnknownReturnType); |
|
672 |
goto getOutOfHere; |
|
673 |
} |
|
9479 | 674 |
|
9342 | 675 |
} else if (returnTypeSymbol == @symbol(boolean)) { |
676 |
__returnType = __get_ffi_type_uint(); |
|
9479 | 677 |
|
9342 | 678 |
} else if (returnTypeSymbol == @symbol(float)) { |
679 |
__returnType = __get_ffi_type_float(); |
|
680 |
} else if (returnTypeSymbol == @symbol(double)) { |
|
681 |
__returnType = __get_ffi_type_double(); |
|
9479 | 682 |
|
9342 | 683 |
} else if (returnTypeSymbol == @symbol(void)) { |
684 |
__returnType = __get_ffi_type_void(); |
|
685 |
__returnValuePointer = NULL; |
|
9479 | 686 |
} else if ((returnTypeSymbol == @symbol(pointer)) || (returnTypeSymbol == @symbol(handle))) { |
9346 | 687 |
__returnType = __get_ffi_type_pointer(); |
9463 | 688 |
} else if (returnTypeSymbol == @symbol(charPointer)) { |
689 |
__returnType = __get_ffi_type_pointer(); |
|
690 |
} else if (returnTypeSymbol == @symbol(wcharPointer)) { |
|
691 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 692 |
} else { |
9524 | 693 |
if (__isSymbol(returnTypeSymbol) |
694 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
695 |
if (! __isBehaviorLike(returnValueClass)) { |
|
696 |
failureCode = @symbol(NonBehaviorReturnType); |
|
697 |
goto getOutOfHere; |
|
698 |
} |
|
699 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
700 |
failureCode = @symbol(NonExternalAddressReturnType); |
|
701 |
goto getOutOfHere; |
|
702 |
} |
|
703 |
__returnType = __get_ffi_type_pointer(); |
|
704 |
returnTypeSymbol = @symbol(pointer); |
|
705 |
} else { |
|
706 |
failureCode = @symbol(UnknownReturnType); |
|
707 |
goto getOutOfHere; |
|
708 |
} |
|
9342 | 709 |
} |
710 |
||
711 |
/* |
|
712 |
* validate the c++ object |
|
713 |
*/ |
|
714 |
if (aCPlusPlusObjectOrNil != nil) { |
|
715 |
struct cPlusPlusInstance { |
|
716 |
void **vTable; |
|
717 |
}; |
|
718 |
struct cPlusPlusInstance *inst; |
|
719 |
||
720 |
if (__isExternalAddressLike(aCPlusPlusObjectOrNil)) { |
|
721 |
inst = (void *)(__externalAddressVal(aCPlusPlusObjectOrNil)); |
|
722 |
} else if (__isExternalBytesLike(aCPlusPlusObjectOrNil)) { |
|
723 |
inst = (void *)(__externalBytesVal(aCPlusPlusObjectOrNil)); |
|
724 |
} else { |
|
725 |
failureCode = @symbol(InvalidInstance); |
|
726 |
goto getOutOfHere; |
|
727 |
} |
|
728 |
__argValues[0].pointerVal = inst; |
|
729 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
730 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
731 |
||
732 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
|
733 |
__argTypes = &__argTypesIncludingThis[1]; |
|
734 |
__argValues = &__argValuesIncludingThis[1]; |
|
735 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 736 |
|
737 |
if (virtual == true) { |
|
738 |
if (! __isSmallInteger(vtOffset)) { |
|
739 |
failureCode = @symbol(InvalidVTableIndex); |
|
740 |
goto getOutOfHere; |
|
741 |
} |
|
742 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
9464 | 743 |
#ifdef VERBOSE |
744 |
printf("virtual codeAddress: %x\n", codeAddress); |
|
745 |
#endif |
|
9347 | 746 |
} |
9342 | 747 |
} else { |
748 |
__numArgsIncludingThis = __numArgs; |
|
9524 | 749 |
#ifdef VERBOSE |
750 |
printf("codeAddress: %x\n", codeAddress); |
|
751 |
#endif |
|
9342 | 752 |
} |
753 |
||
754 |
/* |
|
755 |
* validate all arg types and setup arg-buffers |
|
756 |
*/ |
|
757 |
for (i=0; i<__numArgs; i++) { |
|
758 |
ffi_type *thisType; |
|
759 |
void *argValuePtr; |
|
760 |
OBJ typeSymbol; |
|
761 |
OBJ arg; |
|
762 |
||
9465 | 763 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
764 |
||
9342 | 765 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
766 |
arg = __ArrayInstPtr(arguments)->a_element[i]; |
|
767 |
||
9483 | 768 |
if (typeSymbol == @symbol(handle)) { |
9479 | 769 |
typeSymbol = @symbol(pointer); |
770 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
771 |
typeSymbol = @symbol(pointer); |
|
772 |
} |
|
773 |
||
9342 | 774 |
if (typeSymbol == @symbol(long)) { |
775 |
if (sizeof(long) == sizeof(int)) { |
|
776 |
typeSymbol = @symbol(sint); |
|
777 |
} else { |
|
778 |
if (sizeof(long) == 4) { |
|
779 |
typeSymbol = @symbol(sint32); |
|
780 |
} else if (sizeof(long) == 8) { |
|
781 |
typeSymbol = @symbol(sint64); |
|
782 |
} |
|
783 |
} |
|
784 |
} |
|
785 |
if (typeSymbol == @symbol(ulong)) { |
|
786 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
787 |
typeSymbol = @symbol(uint); |
|
788 |
} else { |
|
789 |
if (sizeof(long) == 4) { |
|
790 |
typeSymbol = @symbol(uint32); |
|
791 |
} else if (sizeof(long) == 8) { |
|
792 |
typeSymbol = @symbol(uint64); |
|
793 |
} |
|
794 |
} |
|
795 |
} |
|
796 |
||
797 |
if (typeSymbol == @symbol(int)) { |
|
798 |
thisType = __get_ffi_type_sint(); |
|
799 |
if (__isSmallInteger(arg)) { |
|
800 |
__argValues[i].iVal = __intVal(arg); |
|
801 |
} else { |
|
802 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
803 |
if (__argValues[i].iVal == 0) { |
|
804 |
failureCode = @symbol(InvalidArgument); |
|
805 |
goto getOutOfHere; |
|
806 |
} |
|
807 |
} |
|
808 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 809 |
|
9342 | 810 |
} else if (typeSymbol == @symbol(uint)) { |
811 |
thisType = __get_ffi_type_uint(); |
|
812 |
||
813 |
if (__isSmallInteger(arg)) { |
|
814 |
__argValues[i].iVal = __intVal(arg); |
|
815 |
} else { |
|
816 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
817 |
if (__argValues[i].iVal == 0) { |
|
818 |
failureCode = @symbol(InvalidArgument); |
|
819 |
goto getOutOfHere; |
|
820 |
} |
|
821 |
} |
|
822 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 823 |
|
9342 | 824 |
} else if (typeSymbol == @symbol(uint8)) { |
825 |
thisType = __get_ffi_type_uint8(); |
|
826 |
if (! __isSmallInteger(arg)) { |
|
827 |
failureCode = @symbol(InvalidArgument); |
|
828 |
goto getOutOfHere; |
|
829 |
} |
|
830 |
__argValues[i].iVal = __intVal(arg); |
|
831 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
832 |
failureCode = @symbol(InvalidArgument); |
|
833 |
goto getOutOfHere; |
|
834 |
} |
|
835 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 836 |
|
9342 | 837 |
} else if (typeSymbol == @symbol(sint8)) { |
838 |
thisType = __get_ffi_type_sint8(); |
|
839 |
if (! __isSmallInteger(arg)) { |
|
840 |
failureCode = @symbol(InvalidArgument); |
|
841 |
goto getOutOfHere; |
|
842 |
} |
|
843 |
__argValues[i].iVal = __intVal(arg); |
|
844 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
845 |
failureCode = @symbol(InvalidArgument); |
|
846 |
goto getOutOfHere; |
|
847 |
} |
|
848 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 849 |
|
9342 | 850 |
} else if (typeSymbol == @symbol(uint16)) { |
851 |
thisType = __get_ffi_type_uint16(); |
|
852 |
if (! __isSmallInteger(arg)) { |
|
853 |
failureCode = @symbol(InvalidArgument); |
|
854 |
goto getOutOfHere; |
|
855 |
} |
|
856 |
__argValues[i].iVal = __intVal(arg); |
|
857 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
858 |
failureCode = @symbol(InvalidArgument); |
|
859 |
goto getOutOfHere; |
|
860 |
} |
|
861 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 862 |
|
9342 | 863 |
} else if (typeSymbol == @symbol(sint16)) { |
864 |
thisType = __get_ffi_type_sint16(); |
|
865 |
if (! __isSmallInteger(arg)) { |
|
866 |
failureCode = @symbol(InvalidArgument); |
|
867 |
goto getOutOfHere; |
|
868 |
} |
|
869 |
__argValues[i].iVal = __intVal(arg); |
|
870 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
871 |
failureCode = @symbol(InvalidArgument); |
|
872 |
goto getOutOfHere; |
|
873 |
} |
|
874 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 875 |
|
876 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
|
877 |
thisType = __get_ffi_type_uint32(); |
|
878 |
if (__isSmallInteger(arg)) { |
|
879 |
__argValues[i].iVal = __intVal(arg); |
|
880 |
} else { |
|
881 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
882 |
if (__argValues[i].iVal == 0) { |
|
883 |
failureCode = @symbol(InvalidArgument); |
|
884 |
goto getOutOfHere; |
|
885 |
} |
|
886 |
} |
|
887 |
argValuePtr = &(__argValues[i].iVal); |
|
888 |
||
9342 | 889 |
} else if (typeSymbol == @symbol(float)) { |
890 |
thisType = __get_ffi_type_float(); |
|
891 |
if (__isSmallInteger(arg)) { |
|
892 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
893 |
} else if (__isFloat(arg)) { |
|
894 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
895 |
} else if (__isShortFloat(arg)) { |
|
896 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
897 |
} else { |
|
898 |
failureCode = @symbol(InvalidArgument); |
|
899 |
goto getOutOfHere; |
|
900 |
} |
|
901 |
argValuePtr = &(__argValues[i].fVal); |
|
9479 | 902 |
|
9342 | 903 |
} else if (typeSymbol == @symbol(double)) { |
904 |
thisType = __get_ffi_type_double(); |
|
905 |
if (__isSmallInteger(arg)) { |
|
906 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
907 |
} else if (__isFloat(arg)) { |
|
908 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
909 |
} else if (__isShortFloat(arg)) { |
|
910 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
911 |
} else { |
|
912 |
failureCode = @symbol(InvalidArgument); |
|
913 |
goto getOutOfHere; |
|
914 |
} |
|
915 |
argValuePtr = &(__argValues[i].dVal); |
|
9479 | 916 |
|
9342 | 917 |
} else if (typeSymbol == @symbol(void)) { |
918 |
thisType = __get_ffi_type_void(); |
|
919 |
argValuePtr = &null; |
|
9479 | 920 |
|
9465 | 921 |
} else if (typeSymbol == @symbol(charPointer)) { |
922 |
thisType = __get_ffi_type_pointer(); |
|
923 |
if (__isString(arg) || __isSymbol(arg)) { |
|
924 |
if (async == true) goto badArgForAsyncCall; |
|
925 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
926 |
} else { |
|
9483 | 927 |
if (__isBytes(arg)) { |
928 |
if (async == true) goto badArgForAsyncCall; |
|
929 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
930 |
} else { |
|
931 |
if (arg == nil) { |
|
932 |
__argValues[i].pointerVal = (void *)0; |
|
933 |
} else { |
|
934 |
failureCode = @symbol(InvalidArgument); |
|
935 |
goto getOutOfHere; |
|
936 |
} |
|
937 |
} |
|
9465 | 938 |
} |
939 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 940 |
|
9342 | 941 |
} else if (typeSymbol == @symbol(pointer)) { |
9524 | 942 |
commonPointerTypeArg: ; |
9342 | 943 |
thisType = __get_ffi_type_pointer(); |
9483 | 944 |
if (arg == nil) { |
945 |
__argValues[i].pointerVal = NULL; |
|
946 |
} else if (__isExternalAddressLike(arg)) { |
|
9342 | 947 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
948 |
} else if (__isExternalBytesLike(arg)) { |
|
949 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
950 |
} else if (__isByteArray(arg)) { |
|
9463 | 951 |
if (async == true) goto badArgForAsyncCall; |
9342 | 952 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
953 |
} else if (__isFloatArray(arg)) { |
|
9463 | 954 |
if (async == true) goto badArgForAsyncCall; |
9342 | 955 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
956 |
} else if (__isDoubleArray(arg)) { |
|
9463 | 957 |
if (async == true) goto badArgForAsyncCall; |
9342 | 958 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
959 |
} else if (__isString(arg) || __isSymbol(arg)) { |
|
9463 | 960 |
if (async == true) { |
9435 | 961 |
badArgForAsyncCall: ; |
962 |
failureCode = @symbol(BadArgForAsyncCall); |
|
963 |
goto getOutOfHere; |
|
964 |
} |
|
9342 | 965 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
9483 | 966 |
} else if (__isBytes(arg)) { |
967 |
char *p = (char *)(__byteArrayVal(arg)); |
|
968 |
int nInstBytes; |
|
969 |
OBJ cls; |
|
970 |
||
971 |
cls = __qClass(arg); |
|
972 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
973 |
__argValues[i].pointerVal = p + nInstBytes; |
|
9342 | 974 |
} else { |
975 |
failureCode = @symbol(InvalidArgument); |
|
976 |
goto getOutOfHere; |
|
977 |
} |
|
978 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 979 |
|
9342 | 980 |
} else if (typeSymbol == @symbol(boolean)) { |
981 |
thisType = __get_ffi_type_uint(); |
|
982 |
||
983 |
if (arg == true) { |
|
984 |
__argValues[i].iVal = 1; |
|
985 |
} else if (arg == false) { |
|
986 |
__argValues[i].iVal = 0; |
|
987 |
} else if (__isSmallInteger(arg)) { |
|
988 |
__argValues[i].iVal = __intVal(arg); |
|
989 |
} else { |
|
990 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
991 |
if (__argValues[i].iVal == 0) { |
|
992 |
failureCode = @symbol(InvalidArgument); |
|
993 |
goto getOutOfHere; |
|
994 |
} |
|
995 |
} |
|
996 |
argValuePtr = &(__argValues[i].iVal); |
|
997 |
} else { |
|
9524 | 998 |
if (__isSymbol(typeSymbol) |
999 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
1000 |
if (! __isBehaviorLike(argValueClass)) { |
|
1001 |
failureCode = @symbol(NonBehaviorArgumentType); |
|
1002 |
goto getOutOfHere; |
|
1003 |
} |
|
1004 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
1005 |
failureCode = @symbol(NonExternalAddressArgumentType); |
|
1006 |
goto getOutOfHere; |
|
1007 |
} |
|
1008 |
goto commonPointerTypeArg; /* sorry */ |
|
1009 |
} else { |
|
1010 |
failureCode = @symbol(UnknownArgumentType); |
|
1011 |
goto getOutOfHere; |
|
1012 |
} |
|
9342 | 1013 |
} |
1014 |
||
1015 |
__argTypes[i] = thisType; |
|
1016 |
__argValuePointers[i] = argValuePtr; |
|
9483 | 1017 |
|
9524 | 1018 |
#ifdef VERBOSE |
9483 | 1019 |
printf("arg%d: %x\n", i, __argValues[i].iVal); |
9524 | 1020 |
#endif |
9342 | 1021 |
} |
9465 | 1022 |
failureInfo = nil; |
9342 | 1023 |
|
1024 |
__callType = FFI_DEFAULT_ABI; |
|
1025 |
||
1026 |
#ifdef CALLTYPE_FFI_STDCALL |
|
9465 | 1027 |
if (callTypeNumber == @global(CALLTYPE_API)) { |
9342 | 1028 |
__callType = CALLTYPE_FFI_STDCALL; |
1029 |
} |
|
1030 |
#endif |
|
1031 |
#ifdef CALLTYPE_FFI_V8 |
|
9465 | 1032 |
if (callTypeNumber == @global(CALLTYPE_V8)) { |
9342 | 1033 |
__callType = CALLTYPE_FFI_V8; |
1034 |
} |
|
1035 |
#endif |
|
1036 |
#ifdef CALLTYPE_FFI_V9 |
|
9465 | 1037 |
if (callTypeNumber == @global(CALLTYPE_V9)) { |
9342 | 1038 |
__callType = CALLTYPE_FFI_V9; |
1039 |
} |
|
1040 |
#endif |
|
1041 |
#ifdef CALLTYPE_FFI_UNIX64 |
|
9465 | 1042 |
if (callTypeNumber == @global(CALLTYPE_UNIX64)) { |
9342 | 1043 |
__callType = CALLTYPE_FFI_UNIX64; |
1044 |
} |
|
1045 |
#endif |
|
1046 |
||
1047 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
1048 |
failureCode = @symbol(FFIPrepareFailed); |
|
1049 |
goto getOutOfHere; |
|
1050 |
} |
|
9463 | 1051 |
if (async == true) { |
9524 | 1052 |
#ifdef VERBOSE |
1053 |
printf("async call 0x%x\n", codeAddress); |
|
1054 |
#endif |
|
9436 | 1055 |
#ifdef WIN32 |
9435 | 1056 |
__STX_C_CALL4( "ffi_call", ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9436 | 1057 |
#else |
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
1058 |
__BEGIN_INTERRUPTABLE__ |
9436 | 1059 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
1060 |
__END_INTERRUPTABLE__ |
9436 | 1061 |
#endif |
9435 | 1062 |
} else { |
9483 | 1063 |
if (unlimitedStack == true) { |
9524 | 1064 |
#ifdef VERBOSE |
1065 |
printf("UNLIMITEDSTACKCALL call 0x%x\n", codeAddress); |
|
1066 |
#endif |
|
9463 | 1067 |
#if 0 |
1068 |
__UNLIMITEDSTACKCALL__(ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
|
1069 |
#endif |
|
1070 |
} else { |
|
9524 | 1071 |
#ifdef VERBOSE |
1072 |
printf("call 0x%x\n", codeAddress); |
|
1073 |
#endif |
|
9463 | 1074 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
1075 |
} |
|
9435 | 1076 |
} |
9524 | 1077 |
#ifdef VERBOSE |
1078 |
printf("retval is %d (0x%x)\n", __returnValue.iVal, __returnValue.iVal); |
|
1079 |
#endif |
|
9479 | 1080 |
if ((returnTypeSymbol == @symbol(sint)) |
1081 |
|| (returnTypeSymbol == @symbol(sint8)) |
|
1082 |
|| (returnTypeSymbol == @symbol(sint16)) |
|
1083 |
|| (returnTypeSymbol == @symbol(sint32))) { |
|
9342 | 1084 |
RETURN ( __MKINT(__returnValue.iVal) ); |
1085 |
} |
|
1086 |
if ((returnTypeSymbol == @symbol(uint)) |
|
1087 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
1088 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
1089 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
1090 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
|
1091 |
} |
|
1092 |
if (returnTypeSymbol == @symbol(boolean)) { |
|
1093 |
RETURN ( __returnValue.iVal ? true : false ); |
|
1094 |
} |
|
1095 |
if (returnTypeSymbol == @symbol(float)) { |
|
1096 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
|
1097 |
} |
|
1098 |
if (returnTypeSymbol == @symbol(double)) { |
|
1099 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
|
1100 |
} |
|
1101 |
if (returnTypeSymbol == @symbol(void)) { |
|
1102 |
RETURN ( nil ); |
|
1103 |
} |
|
9463 | 1104 |
if (returnTypeSymbol == @symbol(char)) { |
1105 |
RETURN ( __MKCHARACTER(__returnValue.iVal & 0xFF) ); |
|
1106 |
} |
|
1107 |
if (returnTypeSymbol == @symbol(wchar)) { |
|
1108 |
RETURN ( __MKUCHARACTER(__returnValue.iVal & 0xFFFF) ); |
|
1109 |
} |
|
9346 | 1110 |
if (returnTypeSymbol == @symbol(handle)) { |
9342 | 1111 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 1112 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
1113 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
|
9463 | 1114 |
} else if (returnTypeSymbol == @symbol(charPointer)) { |
1115 |
returnValue = __MKSTRING(__returnValue.pointerVal); |
|
1116 |
} else if (returnTypeSymbol == @symbol(wcharPointer)) { |
|
1117 |
returnValue = __MKU16STRING(__returnValue.pointerVal); |
|
9342 | 1118 |
} else { |
1119 |
failureCode = @symbol(UnknownReturnType2); |
|
1120 |
} |
|
1121 |
getOutOfHere: ; |
|
1122 |
||
1123 |
#else /* no FFI support */ |
|
1124 |
failureCode = @symbol(FFINotSupported); |
|
1125 |
#endif /* HAVE_FFI */ |
|
1126 |
%}. |
|
1127 |
failureCode notNil ifTrue:[ |
|
1128 |
self primitiveFailed. |
|
1129 |
^ nil |
|
1130 |
]. |
|
1131 |
||
9483 | 1132 |
returnType isSymbol ifTrue:[ |
9524 | 1133 |
returnValueClass notNil ifTrue:[ |
9525 | 1134 |
self isConstReturnValue ifTrue:[ |
1135 |
returnValue changeClassTo:returnValueClass. |
|
1136 |
^ returnValue |
|
1137 |
]. |
|
1138 |
^ returnValueClass fromExternalAddress:returnValue. |
|
9524 | 1139 |
]. |
9483 | 1140 |
] ifFalse:[ |
1141 |
returnType isCPointer ifTrue:[ |
|
1142 |
returnType baseType isCStruct ifTrue:[ |
|
1143 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
1144 |
stClass notNil ifTrue:[ |
|
9525 | 1145 |
self isConstReturnValue ifTrue:[ |
1146 |
returnValue changeClassTo:returnValueClass. |
|
1147 |
^ returnValue |
|
1148 |
]. |
|
9483 | 1149 |
^ stClass fromExternalAddress:returnValue. |
1150 |
]. |
|
9342 | 1151 |
]. |
9483 | 1152 |
returnType baseType isCChar ifTrue:[ |
1153 |
^ returnValue stringAt:1 |
|
1154 |
]. |
|
9346 | 1155 |
]. |
9342 | 1156 |
]. |
1157 |
||
1158 |
^ returnValue |
|
9463 | 1159 |
|
1160 |
"Created: / 01-08-2006 / 13:56:23 / cg" |
|
9465 | 1161 |
"Modified: / 01-08-2006 / 15:59:44 / cg" |
9342 | 1162 |
! ! |
1163 |
||
8533 | 1164 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
1165 |
||
1166 |
version |
|
9525 | 1167 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.42 2006-08-11 11:20:24 ca Exp $' |
8533 | 1168 |
! ! |
9463 | 1169 |
|
1170 |
ExternalLibraryFunction initialize! |