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