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