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