author | Claus Gittinger <cg@exept.de> |
Wed, 02 Jan 2013 13:45:06 +0100 | |
changeset 14625 | 290463096ff5 |
parent 14516 | 359e4d2234af |
child 14632 | 6fe0dc1d5377 |
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 |
" |
|
8533 | 12 |
"{ Package: 'stx:libbasic' }" |
13 |
||
14 |
ExternalFunction subclass:#ExternalLibraryFunction |
|
9465 | 15 |
instanceVariableNames:'flags moduleName returnType argumentTypes owningClass' |
10270 | 16 |
classVariableNames:'DLLPATH FLAG_VIRTUAL FLAG_NONVIRTUAL FLAG_ASYNC |
17 |
FLAG_UNLIMITEDSTACK FLAG_RETVAL_IS_CONST CALLTYPE_MASK |
|
18 |
CALLTYPE_API CALLTYPE_C CALLTYPE_OLE CALLTYPE_V8 CALLTYPE_V9 |
|
14103 | 19 |
CALLTYPE_UNIX64 DllMapping' |
9464 | 20 |
poolDictionaries:'' |
21 |
category:'System-Support' |
|
8533 | 22 |
! |
23 |
||
9322 | 24 |
!ExternalLibraryFunction primitiveDefinitions! |
25 |
%{ |
|
26 |
||
9337 | 27 |
#ifdef HAVE_FFI |
28 |
# include <ffi.h> |
|
9322 | 29 |
# define MAX_ARGS 128 |
9365 | 30 |
|
31 |
extern ffi_type *__get_ffi_type_sint(); |
|
32 |
extern ffi_type *__get_ffi_type_sint8(); |
|
33 |
extern ffi_type *__get_ffi_type_sint16(); |
|
34 |
extern ffi_type *__get_ffi_type_sint32(); |
|
35 |
extern ffi_type *__get_ffi_type_sint64(); |
|
36 |
extern ffi_type *__get_ffi_type_uint(); |
|
37 |
extern ffi_type *__get_ffi_type_uint8(); |
|
38 |
extern ffi_type *__get_ffi_type_uint16(); |
|
39 |
extern ffi_type *__get_ffi_type_uint32(); |
|
40 |
extern ffi_type *__get_ffi_type_uint64(); |
|
41 |
extern ffi_type *__get_ffi_type_float(); |
|
42 |
extern ffi_type *__get_ffi_type_double(); |
|
43 |
extern ffi_type *__get_ffi_type_void(); |
|
44 |
extern ffi_type *__get_ffi_type_pointer(); |
|
45 |
||
13412 | 46 |
#else |
14625 | 47 |
# error "HAVE_FFI not defined!" |
9337 | 48 |
#endif |
9322 | 49 |
|
50 |
%} |
|
51 |
! ! |
|
52 |
||
8728 | 53 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
54 |
||
55 |
copyright |
|
56 |
" |
|
57 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9464 | 58 |
All Rights Reserved |
8728 | 59 |
|
60 |
This software is furnished under a license and may be used |
|
61 |
only in accordance with the terms of that license and with the |
|
62 |
inclusion of the above copyright notice. This software may not |
|
63 |
be provided or otherwise made available to, or used by, any |
|
64 |
other person. No title to or ownership of the software is |
|
65 |
hereby transferred. |
|
66 |
" |
|
9322 | 67 |
! |
68 |
||
9331 | 69 |
documentation |
70 |
" |
|
71 |
instances of me are used to interface to external library functions (as found in a dll/shared object). |
|
72 |
||
12680 | 73 |
Inside a method, when a special external-call pragma such as: |
14625 | 74 |
<api: bool MessageBeep(uint)> |
12680 | 75 |
|
76 |
is encountered by the parser, the compiler generates a call via |
|
14625 | 77 |
<correspondingExternalLibraryFunctionObject> invokeWithArguments: argumentArray. |
9331 | 78 |
|
79 |
In the invoke method, the library is checked to be loaded (and loaded if not already), |
|
9336 | 80 |
the arguments are converted to C and pushed onto the C-stack, the function is called, |
9331 | 81 |
and finally, the return value is converted back from C to a smalltalk object. |
12680 | 82 |
|
83 |
The parser supports the call-syntax of various other smalltalk dialects: |
|
14625 | 84 |
Squeak / ST-X: |
85 |
<cdecl: [async] [virtual|nonVirtual][const] returnType functionNameStringOrIndex ( argType1..argTypeN ) module: moduleName > |
|
86 |
<apicall: [async] [virtual|nonVirtual][const] returnType functionNameStringOrIndex ( argType1..argTypeN ) module: moduleName > |
|
12680 | 87 |
|
14625 | 88 |
Dolphin: |
89 |
<stdcall: [virtual|nonVirtual][const] returnType functionNameStringOrIndex argType1..argTypeN> |
|
90 |
<cdecl: [virtual|nonVirtual][const] returnType functionNameStringOrIndex argType1..argTypeN> |
|
12680 | 91 |
|
14625 | 92 |
ST/V: |
93 |
<api: functionName argType1 .. argTypeN returnType> |
|
94 |
<ccall: functionName argType1 .. argTypeN returnType> |
|
95 |
<ole: vFunctionIndex argType1 .. argTypeN returnType> |
|
12680 | 96 |
|
14625 | 97 |
VisualWorks: |
98 |
<c: ...> |
|
99 |
<c: #define NAME value> |
|
9331 | 100 |
" |
101 |
! |
|
102 |
||
9322 | 103 |
example |
104 |
" |
|
14625 | 105 |
[exBegin] |
106 |
|f| |
|
14037 | 107 |
|
14625 | 108 |
f := ExternalLibraryFunction new. |
109 |
f beCallTypeWINAPI. |
|
14037 | 110 |
|
14625 | 111 |
f name:'MessageBeep' |
112 |
module:'user32.dll' |
|
113 |
returnType:#boolean |
|
114 |
argumentTypes:#(uint). |
|
14037 | 115 |
|
14625 | 116 |
f invokeWith:1. |
117 |
[exEnd] |
|
14037 | 118 |
|
119 |
Synchronous vs. Asynchronous calls: |
|
120 |
||
14516
359e4d2234af
class: ExternalLibraryFunction
Stefan Vogel <sv@exept.de>
parents:
14506
diff
changeset
|
121 |
by default, foreign function calls are synchronous, effectively blocking the whole ST/X system |
14037 | 122 |
(that is by purpose,´because most C-code is not prepared for being interrupted, and also, normal |
123 |
code is not prepared for a garbage collector to move objects around, while another C thread might |
|
124 |
access the data...). |
|
14625 | 125 |
Therefore, the following will block all ST/X activity for 10 seconds |
14037 | 126 |
(try interacting with the launcher while the Sleep is performing): |
127 |
||
14625 | 128 |
[exBegin] |
129 |
|f| |
|
9322 | 130 |
|
14625 | 131 |
f := ExternalLibraryFunction new. |
132 |
f beCallTypeWINAPI. |
|
14037 | 133 |
|
14625 | 134 |
f name:'Sleep' |
135 |
module:'kernel32.dll' |
|
136 |
returnType:#void |
|
137 |
argumentTypes:#(uint). |
|
14037 | 138 |
|
14625 | 139 |
f invokeWith:10000. |
140 |
[exEnd] |
|
9322 | 141 |
|
14037 | 142 |
if you know what you do and you do not pass any possibly moving objects (such as strings) as argument, |
143 |
the call can be made asynchronous. In that case, ONLY the calling thread will be blocked; all other smalltalk |
|
144 |
threads wil continue to execute. |
|
145 |
(try interacting now with the launcher while the Sleep is performing): |
|
14625 | 146 |
[exBegin] |
147 |
|f| |
|
14037 | 148 |
|
14625 | 149 |
f := ExternalLibraryFunction new. |
150 |
f beCallTypeWINAPI. |
|
151 |
f beAsync. |
|
14037 | 152 |
|
14625 | 153 |
f name:'Sleep' |
154 |
module:'kernel32.dll' |
|
155 |
returnType:#void |
|
156 |
argumentTypes:#(uint). |
|
14037 | 157 |
|
14625 | 158 |
f invokeWith:10000. |
159 |
[exEnd] |
|
14037 | 160 |
|
9322 | 161 |
" |
8728 | 162 |
! ! |
8533 | 163 |
|
8550 | 164 |
!ExternalLibraryFunction class methodsFor:'instance creation'! |
165 |
||
9465 | 166 |
name:functionName module:moduleName returnType:returnType argumentTypes:argTypes |
8550 | 167 |
^ self new |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
168 |
name:functionName module:moduleName |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
169 |
returnType:returnType argumentTypes:argTypes |
9465 | 170 |
|
171 |
"Created: / 01-08-2006 / 15:19:08 / cg" |
|
9463 | 172 |
! ! |
173 |
||
174 |
!ExternalLibraryFunction class methodsFor:'class initialization'! |
|
175 |
||
11050 | 176 |
addToDllPath:aDirectoryPathName |
177 |
"can be used during initialization, to add more places for dll-loading" |
|
178 |
||
11053 | 179 |
|oldPath| |
180 |
||
181 |
oldPath := self dllPath. |
|
182 |
(oldPath includes:aDirectoryPathName) ifFalse:[ |
|
11586 | 183 |
self dllPath:(oldPath asOrderedCollection copyWith:aDirectoryPathName) |
11053 | 184 |
] |
11050 | 185 |
! |
186 |
||
14103 | 187 |
dllMapping |
188 |
"allows for dll's to be replaced, |
|
189 |
for example, if you want to use the mozilla sqlite dll |
|
14625 | 190 |
C:\Program Files\Mozilla Firefox\mozsqlite3.dll |
14103 | 191 |
for the sqlite3, execute: |
14625 | 192 |
ExternalLibraryFunction |
193 |
dllMapping at:'sqlite3' |
|
194 |
put: 'C:\Program Files\Mozilla Firefox\mozsqlite3.dll' |
|
14103 | 195 |
" |
196 |
||
197 |
DllMapping isNil ifTrue:[ |
|
14625 | 198 |
DllMapping := Dictionary new. |
14103 | 199 |
]. |
200 |
^ DllMapping |
|
201 |
||
202 |
"Created: / 10-04-2012 / 12:21:45 / cg" |
|
203 |
! |
|
204 |
||
10270 | 205 |
dllPath |
13337 | 206 |
^ DLLPATH |
10270 | 207 |
! |
208 |
||
209 |
dllPath:aCollectionOfDirectoryPathNames |
|
210 |
DLLPATH := aCollectionOfDirectoryPathNames |
|
211 |
! |
|
212 |
||
9463 | 213 |
initialize |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
214 |
"using inline access to corresponding c--defines to avoid duplicate places of knowledge" |
13337 | 215 |
|
216 |
DLLPATH isNil ifTrue:[ |
|
14625 | 217 |
DLLPATH := #('.'). |
218 |
FLAG_VIRTUAL := %{ __MKSMALLINT(__EXTL_FLAG_VIRTUAL) %}. "/ a virtual c++ call |
|
219 |
FLAG_NONVIRTUAL := %{ __MKSMALLINT(__EXTL_FLAG_NONVIRTUAL) %}. "/ a non-virtual c++ call |
|
220 |
FLAG_UNLIMITEDSTACK := %{ __MKSMALLINT(__EXTL_FLAG_UNLIMITEDSTACK) %}. "/ unlimitedstack under unix |
|
221 |
FLAG_ASYNC := %{ __MKSMALLINT(__EXTL_FLAG_ASYNC) %}. "/ async under win32 |
|
222 |
FLAG_RETVAL_IS_CONST := %{ __MKSMALLINT(__EXTL_FLAG_RETVAL_IS_CONST) %}."/ return value is not to be registered for finalization |
|
9463 | 223 |
|
14625 | 224 |
CALLTYPE_API := %{ __MKSMALLINT(__EXTL_CALLTYPE_API) %}. "/ WINAPI-call (win32 only) |
225 |
CALLTYPE_C := %{ __MKSMALLINT(__EXTL_CALLTYPE_C) %}. "/ regular C-call (the default) |
|
226 |
CALLTYPE_V8 := %{ __MKSMALLINT(__EXTL_CALLTYPE_V8) %}. "/ v8 call (sparc only) |
|
227 |
CALLTYPE_V9 := %{ __MKSMALLINT(__EXTL_CALLTYPE_V9) %}. "/ v9 call (sparc only) |
|
228 |
CALLTYPE_UNIX64 := %{ __MKSMALLINT(__EXTL_CALLTYPE_UNIX64) %}. "/ unix64 call (alpha only) |
|
9463 | 229 |
|
14625 | 230 |
CALLTYPE_MASK := %{ __MKSMALLINT(__EXTL_CALLTYPE_MASK) %}. |
13337 | 231 |
]. |
9465 | 232 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
233 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
234 |
self initialize |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
235 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
236 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
237 |
"Modified: / 03-10-2006 / 21:27:47 / cg" |
11053 | 238 |
! |
239 |
||
240 |
removeFromDllPath:aDirectoryPathName |
|
241 |
"remove added places from dll-loading" |
|
242 |
||
243 |
|oldPath| |
|
244 |
||
245 |
oldPath := self dllPath. |
|
246 |
self dllPath:(oldPath asOrderedCollection copyWithout:aDirectoryPathName) |
|
247 |
||
248 |
" |
|
249 |
self dllPath. |
|
11586 | 250 |
self addToDllPath:'C:\aaa\bbb'. |
11053 | 251 |
self dllPath. |
252 |
self removeFromDllPath:'C:\aaa\bbb'. |
|
253 |
self dllPath. |
|
254 |
" |
|
8550 | 255 |
! ! |
256 |
||
9321 | 257 |
!ExternalLibraryFunction class methodsFor:'constants'! |
258 |
||
259 |
callTypeAPI |
|
9463 | 260 |
^ CALLTYPE_API |
9321 | 261 |
|
9463 | 262 |
"Modified: / 01-08-2006 / 13:44:41 / cg" |
9435 | 263 |
! |
264 |
||
9321 | 265 |
callTypeC |
9463 | 266 |
^ CALLTYPE_C |
9321 | 267 |
|
9463 | 268 |
"Modified: / 01-08-2006 / 13:44:49 / cg" |
9435 | 269 |
! |
270 |
||
9321 | 271 |
callTypeCDecl |
9463 | 272 |
^ CALLTYPE_C |
9321 | 273 |
|
9463 | 274 |
"Modified: / 01-08-2006 / 13:44:52 / cg" |
9435 | 275 |
! |
276 |
||
10619 | 277 |
callTypeMASK |
278 |
^ CALLTYPE_MASK |
|
279 |
||
280 |
"Modified: / 01-08-2006 / 13:44:57 / cg" |
|
281 |
! |
|
282 |
||
9321 | 283 |
callTypeOLE |
9463 | 284 |
^ CALLTYPE_OLE |
9435 | 285 |
|
9463 | 286 |
"Modified: / 01-08-2006 / 13:44:57 / cg" |
9321 | 287 |
! ! |
288 |
||
289 |
!ExternalLibraryFunction methodsFor:'accessing'! |
|
290 |
||
291 |
argumentTypes |
|
292 |
^ argumentTypes |
|
9463 | 293 |
! |
294 |
||
11426 | 295 |
argumentTypesString |
11586 | 296 |
^ String |
297 |
streamContents:[:s | |
|
298 |
argumentTypes do:[:eachArgType | |
|
299 |
eachArgType printOn:s. |
|
300 |
] separatedBy:[ |
|
301 |
s nextPutAll:','. |
|
302 |
]. |
|
303 |
]. |
|
11426 | 304 |
! |
305 |
||
9463 | 306 |
beAsync |
307 |
"let this execute in a separate thread, in par with the other execution thread(s). |
|
308 |
Ignored under unix/linux (until those support multiple threads too)." |
|
309 |
||
310 |
flags := (flags ? 0) bitOr: FLAG_ASYNC. |
|
311 |
||
312 |
"Created: / 01-08-2006 / 13:42:38 / cg" |
|
313 |
! |
|
314 |
||
9465 | 315 |
beCallTypeAPI |
316 |
flags := (flags ? 0) bitOr: CALLTYPE_API. |
|
317 |
||
318 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
319 |
! |
|
320 |
||
321 |
beCallTypeC |
|
322 |
flags := (flags ? 0) bitOr: CALLTYPE_C. |
|
323 |
||
324 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
325 |
! |
|
326 |
||
10481 | 327 |
beCallTypeOLE |
328 |
flags := (flags ? 0) bitOr: FLAG_VIRTUAL. |
|
329 |
||
330 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
331 |
! |
|
332 |
||
9465 | 333 |
beCallTypeUNIX64 |
334 |
flags := (flags ? 0) bitOr: CALLTYPE_UNIX64. |
|
335 |
||
336 |
"Created: / 01-08-2006 / 15:13:38 / cg" |
|
337 |
! |
|
338 |
||
339 |
beCallTypeV8 |
|
340 |
flags := (flags ? 0) bitOr: CALLTYPE_V8. |
|
341 |
||
342 |
"Created: / 01-08-2006 / 15:13:28 / cg" |
|
343 |
! |
|
344 |
||
345 |
beCallTypeV9 |
|
346 |
flags := (flags ? 0) bitOr: CALLTYPE_V9. |
|
347 |
||
348 |
"Created: / 01-08-2006 / 15:13:31 / cg" |
|
349 |
! |
|
350 |
||
351 |
beCallTypeWINAPI |
|
352 |
self beCallTypeAPI |
|
353 |
||
354 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
355 |
! |
|
356 |
||
9525 | 357 |
beConstReturnValue |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
358 |
"specify that a pointer return value is not to be finalized |
9525 | 359 |
(i.e. points to static data or data which is freed by c)" |
360 |
||
361 |
flags := (flags ? 0) bitOr: FLAG_RETVAL_IS_CONST. |
|
362 |
||
363 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
364 |
! |
|
365 |
||
9463 | 366 |
beNonVirtualCPP |
367 |
"specify this as a non-virtual c++-function" |
|
368 |
||
369 |
flags := (flags ? 0) bitOr: FLAG_NONVIRTUAL. |
|
370 |
||
371 |
"Created: / 01-08-2006 / 13:56:44 / cg" |
|
372 |
! |
|
373 |
||
374 |
beUnlimitedStack |
|
375 |
"let this execute on the c-stack (as opposed to the thread-stack) |
|
376 |
for unlimited auto-sized-stack under unix/linux. |
|
377 |
Ignored under windows." |
|
378 |
||
379 |
flags := (flags ? 0) bitOr: FLAG_UNLIMITEDSTACK. |
|
380 |
||
381 |
"Created: / 01-08-2006 / 13:41:54 / cg" |
|
382 |
! |
|
383 |
||
384 |
beVirtualCPP |
|
385 |
"specify this as a virtual c++-function" |
|
386 |
||
387 |
flags := (flags ? 0) bitOr: FLAG_VIRTUAL. |
|
388 |
||
389 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
390 |
! |
|
391 |
||
9465 | 392 |
callTypeNumber |
393 |
^ (flags ? 0) bitAnd: CALLTYPE_MASK. |
|
394 |
||
395 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
396 |
! |
|
397 |
||
9463 | 398 |
isAsync |
399 |
"is this executed in a separate thread, in par with the other execution thread(s) ?" |
|
400 |
||
401 |
^ (flags ? 0) bitTest: FLAG_ASYNC. |
|
402 |
||
403 |
"Created: / 01-08-2006 / 13:46:53 / cg" |
|
404 |
! |
|
405 |
||
9519 | 406 |
isCPPFunction |
407 |
"is this a virtual or non-virtual c++-function ?" |
|
408 |
||
409 |
^ (flags ? 0) bitTest: (FLAG_VIRTUAL bitOr: FLAG_NONVIRTUAL). |
|
410 |
||
411 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
412 |
! |
|
413 |
||
9465 | 414 |
isCallTypeAPI |
415 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_API. |
|
416 |
||
417 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
418 |
! |
|
419 |
||
420 |
isCallTypeC |
|
421 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_C. |
|
422 |
||
423 |
"Created: / 01-08-2006 / 15:21:23 / cg" |
|
424 |
! |
|
425 |
||
10483 | 426 |
isCallTypeOLE |
427 |
^ ((flags ? 0) bitTest: FLAG_VIRTUAL). |
|
428 |
||
429 |
"Created: / 01-08-2006 / 15:21:23 / cg" |
|
430 |
! |
|
431 |
||
9525 | 432 |
isConstReturnValue |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
433 |
"is the pointer return value not to be finalized |
9525 | 434 |
(i.e. points to static data or data which is freed by c)" |
435 |
||
436 |
^ (flags ? 0) bitTest: FLAG_RETVAL_IS_CONST. |
|
437 |
||
438 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
439 |
! |
|
440 |
||
9463 | 441 |
isNonVirtualCPP |
442 |
"is this a non-virtual c++-function ?" |
|
443 |
||
444 |
^ (flags ? 0) bitTest: FLAG_NONVIRTUAL. |
|
445 |
||
446 |
"Created: / 01-08-2006 / 13:56:51 / cg" |
|
447 |
! |
|
448 |
||
449 |
isUnlimitedStack |
|
450 |
"will this execute on the c-stack (as opposed to the thread-stack) |
|
451 |
for unlimited auto-sized-stack under unix/linux. |
|
452 |
Ignored under windows." |
|
453 |
||
454 |
^ (flags ? 0) bitTest: FLAG_UNLIMITEDSTACK. |
|
455 |
||
456 |
"Created: / 01-08-2006 / 14:17:07 / cg" |
|
457 |
! |
|
458 |
||
459 |
isVirtualCPP |
|
460 |
"is this a virtual c++-function ?" |
|
461 |
||
462 |
^ (flags ? 0) bitTest: FLAG_VIRTUAL. |
|
463 |
||
464 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
10483 | 465 |
! |
466 |
||
11426 | 467 |
moduleName |
468 |
^ moduleName |
|
469 |
! |
|
470 |
||
10484 | 471 |
returnType |
472 |
^ returnType |
|
473 |
! |
|
474 |
||
10483 | 475 |
vtableIndex |
476 |
name isNumber ifFalse:[^ nil]. |
|
477 |
^ name. |
|
9321 | 478 |
! ! |
479 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
480 |
!ExternalLibraryFunction methodsFor:'invoking'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
481 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
482 |
invoke |
9321 | 483 |
self hasCode ifFalse:[ |
11586 | 484 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
485 |
]. |
10440 | 486 |
^ self invokeFFIWithArguments:nil |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
487 |
! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
488 |
|
9347 | 489 |
invokeCPPVirtualOn:anInstance |
9341 | 490 |
self hasCode ifFalse:[ |
11586 | 491 |
self prepareInvoke. |
9341 | 492 |
]. |
10440 | 493 |
^ self invokeCPPVirtualFFIOn:anInstance withArguments:nil |
9341 | 494 |
! |
495 |
||
9347 | 496 |
invokeCPPVirtualOn:instance with:arg |
9341 | 497 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
498 |
self prepareInvoke. |
9341 | 499 |
]. |
9347 | 500 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg) |
9341 | 501 |
! |
502 |
||
9347 | 503 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 |
9341 | 504 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
505 |
self prepareInvoke. |
9341 | 506 |
]. |
9347 | 507 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2) |
9341 | 508 |
! |
509 |
||
9347 | 510 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 |
9341 | 511 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
512 |
self prepareInvoke. |
9341 | 513 |
]. |
9347 | 514 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3) |
9341 | 515 |
! |
516 |
||
9347 | 517 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 with:arg4 |
9341 | 518 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
519 |
self prepareInvoke. |
9341 | 520 |
]. |
9347 | 521 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
9341 | 522 |
! |
523 |
||
10481 | 524 |
invokeCPPVirtualOn:instance withArguments:args |
525 |
self hasCode ifFalse:[ |
|
11586 | 526 |
self prepareInvoke. |
10481 | 527 |
]. |
528 |
^ self invokeCPPVirtualFFIOn:instance withArguments:args |
|
529 |
! |
|
530 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
531 |
invokeWith:arg |
9321 | 532 |
self hasCode ifFalse:[ |
9464 | 533 |
self prepareInvoke. |
9321 | 534 |
]. |
535 |
^ self invokeFFIWithArguments:(Array with:arg) |
|
536 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
537 |
|
9321 | 538 |
invokeWith:arg1 with:arg2 |
539 |
self hasCode ifFalse:[ |
|
9464 | 540 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
541 |
]. |
9321 | 542 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2) |
543 |
! |
|
544 |
||
545 |
invokeWith:arg1 with:arg2 with:arg3 |
|
546 |
self hasCode ifFalse:[ |
|
9464 | 547 |
self prepareInvoke. |
9321 | 548 |
]. |
549 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
550 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
551 |
|
9321 | 552 |
invokeWith:arg1 with:arg2 with:arg3 with:arg4 |
553 |
self hasCode ifFalse:[ |
|
9464 | 554 |
self prepareInvoke. |
9321 | 555 |
]. |
556 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
557 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
558 |
|
9321 | 559 |
invokeWithArguments:argArray |
560 |
self hasCode ifFalse:[ |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
561 |
self prepareInvoke. |
9321 | 562 |
]. |
9465 | 563 |
^ self invokeFFIwithArguments:argArray forCPPInstance:nil |
564 |
||
565 |
"Modified: / 01-08-2006 / 16:04:08 / cg" |
|
9321 | 566 |
! ! |
567 |
||
568 |
!ExternalLibraryFunction methodsFor:'printing'! |
|
569 |
||
570 |
printOn:aStream |
|
571 |
aStream nextPutAll:'<'. |
|
9465 | 572 |
self isCallTypeAPI ifTrue:[ |
14625 | 573 |
'API:' printOn:aStream. |
9465 | 574 |
] ifFalse:[ |
14625 | 575 |
self isCallTypeOLE ifTrue:[ |
576 |
'OLE:' printOn:aStream. |
|
577 |
] ifFalse:[ |
|
578 |
self isCallTypeC ifTrue:[ |
|
579 |
'C:' printOn:aStream. |
|
580 |
] ifFalse:[ |
|
581 |
self error. |
|
582 |
]. |
|
583 |
]. |
|
9465 | 584 |
]. |
9321 | 585 |
aStream nextPutAll:' '. |
586 |
name printOn:aStream. |
|
9465 | 587 |
moduleName notNil ifTrue:[ |
14625 | 588 |
aStream nextPutAll:' module:'. |
589 |
moduleName printOn:aStream. |
|
9465 | 590 |
]. |
9321 | 591 |
aStream nextPutAll:'>'. |
9465 | 592 |
|
14356 | 593 |
"Modified: / 25-09-2012 / 12:06:14 / cg" |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
594 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
595 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
596 |
!ExternalLibraryFunction methodsFor:'private'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
597 |
|
10213 | 598 |
adjustTypes |
599 |
argumentTypes notNil ifTrue:[ |
|
600 |
argumentTypes := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType]. |
|
601 |
]. |
|
602 |
returnType := self ffiTypeSymbolForType:returnType. |
|
603 |
! |
|
604 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
605 |
linkToModule |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
606 |
"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
|
607 |
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
|
608 |
|
9465 | 609 |
|handle moduleNameUsed functionName| |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
610 |
|
10481 | 611 |
name isNumber ifTrue:[ |
14625 | 612 |
self isCPPFunction ifTrue:[ |
613 |
"/ no need to load a dll. |
|
614 |
^ self |
|
615 |
] |
|
10481 | 616 |
]. |
617 |
||
13782
1994fe87f21e
comment/format in: #linkToModule
Claus Gittinger <cg@exept.de>
parents:
13412
diff
changeset
|
618 |
"/ in some other smalltalks, there is no moduleName in the ffi-spec; |
1994fe87f21e
comment/format in: #linkToModule
Claus Gittinger <cg@exept.de>
parents:
13412
diff
changeset
|
619 |
"/ instead, the class provides the libraryName... |
9465 | 620 |
(moduleNameUsed := moduleName) isNil ifTrue:[ |
14625 | 621 |
owningClass isNil ifTrue:[ |
622 |
self error:'Missing moduleName'. |
|
623 |
]. |
|
624 |
moduleNameUsed := owningClass theNonMetaclass libraryName asSymbol. |
|
9321 | 625 |
]. |
11586 | 626 |
moduleHandle isNil ifTrue:[ |
14625 | 627 |
handle := self loadLibrary:moduleNameUsed. |
628 |
handle isNil ifTrue:[ |
|
629 |
self error:('Cannot find or load dll/module: "%1"' bindWith: moduleNameUsed). |
|
630 |
]. |
|
631 |
moduleHandle := handle. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
632 |
]. |
9341 | 633 |
name isNumber ifFalse:[ |
14625 | 634 |
functionName := name. |
635 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
|
636 |
functionName := ('_', functionName) asSymbol. |
|
9418
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
637 |
|
14625 | 638 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
639 |
moduleHandle := nil. |
|
640 |
self error:'Missing function: ', name, ' in module: ', moduleNameUsed. |
|
641 |
]. |
|
642 |
]. |
|
9321 | 643 |
]. |
9392 | 644 |
|
14103 | 645 |
"Modified: / 10-04-2012 / 12:12:44 / cg" |
9321 | 646 |
! |
647 |
||
10270 | 648 |
loadLibrary:dllName |
12436 | 649 |
|handle nameString filename| |
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
650 |
|
14103 | 651 |
filename := dllName. |
652 |
DllMapping notNil ifTrue:[ |
|
14625 | 653 |
filename := DllMapping at:filename ifAbsent:[ filename ] |
14103 | 654 |
]. |
655 |
||
656 |
filename := filename asFilename. |
|
12436 | 657 |
nameString := filename name. |
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
658 |
|
12937 | 659 |
"try to load, maybe the system knows where to find the dll" |
660 |
handle := ObjectFileLoader loadDynamicObject:filename. |
|
661 |
handle notNil ifTrue:[^ handle ]. |
|
662 |
||
12436 | 663 |
filename isAbsolute ifFalse:[ |
14625 | 664 |
"First ask the class defining the ExternalFunction for the location of the dlls ..." |
665 |
owningClass notNil ifTrue:[ |
|
666 |
owningClass dllPath do:[:eachDirectory | |
|
667 |
handle := ObjectFileLoader |
|
668 |
loadDynamicObject:(eachDirectory asFilename construct:nameString) pathName. |
|
669 |
handle notNil ifTrue:[^ handle ]. |
|
670 |
]. |
|
671 |
]. |
|
672 |
".. then ask the system" |
|
673 |
self class dllPath do:[:eachDirectory | |
|
674 |
handle := ObjectFileLoader |
|
675 |
loadDynamicObject:(eachDirectory asFilename construct:nameString) pathName. |
|
676 |
handle notNil ifTrue:[^ handle ]. |
|
677 |
]. |
|
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
678 |
]. |
10270 | 679 |
|
12436 | 680 |
filename suffix isEmpty ifTrue:[ |
14625 | 681 |
"/ try again with the OS-specific dll-extension |
682 |
^ self loadLibrary:(filename withSuffix:ObjectFileLoader sharedLibrarySuffix) |
|
10270 | 683 |
]. |
12436 | 684 |
|
10270 | 685 |
^ nil |
14103 | 686 |
|
687 |
"Modified: / 10-04-2012 / 12:21:06 / cg" |
|
10270 | 688 |
! |
689 |
||
9321 | 690 |
prepareInvoke |
10270 | 691 |
(moduleHandle isNil or:[self hasCode not]) ifTrue:[ |
12450 | 692 |
self linkToModule. |
693 |
self adjustTypes. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
694 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
695 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
696 |
|
8550 | 697 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
698 |
||
9327 | 699 |
ffiTypeSymbolForType:aType |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
700 |
"map type to one of the ffi-supported ones: |
14625 | 701 |
sint8, sint16, sint32, sint64 |
702 |
uint8, uint16, uint32, uint64 |
|
703 |
bool void pointer handle |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
704 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
705 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
706 |
aType == #sint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
707 |
aType == #sint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
708 |
aType == #sint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
709 |
aType == #sint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
710 |
aType == #uint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
711 |
aType == #uint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
712 |
aType == #uint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
713 |
aType == #uint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
714 |
aType == #double ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
715 |
aType == #float ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
716 |
aType == #char ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
717 |
aType == #void ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
718 |
aType == #bool ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
719 |
aType == #pointer ifTrue:[^ aType ]. |
12657
7da296a8eeb6
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12656
diff
changeset
|
720 |
aType == #charPointer ifTrue:[^ aType ]. |
7da296a8eeb6
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12656
diff
changeset
|
721 |
aType == #wcharPointer ifTrue:[^ aType ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
722 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
723 |
aType == #int8 ifTrue:[^ #sint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
724 |
aType == #int16 ifTrue:[^ #sint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
725 |
aType == #int32 ifTrue:[^ #sint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
726 |
aType == #int64 ifTrue:[^ #sint64 ]. |
12450 | 727 |
|
728 |
aType == #voidPointer ifTrue:[^ #pointer ]. |
|
729 |
aType == #uint8Pointer ifTrue:[^ #pointer ]. |
|
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
730 |
aType == #voidPointerPointer ifTrue:[^ #pointer ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
731 |
|
10213 | 732 |
aType == #short ifTrue:[^ #sint16 ]. |
11586 | 733 |
aType == #long ifTrue:[^ #long ]. |
734 |
aType == #int ifTrue:[^ #int ]. |
|
735 |
aType == #uint ifTrue:[^ #uint ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
736 |
aType == #ushort ifTrue:[^ #uint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
737 |
aType == #unsignedShort ifTrue:[^ #uint16 ]. |
11586 | 738 |
aType == #ulong ifTrue:[^ #ulong ]. |
739 |
aType == #unsignedLong ifTrue:[^ #ulong ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
740 |
aType == #uchar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
741 |
aType == #unsignedChar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
742 |
aType == #byte ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
743 |
aType == #dword ifTrue:[^ #uint32 ]. |
10213 | 744 |
aType == #sdword ifTrue:[^ #sint32 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
745 |
aType == #word ifTrue:[^ #uint16 ]. |
10213 | 746 |
aType == #sword ifTrue:[^ #sint16 ]. |
12503
cb1a875c3d63
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12474
diff
changeset
|
747 |
aType == #longlong ifTrue:[^ #sint64 ]. |
12450 | 748 |
aType == #longLong ifTrue:[^ #sint64 ]. |
12503
cb1a875c3d63
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12474
diff
changeset
|
749 |
aType == #ulonglong ifTrue:[^ #uint64 ]. |
12450 | 750 |
aType == #ulongLong ifTrue:[^ #uint64 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
751 |
aType == #handle ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
752 |
aType == #lpstr ifTrue:[^ #charPointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
753 |
aType == #hresult ifTrue:[^ #uint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
754 |
aType == #boolean ifTrue:[^ #bool ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
755 |
aType == #ulongReturn ifTrue:[^ #uint32 ]. "/ TODO - care for 64bit machines |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
756 |
aType == #none ifTrue:[^ #void ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
757 |
aType == #struct ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
758 |
aType == #structIn ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
759 |
aType == #structOut ifTrue:[^ #pointer ]. |
10981 | 760 |
aType == #unsigned ifTrue:[^ #uint ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
761 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
762 |
(aType isString or:[aType isSymbol]) ifFalse:[ |
14625 | 763 |
CType isNil ifTrue:[ |
764 |
self error:'unknown type'. |
|
765 |
]. |
|
766 |
^ aType typeSymbol. |
|
9483 | 767 |
]. |
768 |
||
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
769 |
(aType endsWith:'*') ifTrue:[ |
14625 | 770 |
^ #pointer. |
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
771 |
]. |
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
772 |
(aType endsWith:'Pointer') ifTrue:[ |
14625 | 773 |
^ #pointer. |
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
774 |
]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
775 |
^ aType |
10625 | 776 |
|
777 |
"Modified: / 14-06-2007 / 17:21:42 / cg" |
|
9322 | 778 |
! |
779 |
||
9466 | 780 |
name:functionNameOrVirtualIndex module:aModuleName returnType:aReturnType argumentTypes:argTypes |
781 |
name := functionNameOrVirtualIndex. |
|
782 |
functionNameOrVirtualIndex isNumber ifTrue:[ |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
783 |
self beVirtualCPP. |
9466 | 784 |
]. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
785 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
786 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
787 |
argumentTypes := argTypes. |
9465 | 788 |
|
789 |
"Created: / 01-08-2006 / 15:19:52 / cg" |
|
9466 | 790 |
"Modified: / 02-08-2006 / 17:20:13 / cg" |
9465 | 791 |
! |
792 |
||
11426 | 793 |
owningClass |
794 |
^ owningClass |
|
795 |
! |
|
796 |
||
9465 | 797 |
owningClass:aClass |
798 |
owningClass := aClass. |
|
799 |
||
800 |
"Created: / 01-08-2006 / 15:22:50 / cg" |
|
10603 | 801 |
! |
802 |
||
11586 | 803 |
setModuleName:aModuleName |
10603 | 804 |
aModuleName ~= moduleName ifTrue:[ |
11586 | 805 |
self code:nil. |
806 |
moduleHandle := nil. |
|
807 |
moduleName := aModuleName. |
|
10603 | 808 |
]. |
809 |
||
810 |
"Created: / 07-06-2007 / 10:20:17 / cg" |
|
8550 | 811 |
! ! |
812 |
||
9342 | 813 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
814 |
||
9347 | 815 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
9463 | 816 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance |
817 |
||
818 |
"Modified: / 01-08-2006 / 13:55:30 / cg" |
|
9342 | 819 |
! |
820 |
||
9347 | 821 |
invokeFFIWithArguments:arguments |
9463 | 822 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil |
823 |
||
824 |
"Modified: / 01-08-2006 / 13:55:35 / cg" |
|
9347 | 825 |
! |
826 |
||
10440 | 827 |
invokeFFIwithArguments:argumentsOrNil forCPPInstance:aCPlusPlusObjectOrNil |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
828 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo returnValue stClass vtOffset |
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
829 |
virtual async unlimitedStack callTypeNumber returnValueClass argValueClass |
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
830 |
oldReturnType oldArgumentTypes |
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
831 |
| |
9342 | 832 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
833 |
argTypeSymbols := argumentTypes. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
834 |
returnTypeSymbol := returnType. |
9346 | 835 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
836 |
virtual := self isVirtualCPP. |
9524 | 837 |
(virtual "or:[self isNonVirtualCPP]") ifTrue:[ |
14625 | 838 |
aCPlusPlusObjectOrNil isNil ifTrue:[ |
839 |
"/ must have a c++ object instance |
|
840 |
self primitiveFailed. |
|
841 |
]. |
|
9463 | 842 |
|
14625 | 843 |
"/ and it must be a kind of ExternalStructure !! |
844 |
(aCPlusPlusObjectOrNil isKindOf:ExternalStructure) ifFalse:[ |
|
845 |
self primitiveFailed. |
|
846 |
]. |
|
847 |
virtual ifTrue:[ |
|
848 |
vtOffset := name. |
|
849 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
850 |
self primitiveFailed. |
|
851 |
] |
|
852 |
]. |
|
9463 | 853 |
] ifFalse:[ |
14625 | 854 |
aCPlusPlusObjectOrNil notNil ifTrue:[ |
855 |
"/ must NOT have a c++ object instance |
|
856 |
self primitiveFailed. |
|
857 |
]. |
|
9342 | 858 |
]. |
9463 | 859 |
async := self isAsync. |
860 |
unlimitedStack := self isUnlimitedStack. |
|
9465 | 861 |
callTypeNumber := self callTypeNumber. |
10512 | 862 |
"/ Transcript show:name; show:' async:'; showCR:async. |
9342 | 863 |
|
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
864 |
%{ /* STACK: 100000 */ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
865 |
#ifdef HAVE_FFI |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
866 |
# ifdef __GNUC__ |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
867 |
# define HAS_LONGLONG |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
868 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
869 |
# if defined(__BORLANDC__) || defined(__VISUALC__) |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
870 |
# define HAS_INT64 |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
871 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
872 |
|
9342 | 873 |
ffi_cif __cif; |
874 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
875 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
876 |
ffi_type *__returnType = NULL; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
877 |
|
9342 | 878 |
union u { |
14625 | 879 |
int iVal; |
880 |
float fVal; |
|
881 |
double dVal; |
|
882 |
void *pointerVal; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
883 |
# if defined(HAS_LONGLONG) |
14625 | 884 |
long long longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
885 |
# else |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
886 |
# ifdef HAS_INT64 |
14625 | 887 |
__int64__ longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
888 |
# else |
14625 | 889 |
struct ll { long low; long hi; } longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
890 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
891 |
# endif |
9342 | 892 |
}; |
893 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
894 |
union u *__argValues = __argValuesIncludingThis; |
|
895 |
union u __returnValue; |
|
896 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
897 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
898 |
void *__returnValuePointer; |
|
899 |
int __numArgs, __numArgsIncludingThis; |
|
900 |
static int null = 0; |
|
901 |
int i; |
|
902 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
903 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
|
10440 | 904 |
int __numArgsWanted; |
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
905 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
906 |
# define __FAIL__(fcode) \ |
10603 | 907 |
{ \ |
14625 | 908 |
failureCode = fcode; goto getOutOfHere; \ |
10603 | 909 |
} |
9342 | 910 |
|
10440 | 911 |
if (argumentsOrNil == nil) { |
14625 | 912 |
__numArgs = 0; |
10440 | 913 |
} else if (__isArray(argumentsOrNil)) { |
14625 | 914 |
__numArgs = __arraySize(argumentsOrNil); |
9342 | 915 |
} else { |
14625 | 916 |
__FAIL__(@symbol(BadArgumentVector)) |
10440 | 917 |
} |
918 |
if (argTypeSymbols == nil) { |
|
14625 | 919 |
__numArgsWanted = 0; |
10440 | 920 |
} else if (__isArray(argTypeSymbols)) { |
14625 | 921 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10440 | 922 |
} else { |
14625 | 923 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10440 | 924 |
} |
925 |
||
926 |
if (__numArgs != __numArgsWanted) { |
|
14625 | 927 |
__FAIL__(@symbol(ArgumentCountMismatch)) |
9342 | 928 |
} |
929 |
if (__numArgs > MAX_ARGS) { |
|
14625 | 930 |
__FAIL__(@symbol(TooManyArguments)) |
9342 | 931 |
} |
932 |
||
933 |
/* |
|
934 |
* validate the return type |
|
935 |
*/ |
|
936 |
__returnValuePointer = &__returnValue; |
|
9479 | 937 |
|
9483 | 938 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
14625 | 939 |
returnTypeSymbol = @symbol(handle); |
9483 | 940 |
} |
9479 | 941 |
|
9342 | 942 |
if (returnTypeSymbol == @symbol(int)) { |
14625 | 943 |
__returnType = __get_ffi_type_sint(); |
9342 | 944 |
} else if (returnTypeSymbol == @symbol(uint)) { |
14625 | 945 |
__returnType = __get_ffi_type_uint(); |
9479 | 946 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
14625 | 947 |
__returnType = __get_ffi_type_uint8(); |
9479 | 948 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
14625 | 949 |
__returnType = __get_ffi_type_uint16(); |
9479 | 950 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
14625 | 951 |
__returnType = __get_ffi_type_uint32(); |
9479 | 952 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
14625 | 953 |
__returnType = __get_ffi_type_uint64(); |
9479 | 954 |
|
955 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
14625 | 956 |
__returnType = __get_ffi_type_sint(); |
9479 | 957 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
14625 | 958 |
__returnType = __get_ffi_type_sint8(); |
9479 | 959 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
14625 | 960 |
__returnType = __get_ffi_type_sint16(); |
9479 | 961 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
14625 | 962 |
__returnType = __get_ffi_type_sint32(); |
9479 | 963 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
14625 | 964 |
__returnType = __get_ffi_type_sint64(); |
9479 | 965 |
|
9342 | 966 |
} else if (returnTypeSymbol == @symbol(long)) { |
14625 | 967 |
if (sizeof(long) == 4) { |
968 |
returnTypeSymbol = @symbol(sint32); |
|
969 |
__returnType = __get_ffi_type_sint32(); |
|
970 |
} else if (sizeof(long) == 8) { |
|
971 |
returnTypeSymbol = @symbol(sint64); |
|
972 |
__returnType = __get_ffi_type_sint64(); |
|
973 |
} else { |
|
974 |
__FAIL__(@symbol(UnknownReturnType)) |
|
975 |
} |
|
9479 | 976 |
|
9342 | 977 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
14625 | 978 |
if (sizeof(long) == 4) { |
979 |
returnTypeSymbol = @symbol(uint32); |
|
980 |
__returnType = __get_ffi_type_uint32(); |
|
981 |
}else if (sizeof(long) == 8) { |
|
982 |
returnTypeSymbol = @symbol(uint64); |
|
983 |
__returnType = __get_ffi_type_uint64(); |
|
984 |
} else { |
|
985 |
__FAIL__(@symbol(UnknownReturnType)) |
|
986 |
} |
|
9479 | 987 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
988 |
} else if (returnTypeSymbol == @symbol(bool)) { |
14625 | 989 |
__returnType = __get_ffi_type_uint(); |
9479 | 990 |
|
9342 | 991 |
} else if (returnTypeSymbol == @symbol(float)) { |
14625 | 992 |
__returnType = __get_ffi_type_float(); |
9342 | 993 |
} else if (returnTypeSymbol == @symbol(double)) { |
14625 | 994 |
__returnType = __get_ffi_type_double(); |
9479 | 995 |
|
9342 | 996 |
} else if (returnTypeSymbol == @symbol(void)) { |
14625 | 997 |
__returnType = __get_ffi_type_void(); |
998 |
__returnValuePointer = NULL; |
|
11586 | 999 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
14625 | 1000 |
|| (returnTypeSymbol == @symbol(handle)) |
1001 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
1002 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
1003 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
1004 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
1005 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
1006 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
1007 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
1008 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 1009 |
} else { |
14625 | 1010 |
if (__isSymbol(returnTypeSymbol) |
1011 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
1012 |
if (! __isBehaviorLike(returnValueClass)) { |
|
1013 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
1014 |
} |
|
1015 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
1016 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
1017 |
} |
|
1018 |
__returnType = __get_ffi_type_pointer(); |
|
1019 |
returnTypeSymbol = @symbol(pointer); |
|
1020 |
} else { |
|
1021 |
__FAIL__(@symbol(UnknownReturnType)) |
|
1022 |
} |
|
9342 | 1023 |
} |
1024 |
||
1025 |
/* |
|
1026 |
* validate the c++ object |
|
1027 |
*/ |
|
1028 |
if (aCPlusPlusObjectOrNil != nil) { |
|
14625 | 1029 |
struct cPlusPlusInstance { |
1030 |
void **vTable; |
|
1031 |
}; |
|
1032 |
struct cPlusPlusInstance *inst; |
|
9342 | 1033 |
|
14625 | 1034 |
if (__isExternalAddressLike(aCPlusPlusObjectOrNil)) { |
1035 |
inst = (void *)(__externalAddressVal(aCPlusPlusObjectOrNil)); |
|
1036 |
} else if (__isExternalBytesLike(aCPlusPlusObjectOrNil)) { |
|
1037 |
inst = (void *)(__externalBytesVal(aCPlusPlusObjectOrNil)); |
|
1038 |
} else { |
|
1039 |
__FAIL__(@symbol(InvalidInstance)) |
|
1040 |
} |
|
1041 |
__argValues[0].pointerVal = inst; |
|
1042 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
1043 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
9342 | 1044 |
|
14625 | 1045 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
1046 |
__argTypes = &__argTypesIncludingThis[1]; |
|
1047 |
__argValues = &__argValuesIncludingThis[1]; |
|
1048 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 1049 |
|
14625 | 1050 |
if (virtual == true) { |
1051 |
if (! __isSmallInteger(vtOffset)) { |
|
1052 |
__FAIL__(@symbol(InvalidVTableIndex)) |
|
1053 |
} |
|
1054 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1055 |
# ifdef VERBOSE |
14625 | 1056 |
printf("virtual codeAddress: %x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1057 |
# endif |
14625 | 1058 |
} |
9342 | 1059 |
} else { |
14625 | 1060 |
__numArgsIncludingThis = __numArgs; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1061 |
# ifdef VERBOSE |
14625 | 1062 |
printf("codeAddress: %x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1063 |
# endif |
9342 | 1064 |
} |
1065 |
||
1066 |
/* |
|
1067 |
* validate all arg types and setup arg-buffers |
|
1068 |
*/ |
|
1069 |
for (i=0; i<__numArgs; i++) { |
|
14625 | 1070 |
ffi_type *thisType; |
1071 |
void *argValuePtr; |
|
1072 |
OBJ typeSymbol; |
|
1073 |
OBJ arg; |
|
9342 | 1074 |
|
14625 | 1075 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
9465 | 1076 |
|
14625 | 1077 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
1078 |
arg = __ArrayInstPtr(argumentsOrNil)->a_element[i]; |
|
9342 | 1079 |
|
14625 | 1080 |
if (typeSymbol == @symbol(handle)) { |
1081 |
typeSymbol = @symbol(pointer); |
|
1082 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
1083 |
typeSymbol = @symbol(pointer); |
|
1084 |
} |
|
9479 | 1085 |
|
14625 | 1086 |
if (typeSymbol == @symbol(long)) { |
1087 |
if (sizeof(long) == sizeof(int)) { |
|
1088 |
typeSymbol = @symbol(sint); |
|
1089 |
} else { |
|
1090 |
if (sizeof(long) == 4) { |
|
1091 |
typeSymbol = @symbol(sint32); |
|
1092 |
} else if (sizeof(long) == 8) { |
|
1093 |
typeSymbol = @symbol(sint64); |
|
1094 |
} |
|
1095 |
} |
|
1096 |
} |
|
1097 |
if (typeSymbol == @symbol(ulong)) { |
|
1098 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
1099 |
typeSymbol = @symbol(uint); |
|
1100 |
} else { |
|
1101 |
if (sizeof(long) == 4) { |
|
1102 |
typeSymbol = @symbol(uint32); |
|
1103 |
} else if (sizeof(long) == 8) { |
|
1104 |
typeSymbol = @symbol(uint64); |
|
1105 |
} |
|
1106 |
} |
|
1107 |
} |
|
9342 | 1108 |
|
14625 | 1109 |
if (typeSymbol == @symbol(int) || typeSymbol == @symbol(sint)) { |
1110 |
thisType = __get_ffi_type_sint(); |
|
1111 |
if (__isSmallInteger(arg)) { |
|
1112 |
__argValues[i].iVal = __intVal(arg); |
|
1113 |
} else { |
|
1114 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
1115 |
if (__argValues[i].iVal == 0) { |
|
1116 |
__FAIL__(@symbol(InvalidArgument)) |
|
1117 |
} |
|
1118 |
} |
|
1119 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1120 |
|
14625 | 1121 |
} else if (typeSymbol == @symbol(uint)) { |
1122 |
thisType = __get_ffi_type_uint(); |
|
9342 | 1123 |
|
14625 | 1124 |
if (__isSmallInteger(arg)) { |
1125 |
__argValues[i].iVal = __intVal(arg); |
|
1126 |
} else { |
|
1127 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1128 |
if (__argValues[i].iVal == 0) { |
|
1129 |
__FAIL__(@symbol(InvalidArgument)) |
|
1130 |
} |
|
1131 |
} |
|
1132 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1133 |
|
14625 | 1134 |
} else if (typeSymbol == @symbol(uint8)) { |
1135 |
thisType = __get_ffi_type_uint8(); |
|
1136 |
if (! __isSmallInteger(arg)) { |
|
1137 |
__FAIL__(@symbol(InvalidArgument)) |
|
1138 |
} |
|
1139 |
__argValues[i].iVal = __intVal(arg); |
|
1140 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
1141 |
__FAIL__(@symbol(InvalidArgument)) |
|
1142 |
} |
|
1143 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1144 |
|
14625 | 1145 |
} else if (typeSymbol == @symbol(sint8)) { |
1146 |
thisType = __get_ffi_type_sint8(); |
|
1147 |
if (! __isSmallInteger(arg)) { |
|
1148 |
__FAIL__(@symbol(InvalidArgument)) |
|
1149 |
} |
|
1150 |
__argValues[i].iVal = __intVal(arg); |
|
1151 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
1152 |
__FAIL__(@symbol(InvalidArgument)) |
|
1153 |
} |
|
1154 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1155 |
|
14625 | 1156 |
} else if (typeSymbol == @symbol(uint16)) { |
1157 |
thisType = __get_ffi_type_uint16(); |
|
1158 |
if (! __isSmallInteger(arg)) { |
|
1159 |
__FAIL__(@symbol(InvalidArgument)) |
|
1160 |
} |
|
1161 |
__argValues[i].iVal = __intVal(arg); |
|
1162 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
1163 |
__FAIL__(@symbol(InvalidArgument)) |
|
1164 |
} |
|
1165 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1166 |
|
14625 | 1167 |
} else if (typeSymbol == @symbol(sint16)) { |
1168 |
thisType = __get_ffi_type_sint16(); |
|
1169 |
if (! __isSmallInteger(arg)) { |
|
1170 |
__FAIL__(@symbol(InvalidArgument)) |
|
1171 |
} |
|
1172 |
__argValues[i].iVal = __intVal(arg); |
|
1173 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
1174 |
__FAIL__(@symbol(InvalidArgument)) |
|
1175 |
} |
|
1176 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1177 |
|
14625 | 1178 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
1179 |
thisType = __get_ffi_type_uint32(); |
|
1180 |
if (__isSmallInteger(arg)) { |
|
1181 |
__argValues[i].iVal = __intVal(arg); |
|
1182 |
} else { |
|
1183 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1184 |
if (__argValues[i].iVal == 0) { |
|
1185 |
__FAIL__(@symbol(InvalidArgument)) |
|
1186 |
} |
|
1187 |
} |
|
1188 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1189 |
|
14625 | 1190 |
} else if (typeSymbol == @symbol(float)) { |
1191 |
thisType = __get_ffi_type_float(); |
|
1192 |
if (__isSmallInteger(arg)) { |
|
1193 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
1194 |
} else if (__isFloat(arg)) { |
|
1195 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
1196 |
} else if (__isShortFloat(arg)) { |
|
1197 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
1198 |
} else { |
|
1199 |
__FAIL__(@symbol(InvalidArgument)) |
|
1200 |
} |
|
1201 |
argValuePtr = &(__argValues[i].fVal); |
|
9479 | 1202 |
|
14625 | 1203 |
} else if (typeSymbol == @symbol(double)) { |
1204 |
thisType = __get_ffi_type_double(); |
|
1205 |
if (__isSmallInteger(arg)) { |
|
1206 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
1207 |
} else if (__isFloat(arg)) { |
|
1208 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
1209 |
} else if (__isShortFloat(arg)) { |
|
1210 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
1211 |
} else { |
|
1212 |
__FAIL__(@symbol(InvalidArgument)) |
|
1213 |
} |
|
1214 |
argValuePtr = &(__argValues[i].dVal); |
|
9479 | 1215 |
|
14625 | 1216 |
} else if (typeSymbol == @symbol(void)) { |
1217 |
thisType = __get_ffi_type_void(); |
|
1218 |
argValuePtr = &null; |
|
9479 | 1219 |
|
14625 | 1220 |
} else if (typeSymbol == @symbol(charPointer)) { |
1221 |
thisType = __get_ffi_type_pointer(); |
|
1222 |
if (__isStringLike(arg)) { |
|
1223 |
if (async == true) goto badArgForAsyncCall; |
|
1224 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1225 |
} else if (__isBytes(arg)) { |
|
1226 |
if (async == true) goto badArgForAsyncCall; |
|
1227 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1228 |
} else if (__isExternalAddressLike(arg)) { |
|
1229 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1230 |
} else if (__isExternalBytesLike(arg)) { |
|
1231 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1232 |
} else { |
|
1233 |
if (arg == nil) { |
|
1234 |
__argValues[i].pointerVal = (void *)0; |
|
1235 |
} else { |
|
1236 |
__FAIL__(@symbol(InvalidArgument)) |
|
1237 |
} |
|
1238 |
} |
|
1239 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1240 |
|
14625 | 1241 |
} else if (typeSymbol == @symbol(wcharPointer)) { |
1242 |
thisType = __get_ffi_type_pointer(); |
|
1243 |
if (__isUnicode16String(arg)) { |
|
1244 |
if (async == true) goto badArgForAsyncCall; |
|
1245 |
__argValues[i].pointerVal = (void *)(__unicode16StringVal(arg)); |
|
1246 |
} else if (__isBytes(arg)) { |
|
1247 |
if (async == true) goto badArgForAsyncCall; |
|
1248 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1249 |
} else if (__isExternalAddressLike(arg)) { |
|
1250 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1251 |
} else if (__isExternalBytesLike(arg)) { |
|
1252 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1253 |
} else { |
|
1254 |
if (arg == nil) { |
|
1255 |
__argValues[i].pointerVal = (void *)0; |
|
1256 |
} else { |
|
1257 |
__FAIL__(@symbol(InvalidArgument)) |
|
1258 |
} |
|
1259 |
} |
|
1260 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
12656 | 1261 |
|
14625 | 1262 |
} else if (typeSymbol == @symbol(floatPointer)) { |
1263 |
thisType = __get_ffi_type_pointer(); |
|
1264 |
if (__isBytes(arg)) { |
|
1265 |
if (async == true) goto badArgForAsyncCall; |
|
1266 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1267 |
} else if (__isExternalAddressLike(arg)) { |
|
1268 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1269 |
} else if (__isExternalBytesLike(arg)) { |
|
1270 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1271 |
} else if (__isFloats(arg)) { |
|
1272 |
char *p = (char *)(__FloatArrayInstPtr(arg)->f_element); |
|
1273 |
int nInstBytes; |
|
1274 |
OBJ cls; |
|
10603 | 1275 |
|
14625 | 1276 |
if (async == true) goto badArgForAsyncCall; |
1277 |
cls = __qClass(arg); |
|
1278 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1279 |
p = p + nInstBytes; |
|
1280 |
__argValues[i].pointerVal = p; |
|
1281 |
} else { |
|
1282 |
if (arg == nil) { |
|
1283 |
__argValues[i].pointerVal = (void *)0; |
|
1284 |
} else { |
|
1285 |
__FAIL__(@symbol(InvalidArgument)) |
|
1286 |
} |
|
1287 |
} |
|
1288 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1289 |
|
14625 | 1290 |
} else if (typeSymbol == @symbol(doublePointer)) { |
1291 |
thisType = __get_ffi_type_pointer(); |
|
1292 |
if (__isBytes(arg)) { |
|
1293 |
if (async == true) goto badArgForAsyncCall; |
|
1294 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1295 |
} else if (__isExternalAddressLike(arg)) { |
|
1296 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1297 |
} else if (__isExternalBytesLike(arg)) { |
|
1298 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1299 |
} else if (__isDoubles(arg)) { |
|
1300 |
char *p = (char *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1301 |
int nInstBytes; |
|
1302 |
OBJ cls; |
|
10603 | 1303 |
|
14625 | 1304 |
if (async == true) goto badArgForAsyncCall; |
1305 |
cls = __qClass(arg); |
|
1306 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1307 |
p = p + nInstBytes; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1308 |
# ifdef __NEED_DOUBLE_ALIGN |
14625 | 1309 |
if ((INT)(__DoubleArrayInstPtr(arg)->d_element) & (__DOUBLE_ALIGN-1)) { |
1310 |
int delta = __DOUBLE_ALIGN - ((INT)p & (__DOUBLE_ALIGN-1)); |
|
10603 | 1311 |
|
14625 | 1312 |
p += delta; |
1313 |
} |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1314 |
# endif |
14625 | 1315 |
__argValues[i].pointerVal = p; |
1316 |
} else { |
|
1317 |
if (arg == nil) { |
|
1318 |
__argValues[i].pointerVal = (void *)0; |
|
1319 |
} else { |
|
1320 |
__FAIL__(@symbol(InvalidArgument)) |
|
1321 |
} |
|
1322 |
} |
|
1323 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1324 |
|
14625 | 1325 |
} else if (typeSymbol == @symbol(pointer)) { |
9524 | 1326 |
commonPointerTypeArg: ; |
14625 | 1327 |
thisType = __get_ffi_type_pointer(); |
1328 |
if (arg == nil) { |
|
1329 |
__argValues[i].pointerVal = NULL; |
|
1330 |
} else if (__isExternalAddressLike(arg)) { |
|
1331 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1332 |
} else if (__isExternalBytesLike(arg)) { |
|
1333 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1334 |
} else if (__isByteArrayLike(arg)) { |
|
1335 |
if (async == true) goto badArgForAsyncCall; |
|
1336 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1337 |
} else if (__isWordArray(arg) || __isSignedWordArray(arg) |
|
1338 |
|| __isIntegerArray(arg) || __isSignedIntegerArray(arg)) { |
|
1339 |
if (async == true) goto badArgForAsyncCall; |
|
1340 |
__argValues[i].pointerVal = (void *)(__integerArrayVal(arg)); |
|
1341 |
} else if (__isFloatArray(arg)) { |
|
1342 |
if (async == true) goto badArgForAsyncCall; |
|
1343 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
|
1344 |
} else if (__isDoubleArray(arg)) { |
|
1345 |
if (async == true) goto badArgForAsyncCall; |
|
1346 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1347 |
} else if (__isStringLike(arg)) { |
|
1348 |
if (async == true) { |
|
9435 | 1349 |
badArgForAsyncCall: ; |
14625 | 1350 |
__FAIL__(@symbol(BadArgForAsyncCall)) |
1351 |
} |
|
1352 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1353 |
} else if (__isBytes(arg) || __isWords(arg) || __isLongs(arg)) { |
|
1354 |
char *p = (char *)(__byteArrayVal(arg)); |
|
1355 |
int nInstBytes; |
|
1356 |
OBJ cls; |
|
9483 | 1357 |
|
14625 | 1358 |
if (async == true) goto badArgForAsyncCall; |
1359 |
cls = __qClass(arg); |
|
1360 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1361 |
__argValues[i].pointerVal = p + nInstBytes; |
|
1362 |
} else { |
|
1363 |
__FAIL__(@symbol(InvalidArgument)) |
|
1364 |
} |
|
1365 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1366 |
|
14625 | 1367 |
} else if (typeSymbol == @symbol(bool)) { |
1368 |
thisType = __get_ffi_type_uint(); |
|
9342 | 1369 |
|
14625 | 1370 |
if (arg == true) { |
1371 |
__argValues[i].iVal = 1; |
|
1372 |
} else if (arg == false) { |
|
1373 |
__argValues[i].iVal = 0; |
|
1374 |
} else if (__isSmallInteger(arg)) { |
|
1375 |
__argValues[i].iVal = __intVal(arg); |
|
1376 |
} else { |
|
1377 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1378 |
if (__argValues[i].iVal == 0) { |
|
1379 |
__FAIL__(@symbol(InvalidArgument)) |
|
1380 |
} |
|
1381 |
} |
|
1382 |
argValuePtr = &(__argValues[i].iVal); |
|
1383 |
} else { |
|
1384 |
if (__isSymbol(typeSymbol) |
|
1385 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
1386 |
if (! __isBehaviorLike(argValueClass)) { |
|
1387 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
1388 |
} |
|
1389 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
1390 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
1391 |
} |
|
1392 |
goto commonPointerTypeArg; /* sorry */ |
|
1393 |
} else { |
|
1394 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
1395 |
} |
|
1396 |
} |
|
9342 | 1397 |
|
14625 | 1398 |
__argTypes[i] = thisType; |
1399 |
__argValuePointers[i] = argValuePtr; |
|
9483 | 1400 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1401 |
# ifdef VERBOSE |
14625 | 1402 |
printf("arg%d: %x\n", i, __argValues[i].iVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1403 |
# endif |
9342 | 1404 |
} |
9465 | 1405 |
failureInfo = nil; |
9342 | 1406 |
|
1407 |
__callType = FFI_DEFAULT_ABI; |
|
1408 |
||
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1409 |
# ifdef CALLTYPE_FFI_STDCALL |
9465 | 1410 |
if (callTypeNumber == @global(CALLTYPE_API)) { |
14625 | 1411 |
__callType = CALLTYPE_FFI_STDCALL; |
9342 | 1412 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1413 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1414 |
# ifdef CALLTYPE_FFI_V8 |
9465 | 1415 |
if (callTypeNumber == @global(CALLTYPE_V8)) { |
14625 | 1416 |
__callType = CALLTYPE_FFI_V8; |
9342 | 1417 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1418 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1419 |
# ifdef CALLTYPE_FFI_V9 |
9465 | 1420 |
if (callTypeNumber == @global(CALLTYPE_V9)) { |
14625 | 1421 |
__callType = CALLTYPE_FFI_V9; |
9342 | 1422 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1423 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1424 |
# ifdef CALLTYPE_FFI_UNIX64 |
9465 | 1425 |
if (callTypeNumber == @global(CALLTYPE_UNIX64)) { |
14625 | 1426 |
__callType = CALLTYPE_FFI_UNIX64; |
9342 | 1427 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1428 |
# endif |
9342 | 1429 |
|
1430 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
14625 | 1431 |
__FAIL__(@symbol(FFIPrepareFailed)) |
9342 | 1432 |
} |
9463 | 1433 |
if (async == true) { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1434 |
# ifdef VERBOSE |
14625 | 1435 |
printf("async call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1436 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1437 |
# ifdef WIN32 |
14625 | 1438 |
__STX_C_CALL4( "ffi_call", ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1439 |
# else |
14625 | 1440 |
__BEGIN_INTERRUPTABLE__ |
1441 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
|
1442 |
__END_INTERRUPTABLE__ |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1443 |
# endif |
9435 | 1444 |
} else { |
14625 | 1445 |
if (unlimitedStack == true) { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1446 |
# ifdef VERBOSE |
14625 | 1447 |
printf("UNLIMITEDSTACKCALL call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1448 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1449 |
# if 0 |
14625 | 1450 |
__UNLIMITEDSTACKCALL__(ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1451 |
# endif |
14625 | 1452 |
} else { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1453 |
# ifdef VERBOSE |
14625 | 1454 |
printf("call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1455 |
# endif |
14625 | 1456 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
1457 |
} |
|
9435 | 1458 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1459 |
# ifdef VERBOSE |
9524 | 1460 |
printf("retval is %d (0x%x)\n", __returnValue.iVal, __returnValue.iVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1461 |
# endif |
11586 | 1462 |
if ((returnTypeSymbol == @symbol(int)) |
1463 |
|| (returnTypeSymbol == @symbol(sint)) |
|
9479 | 1464 |
|| (returnTypeSymbol == @symbol(sint8)) |
1465 |
|| (returnTypeSymbol == @symbol(sint16)) |
|
1466 |
|| (returnTypeSymbol == @symbol(sint32))) { |
|
14625 | 1467 |
RETURN ( __MKINT(__returnValue.iVal) ); |
9342 | 1468 |
} |
1469 |
if ((returnTypeSymbol == @symbol(uint)) |
|
1470 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
1471 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
1472 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
14625 | 1473 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
9342 | 1474 |
} |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
1475 |
if (returnTypeSymbol == @symbol(bool)) { |
14625 | 1476 |
RETURN ( __returnValue.iVal ? true : false ); |
9342 | 1477 |
} |
1478 |
if (returnTypeSymbol == @symbol(float)) { |
|
14625 | 1479 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
9342 | 1480 |
} |
1481 |
if (returnTypeSymbol == @symbol(double)) { |
|
14625 | 1482 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
9342 | 1483 |
} |
1484 |
if (returnTypeSymbol == @symbol(void)) { |
|
14625 | 1485 |
RETURN ( nil ); |
9342 | 1486 |
} |
9463 | 1487 |
if (returnTypeSymbol == @symbol(char)) { |
14625 | 1488 |
RETURN ( __MKCHARACTER(__returnValue.iVal & 0xFF) ); |
9463 | 1489 |
} |
1490 |
if (returnTypeSymbol == @symbol(wchar)) { |
|
14625 | 1491 |
RETURN ( __MKUCHARACTER(__returnValue.iVal & 0xFFFF) ); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1492 |
} |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1493 |
if (returnTypeSymbol == @symbol(sint64)) { |
14625 | 1494 |
RETURN ( __MKINT64(&__returnValue.longLongVal) ); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1495 |
} |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1496 |
if (returnTypeSymbol == @symbol(uint64)) { |
14625 | 1497 |
RETURN ( __MKUINT64(&__returnValue.longLongVal) ); |
9463 | 1498 |
} |
11586 | 1499 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1500 |
# ifdef VERBOSE |
11586 | 1501 |
printf("%x\n", __returnValue.pointerVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1502 |
# endif |
9346 | 1503 |
if (returnTypeSymbol == @symbol(handle)) { |
14625 | 1504 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 1505 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
14625 | 1506 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
10603 | 1507 |
} else if (returnTypeSymbol == @symbol(bytePointer)) { |
14625 | 1508 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
9463 | 1509 |
} else if (returnTypeSymbol == @symbol(charPointer)) { |
14625 | 1510 |
returnValue = __MKSTRING(__returnValue.pointerVal); |
9463 | 1511 |
} else if (returnTypeSymbol == @symbol(wcharPointer)) { |
14625 | 1512 |
returnValue = __MKU16STRING(__returnValue.pointerVal); |
9342 | 1513 |
} else { |
14625 | 1514 |
__FAIL__(@symbol(UnknownReturnType2)) |
9342 | 1515 |
} |
10603 | 1516 |
#else /* no FFI support */ |
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10614
diff
changeset
|
1517 |
failureCode = @symbol(FFINotSupported); |
10603 | 1518 |
#endif /* HAVE_FFI */ |
9342 | 1519 |
getOutOfHere: ; |
1520 |
%}. |
|
1521 |
failureCode notNil ifTrue:[ |
|
14625 | 1522 |
(failureCode == #UnknownReturnType or:[ failureCode == #UnknownArgumentType ]) ifTrue:[ |
1523 |
oldReturnType := returnType. |
|
1524 |
oldArgumentTypes := argumentTypes. |
|
1525 |
self adjustTypes. |
|
1526 |
((oldReturnType ~= returnType) or:[oldArgumentTypes ~= argumentTypes]) ifTrue:[ |
|
1527 |
thisContext restart |
|
1528 |
]. |
|
1529 |
]. |
|
1530 |
(failureCode == #BadArgForAsyncCall) ifTrue:[ |
|
1531 |
^ self tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aCPlusPlusObjectOrNil |
|
1532 |
]. |
|
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
1533 |
|
14625 | 1534 |
self primitiveFailed. "see failureCode and failureInfo for details" |
1535 |
^ nil |
|
9342 | 1536 |
]. |
1537 |
||
9483 | 1538 |
returnType isSymbol ifTrue:[ |
14625 | 1539 |
returnValueClass notNil ifTrue:[ |
1540 |
self isConstReturnValue ifTrue:[ |
|
1541 |
returnValue changeClassTo:returnValueClass. |
|
1542 |
^ returnValue |
|
1543 |
]. |
|
1544 |
^ returnValueClass fromExternalAddress:returnValue. |
|
1545 |
]. |
|
9483 | 1546 |
] ifFalse:[ |
14625 | 1547 |
returnType isCPointer ifTrue:[ |
1548 |
returnType baseType isCStruct ifTrue:[ |
|
1549 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
1550 |
stClass notNil ifTrue:[ |
|
1551 |
self isConstReturnValue ifTrue:[ |
|
1552 |
returnValue changeClassTo:returnValueClass. |
|
1553 |
^ returnValue |
|
1554 |
]. |
|
1555 |
^ stClass fromExternalAddress:returnValue. |
|
1556 |
]. |
|
1557 |
]. |
|
1558 |
returnType baseType isCChar ifTrue:[ |
|
1559 |
^ returnValue stringAt:1 |
|
1560 |
]. |
|
1561 |
]. |
|
9342 | 1562 |
]. |
1563 |
||
1564 |
^ returnValue |
|
9463 | 1565 |
|
1566 |
"Created: / 01-08-2006 / 13:56:23 / cg" |
|
10603 | 1567 |
"Modified: / 11-06-2007 / 01:50:36 / cg" |
14037 | 1568 |
! |
1569 |
||
1570 |
tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aCPlusPlusObjectOrNil |
|
1571 |
"invoked by the call primitive, iff GC-unsave arguments where passed to the call. |
|
1572 |
Here, allocate non-movable blocks of memory and copy the arguments into them, |
|
1573 |
then try the call again, copy changed values back, and release the memeory." |
|
1574 |
||
1575 |
|saveArguments anyBadArg result originalToSaveArgMapping| |
|
1576 |
||
1577 |
argumentsOrNil isNil ifTrue:[ |
|
14625 | 1578 |
^ self primitiveFailed |
14037 | 1579 |
]. |
14506
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1580 |
thisContext isRecursive ifTrue: [^self primitiveFailed]. |
14037 | 1581 |
|
1582 |
anyBadArg := false. |
|
1583 |
originalToSaveArgMapping := IdentityDictionary new. |
|
1584 |
||
14625 | 1585 |
saveArguments := argumentsOrNil |
1586 |
collect:[:eachArg | |
|
1587 |
|saveArg| |
|
14037 | 1588 |
|
14625 | 1589 |
(originalToSaveArgMapping includesKey:eachArg) ifTrue:[ |
1590 |
saveArg := originalToSaveArgMapping at:eachArg |
|
1591 |
] ifFalse:[ |
|
1592 |
eachArg isString ifTrue:[ |
|
1593 |
saveArg := (ExternalBytes fromString:eachArg) register. |
|
1594 |
anyBadArg := true. |
|
1595 |
originalToSaveArgMapping at:eachArg put:saveArg. |
|
1596 |
] ifFalse:[ |
|
1597 |
eachArg isByteCollection ifTrue:[ |
|
1598 |
saveArg := (ExternalBytes from:eachArg) register. |
|
1599 |
originalToSaveArgMapping at:eachArg put:saveArg. |
|
1600 |
anyBadArg := true. |
|
1601 |
] ifFalse:[ |
|
1602 |
saveArg := eachArg |
|
1603 |
] |
|
1604 |
]. |
|
1605 |
]. |
|
1606 |
saveArg |
|
1607 |
]. |
|
14037 | 1608 |
|
1609 |
anyBadArg ifFalse:[ |
|
14625 | 1610 |
"avoid recursion..." |
1611 |
^ self primitiveFailed |
|
14037 | 1612 |
]. |
1613 |
||
1614 |
result := self invokeFFIwithArguments:saveArguments forCPPInstance:aCPlusPlusObjectOrNil. |
|
1615 |
||
1616 |
"/ copy back !! |
|
1617 |
originalToSaveArgMapping keysAndValuesDo:[:arg :saveArg | |
|
14625 | 1618 |
arg isSymbol ifFalse:[ |
1619 |
arg replaceFrom:1 to:(arg size) with:saveArg startingAt:1. |
|
1620 |
]. |
|
1621 |
saveArg free. |
|
14037 | 1622 |
]. |
1623 |
^ result. |
|
14506
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1624 |
|
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1625 |
"Modified (format): / 06-11-2012 / 10:52:41 / anwild" |
9342 | 1626 |
! ! |
1627 |
||
10481 | 1628 |
!ExternalLibraryFunction methodsFor:'testing'! |
1629 |
||
10603 | 1630 |
isExternalLibraryFunction |
1631 |
"return true, if the receiver is some kind of externalLibrary function; |
|
1632 |
true is returned here" |
|
1633 |
||
1634 |
^true |
|
1635 |
||
1636 |
"Created: / 07-06-2007 / 10:36:40 / cg" |
|
10481 | 1637 |
! ! |
1638 |
||
8533 | 1639 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
1640 |
||
13412 | 1641 |
version_CVS |
14625 | 1642 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.91 2013-01-02 12:45:06 cg Exp $' |
12436 | 1643 |
! |
1644 |
||
13412 | 1645 |
version_SVN |
13782
1994fe87f21e
comment/format in: #linkToModule
Claus Gittinger <cg@exept.de>
parents:
13412
diff
changeset
|
1646 |
^ '§ Id: ExternalLibraryFunction.st 10643 2011-06-08 21:53:07Z vranyj1 §' |
8533 | 1647 |
! ! |
9463 | 1648 |
|
1649 |
ExternalLibraryFunction initialize! |