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