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