author | Claus Gittinger <cg@exept.de> |
Wed, 04 Nov 2009 17:10:06 +0100 | |
changeset 12436 | 92a968c9ca92 |
parent 11586 | a3b2eef8a74c |
child 12449 | ca958524b42f |
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:[ |
12436 | 517 |
self isCPPFunction ifTrue:[ |
518 |
"/ no need to load a dll. |
|
519 |
^ self |
|
520 |
] |
|
10481 | 521 |
]. |
522 |
||
9465 | 523 |
(moduleNameUsed := moduleName) isNil ifTrue:[ |
12436 | 524 |
owningClass isNil ifTrue:[ |
525 |
self error:'Missing moduleName'. |
|
526 |
]. |
|
527 |
moduleNameUsed := owningClass theNonMetaclass libraryName asSymbol. |
|
9321 | 528 |
]. |
11586 | 529 |
moduleHandle isNil ifTrue:[ |
12436 | 530 |
handle := self loadLibrary:moduleNameUsed. |
531 |
handle isNil ifTrue:[ |
|
532 |
self error:('Cannot load module: "%1"' bindWith: 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:[ |
12436 | 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 |
|
12436 | 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 |
12436 | 552 |
|handle nameString filename| |
10279
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. |
12436 | 555 |
nameString := filename name. |
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
556 |
|
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
557 |
filename exists ifTrue:[ |
12436 | 558 |
handle := ObjectFileLoader loadDynamicObject:(filename pathName). |
559 |
handle notNil ifTrue:[^ handle ]. |
|
560 |
]. |
|
561 |
filename isAbsolute ifFalse:[ |
|
562 |
self class dllPath do:[:eachDirectory | |
|
563 |
handle := ObjectFileLoader |
|
564 |
loadDynamicObject:(eachDirectory asFilename construct:nameString) pathName. |
|
565 |
handle notNil ifTrue:[^ handle ]. |
|
566 |
]. |
|
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
567 |
]. |
10270 | 568 |
|
12436 | 569 |
filename suffix isEmpty ifTrue:[ |
570 |
"/ try again with the OS-specific dll-extension |
|
571 |
^ self loadLibrary:(filename withSuffix:ObjectFileLoader sharedLibrarySuffix) |
|
10270 | 572 |
]. |
12436 | 573 |
|
10270 | 574 |
^ nil |
575 |
! |
|
576 |
||
9321 | 577 |
prepareInvoke |
10270 | 578 |
(moduleHandle isNil or:[self hasCode not]) ifTrue:[ |
11586 | 579 |
self linkToModule. |
580 |
self adjustTypes. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
581 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
582 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
583 |
|
8550 | 584 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
585 |
||
9327 | 586 |
ffiTypeSymbolForType:aType |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
587 |
"map type to one of the ffi-supported ones: |
11586 | 588 |
sint8, sint16, sint32, sint64 |
589 |
uint8, uint16, uint32, uint64 |
|
590 |
bool void handle |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
591 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
592 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
593 |
aType == #sint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
594 |
aType == #sint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
595 |
aType == #sint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
596 |
aType == #sint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
597 |
aType == #uint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
598 |
aType == #uint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
599 |
aType == #uint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
600 |
aType == #uint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
601 |
aType == #double ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
602 |
aType == #float ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
603 |
aType == #char ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
604 |
aType == #void ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
605 |
aType == #bool ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
606 |
aType == #pointer ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
607 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
608 |
aType == #int8 ifTrue:[^ #sint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
609 |
aType == #int16 ifTrue:[^ #sint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
610 |
aType == #int32 ifTrue:[^ #sint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
611 |
aType == #int64 ifTrue:[^ #sint64 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
612 |
aType == #voidPointer ifTrue:[^ #pointer ]. |
10625 | 613 |
aType == #uint8Pointer ifTrue:[^ #pointer ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
614 |
|
10213 | 615 |
aType == #short ifTrue:[^ #sint16 ]. |
11586 | 616 |
aType == #long ifTrue:[^ #long ]. |
617 |
aType == #int ifTrue:[^ #int ]. |
|
618 |
aType == #uint ifTrue:[^ #uint ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
619 |
aType == #ushort ifTrue:[^ #uint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
620 |
aType == #unsignedShort ifTrue:[^ #uint16 ]. |
11586 | 621 |
aType == #ulong ifTrue:[^ #ulong ]. |
622 |
aType == #unsignedLong ifTrue:[^ #ulong ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
623 |
aType == #uchar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
624 |
aType == #unsignedChar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
625 |
aType == #byte ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
626 |
aType == #dword ifTrue:[^ #uint32 ]. |
10213 | 627 |
aType == #sdword ifTrue:[^ #sint32 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
628 |
aType == #word ifTrue:[^ #uint16 ]. |
10213 | 629 |
aType == #sword ifTrue:[^ #sint16 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
630 |
aType == #handle ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
631 |
aType == #lpstr ifTrue:[^ #charPointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
632 |
aType == #hresult ifTrue:[^ #uint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
633 |
aType == #boolean ifTrue:[^ #bool ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
634 |
aType == #ulongReturn ifTrue:[^ #uint32 ]. "/ TODO - care for 64bit machines |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
635 |
aType == #none ifTrue:[^ #void ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
636 |
aType == #struct ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
637 |
aType == #structIn ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
638 |
aType == #structOut ifTrue:[^ #pointer ]. |
10981 | 639 |
aType == #unsigned ifTrue:[^ #uint ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
640 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
641 |
(aType isString or:[aType isSymbol]) ifFalse:[ |
11586 | 642 |
CType isNil ifTrue:[ |
643 |
self error:'unknown type'. |
|
644 |
]. |
|
645 |
^ aType typeSymbol. |
|
9483 | 646 |
]. |
647 |
||
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
648 |
^ aType |
10625 | 649 |
|
650 |
"Modified: / 14-06-2007 / 17:21:42 / cg" |
|
9322 | 651 |
! |
652 |
||
9466 | 653 |
name:functionNameOrVirtualIndex module:aModuleName returnType:aReturnType argumentTypes:argTypes |
654 |
name := functionNameOrVirtualIndex. |
|
655 |
functionNameOrVirtualIndex isNumber ifTrue:[ |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
656 |
self beVirtualCPP. |
9466 | 657 |
]. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
658 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
659 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
660 |
argumentTypes := argTypes. |
9465 | 661 |
|
662 |
"Created: / 01-08-2006 / 15:19:52 / cg" |
|
9466 | 663 |
"Modified: / 02-08-2006 / 17:20:13 / cg" |
9465 | 664 |
! |
665 |
||
11426 | 666 |
owningClass |
667 |
^ owningClass |
|
668 |
! |
|
669 |
||
9465 | 670 |
owningClass:aClass |
671 |
owningClass := aClass. |
|
672 |
||
673 |
"Created: / 01-08-2006 / 15:22:50 / cg" |
|
10603 | 674 |
! |
675 |
||
11586 | 676 |
setModuleName:aModuleName |
10603 | 677 |
aModuleName ~= moduleName ifTrue:[ |
11586 | 678 |
self code:nil. |
679 |
moduleHandle := nil. |
|
680 |
moduleName := aModuleName. |
|
10603 | 681 |
]. |
682 |
||
683 |
"Created: / 07-06-2007 / 10:20:17 / cg" |
|
8550 | 684 |
! ! |
685 |
||
9342 | 686 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
687 |
||
9347 | 688 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
9463 | 689 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance |
690 |
||
691 |
"Modified: / 01-08-2006 / 13:55:30 / cg" |
|
9342 | 692 |
! |
693 |
||
9347 | 694 |
invokeFFIWithArguments:arguments |
9463 | 695 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil |
696 |
||
697 |
"Modified: / 01-08-2006 / 13:55:35 / cg" |
|
9347 | 698 |
! |
699 |
||
10440 | 700 |
invokeFFIwithArguments:argumentsOrNil forCPPInstance:aCPlusPlusObjectOrNil |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
701 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo returnValue stClass vtOffset |
9524 | 702 |
virtual async unlimitedStack callTypeNumber returnValueClass argValueClass| |
9342 | 703 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
704 |
argTypeSymbols := argumentTypes. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
705 |
returnTypeSymbol := returnType. |
9346 | 706 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
707 |
virtual := self isVirtualCPP. |
9524 | 708 |
(virtual "or:[self isNonVirtualCPP]") ifTrue:[ |
11586 | 709 |
aCPlusPlusObjectOrNil isNil ifTrue:[ |
710 |
"/ must have a c++ object instance |
|
711 |
self primitiveFailed. |
|
712 |
]. |
|
9463 | 713 |
|
11586 | 714 |
"/ and it must be a kind of ExternalStructure !! |
715 |
(aCPlusPlusObjectOrNil isKindOf:ExternalStructure) ifFalse:[ |
|
716 |
self primitiveFailed. |
|
717 |
]. |
|
718 |
virtual ifTrue:[ |
|
719 |
vtOffset := name. |
|
720 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
721 |
self primitiveFailed. |
|
722 |
] |
|
723 |
]. |
|
9463 | 724 |
] ifFalse:[ |
11586 | 725 |
aCPlusPlusObjectOrNil notNil ifTrue:[ |
726 |
"/ must NOT have a c++ object instance |
|
727 |
self primitiveFailed. |
|
728 |
]. |
|
9342 | 729 |
]. |
9463 | 730 |
async := self isAsync. |
731 |
unlimitedStack := self isUnlimitedStack. |
|
9465 | 732 |
callTypeNumber := self callTypeNumber. |
10512 | 733 |
"/ Transcript show:name; show:' async:'; showCR:async. |
9342 | 734 |
|
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
735 |
%{ /* STACK: 100000 */ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
736 |
#ifdef HAVE_FFI |
9342 | 737 |
ffi_cif __cif; |
738 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
739 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
740 |
ffi_type *__returnType = NULL; |
|
741 |
union u { |
|
11586 | 742 |
int iVal; |
743 |
float fVal; |
|
744 |
double dVal; |
|
745 |
void *pointerVal; |
|
9342 | 746 |
}; |
747 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
748 |
union u *__argValues = __argValuesIncludingThis; |
|
749 |
union u __returnValue; |
|
750 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
751 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
752 |
void *__returnValuePointer; |
|
753 |
int __numArgs, __numArgsIncludingThis; |
|
754 |
static int null = 0; |
|
755 |
int i; |
|
756 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
757 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
|
10440 | 758 |
int __numArgsWanted; |
10603 | 759 |
#define __FAIL__(fcode) \ |
760 |
{ \ |
|
11586 | 761 |
failureCode = fcode; goto getOutOfHere; \ |
10603 | 762 |
} |
9342 | 763 |
|
10440 | 764 |
if (argumentsOrNil == nil) { |
11586 | 765 |
__numArgs = 0; |
10440 | 766 |
} else if (__isArray(argumentsOrNil)) { |
11586 | 767 |
__numArgs = __arraySize(argumentsOrNil); |
9342 | 768 |
} else { |
11586 | 769 |
__FAIL__(@symbol(BadArgumentVector)) |
10440 | 770 |
} |
771 |
if (argTypeSymbols == nil) { |
|
11586 | 772 |
__numArgsWanted = 0; |
10440 | 773 |
} else if (__isArray(argTypeSymbols)) { |
11586 | 774 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10440 | 775 |
} else { |
11586 | 776 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10440 | 777 |
} |
778 |
||
779 |
if (__numArgs != __numArgsWanted) { |
|
11586 | 780 |
__FAIL__(@symbol(ArgumentCountMismatch)) |
9342 | 781 |
} |
782 |
if (__numArgs > MAX_ARGS) { |
|
11586 | 783 |
__FAIL__(@symbol(TooManyArguments)) |
9342 | 784 |
} |
785 |
||
786 |
/* |
|
787 |
* validate the return type |
|
788 |
*/ |
|
789 |
__returnValuePointer = &__returnValue; |
|
9479 | 790 |
|
9483 | 791 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
11586 | 792 |
returnTypeSymbol = @symbol(handle); |
9483 | 793 |
} |
9479 | 794 |
|
9342 | 795 |
if (returnTypeSymbol == @symbol(int)) { |
11586 | 796 |
__returnType = __get_ffi_type_sint(); |
9342 | 797 |
} else if (returnTypeSymbol == @symbol(uint)) { |
11586 | 798 |
__returnType = __get_ffi_type_uint(); |
9479 | 799 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
11586 | 800 |
__returnType = __get_ffi_type_uint8(); |
9479 | 801 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
11586 | 802 |
__returnType = __get_ffi_type_uint16(); |
9479 | 803 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
11586 | 804 |
__returnType = __get_ffi_type_uint32(); |
9479 | 805 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
11586 | 806 |
__returnType = __get_ffi_type_uint64(); |
9479 | 807 |
|
808 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
11586 | 809 |
__returnType = __get_ffi_type_sint(); |
9479 | 810 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
11586 | 811 |
__returnType = __get_ffi_type_sint8(); |
9479 | 812 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
11586 | 813 |
__returnType = __get_ffi_type_sint16(); |
9479 | 814 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
11586 | 815 |
__returnType = __get_ffi_type_sint32(); |
9479 | 816 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
11586 | 817 |
__returnType = __get_ffi_type_sint64(); |
9479 | 818 |
|
9342 | 819 |
} else if (returnTypeSymbol == @symbol(long)) { |
11586 | 820 |
if (sizeof(long) == 4) { |
821 |
returnTypeSymbol = @symbol(sint32); |
|
822 |
__returnType = __get_ffi_type_sint32(); |
|
823 |
} else if (sizeof(long) == 8) { |
|
824 |
returnTypeSymbol = @symbol(sint64); |
|
825 |
__returnType = __get_ffi_type_sint64(); |
|
826 |
} else { |
|
827 |
__FAIL__(@symbol(UnknownReturnType)) |
|
828 |
} |
|
9479 | 829 |
|
9342 | 830 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
11586 | 831 |
if (sizeof(long) == 4) { |
832 |
returnTypeSymbol = @symbol(uint32); |
|
833 |
__returnType = __get_ffi_type_uint32(); |
|
834 |
}else if (sizeof(long) == 8) { |
|
835 |
returnTypeSymbol = @symbol(uint64); |
|
836 |
__returnType = __get_ffi_type_uint64(); |
|
837 |
} else { |
|
838 |
__FAIL__(@symbol(UnknownReturnType)) |
|
839 |
} |
|
9479 | 840 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
841 |
} else if (returnTypeSymbol == @symbol(bool)) { |
11586 | 842 |
__returnType = __get_ffi_type_uint(); |
9479 | 843 |
|
9342 | 844 |
} else if (returnTypeSymbol == @symbol(float)) { |
11586 | 845 |
__returnType = __get_ffi_type_float(); |
9342 | 846 |
} else if (returnTypeSymbol == @symbol(double)) { |
11586 | 847 |
__returnType = __get_ffi_type_double(); |
9479 | 848 |
|
9342 | 849 |
} else if (returnTypeSymbol == @symbol(void)) { |
11586 | 850 |
__returnType = __get_ffi_type_void(); |
851 |
__returnValuePointer = NULL; |
|
852 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
|
853 |
|| (returnTypeSymbol == @symbol(handle)) |
|
854 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
855 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
856 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
857 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
858 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
859 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
860 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
861 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 862 |
} else { |
11586 | 863 |
if (__isSymbol(returnTypeSymbol) |
864 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
865 |
if (! __isBehaviorLike(returnValueClass)) { |
|
866 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
867 |
} |
|
868 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
869 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
870 |
} |
|
871 |
__returnType = __get_ffi_type_pointer(); |
|
872 |
returnTypeSymbol = @symbol(pointer); |
|
873 |
} else { |
|
874 |
__FAIL__(@symbol(UnknownReturnType)) |
|
875 |
} |
|
9342 | 876 |
} |
877 |
||
878 |
/* |
|
879 |
* validate the c++ object |
|
880 |
*/ |
|
881 |
if (aCPlusPlusObjectOrNil != nil) { |
|
11586 | 882 |
struct cPlusPlusInstance { |
883 |
void **vTable; |
|
884 |
}; |
|
885 |
struct cPlusPlusInstance *inst; |
|
9342 | 886 |
|
11586 | 887 |
if (__isExternalAddressLike(aCPlusPlusObjectOrNil)) { |
888 |
inst = (void *)(__externalAddressVal(aCPlusPlusObjectOrNil)); |
|
889 |
} else if (__isExternalBytesLike(aCPlusPlusObjectOrNil)) { |
|
890 |
inst = (void *)(__externalBytesVal(aCPlusPlusObjectOrNil)); |
|
891 |
} else { |
|
892 |
__FAIL__(@symbol(InvalidInstance)) |
|
893 |
} |
|
894 |
__argValues[0].pointerVal = inst; |
|
895 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
896 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
9342 | 897 |
|
11586 | 898 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
899 |
__argTypes = &__argTypesIncludingThis[1]; |
|
900 |
__argValues = &__argValuesIncludingThis[1]; |
|
901 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 902 |
|
11586 | 903 |
if (virtual == true) { |
904 |
if (! __isSmallInteger(vtOffset)) { |
|
905 |
__FAIL__(@symbol(InvalidVTableIndex)) |
|
906 |
} |
|
907 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
9464 | 908 |
#ifdef VERBOSE |
11586 | 909 |
printf("virtual codeAddress: %x\n", codeAddress); |
9464 | 910 |
#endif |
11586 | 911 |
} |
9342 | 912 |
} else { |
11586 | 913 |
__numArgsIncludingThis = __numArgs; |
9524 | 914 |
#ifdef VERBOSE |
11586 | 915 |
printf("codeAddress: %x\n", codeAddress); |
9524 | 916 |
#endif |
9342 | 917 |
} |
918 |
||
919 |
/* |
|
920 |
* validate all arg types and setup arg-buffers |
|
921 |
*/ |
|
922 |
for (i=0; i<__numArgs; i++) { |
|
11586 | 923 |
ffi_type *thisType; |
924 |
void *argValuePtr; |
|
925 |
OBJ typeSymbol; |
|
926 |
OBJ arg; |
|
9342 | 927 |
|
11586 | 928 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
9465 | 929 |
|
11586 | 930 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
931 |
arg = __ArrayInstPtr(argumentsOrNil)->a_element[i]; |
|
9342 | 932 |
|
11586 | 933 |
if (typeSymbol == @symbol(handle)) { |
934 |
typeSymbol = @symbol(pointer); |
|
935 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
936 |
typeSymbol = @symbol(pointer); |
|
937 |
} |
|
9479 | 938 |
|
11586 | 939 |
if (typeSymbol == @symbol(long)) { |
940 |
if (sizeof(long) == sizeof(int)) { |
|
941 |
typeSymbol = @symbol(sint); |
|
942 |
} else { |
|
943 |
if (sizeof(long) == 4) { |
|
944 |
typeSymbol = @symbol(sint32); |
|
945 |
} else if (sizeof(long) == 8) { |
|
946 |
typeSymbol = @symbol(sint64); |
|
947 |
} |
|
948 |
} |
|
949 |
} |
|
950 |
if (typeSymbol == @symbol(ulong)) { |
|
951 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
952 |
typeSymbol = @symbol(uint); |
|
953 |
} else { |
|
954 |
if (sizeof(long) == 4) { |
|
955 |
typeSymbol = @symbol(uint32); |
|
956 |
} else if (sizeof(long) == 8) { |
|
957 |
typeSymbol = @symbol(uint64); |
|
958 |
} |
|
959 |
} |
|
960 |
} |
|
9342 | 961 |
|
11586 | 962 |
if (typeSymbol == @symbol(int) || typeSymbol == @symbol(sint)) { |
963 |
thisType = __get_ffi_type_sint(); |
|
964 |
if (__isSmallInteger(arg)) { |
|
965 |
__argValues[i].iVal = __intVal(arg); |
|
966 |
} else { |
|
967 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
968 |
if (__argValues[i].iVal == 0) { |
|
969 |
__FAIL__(@symbol(InvalidArgument)) |
|
970 |
} |
|
971 |
} |
|
972 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 973 |
|
11586 | 974 |
} else if (typeSymbol == @symbol(uint)) { |
975 |
thisType = __get_ffi_type_uint(); |
|
9342 | 976 |
|
11586 | 977 |
if (__isSmallInteger(arg)) { |
978 |
__argValues[i].iVal = __intVal(arg); |
|
979 |
} else { |
|
980 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
981 |
if (__argValues[i].iVal == 0) { |
|
982 |
__FAIL__(@symbol(InvalidArgument)) |
|
983 |
} |
|
984 |
} |
|
985 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 986 |
|
11586 | 987 |
} else if (typeSymbol == @symbol(uint8)) { |
988 |
thisType = __get_ffi_type_uint8(); |
|
989 |
if (! __isSmallInteger(arg)) { |
|
990 |
__FAIL__(@symbol(InvalidArgument)) |
|
991 |
} |
|
992 |
__argValues[i].iVal = __intVal(arg); |
|
993 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
994 |
__FAIL__(@symbol(InvalidArgument)) |
|
995 |
} |
|
996 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 997 |
|
11586 | 998 |
} else if (typeSymbol == @symbol(sint8)) { |
999 |
thisType = __get_ffi_type_sint8(); |
|
1000 |
if (! __isSmallInteger(arg)) { |
|
1001 |
__FAIL__(@symbol(InvalidArgument)) |
|
1002 |
} |
|
1003 |
__argValues[i].iVal = __intVal(arg); |
|
1004 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
1005 |
__FAIL__(@symbol(InvalidArgument)) |
|
1006 |
} |
|
1007 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1008 |
|
11586 | 1009 |
} else if (typeSymbol == @symbol(uint16)) { |
1010 |
thisType = __get_ffi_type_uint16(); |
|
1011 |
if (! __isSmallInteger(arg)) { |
|
1012 |
__FAIL__(@symbol(InvalidArgument)) |
|
1013 |
} |
|
1014 |
__argValues[i].iVal = __intVal(arg); |
|
1015 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
1016 |
__FAIL__(@symbol(InvalidArgument)) |
|
1017 |
} |
|
1018 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1019 |
|
11586 | 1020 |
} else if (typeSymbol == @symbol(sint16)) { |
1021 |
thisType = __get_ffi_type_sint16(); |
|
1022 |
if (! __isSmallInteger(arg)) { |
|
1023 |
__FAIL__(@symbol(InvalidArgument)) |
|
1024 |
} |
|
1025 |
__argValues[i].iVal = __intVal(arg); |
|
1026 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
1027 |
__FAIL__(@symbol(InvalidArgument)) |
|
1028 |
} |
|
1029 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1030 |
|
11586 | 1031 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
1032 |
thisType = __get_ffi_type_uint32(); |
|
1033 |
if (__isSmallInteger(arg)) { |
|
1034 |
__argValues[i].iVal = __intVal(arg); |
|
1035 |
} else { |
|
1036 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1037 |
if (__argValues[i].iVal == 0) { |
|
1038 |
__FAIL__(@symbol(InvalidArgument)) |
|
1039 |
} |
|
1040 |
} |
|
1041 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1042 |
|
11586 | 1043 |
} else if (typeSymbol == @symbol(float)) { |
1044 |
thisType = __get_ffi_type_float(); |
|
1045 |
if (__isSmallInteger(arg)) { |
|
1046 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
1047 |
} else if (__isFloat(arg)) { |
|
1048 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
1049 |
} else if (__isShortFloat(arg)) { |
|
1050 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
1051 |
} else { |
|
1052 |
__FAIL__(@symbol(InvalidArgument)) |
|
1053 |
} |
|
1054 |
argValuePtr = &(__argValues[i].fVal); |
|
9479 | 1055 |
|
11586 | 1056 |
} else if (typeSymbol == @symbol(double)) { |
1057 |
thisType = __get_ffi_type_double(); |
|
1058 |
if (__isSmallInteger(arg)) { |
|
1059 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
1060 |
} else if (__isFloat(arg)) { |
|
1061 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
1062 |
} else if (__isShortFloat(arg)) { |
|
1063 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
1064 |
} else { |
|
1065 |
__FAIL__(@symbol(InvalidArgument)) |
|
1066 |
} |
|
1067 |
argValuePtr = &(__argValues[i].dVal); |
|
9479 | 1068 |
|
11586 | 1069 |
} else if (typeSymbol == @symbol(void)) { |
1070 |
thisType = __get_ffi_type_void(); |
|
1071 |
argValuePtr = &null; |
|
9479 | 1072 |
|
11586 | 1073 |
} else if (typeSymbol == @symbol(charPointer)) { |
1074 |
thisType = __get_ffi_type_pointer(); |
|
1075 |
if (__isString(arg) || __isSymbol(arg)) { |
|
1076 |
if (async == true) goto badArgForAsyncCall; |
|
1077 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1078 |
} else if (__isBytes(arg)) { |
|
1079 |
if (async == true) goto badArgForAsyncCall; |
|
1080 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1081 |
} else if (__isExternalAddressLike(arg)) { |
|
1082 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1083 |
} else if (__isExternalBytesLike(arg)) { |
|
1084 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1085 |
} else { |
|
1086 |
if (arg == nil) { |
|
1087 |
__argValues[i].pointerVal = (void *)0; |
|
1088 |
} else { |
|
1089 |
__FAIL__(@symbol(InvalidArgument)) |
|
1090 |
} |
|
1091 |
} |
|
1092 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1093 |
|
11586 | 1094 |
} else if (typeSymbol == @symbol(floatPointer)) { |
1095 |
thisType = __get_ffi_type_pointer(); |
|
1096 |
if (__isBytes(arg)) { |
|
1097 |
if (async == true) goto badArgForAsyncCall; |
|
1098 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1099 |
} else if (__isExternalAddressLike(arg)) { |
|
1100 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1101 |
} else if (__isExternalBytesLike(arg)) { |
|
1102 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1103 |
} else if (__isFloats(arg)) { |
|
1104 |
char *p = (char *)(__FloatArrayInstPtr(arg)->f_element); |
|
1105 |
int nInstBytes; |
|
1106 |
OBJ cls; |
|
10603 | 1107 |
|
11586 | 1108 |
if (async == true) goto badArgForAsyncCall; |
1109 |
cls = __qClass(arg); |
|
1110 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1111 |
p = p + nInstBytes; |
|
1112 |
__argValues[i].pointerVal = p; |
|
1113 |
} else { |
|
1114 |
if (arg == nil) { |
|
1115 |
__argValues[i].pointerVal = (void *)0; |
|
1116 |
} else { |
|
1117 |
__FAIL__(@symbol(InvalidArgument)) |
|
1118 |
} |
|
1119 |
} |
|
1120 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1121 |
|
11586 | 1122 |
} else if (typeSymbol == @symbol(doublePointer)) { |
1123 |
thisType = __get_ffi_type_pointer(); |
|
1124 |
if (__isBytes(arg)) { |
|
1125 |
if (async == true) goto badArgForAsyncCall; |
|
1126 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1127 |
} else if (__isExternalAddressLike(arg)) { |
|
1128 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1129 |
} else if (__isExternalBytesLike(arg)) { |
|
1130 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1131 |
} else if (__isDoubles(arg)) { |
|
1132 |
char *p = (char *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1133 |
int nInstBytes; |
|
1134 |
OBJ cls; |
|
10603 | 1135 |
|
11586 | 1136 |
if (async == true) goto badArgForAsyncCall; |
1137 |
cls = __qClass(arg); |
|
1138 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1139 |
p = p + nInstBytes; |
|
10603 | 1140 |
#ifdef __NEED_DOUBLE_ALIGN |
11586 | 1141 |
if ((INT)pFirst & (__DOUBLE_ALIGN-1)) { |
1142 |
int delta = __DOUBLE_ALIGN - ((INT)p & (__DOUBLE_ALIGN-1)); |
|
10603 | 1143 |
|
11586 | 1144 |
p += delta; |
1145 |
} |
|
10603 | 1146 |
#endif |
11586 | 1147 |
__argValues[i].pointerVal = p; |
1148 |
} else { |
|
1149 |
if (arg == nil) { |
|
1150 |
__argValues[i].pointerVal = (void *)0; |
|
1151 |
} else { |
|
1152 |
__FAIL__(@symbol(InvalidArgument)) |
|
1153 |
} |
|
1154 |
} |
|
1155 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1156 |
|
11586 | 1157 |
} else if (typeSymbol == @symbol(pointer)) { |
9524 | 1158 |
commonPointerTypeArg: ; |
11586 | 1159 |
thisType = __get_ffi_type_pointer(); |
1160 |
if (arg == nil) { |
|
1161 |
__argValues[i].pointerVal = NULL; |
|
1162 |
} else if (__isExternalAddressLike(arg)) { |
|
1163 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1164 |
} else if (__isExternalBytesLike(arg)) { |
|
1165 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1166 |
} else if (__isByteArray(arg)) { |
|
1167 |
if (async == true) goto badArgForAsyncCall; |
|
1168 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1169 |
} else if (__isFloatArray(arg)) { |
|
1170 |
if (async == true) goto badArgForAsyncCall; |
|
1171 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
|
1172 |
} else if (__isDoubleArray(arg)) { |
|
1173 |
if (async == true) goto badArgForAsyncCall; |
|
1174 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1175 |
} else if (__isString(arg) || __isSymbol(arg)) { |
|
1176 |
if (async == true) { |
|
9435 | 1177 |
badArgForAsyncCall: ; |
11586 | 1178 |
__FAIL__(@symbol(BadArgForAsyncCall)) |
1179 |
} |
|
1180 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1181 |
} else if (__isBytes(arg) || __isWords(arg) || __isLongs(arg)) { |
|
1182 |
char *p = (char *)(__byteArrayVal(arg)); |
|
1183 |
int nInstBytes; |
|
1184 |
OBJ cls; |
|
9483 | 1185 |
|
11586 | 1186 |
if (async == true) goto badArgForAsyncCall; |
1187 |
cls = __qClass(arg); |
|
1188 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1189 |
__argValues[i].pointerVal = p + nInstBytes; |
|
1190 |
} else { |
|
1191 |
__FAIL__(@symbol(InvalidArgument)) |
|
1192 |
} |
|
1193 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1194 |
|
11586 | 1195 |
} else if (typeSymbol == @symbol(bool)) { |
1196 |
thisType = __get_ffi_type_uint(); |
|
9342 | 1197 |
|
11586 | 1198 |
if (arg == true) { |
1199 |
__argValues[i].iVal = 1; |
|
1200 |
} else if (arg == false) { |
|
1201 |
__argValues[i].iVal = 0; |
|
1202 |
} else if (__isSmallInteger(arg)) { |
|
1203 |
__argValues[i].iVal = __intVal(arg); |
|
1204 |
} else { |
|
1205 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1206 |
if (__argValues[i].iVal == 0) { |
|
1207 |
__FAIL__(@symbol(InvalidArgument)) |
|
1208 |
} |
|
1209 |
} |
|
1210 |
argValuePtr = &(__argValues[i].iVal); |
|
1211 |
} else { |
|
1212 |
if (__isSymbol(typeSymbol) |
|
1213 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
1214 |
if (! __isBehaviorLike(argValueClass)) { |
|
1215 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
1216 |
} |
|
1217 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
1218 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
1219 |
} |
|
1220 |
goto commonPointerTypeArg; /* sorry */ |
|
1221 |
} else { |
|
1222 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
1223 |
} |
|
1224 |
} |
|
9342 | 1225 |
|
11586 | 1226 |
__argTypes[i] = thisType; |
1227 |
__argValuePointers[i] = argValuePtr; |
|
9483 | 1228 |
|
9524 | 1229 |
#ifdef VERBOSE |
11586 | 1230 |
printf("arg%d: %x\n", i, __argValues[i].iVal); |
9524 | 1231 |
#endif |
9342 | 1232 |
} |
9465 | 1233 |
failureInfo = nil; |
9342 | 1234 |
|
1235 |
__callType = FFI_DEFAULT_ABI; |
|
1236 |
||
1237 |
#ifdef CALLTYPE_FFI_STDCALL |
|
9465 | 1238 |
if (callTypeNumber == @global(CALLTYPE_API)) { |
11586 | 1239 |
__callType = CALLTYPE_FFI_STDCALL; |
9342 | 1240 |
} |
1241 |
#endif |
|
1242 |
#ifdef CALLTYPE_FFI_V8 |
|
9465 | 1243 |
if (callTypeNumber == @global(CALLTYPE_V8)) { |
11586 | 1244 |
__callType = CALLTYPE_FFI_V8; |
9342 | 1245 |
} |
1246 |
#endif |
|
1247 |
#ifdef CALLTYPE_FFI_V9 |
|
9465 | 1248 |
if (callTypeNumber == @global(CALLTYPE_V9)) { |
11586 | 1249 |
__callType = CALLTYPE_FFI_V9; |
9342 | 1250 |
} |
1251 |
#endif |
|
1252 |
#ifdef CALLTYPE_FFI_UNIX64 |
|
9465 | 1253 |
if (callTypeNumber == @global(CALLTYPE_UNIX64)) { |
11586 | 1254 |
__callType = CALLTYPE_FFI_UNIX64; |
9342 | 1255 |
} |
1256 |
#endif |
|
1257 |
||
1258 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
11586 | 1259 |
__FAIL__(@symbol(FFIPrepareFailed)) |
9342 | 1260 |
} |
9463 | 1261 |
if (async == true) { |
9524 | 1262 |
#ifdef VERBOSE |
11586 | 1263 |
printf("async call 0x%x\n", codeAddress); |
9524 | 1264 |
#endif |
9436 | 1265 |
#ifdef WIN32 |
11586 | 1266 |
__STX_C_CALL4( "ffi_call", ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9436 | 1267 |
#else |
11586 | 1268 |
__BEGIN_INTERRUPTABLE__ |
1269 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
|
1270 |
__END_INTERRUPTABLE__ |
|
9436 | 1271 |
#endif |
9435 | 1272 |
} else { |
11586 | 1273 |
if (unlimitedStack == true) { |
9524 | 1274 |
#ifdef VERBOSE |
11586 | 1275 |
printf("UNLIMITEDSTACKCALL call 0x%x\n", codeAddress); |
9524 | 1276 |
#endif |
9463 | 1277 |
#if 0 |
11586 | 1278 |
__UNLIMITEDSTACKCALL__(ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9463 | 1279 |
#endif |
11586 | 1280 |
} else { |
9524 | 1281 |
#ifdef VERBOSE |
11586 | 1282 |
printf("call 0x%x\n", codeAddress); |
9524 | 1283 |
#endif |
11586 | 1284 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
1285 |
} |
|
9435 | 1286 |
} |
9524 | 1287 |
#ifdef VERBOSE |
1288 |
printf("retval is %d (0x%x)\n", __returnValue.iVal, __returnValue.iVal); |
|
1289 |
#endif |
|
11586 | 1290 |
if ((returnTypeSymbol == @symbol(int)) |
1291 |
|| (returnTypeSymbol == @symbol(sint)) |
|
9479 | 1292 |
|| (returnTypeSymbol == @symbol(sint8)) |
1293 |
|| (returnTypeSymbol == @symbol(sint16)) |
|
1294 |
|| (returnTypeSymbol == @symbol(sint32))) { |
|
11586 | 1295 |
RETURN ( __MKINT(__returnValue.iVal) ); |
9342 | 1296 |
} |
1297 |
if ((returnTypeSymbol == @symbol(uint)) |
|
1298 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
1299 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
1300 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
11586 | 1301 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
9342 | 1302 |
} |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
1303 |
if (returnTypeSymbol == @symbol(bool)) { |
11586 | 1304 |
RETURN ( __returnValue.iVal ? true : false ); |
9342 | 1305 |
} |
1306 |
if (returnTypeSymbol == @symbol(float)) { |
|
11586 | 1307 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
9342 | 1308 |
} |
1309 |
if (returnTypeSymbol == @symbol(double)) { |
|
11586 | 1310 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
9342 | 1311 |
} |
1312 |
if (returnTypeSymbol == @symbol(void)) { |
|
11586 | 1313 |
RETURN ( nil ); |
9342 | 1314 |
} |
9463 | 1315 |
if (returnTypeSymbol == @symbol(char)) { |
11586 | 1316 |
RETURN ( __MKCHARACTER(__returnValue.iVal & 0xFF) ); |
9463 | 1317 |
} |
1318 |
if (returnTypeSymbol == @symbol(wchar)) { |
|
11586 | 1319 |
RETURN ( __MKUCHARACTER(__returnValue.iVal & 0xFFFF) ); |
9463 | 1320 |
} |
11586 | 1321 |
|
1322 |
#ifdef VERBOSE |
|
1323 |
printf("%x\n", __returnValue.pointerVal); |
|
1324 |
#endif |
|
9346 | 1325 |
if (returnTypeSymbol == @symbol(handle)) { |
11586 | 1326 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 1327 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
11586 | 1328 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
10603 | 1329 |
} else if (returnTypeSymbol == @symbol(bytePointer)) { |
11586 | 1330 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
9463 | 1331 |
} else if (returnTypeSymbol == @symbol(charPointer)) { |
11586 | 1332 |
returnValue = __MKSTRING(__returnValue.pointerVal); |
9463 | 1333 |
} else if (returnTypeSymbol == @symbol(wcharPointer)) { |
11586 | 1334 |
returnValue = __MKU16STRING(__returnValue.pointerVal); |
9342 | 1335 |
} else { |
11586 | 1336 |
__FAIL__(@symbol(UnknownReturnType2)) |
9342 | 1337 |
} |
10603 | 1338 |
#else /* no FFI support */ |
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10614
diff
changeset
|
1339 |
failureCode = @symbol(FFINotSupported); |
10603 | 1340 |
#endif /* HAVE_FFI */ |
9342 | 1341 |
getOutOfHere: ; |
1342 |
%}. |
|
1343 |
failureCode notNil ifTrue:[ |
|
11586 | 1344 |
self primitiveFailed. "see failureCode and failureInfo for details" |
1345 |
^ nil |
|
9342 | 1346 |
]. |
1347 |
||
9483 | 1348 |
returnType isSymbol ifTrue:[ |
11586 | 1349 |
returnValueClass notNil ifTrue:[ |
1350 |
self isConstReturnValue ifTrue:[ |
|
1351 |
returnValue changeClassTo:returnValueClass. |
|
1352 |
^ returnValue |
|
1353 |
]. |
|
1354 |
^ returnValueClass fromExternalAddress:returnValue. |
|
1355 |
]. |
|
9483 | 1356 |
] ifFalse:[ |
11586 | 1357 |
returnType isCPointer ifTrue:[ |
1358 |
returnType baseType isCStruct ifTrue:[ |
|
1359 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
1360 |
stClass notNil ifTrue:[ |
|
1361 |
self isConstReturnValue ifTrue:[ |
|
1362 |
returnValue changeClassTo:returnValueClass. |
|
1363 |
^ returnValue |
|
1364 |
]. |
|
1365 |
^ stClass fromExternalAddress:returnValue. |
|
1366 |
]. |
|
1367 |
]. |
|
1368 |
returnType baseType isCChar ifTrue:[ |
|
1369 |
^ returnValue stringAt:1 |
|
1370 |
]. |
|
1371 |
]. |
|
9342 | 1372 |
]. |
1373 |
||
1374 |
^ returnValue |
|
9463 | 1375 |
|
1376 |
"Created: / 01-08-2006 / 13:56:23 / cg" |
|
10603 | 1377 |
"Modified: / 11-06-2007 / 01:50:36 / cg" |
9342 | 1378 |
! ! |
1379 |
||
10481 | 1380 |
!ExternalLibraryFunction methodsFor:'testing'! |
1381 |
||
10603 | 1382 |
isExternalLibraryFunction |
1383 |
"return true, if the receiver is some kind of externalLibrary function; |
|
1384 |
true is returned here" |
|
1385 |
||
1386 |
^true |
|
1387 |
||
1388 |
"Created: / 07-06-2007 / 10:36:40 / cg" |
|
10481 | 1389 |
! ! |
1390 |
||
8533 | 1391 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
1392 |
||
1393 |
version |
|
12436 | 1394 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.67 2009-11-04 16:10:06 cg Exp $' |
1395 |
! |
|
1396 |
||
1397 |
version_CVS |
|
1398 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.67 2009-11-04 16:10:06 cg Exp $' |
|
8533 | 1399 |
! ! |
9463 | 1400 |
|
1401 |
ExternalLibraryFunction initialize! |