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