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