author | Claus Gittinger <cg@exept.de> |
Thu, 10 Mar 2016 02:01:13 +0100 | |
changeset 19311 | 6f1d1ea6dba8 |
parent 18240 | 28af09029a8b |
child 19313 | 4e56f471a6c3 |
child 19331 | 59f77658de07 |
permissions | -rw-r--r-- |
19311 | 1 |
"{ Encoding: utf8 }" |
2 |
||
8728 | 3 |
" |
4 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9464 | 5 |
All Rights Reserved |
8728 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
8533 | 14 |
"{ Package: 'stx:libbasic' }" |
15 |
||
19311 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
8533 | 18 |
ExternalFunction subclass:#ExternalLibraryFunction |
9465 | 19 |
instanceVariableNames:'flags moduleName returnType argumentTypes owningClass' |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
20 |
classVariableNames:'DLLPATH FLAG_VIRTUAL FLAG_NONVIRTUAL FLAG_OBJECTIVEC FLAG_ASYNC |
10270 | 21 |
FLAG_UNLIMITEDSTACK FLAG_RETVAL_IS_CONST CALLTYPE_MASK |
22 |
CALLTYPE_API CALLTYPE_C CALLTYPE_OLE CALLTYPE_V8 CALLTYPE_V9 |
|
14103 | 23 |
CALLTYPE_UNIX64 DllMapping' |
9464 | 24 |
poolDictionaries:'' |
25 |
category:'System-Support' |
|
8533 | 26 |
! |
27 |
||
9322 | 28 |
!ExternalLibraryFunction primitiveDefinitions! |
29 |
%{ |
|
30 |
||
9337 | 31 |
#ifdef HAVE_FFI |
32 |
# include <ffi.h> |
|
9322 | 33 |
# define MAX_ARGS 128 |
9365 | 34 |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
35 |
# ifdef USE_STANDARD_FFI |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
36 |
# define __get_ffi_type_sint() &ffi_type_sint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
37 |
# define __get_ffi_type_sint8() &ffi_type_sint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
38 |
# define __get_ffi_type_sint16() &ffi_type_sint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
39 |
# define __get_ffi_type_sint32() &ffi_type_sint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
40 |
# define __get_ffi_type_sint64() &ffi_type_sint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
41 |
# define __get_ffi_type_uint() &ffi_type_uint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
42 |
# define __get_ffi_type_uint8() &ffi_type_uint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
43 |
# define __get_ffi_type_uint16() &ffi_type_uint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
44 |
# define __get_ffi_type_uint32() &ffi_type_uint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
45 |
# define __get_ffi_type_uint64() &ffi_type_uint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
46 |
# define __get_ffi_type_float() &ffi_type_float |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
47 |
# define __get_ffi_type_double() &ffi_type_double |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
48 |
# define __get_ffi_type_void() &ffi_type_void |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
49 |
# define __get_ffi_type_pointer() &ffi_type_pointer |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
50 |
# else |
9365 | 51 |
extern ffi_type *__get_ffi_type_sint(); |
52 |
extern ffi_type *__get_ffi_type_sint8(); |
|
53 |
extern ffi_type *__get_ffi_type_sint16(); |
|
54 |
extern ffi_type *__get_ffi_type_sint32(); |
|
55 |
extern ffi_type *__get_ffi_type_sint64(); |
|
56 |
extern ffi_type *__get_ffi_type_uint(); |
|
57 |
extern ffi_type *__get_ffi_type_uint8(); |
|
58 |
extern ffi_type *__get_ffi_type_uint16(); |
|
59 |
extern ffi_type *__get_ffi_type_uint32(); |
|
60 |
extern ffi_type *__get_ffi_type_uint64(); |
|
61 |
extern ffi_type *__get_ffi_type_float(); |
|
62 |
extern ffi_type *__get_ffi_type_double(); |
|
63 |
extern ffi_type *__get_ffi_type_void(); |
|
64 |
extern ffi_type *__get_ffi_type_pointer(); |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
65 |
# endif |
9365 | 66 |
|
9337 | 67 |
#endif |
9322 | 68 |
|
69 |
%} |
|
70 |
! ! |
|
71 |
||
8728 | 72 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
73 |
||
74 |
copyright |
|
75 |
" |
|
76 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9464 | 77 |
All Rights Reserved |
8728 | 78 |
|
79 |
This software is furnished under a license and may be used |
|
80 |
only in accordance with the terms of that license and with the |
|
81 |
inclusion of the above copyright notice. This software may not |
|
82 |
be provided or otherwise made available to, or used by, any |
|
83 |
other person. No title to or ownership of the software is |
|
84 |
hereby transferred. |
|
85 |
" |
|
9322 | 86 |
! |
87 |
||
9331 | 88 |
documentation |
89 |
" |
|
90 |
instances of me are used to interface to external library functions (as found in a dll/shared object). |
|
91 |
||
12680 | 92 |
Inside a method, when a special external-call pragma such as: |
14625 | 93 |
<api: bool MessageBeep(uint)> |
12680 | 94 |
|
95 |
is encountered by the parser, the compiler generates a call via |
|
14625 | 96 |
<correspondingExternalLibraryFunctionObject> invokeWithArguments: argumentArray. |
9331 | 97 |
|
98 |
In the invoke method, the library is checked to be loaded (and loaded if not already), |
|
9336 | 99 |
the arguments are converted to C and pushed onto the C-stack, the function is called, |
9331 | 100 |
and finally, the return value is converted back from C to a smalltalk object. |
12680 | 101 |
|
102 |
The parser supports the call-syntax of various other smalltalk dialects: |
|
14625 | 103 |
Squeak / ST-X: |
104 |
<cdecl: [async] [virtual|nonVirtual][const] returnType functionNameStringOrIndex ( argType1..argTypeN ) module: moduleName > |
|
105 |
<apicall: [async] [virtual|nonVirtual][const] returnType functionNameStringOrIndex ( argType1..argTypeN ) module: moduleName > |
|
12680 | 106 |
|
14625 | 107 |
Dolphin: |
108 |
<stdcall: [virtual|nonVirtual][const] returnType functionNameStringOrIndex argType1..argTypeN> |
|
109 |
<cdecl: [virtual|nonVirtual][const] returnType functionNameStringOrIndex argType1..argTypeN> |
|
12680 | 110 |
|
14625 | 111 |
ST/V: |
112 |
<api: functionName argType1 .. argTypeN returnType> |
|
113 |
<ccall: functionName argType1 .. argTypeN returnType> |
|
114 |
<ole: vFunctionIndex argType1 .. argTypeN returnType> |
|
12680 | 115 |
|
14625 | 116 |
VisualWorks: |
117 |
<c: ...> |
|
118 |
<c: #define NAME value> |
|
9331 | 119 |
" |
120 |
! |
|
121 |
||
9322 | 122 |
example |
123 |
" |
|
14625 | 124 |
[exBegin] |
125 |
|f| |
|
14037 | 126 |
|
14625 | 127 |
f := ExternalLibraryFunction new. |
128 |
f beCallTypeWINAPI. |
|
14037 | 129 |
|
14625 | 130 |
f name:'MessageBeep' |
131 |
module:'user32.dll' |
|
132 |
returnType:#boolean |
|
133 |
argumentTypes:#(uint). |
|
14037 | 134 |
|
14625 | 135 |
f invokeWith:1. |
136 |
[exEnd] |
|
14037 | 137 |
|
138 |
Synchronous vs. Asynchronous calls: |
|
139 |
||
14516
359e4d2234af
class: ExternalLibraryFunction
Stefan Vogel <sv@exept.de>
parents:
14506
diff
changeset
|
140 |
by default, foreign function calls are synchronous, effectively blocking the whole ST/X system |
19311 | 141 |
(that is by purpose,´because most C-code is not prepared for being interrupted, and also, normal |
14037 | 142 |
code is not prepared for a garbage collector to move objects around, while another C thread might |
143 |
access the data...). |
|
14625 | 144 |
Therefore, the following will block all ST/X activity for 10 seconds |
14037 | 145 |
(try interacting with the launcher while the Sleep is performing): |
146 |
||
14625 | 147 |
[exBegin] |
148 |
|f| |
|
9322 | 149 |
|
14625 | 150 |
f := ExternalLibraryFunction new. |
151 |
f beCallTypeWINAPI. |
|
14037 | 152 |
|
14625 | 153 |
f name:'Sleep' |
154 |
module:'kernel32.dll' |
|
155 |
returnType:#void |
|
156 |
argumentTypes:#(uint). |
|
14037 | 157 |
|
14625 | 158 |
f invokeWith:10000. |
159 |
[exEnd] |
|
9322 | 160 |
|
14037 | 161 |
if you know what you do and you do not pass any possibly moving objects (such as strings) as argument, |
162 |
the call can be made asynchronous. In that case, ONLY the calling thread will be blocked; all other smalltalk |
|
163 |
threads wil continue to execute. |
|
164 |
(try interacting now with the launcher while the Sleep is performing): |
|
14625 | 165 |
[exBegin] |
166 |
|f| |
|
14037 | 167 |
|
14625 | 168 |
f := ExternalLibraryFunction new. |
169 |
f beCallTypeWINAPI. |
|
170 |
f beAsync. |
|
14037 | 171 |
|
14625 | 172 |
f name:'Sleep' |
173 |
module:'kernel32.dll' |
|
174 |
returnType:#void |
|
175 |
argumentTypes:#(uint). |
|
14037 | 176 |
|
14625 | 177 |
f invokeWith:10000. |
178 |
[exEnd] |
|
14037 | 179 |
|
9322 | 180 |
" |
8728 | 181 |
! ! |
8533 | 182 |
|
8550 | 183 |
!ExternalLibraryFunction class methodsFor:'instance creation'! |
184 |
||
9465 | 185 |
name:functionName module:moduleName returnType:returnType argumentTypes:argTypes |
8550 | 186 |
^ self new |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
187 |
name:functionName module:moduleName |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
188 |
returnType:returnType argumentTypes:argTypes |
9465 | 189 |
|
190 |
"Created: / 01-08-2006 / 15:19:08 / cg" |
|
9463 | 191 |
! ! |
192 |
||
193 |
!ExternalLibraryFunction class methodsFor:'class initialization'! |
|
194 |
||
11050 | 195 |
addToDllPath:aDirectoryPathName |
196 |
"can be used during initialization, to add more places for dll-loading" |
|
197 |
||
11053 | 198 |
|oldPath| |
199 |
||
200 |
oldPath := self dllPath. |
|
201 |
(oldPath includes:aDirectoryPathName) ifFalse:[ |
|
11586 | 202 |
self dllPath:(oldPath asOrderedCollection copyWith:aDirectoryPathName) |
11053 | 203 |
] |
11050 | 204 |
! |
205 |
||
14103 | 206 |
dllMapping |
207 |
"allows for dll's to be replaced, |
|
208 |
for example, if you want to use the mozilla sqlite dll |
|
14625 | 209 |
C:\Program Files\Mozilla Firefox\mozsqlite3.dll |
14103 | 210 |
for the sqlite3, execute: |
14625 | 211 |
ExternalLibraryFunction |
212 |
dllMapping at:'sqlite3' |
|
213 |
put: 'C:\Program Files\Mozilla Firefox\mozsqlite3.dll' |
|
14103 | 214 |
" |
215 |
||
216 |
DllMapping isNil ifTrue:[ |
|
14625 | 217 |
DllMapping := Dictionary new. |
14103 | 218 |
]. |
219 |
^ DllMapping |
|
220 |
||
221 |
"Created: / 10-04-2012 / 12:21:45 / cg" |
|
222 |
! |
|
223 |
||
10270 | 224 |
dllPath |
13337 | 225 |
^ DLLPATH |
10270 | 226 |
! |
227 |
||
228 |
dllPath:aCollectionOfDirectoryPathNames |
|
229 |
DLLPATH := aCollectionOfDirectoryPathNames |
|
230 |
! |
|
231 |
||
9463 | 232 |
initialize |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
233 |
"using inline access to corresponding c--defines to avoid duplicate places of knowledge" |
13337 | 234 |
|
235 |
DLLPATH isNil ifTrue:[ |
|
14625 | 236 |
DLLPATH := #('.'). |
18228 | 237 |
%{ |
18240
28af09029a8b
ifdef for SCHTEAM engine changed (not relevant for ST/X)
Claus Gittinger <cg@exept.de>
parents:
18228
diff
changeset
|
238 |
#ifndef __SCHTEAM__ |
18228 | 239 |
@global(FLAG_VIRTUAL) = __MKSMALLINT(__EXTL_FLAG_VIRTUAL); // a virtual c++ call |
240 |
@global(FLAG_NONVIRTUAL) = __MKSMALLINT(__EXTL_FLAG_NONVIRTUAL); // a non-virtual c++ call |
|
241 |
@global(FLAG_OBJECTIVEC) = __MKSMALLINT(__EXTL_FLAG_OBJECTIVEC); // an objectiveC message send |
|
242 |
@global(FLAG_UNLIMITEDSTACK) = __MKSMALLINT(__EXTL_FLAG_UNLIMITEDSTACK); // unlimitedstack under unix |
|
243 |
@global(FLAG_ASYNC) = __MKSMALLINT(__EXTL_FLAG_ASYNC); // async under win32 |
|
244 |
@global(FLAG_RETVAL_IS_CONST) = __MKSMALLINT(__EXTL_FLAG_RETVAL_IS_CONST); // return value is not to be registered for finalization |
|
9463 | 245 |
|
18228 | 246 |
@global(CALLTYPE_API) = __MKSMALLINT(__EXTL_CALLTYPE_API); // WINAPI-call (win32 only) |
247 |
@global(CALLTYPE_C) = __MKSMALLINT(__EXTL_CALLTYPE_C); // regular C-call (the default) |
|
248 |
@global(CALLTYPE_V8) = __MKSMALLINT(__EXTL_CALLTYPE_V8); // v8 call (sparc only) |
|
249 |
@global(CALLTYPE_V9) = __MKSMALLINT(__EXTL_CALLTYPE_V9); // v9 call (sparc only) |
|
250 |
@global(CALLTYPE_UNIX64) = __MKSMALLINT(__EXTL_CALLTYPE_UNIX64); // unix64 call (alpha only) |
|
9463 | 251 |
|
18228 | 252 |
@global(CALLTYPE_MASK) = __MKSMALLINT(__EXTL_CALLTYPE_MASK); |
253 |
#endif |
|
254 |
%} |
|
13337 | 255 |
]. |
9465 | 256 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
257 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
258 |
self initialize |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
259 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
260 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
261 |
"Modified: / 03-10-2006 / 21:27:47 / cg" |
11053 | 262 |
! |
263 |
||
264 |
removeFromDllPath:aDirectoryPathName |
|
265 |
"remove added places from dll-loading" |
|
266 |
||
267 |
|oldPath| |
|
268 |
||
269 |
oldPath := self dllPath. |
|
270 |
self dllPath:(oldPath asOrderedCollection copyWithout:aDirectoryPathName) |
|
271 |
||
272 |
" |
|
273 |
self dllPath. |
|
11586 | 274 |
self addToDllPath:'C:\aaa\bbb'. |
11053 | 275 |
self dllPath. |
276 |
self removeFromDllPath:'C:\aaa\bbb'. |
|
277 |
self dllPath. |
|
278 |
" |
|
8550 | 279 |
! ! |
280 |
||
9321 | 281 |
!ExternalLibraryFunction class methodsFor:'constants'! |
282 |
||
283 |
callTypeAPI |
|
9463 | 284 |
^ CALLTYPE_API |
9321 | 285 |
|
9463 | 286 |
"Modified: / 01-08-2006 / 13:44:41 / cg" |
9435 | 287 |
! |
288 |
||
9321 | 289 |
callTypeC |
9463 | 290 |
^ CALLTYPE_C |
9321 | 291 |
|
9463 | 292 |
"Modified: / 01-08-2006 / 13:44:49 / cg" |
9435 | 293 |
! |
294 |
||
9321 | 295 |
callTypeCDecl |
9463 | 296 |
^ CALLTYPE_C |
9321 | 297 |
|
9463 | 298 |
"Modified: / 01-08-2006 / 13:44:52 / cg" |
9435 | 299 |
! |
300 |
||
10619 | 301 |
callTypeMASK |
302 |
^ CALLTYPE_MASK |
|
303 |
||
304 |
"Modified: / 01-08-2006 / 13:44:57 / cg" |
|
305 |
! |
|
306 |
||
9321 | 307 |
callTypeOLE |
9463 | 308 |
^ CALLTYPE_OLE |
9435 | 309 |
|
9463 | 310 |
"Modified: / 01-08-2006 / 13:44:57 / cg" |
9321 | 311 |
! ! |
312 |
||
313 |
!ExternalLibraryFunction methodsFor:'accessing'! |
|
314 |
||
315 |
argumentTypes |
|
316 |
^ argumentTypes |
|
9463 | 317 |
! |
318 |
||
11426 | 319 |
argumentTypesString |
11586 | 320 |
^ String |
321 |
streamContents:[:s | |
|
322 |
argumentTypes do:[:eachArgType | |
|
323 |
eachArgType printOn:s. |
|
324 |
] separatedBy:[ |
|
325 |
s nextPutAll:','. |
|
326 |
]. |
|
327 |
]. |
|
11426 | 328 |
! |
329 |
||
9463 | 330 |
beAsync |
331 |
"let this execute in a separate thread, in par with the other execution thread(s). |
|
332 |
Ignored under unix/linux (until those support multiple threads too)." |
|
333 |
||
334 |
flags := (flags ? 0) bitOr: FLAG_ASYNC. |
|
335 |
||
336 |
"Created: / 01-08-2006 / 13:42:38 / cg" |
|
337 |
! |
|
338 |
||
9465 | 339 |
beCallTypeAPI |
340 |
flags := (flags ? 0) bitOr: CALLTYPE_API. |
|
341 |
||
342 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
343 |
! |
|
344 |
||
345 |
beCallTypeC |
|
346 |
flags := (flags ? 0) bitOr: CALLTYPE_C. |
|
347 |
||
348 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
349 |
! |
|
350 |
||
10481 | 351 |
beCallTypeOLE |
352 |
flags := (flags ? 0) bitOr: FLAG_VIRTUAL. |
|
353 |
||
354 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
355 |
! |
|
356 |
||
9465 | 357 |
beCallTypeUNIX64 |
358 |
flags := (flags ? 0) bitOr: CALLTYPE_UNIX64. |
|
359 |
||
360 |
"Created: / 01-08-2006 / 15:13:38 / cg" |
|
361 |
! |
|
362 |
||
363 |
beCallTypeV8 |
|
364 |
flags := (flags ? 0) bitOr: CALLTYPE_V8. |
|
365 |
||
366 |
"Created: / 01-08-2006 / 15:13:28 / cg" |
|
367 |
! |
|
368 |
||
369 |
beCallTypeV9 |
|
370 |
flags := (flags ? 0) bitOr: CALLTYPE_V9. |
|
371 |
||
372 |
"Created: / 01-08-2006 / 15:13:31 / cg" |
|
373 |
! |
|
374 |
||
375 |
beCallTypeWINAPI |
|
376 |
self beCallTypeAPI |
|
377 |
||
378 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
379 |
! |
|
380 |
||
9525 | 381 |
beConstReturnValue |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
382 |
"specify that a pointer return value is not to be finalized |
9525 | 383 |
(i.e. points to static data or data which is freed by c)" |
384 |
||
385 |
flags := (flags ? 0) bitOr: FLAG_RETVAL_IS_CONST. |
|
386 |
||
387 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
388 |
! |
|
389 |
||
9463 | 390 |
beNonVirtualCPP |
391 |
"specify this as a non-virtual c++-function" |
|
392 |
||
393 |
flags := (flags ? 0) bitOr: FLAG_NONVIRTUAL. |
|
394 |
||
395 |
"Created: / 01-08-2006 / 13:56:44 / cg" |
|
396 |
! |
|
397 |
||
19311 | 398 |
beObjectiveC |
399 |
"specify this as an objective-c message send" |
|
400 |
||
401 |
flags := (flags ? 0) bitOr: FLAG_OBJECTIVEC. |
|
402 |
||
403 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
404 |
! |
|
405 |
||
9463 | 406 |
beUnlimitedStack |
407 |
"let this execute on the c-stack (as opposed to the thread-stack) |
|
408 |
for unlimited auto-sized-stack under unix/linux. |
|
409 |
Ignored under windows." |
|
410 |
||
411 |
flags := (flags ? 0) bitOr: FLAG_UNLIMITEDSTACK. |
|
412 |
||
413 |
"Created: / 01-08-2006 / 13:41:54 / cg" |
|
414 |
! |
|
415 |
||
416 |
beVirtualCPP |
|
417 |
"specify this as a virtual c++-function" |
|
418 |
||
419 |
flags := (flags ? 0) bitOr: FLAG_VIRTUAL. |
|
420 |
||
421 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
422 |
! |
|
423 |
||
9465 | 424 |
callTypeNumber |
425 |
^ (flags ? 0) bitAnd: CALLTYPE_MASK. |
|
426 |
||
427 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
428 |
! |
|
429 |
||
9463 | 430 |
isAsync |
431 |
"is this executed in a separate thread, in par with the other execution thread(s) ?" |
|
432 |
||
433 |
^ (flags ? 0) bitTest: FLAG_ASYNC. |
|
434 |
||
435 |
"Created: / 01-08-2006 / 13:46:53 / cg" |
|
436 |
! |
|
437 |
||
9519 | 438 |
isCPPFunction |
439 |
"is this a virtual or non-virtual c++-function ?" |
|
440 |
||
441 |
^ (flags ? 0) bitTest: (FLAG_VIRTUAL bitOr: FLAG_NONVIRTUAL). |
|
442 |
||
443 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
444 |
! |
|
445 |
||
9465 | 446 |
isCallTypeAPI |
447 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_API. |
|
448 |
||
449 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
450 |
! |
|
451 |
||
452 |
isCallTypeC |
|
453 |
^ ((flags ? 0) bitAnd: CALLTYPE_MASK) == CALLTYPE_C. |
|
454 |
||
455 |
"Created: / 01-08-2006 / 15:21:23 / cg" |
|
456 |
! |
|
457 |
||
10483 | 458 |
isCallTypeOLE |
459 |
^ ((flags ? 0) bitTest: FLAG_VIRTUAL). |
|
460 |
||
461 |
"Created: / 01-08-2006 / 15:21:23 / cg" |
|
462 |
! |
|
463 |
||
9525 | 464 |
isConstReturnValue |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
465 |
"is the pointer return value not to be finalized |
9525 | 466 |
(i.e. points to static data or data which is freed by c)" |
467 |
||
468 |
^ (flags ? 0) bitTest: FLAG_RETVAL_IS_CONST. |
|
469 |
||
470 |
"Created: / 01-08-2006 / 13:56:48 / cg" |
|
471 |
! |
|
472 |
||
9463 | 473 |
isNonVirtualCPP |
474 |
"is this a non-virtual c++-function ?" |
|
475 |
||
476 |
^ (flags ? 0) bitTest: FLAG_NONVIRTUAL. |
|
477 |
||
478 |
"Created: / 01-08-2006 / 13:56:51 / cg" |
|
479 |
! |
|
480 |
||
19311 | 481 |
isObjectiveC |
482 |
"is this an objective-C message?" |
|
483 |
||
484 |
^ (flags ? 0) bitTest: FLAG_OBJECTIVEC. |
|
485 |
! |
|
486 |
||
9463 | 487 |
isUnlimitedStack |
488 |
"will this execute on the c-stack (as opposed to the thread-stack) |
|
489 |
for unlimited auto-sized-stack under unix/linux. |
|
490 |
Ignored under windows." |
|
491 |
||
492 |
^ (flags ? 0) bitTest: FLAG_UNLIMITEDSTACK. |
|
493 |
||
494 |
"Created: / 01-08-2006 / 14:17:07 / cg" |
|
495 |
! |
|
496 |
||
497 |
isVirtualCPP |
|
498 |
"is this a virtual c++-function ?" |
|
499 |
||
500 |
^ (flags ? 0) bitTest: FLAG_VIRTUAL. |
|
501 |
||
502 |
"Created: / 01-08-2006 / 13:56:54 / cg" |
|
10483 | 503 |
! |
504 |
||
11426 | 505 |
moduleName |
506 |
^ moduleName |
|
507 |
! |
|
508 |
||
10484 | 509 |
returnType |
510 |
^ returnType |
|
511 |
! |
|
512 |
||
10483 | 513 |
vtableIndex |
514 |
name isNumber ifFalse:[^ nil]. |
|
515 |
^ name. |
|
9321 | 516 |
! ! |
517 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
518 |
!ExternalLibraryFunction methodsFor:'invoking'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
519 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
520 |
invoke |
9321 | 521 |
self hasCode ifFalse:[ |
19311 | 522 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
523 |
]. |
19311 | 524 |
^ self invokeFFIwithArguments:nil forCPPInstance:nil |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
525 |
! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
526 |
|
9347 | 527 |
invokeCPPVirtualOn:anInstance |
9341 | 528 |
self hasCode ifFalse:[ |
11586 | 529 |
self prepareInvoke. |
9341 | 530 |
]. |
10440 | 531 |
^ self invokeCPPVirtualFFIOn:anInstance withArguments:nil |
9341 | 532 |
! |
533 |
||
9347 | 534 |
invokeCPPVirtualOn:instance with:arg |
9341 | 535 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
536 |
self prepareInvoke. |
9341 | 537 |
]. |
9347 | 538 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg) |
9341 | 539 |
! |
540 |
||
9347 | 541 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 |
9341 | 542 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
543 |
self prepareInvoke. |
9341 | 544 |
]. |
9347 | 545 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2) |
9341 | 546 |
! |
547 |
||
9347 | 548 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 |
9341 | 549 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
550 |
self prepareInvoke. |
9341 | 551 |
]. |
9347 | 552 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3) |
9341 | 553 |
! |
554 |
||
9347 | 555 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 with:arg4 |
9341 | 556 |
self hasCode ifFalse:[ |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
557 |
self prepareInvoke. |
9341 | 558 |
]. |
9347 | 559 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
9341 | 560 |
! |
561 |
||
10481 | 562 |
invokeCPPVirtualOn:instance withArguments:args |
563 |
self hasCode ifFalse:[ |
|
11586 | 564 |
self prepareInvoke. |
10481 | 565 |
]. |
566 |
^ self invokeCPPVirtualFFIOn:instance withArguments:args |
|
567 |
! |
|
568 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
569 |
invokeWith:arg |
9321 | 570 |
self hasCode ifFalse:[ |
19311 | 571 |
self prepareInvoke. |
9321 | 572 |
]. |
19311 | 573 |
^ self invokeFFIwithArguments:(Array with:arg) forCPPInstance:nil |
9321 | 574 |
! |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
575 |
|
9321 | 576 |
invokeWith:arg1 with:arg2 |
577 |
self hasCode ifFalse:[ |
|
19311 | 578 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
579 |
]. |
19311 | 580 |
^ self invokeFFIwithArguments:(Array with:arg1 with:arg2) forCPPInstance:nil |
9321 | 581 |
! |
582 |
||
583 |
invokeWith:arg1 with:arg2 with:arg3 |
|
584 |
self hasCode ifFalse:[ |
|
19311 | 585 |
self prepareInvoke. |
9321 | 586 |
]. |
19311 | 587 |
^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3) forCPPInstance:nil |
9321 | 588 |
! |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
589 |
|
9321 | 590 |
invokeWith:arg1 with:arg2 with:arg3 with:arg4 |
591 |
self hasCode ifFalse:[ |
|
19311 | 592 |
self prepareInvoke. |
9321 | 593 |
]. |
19311 | 594 |
^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) forCPPInstance:nil |
9321 | 595 |
! |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
596 |
|
9321 | 597 |
invokeWithArguments:argArray |
598 |
self hasCode ifFalse:[ |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
599 |
self prepareInvoke. |
9321 | 600 |
]. |
9465 | 601 |
^ self invokeFFIwithArguments:argArray forCPPInstance:nil |
602 |
||
603 |
"Modified: / 01-08-2006 / 16:04:08 / cg" |
|
9321 | 604 |
! ! |
605 |
||
606 |
!ExternalLibraryFunction methodsFor:'printing'! |
|
607 |
||
608 |
printOn:aStream |
|
609 |
aStream nextPutAll:'<'. |
|
9465 | 610 |
self isCallTypeAPI ifTrue:[ |
14625 | 611 |
'API:' printOn:aStream. |
9465 | 612 |
] ifFalse:[ |
14625 | 613 |
self isCallTypeOLE ifTrue:[ |
614 |
'OLE:' printOn:aStream. |
|
615 |
] ifFalse:[ |
|
616 |
self isCallTypeC ifTrue:[ |
|
617 |
'C:' printOn:aStream. |
|
618 |
] ifFalse:[ |
|
619 |
self error. |
|
620 |
]. |
|
621 |
]. |
|
9465 | 622 |
]. |
9321 | 623 |
aStream nextPutAll:' '. |
624 |
name printOn:aStream. |
|
9465 | 625 |
moduleName notNil ifTrue:[ |
14625 | 626 |
aStream nextPutAll:' module:'. |
627 |
moduleName printOn:aStream. |
|
9465 | 628 |
]. |
9321 | 629 |
aStream nextPutAll:'>'. |
9465 | 630 |
|
14356 | 631 |
"Modified: / 25-09-2012 / 12:06:14 / cg" |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
632 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
633 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
634 |
!ExternalLibraryFunction methodsFor:'private'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
635 |
|
10213 | 636 |
adjustTypes |
637 |
argumentTypes notNil ifTrue:[ |
|
638 |
argumentTypes := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType]. |
|
639 |
]. |
|
640 |
returnType := self ffiTypeSymbolForType:returnType. |
|
641 |
! |
|
642 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
643 |
linkToModule |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
644 |
"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
|
645 |
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
|
646 |
|
9465 | 647 |
|handle moduleNameUsed functionName| |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
648 |
|
10481 | 649 |
name isNumber ifTrue:[ |
14625 | 650 |
self isCPPFunction ifTrue:[ |
651 |
"/ no need to load a dll. |
|
652 |
^ self |
|
653 |
] |
|
10481 | 654 |
]. |
655 |
||
13782
1994fe87f21e
comment/format in: #linkToModule
Claus Gittinger <cg@exept.de>
parents:
13412
diff
changeset
|
656 |
"/ in some other smalltalks, there is no moduleName in the ffi-spec; |
1994fe87f21e
comment/format in: #linkToModule
Claus Gittinger <cg@exept.de>
parents:
13412
diff
changeset
|
657 |
"/ instead, the class provides the libraryName... |
9465 | 658 |
(moduleNameUsed := moduleName) isNil ifTrue:[ |
14625 | 659 |
owningClass isNil ifTrue:[ |
660 |
self error:'Missing moduleName'. |
|
661 |
]. |
|
662 |
moduleNameUsed := owningClass theNonMetaclass libraryName asSymbol. |
|
9321 | 663 |
]. |
11586 | 664 |
moduleHandle isNil ifTrue:[ |
14625 | 665 |
handle := self loadLibrary:moduleNameUsed. |
666 |
handle isNil ifTrue:[ |
|
667 |
self error:('Cannot find or load dll/module: "%1"' bindWith: moduleNameUsed). |
|
668 |
]. |
|
669 |
moduleHandle := handle. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
670 |
]. |
9341 | 671 |
name isNumber ifFalse:[ |
14625 | 672 |
functionName := name. |
673 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
|
674 |
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
|
675 |
|
14625 | 676 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
677 |
moduleHandle := nil. |
|
678 |
self error:'Missing function: ', name, ' in module: ', moduleNameUsed. |
|
679 |
]. |
|
680 |
]. |
|
9321 | 681 |
]. |
9392 | 682 |
|
14103 | 683 |
"Modified: / 10-04-2012 / 12:12:44 / cg" |
9321 | 684 |
! |
685 |
||
10270 | 686 |
loadLibrary:dllName |
12436 | 687 |
|handle nameString filename| |
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
688 |
|
14103 | 689 |
filename := dllName. |
690 |
DllMapping notNil ifTrue:[ |
|
14625 | 691 |
filename := DllMapping at:filename ifAbsent:[ filename ] |
14103 | 692 |
]. |
693 |
||
694 |
filename := filename asFilename. |
|
12436 | 695 |
nameString := filename name. |
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
696 |
|
12937 | 697 |
"try to load, maybe the system knows where to find the dll" |
698 |
handle := ObjectFileLoader loadDynamicObject:filename. |
|
699 |
handle notNil ifTrue:[^ handle ]. |
|
700 |
||
12436 | 701 |
filename isAbsolute ifFalse:[ |
14625 | 702 |
"First ask the class defining the ExternalFunction for the location of the dlls ..." |
703 |
owningClass notNil ifTrue:[ |
|
704 |
owningClass dllPath do:[:eachDirectory | |
|
705 |
handle := ObjectFileLoader |
|
706 |
loadDynamicObject:(eachDirectory asFilename construct:nameString) pathName. |
|
707 |
handle notNil ifTrue:[^ handle ]. |
|
708 |
]. |
|
709 |
]. |
|
710 |
".. then ask the system" |
|
711 |
self class dllPath do:[:eachDirectory | |
|
712 |
handle := ObjectFileLoader |
|
713 |
loadDynamicObject:(eachDirectory asFilename construct:nameString) pathName. |
|
714 |
handle notNil ifTrue:[^ handle ]. |
|
715 |
]. |
|
10279
60c42983fdd4
first test whether the dllName is the complete filename
ca
parents:
10270
diff
changeset
|
716 |
]. |
10270 | 717 |
|
12436 | 718 |
filename suffix isEmpty ifTrue:[ |
14625 | 719 |
"/ try again with the OS-specific dll-extension |
720 |
^ self loadLibrary:(filename withSuffix:ObjectFileLoader sharedLibrarySuffix) |
|
10270 | 721 |
]. |
12436 | 722 |
|
10270 | 723 |
^ nil |
14103 | 724 |
|
725 |
"Modified: / 10-04-2012 / 12:21:06 / cg" |
|
10270 | 726 |
! |
727 |
||
9321 | 728 |
prepareInvoke |
10270 | 729 |
(moduleHandle isNil or:[self hasCode not]) ifTrue:[ |
12450 | 730 |
self linkToModule. |
731 |
self adjustTypes. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
732 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
733 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
734 |
|
8550 | 735 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
736 |
||
9327 | 737 |
ffiTypeSymbolForType:aType |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
738 |
"map type to one of the ffi-supported ones: |
19311 | 739 |
sint8, sint16, sint32, sint64 |
740 |
uint8, uint16, uint32, uint64 |
|
741 |
bool void pointer handle |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
742 |
" |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
743 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
744 |
aType == #sint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
745 |
aType == #sint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
746 |
aType == #sint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
747 |
aType == #sint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
748 |
aType == #uint8 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
749 |
aType == #uint16 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
750 |
aType == #uint32 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
751 |
aType == #uint64 ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
752 |
aType == #double ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
753 |
aType == #float ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
754 |
aType == #char ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
755 |
aType == #void ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
756 |
aType == #bool ifTrue:[^ aType ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
757 |
aType == #pointer ifTrue:[^ aType ]. |
12657
7da296a8eeb6
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12656
diff
changeset
|
758 |
aType == #charPointer ifTrue:[^ aType ]. |
7da296a8eeb6
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12656
diff
changeset
|
759 |
aType == #wcharPointer ifTrue:[^ aType ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
760 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
761 |
aType == #int8 ifTrue:[^ #sint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
762 |
aType == #int16 ifTrue:[^ #sint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
763 |
aType == #int32 ifTrue:[^ #sint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
764 |
aType == #int64 ifTrue:[^ #sint64 ]. |
12450 | 765 |
|
766 |
aType == #voidPointer ifTrue:[^ #pointer ]. |
|
767 |
aType == #uint8Pointer ifTrue:[^ #pointer ]. |
|
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
768 |
aType == #voidPointerPointer ifTrue:[^ #pointer ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
769 |
|
10213 | 770 |
aType == #short ifTrue:[^ #sint16 ]. |
11586 | 771 |
aType == #long ifTrue:[^ #long ]. |
772 |
aType == #int ifTrue:[^ #int ]. |
|
773 |
aType == #uint ifTrue:[^ #uint ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
774 |
aType == #ushort ifTrue:[^ #uint16 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
775 |
aType == #unsignedShort ifTrue:[^ #uint16 ]. |
11586 | 776 |
aType == #ulong ifTrue:[^ #ulong ]. |
777 |
aType == #unsignedLong ifTrue:[^ #ulong ]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
778 |
aType == #uchar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
779 |
aType == #unsignedChar ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
780 |
aType == #byte ifTrue:[^ #uint8 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
781 |
aType == #dword ifTrue:[^ #uint32 ]. |
10213 | 782 |
aType == #sdword ifTrue:[^ #sint32 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
783 |
aType == #word ifTrue:[^ #uint16 ]. |
10213 | 784 |
aType == #sword ifTrue:[^ #sint16 ]. |
12503
cb1a875c3d63
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12474
diff
changeset
|
785 |
aType == #longlong ifTrue:[^ #sint64 ]. |
12450 | 786 |
aType == #longLong ifTrue:[^ #sint64 ]. |
12503
cb1a875c3d63
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12474
diff
changeset
|
787 |
aType == #ulonglong ifTrue:[^ #uint64 ]. |
12450 | 788 |
aType == #ulongLong ifTrue:[^ #uint64 ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
789 |
aType == #handle ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
790 |
aType == #lpstr ifTrue:[^ #charPointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
791 |
aType == #hresult ifTrue:[^ #uint32 ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
792 |
aType == #boolean ifTrue:[^ #bool ]. |
19311 | 793 |
"/ care for 64bit machines |
794 |
ExternalAddress pointerSize == 8 ifTrue:[ |
|
795 |
aType == #ulongReturn ifTrue:[^ #uint64 ]. |
|
796 |
] ifFalse:[ |
|
797 |
aType == #ulongReturn ifTrue:[^ #uint32 ]. |
|
798 |
]. |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
799 |
aType == #none ifTrue:[^ #void ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
800 |
aType == #struct ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
801 |
aType == #structIn ifTrue:[^ #pointer ]. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
802 |
aType == #structOut ifTrue:[^ #pointer ]. |
10981 | 803 |
aType == #unsigned ifTrue:[^ #uint ]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
804 |
|
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
805 |
(aType isString or:[aType isSymbol]) ifFalse:[ |
19311 | 806 |
CType isNil ifTrue:[ |
807 |
self error:'unknown type'. |
|
808 |
]. |
|
809 |
^ aType typeSymbol. |
|
9483 | 810 |
]. |
811 |
||
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
812 |
(aType endsWith:'*') ifTrue:[ |
19311 | 813 |
^ #pointer. |
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
814 |
]. |
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
815 |
(aType endsWith:'Pointer') ifTrue:[ |
19311 | 816 |
^ #pointer. |
12449
ca958524b42f
comment/format in: #prepareInvoke
Claus Gittinger <cg@exept.de>
parents:
12436
diff
changeset
|
817 |
]. |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
818 |
^ aType |
10625 | 819 |
|
820 |
"Modified: / 14-06-2007 / 17:21:42 / cg" |
|
9322 | 821 |
! |
822 |
||
9466 | 823 |
name:functionNameOrVirtualIndex module:aModuleName returnType:aReturnType argumentTypes:argTypes |
824 |
name := functionNameOrVirtualIndex. |
|
825 |
functionNameOrVirtualIndex isNumber ifTrue:[ |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
826 |
self beVirtualCPP. |
9466 | 827 |
]. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
828 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
829 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
830 |
argumentTypes := argTypes. |
9465 | 831 |
|
832 |
"Created: / 01-08-2006 / 15:19:52 / cg" |
|
9466 | 833 |
"Modified: / 02-08-2006 / 17:20:13 / cg" |
9465 | 834 |
! |
835 |
||
11426 | 836 |
owningClass |
837 |
^ owningClass |
|
838 |
! |
|
839 |
||
9465 | 840 |
owningClass:aClass |
841 |
owningClass := aClass. |
|
842 |
||
843 |
"Created: / 01-08-2006 / 15:22:50 / cg" |
|
10603 | 844 |
! |
845 |
||
11586 | 846 |
setModuleName:aModuleName |
10603 | 847 |
aModuleName ~= moduleName ifTrue:[ |
11586 | 848 |
self code:nil. |
849 |
moduleHandle := nil. |
|
850 |
moduleName := aModuleName. |
|
10603 | 851 |
]. |
852 |
||
853 |
"Created: / 07-06-2007 / 10:20:17 / cg" |
|
8550 | 854 |
! ! |
855 |
||
9342 | 856 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
857 |
||
9347 | 858 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
9463 | 859 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance |
860 |
||
861 |
"Modified: / 01-08-2006 / 13:55:30 / cg" |
|
9342 | 862 |
! |
863 |
||
9347 | 864 |
invokeFFIWithArguments:arguments |
9463 | 865 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil |
866 |
||
867 |
"Modified: / 01-08-2006 / 13:55:35 / cg" |
|
9347 | 868 |
! |
869 |
||
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
870 |
invokeFFIwithArguments:argumentsOrNil forCPPInstance:aReceiverOrNil |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
871 |
"basic invoke mechanism. Calls the function represented by the receiver with argumentsOrNil. |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
872 |
For cplusplus, aReceiverOrNil is required to be an externalStructure like object; |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
873 |
for objectiveC, it must be an ObjectiveC object" |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
874 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
875 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo returnValue stClass vtOffset |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
876 |
virtual objectiveC async unlimitedStack callTypeNumber returnValueClass argValueClass |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
877 |
oldReturnType oldArgumentTypes| |
9342 | 878 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
879 |
argTypeSymbols := argumentTypes. |
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
880 |
returnTypeSymbol := returnType. |
9346 | 881 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
882 |
virtual := self isVirtualCPP. |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
883 |
objectiveC := self isObjectiveC. |
9524 | 884 |
(virtual "or:[self isNonVirtualCPP]") ifTrue:[ |
19311 | 885 |
aReceiverOrNil isNil ifTrue:[ |
886 |
"/ must have a c++ object instance |
|
887 |
self primitiveFailed. |
|
888 |
]. |
|
9463 | 889 |
|
19311 | 890 |
"/ and it must be a kind of ExternalStructure !! |
891 |
(aReceiverOrNil isKindOf:ExternalStructure) ifFalse:[ |
|
892 |
self primitiveFailed. |
|
893 |
]. |
|
894 |
virtual ifTrue:[ |
|
895 |
vtOffset := name. |
|
896 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
897 |
self primitiveFailed. |
|
898 |
] |
|
899 |
]. |
|
9463 | 900 |
] ifFalse:[ |
19311 | 901 |
objectiveC ifTrue:[ |
902 |
aReceiverOrNil isNil ifTrue:[ |
|
903 |
"/ must have an objective-c object instance |
|
904 |
self primitiveFailed. |
|
905 |
]. |
|
906 |
(aReceiverOrNil isObjectiveCObject) ifFalse:[ |
|
907 |
self primitiveFailed |
|
908 |
] |
|
909 |
] ifFalse:[ |
|
910 |
aReceiverOrNil notNil ifTrue:[ |
|
911 |
"/ must NOT have a c++/objectiveC object instance |
|
912 |
self primitiveFailed. |
|
913 |
] |
|
914 |
]. |
|
9342 | 915 |
]. |
9463 | 916 |
async := self isAsync. |
917 |
unlimitedStack := self isUnlimitedStack. |
|
9465 | 918 |
callTypeNumber := self callTypeNumber. |
10512 | 919 |
"/ Transcript show:name; show:' async:'; showCR:async. |
9342 | 920 |
|
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
921 |
%{ /* STACK: 100000 */ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
922 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
923 |
#ifdef HAVE_FFI |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
924 |
# ifdef __GNUC__ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
925 |
# ifndef HAS_LONGLONG |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
926 |
# define HAS_LONGLONG |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
927 |
# endif |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
928 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
929 |
# if defined(__BORLANDC__) || defined(__VISUALC__) |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
930 |
# define HAS_INT64 |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
931 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
932 |
|
9342 | 933 |
ffi_cif __cif; |
934 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
935 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
936 |
ffi_type *__returnType = NULL; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
937 |
|
9342 | 938 |
union u { |
19311 | 939 |
INT iVal; |
940 |
float fVal; |
|
941 |
double dVal; |
|
942 |
void *pointerVal; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
943 |
# if defined(HAS_LONGLONG) |
19311 | 944 |
long long longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
945 |
# else |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
946 |
# ifdef HAS_INT64 |
19311 | 947 |
__int64__ longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
948 |
# else |
19311 | 949 |
struct ll { long low; long hi; } longLongVal; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
950 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
951 |
# endif |
9342 | 952 |
}; |
953 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
954 |
union u *__argValues = __argValuesIncludingThis; |
|
955 |
union u __returnValue; |
|
956 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
957 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
958 |
void *__returnValuePointer; |
|
959 |
int __numArgs, __numArgsIncludingThis; |
|
19311 | 960 |
static INT null = 0; |
9342 | 961 |
int i; |
962 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
963 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
|
10440 | 964 |
int __numArgsWanted; |
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
965 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
966 |
# define __FAIL__(fcode) \ |
10603 | 967 |
{ \ |
19311 | 968 |
failureCode = fcode; goto getOutOfHere; \ |
10603 | 969 |
} |
9342 | 970 |
|
10440 | 971 |
if (argumentsOrNil == nil) { |
19311 | 972 |
__numArgs = 0; |
10440 | 973 |
} else if (__isArray(argumentsOrNil)) { |
19311 | 974 |
__numArgs = __arraySize(argumentsOrNil); |
9342 | 975 |
} else { |
19311 | 976 |
__FAIL__(@symbol(BadArgumentVector)) |
10440 | 977 |
} |
978 |
if (argTypeSymbols == nil) { |
|
19311 | 979 |
__numArgsWanted = 0; |
10440 | 980 |
} else if (__isArray(argTypeSymbols)) { |
19311 | 981 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10440 | 982 |
} else { |
19311 | 983 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10440 | 984 |
} |
985 |
||
986 |
if (__numArgs != __numArgsWanted) { |
|
19311 | 987 |
__FAIL__(@symbol(ArgumentCountMismatch)) |
9342 | 988 |
} |
989 |
if (__numArgs > MAX_ARGS) { |
|
19311 | 990 |
__FAIL__(@symbol(TooManyArguments)) |
9342 | 991 |
} |
992 |
||
993 |
/* |
|
994 |
* validate the return type |
|
995 |
*/ |
|
996 |
__returnValuePointer = &__returnValue; |
|
9479 | 997 |
|
9483 | 998 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
19311 | 999 |
returnTypeSymbol = @symbol(handle); |
9483 | 1000 |
} |
9479 | 1001 |
|
9342 | 1002 |
if (returnTypeSymbol == @symbol(int)) { |
19311 | 1003 |
__returnType = __get_ffi_type_sint(); |
9342 | 1004 |
} else if (returnTypeSymbol == @symbol(uint)) { |
19311 | 1005 |
__returnType = __get_ffi_type_uint(); |
9479 | 1006 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
19311 | 1007 |
__returnType = __get_ffi_type_uint8(); |
9479 | 1008 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
19311 | 1009 |
__returnType = __get_ffi_type_uint16(); |
9479 | 1010 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
19311 | 1011 |
__returnType = __get_ffi_type_uint32(); |
9479 | 1012 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
19311 | 1013 |
__returnType = __get_ffi_type_uint64(); |
9479 | 1014 |
|
1015 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
19311 | 1016 |
__returnType = __get_ffi_type_sint(); |
9479 | 1017 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
19311 | 1018 |
__returnType = __get_ffi_type_sint8(); |
9479 | 1019 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
19311 | 1020 |
__returnType = __get_ffi_type_sint16(); |
9479 | 1021 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
19311 | 1022 |
__returnType = __get_ffi_type_sint32(); |
9479 | 1023 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
19311 | 1024 |
__returnType = __get_ffi_type_sint64(); |
9479 | 1025 |
|
9342 | 1026 |
} else if (returnTypeSymbol == @symbol(long)) { |
19311 | 1027 |
if (sizeof(long) == 4) { |
1028 |
returnTypeSymbol = @symbol(sint32); |
|
1029 |
__returnType = __get_ffi_type_sint32(); |
|
1030 |
} else if (sizeof(long) == 8) { |
|
1031 |
returnTypeSymbol = @symbol(sint64); |
|
1032 |
__returnType = __get_ffi_type_sint64(); |
|
1033 |
} else { |
|
1034 |
__FAIL__(@symbol(UnknownReturnType)) |
|
1035 |
} |
|
9479 | 1036 |
|
9342 | 1037 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
19311 | 1038 |
if (sizeof(long) == 4) { |
1039 |
returnTypeSymbol = @symbol(uint32); |
|
1040 |
__returnType = __get_ffi_type_uint32(); |
|
1041 |
}else if (sizeof(long) == 8) { |
|
1042 |
returnTypeSymbol = @symbol(uint64); |
|
1043 |
__returnType = __get_ffi_type_uint64(); |
|
1044 |
} else { |
|
1045 |
__FAIL__(@symbol(UnknownReturnType)) |
|
1046 |
} |
|
9479 | 1047 |
|
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
1048 |
} else if (returnTypeSymbol == @symbol(bool)) { |
19311 | 1049 |
__returnType = __get_ffi_type_uint(); |
9479 | 1050 |
|
9342 | 1051 |
} else if (returnTypeSymbol == @symbol(float)) { |
19311 | 1052 |
__returnType = __get_ffi_type_float(); |
9342 | 1053 |
} else if (returnTypeSymbol == @symbol(double)) { |
19311 | 1054 |
__returnType = __get_ffi_type_double(); |
9479 | 1055 |
|
9342 | 1056 |
} else if (returnTypeSymbol == @symbol(void)) { |
19311 | 1057 |
__returnType = __get_ffi_type_void(); |
1058 |
__returnValuePointer = NULL; |
|
11586 | 1059 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
19311 | 1060 |
|| (returnTypeSymbol == @symbol(handle)) |
1061 |
|| (returnTypeSymbol == @symbol(charPointer)) |
|
1062 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
1063 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
1064 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
1065 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
1066 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
1067 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
1068 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 1069 |
} else { |
19311 | 1070 |
if (__isSymbol(returnTypeSymbol) |
1071 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
1072 |
if (! __isBehaviorLike(returnValueClass)) { |
|
1073 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
1074 |
} |
|
1075 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
1076 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
1077 |
} |
|
1078 |
__returnType = __get_ffi_type_pointer(); |
|
1079 |
returnTypeSymbol = @symbol(pointer); |
|
1080 |
} else { |
|
1081 |
__FAIL__(@symbol(UnknownReturnType)) |
|
1082 |
} |
|
9342 | 1083 |
} |
1084 |
||
1085 |
/* |
|
1086 |
* validate the c++ object |
|
1087 |
*/ |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
1088 |
if (aReceiverOrNil != nil) { |
19311 | 1089 |
struct cPlusPlusInstance { |
1090 |
void **vTable; |
|
1091 |
}; |
|
1092 |
struct cPlusPlusInstance *inst; |
|
9342 | 1093 |
|
19311 | 1094 |
if (__isExternalAddressLike(aReceiverOrNil)) { |
1095 |
inst = (void *)(__externalAddressVal(aReceiverOrNil)); |
|
1096 |
} else if (__isExternalBytesLike(aReceiverOrNil)) { |
|
1097 |
inst = (void *)(__externalBytesVal(aReceiverOrNil)); |
|
1098 |
} else { |
|
1099 |
__FAIL__(@symbol(InvalidInstance)) |
|
1100 |
} |
|
1101 |
__argValues[0].pointerVal = inst; |
|
1102 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
1103 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
9342 | 1104 |
|
19311 | 1105 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
1106 |
__argTypes = &__argTypesIncludingThis[1]; |
|
1107 |
__argValues = &__argValuesIncludingThis[1]; |
|
1108 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 1109 |
|
19311 | 1110 |
if (virtual == true) { |
1111 |
if (! __isSmallInteger(vtOffset)) { |
|
1112 |
__FAIL__(@symbol(InvalidVTableIndex)) |
|
1113 |
} |
|
1114 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1115 |
# ifdef VERBOSE |
19311 | 1116 |
printf("virtual codeAddress: %x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1117 |
# endif |
19311 | 1118 |
} |
9342 | 1119 |
} else { |
19311 | 1120 |
__numArgsIncludingThis = __numArgs; |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1121 |
# ifdef VERBOSE |
19311 | 1122 |
printf("codeAddress: %x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1123 |
# endif |
9342 | 1124 |
} |
1125 |
||
1126 |
/* |
|
1127 |
* validate all arg types and setup arg-buffers |
|
1128 |
*/ |
|
1129 |
for (i=0; i<__numArgs; i++) { |
|
19311 | 1130 |
ffi_type *thisType; |
1131 |
void *argValuePtr; |
|
1132 |
OBJ typeSymbol; |
|
1133 |
OBJ arg; |
|
9342 | 1134 |
|
19311 | 1135 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
9465 | 1136 |
|
19311 | 1137 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
1138 |
arg = __ArrayInstPtr(argumentsOrNil)->a_element[i]; |
|
9342 | 1139 |
|
19311 | 1140 |
if (typeSymbol == @symbol(handle)) { |
1141 |
typeSymbol = @symbol(pointer); |
|
1142 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
1143 |
typeSymbol = @symbol(pointer); |
|
1144 |
} |
|
9479 | 1145 |
|
19311 | 1146 |
if (typeSymbol == @symbol(long)) { |
1147 |
if (sizeof(long) == sizeof(int)) { |
|
1148 |
typeSymbol = @symbol(sint); |
|
1149 |
} else { |
|
1150 |
if (sizeof(long) == 4) { |
|
1151 |
typeSymbol = @symbol(sint32); |
|
1152 |
} else if (sizeof(long) == 8) { |
|
1153 |
typeSymbol = @symbol(sint64); |
|
1154 |
} |
|
1155 |
} |
|
1156 |
} |
|
1157 |
if (typeSymbol == @symbol(ulong)) { |
|
1158 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
1159 |
typeSymbol = @symbol(uint); |
|
1160 |
} else { |
|
1161 |
if (sizeof(long) == 4) { |
|
1162 |
typeSymbol = @symbol(uint32); |
|
1163 |
} else if (sizeof(long) == 8) { |
|
1164 |
typeSymbol = @symbol(uint64); |
|
1165 |
} |
|
1166 |
} |
|
1167 |
} |
|
9342 | 1168 |
|
19311 | 1169 |
if (typeSymbol == @symbol(int) || typeSymbol == @symbol(sint)) { |
1170 |
thisType = __get_ffi_type_sint(); |
|
1171 |
if (__isSmallInteger(arg)) { |
|
1172 |
__argValues[i].iVal = __intVal(arg); |
|
1173 |
} else { |
|
1174 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
1175 |
if (__argValues[i].iVal == 0) { |
|
1176 |
__FAIL__(@symbol(InvalidArgument)) |
|
1177 |
} |
|
1178 |
} |
|
1179 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1180 |
|
19311 | 1181 |
} else if (typeSymbol == @symbol(uint)) { |
1182 |
thisType = __get_ffi_type_uint(); |
|
9342 | 1183 |
|
19311 | 1184 |
if (__isSmallInteger(arg)) { |
1185 |
__argValues[i].iVal = __intVal(arg); |
|
1186 |
} else { |
|
1187 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1188 |
if (__argValues[i].iVal == 0) { |
|
1189 |
__FAIL__(@symbol(InvalidArgument)) |
|
1190 |
} |
|
1191 |
} |
|
1192 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1193 |
|
19311 | 1194 |
} else if (typeSymbol == @symbol(uint8)) { |
1195 |
thisType = __get_ffi_type_uint8(); |
|
1196 |
if (! __isSmallInteger(arg)) { |
|
1197 |
__FAIL__(@symbol(InvalidArgument)) |
|
1198 |
} |
|
1199 |
__argValues[i].iVal = __intVal(arg); |
|
1200 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
1201 |
__FAIL__(@symbol(InvalidArgument)) |
|
1202 |
} |
|
1203 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1204 |
|
19311 | 1205 |
} else if (typeSymbol == @symbol(sint8)) { |
1206 |
thisType = __get_ffi_type_sint8(); |
|
1207 |
if (! __isSmallInteger(arg)) { |
|
1208 |
__FAIL__(@symbol(InvalidArgument)) |
|
1209 |
} |
|
1210 |
__argValues[i].iVal = __intVal(arg); |
|
1211 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
1212 |
__FAIL__(@symbol(InvalidArgument)) |
|
1213 |
} |
|
1214 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1215 |
|
19311 | 1216 |
} else if (typeSymbol == @symbol(uint16)) { |
1217 |
thisType = __get_ffi_type_uint16(); |
|
1218 |
if (! __isSmallInteger(arg)) { |
|
1219 |
__FAIL__(@symbol(InvalidArgument)) |
|
1220 |
} |
|
1221 |
__argValues[i].iVal = __intVal(arg); |
|
1222 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
1223 |
__FAIL__(@symbol(InvalidArgument)) |
|
1224 |
} |
|
1225 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1226 |
|
19311 | 1227 |
} else if (typeSymbol == @symbol(sint16)) { |
1228 |
thisType = __get_ffi_type_sint16(); |
|
1229 |
if (! __isSmallInteger(arg)) { |
|
1230 |
__FAIL__(@symbol(InvalidArgument)) |
|
1231 |
} |
|
1232 |
__argValues[i].iVal = __intVal(arg); |
|
1233 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
1234 |
__FAIL__(@symbol(InvalidArgument)) |
|
1235 |
} |
|
1236 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1237 |
|
19311 | 1238 |
} else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) { |
1239 |
thisType = __get_ffi_type_uint32(); |
|
1240 |
if (__isSmallInteger(arg)) { |
|
1241 |
__argValues[i].iVal = __intVal(arg); |
|
1242 |
} else { |
|
1243 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1244 |
if (__argValues[i].iVal == 0) { |
|
1245 |
__FAIL__(@symbol(InvalidArgument)) |
|
1246 |
} |
|
1247 |
} |
|
1248 |
argValuePtr = &(__argValues[i].iVal); |
|
9479 | 1249 |
|
19311 | 1250 |
} else if (typeSymbol == @symbol(float)) { |
1251 |
thisType = __get_ffi_type_float(); |
|
1252 |
if (__isSmallInteger(arg)) { |
|
1253 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
1254 |
} else if (__isFloat(arg)) { |
|
1255 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
1256 |
} else if (__isShortFloat(arg)) { |
|
1257 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
1258 |
} else { |
|
1259 |
__FAIL__(@symbol(InvalidArgument)) |
|
1260 |
} |
|
1261 |
argValuePtr = &(__argValues[i].fVal); |
|
9479 | 1262 |
|
19311 | 1263 |
} else if (typeSymbol == @symbol(double)) { |
1264 |
thisType = __get_ffi_type_double(); |
|
1265 |
if (__isSmallInteger(arg)) { |
|
1266 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
1267 |
} else if (__isFloat(arg)) { |
|
1268 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
1269 |
} else if (__isShortFloat(arg)) { |
|
1270 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
1271 |
} else { |
|
1272 |
__FAIL__(@symbol(InvalidArgument)) |
|
1273 |
} |
|
1274 |
argValuePtr = &(__argValues[i].dVal); |
|
9479 | 1275 |
|
19311 | 1276 |
} else if (typeSymbol == @symbol(void)) { |
1277 |
thisType = __get_ffi_type_void(); |
|
1278 |
argValuePtr = &null; |
|
9479 | 1279 |
|
19311 | 1280 |
} else if (typeSymbol == @symbol(charPointer)) { |
1281 |
thisType = __get_ffi_type_pointer(); |
|
1282 |
if (__isStringLike(arg)) { |
|
1283 |
if (async == true) goto badArgForAsyncCall; |
|
1284 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1285 |
} else if (__isBytes(arg)) { |
|
1286 |
if (async == true) goto badArgForAsyncCall; |
|
1287 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1288 |
} else if (__isExternalAddressLike(arg)) { |
|
1289 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1290 |
} else if (__isExternalBytesLike(arg)) { |
|
1291 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1292 |
} else { |
|
1293 |
if (arg == nil) { |
|
1294 |
__argValues[i].pointerVal = (void *)0; |
|
1295 |
} else { |
|
1296 |
__FAIL__(@symbol(InvalidArgument)) |
|
1297 |
} |
|
1298 |
} |
|
1299 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1300 |
|
19311 | 1301 |
} else if (typeSymbol == @symbol(wcharPointer)) { |
1302 |
thisType = __get_ffi_type_pointer(); |
|
1303 |
if (__isUnicode16String(arg)) { |
|
1304 |
if (async == true) goto badArgForAsyncCall; |
|
1305 |
__argValues[i].pointerVal = (void *)(__unicode16StringVal(arg)); |
|
1306 |
} else if (__isBytes(arg)) { |
|
1307 |
if (async == true) goto badArgForAsyncCall; |
|
1308 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1309 |
} else if (__isExternalAddressLike(arg)) { |
|
1310 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1311 |
} else if (__isExternalBytesLike(arg)) { |
|
1312 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1313 |
} else { |
|
1314 |
if (arg == nil) { |
|
1315 |
__argValues[i].pointerVal = (void *)0; |
|
1316 |
} else { |
|
1317 |
__FAIL__(@symbol(InvalidArgument)) |
|
1318 |
} |
|
1319 |
} |
|
1320 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
12656 | 1321 |
|
19311 | 1322 |
} else if (typeSymbol == @symbol(floatPointer)) { |
1323 |
thisType = __get_ffi_type_pointer(); |
|
1324 |
if (__isBytes(arg)) { |
|
1325 |
if (async == true) goto badArgForAsyncCall; |
|
1326 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1327 |
} else if (__isExternalAddressLike(arg)) { |
|
1328 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1329 |
} else if (__isExternalBytesLike(arg)) { |
|
1330 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1331 |
} else if (__isFloats(arg)) { |
|
1332 |
char *p = (char *)(__FloatArrayInstPtr(arg)->f_element); |
|
1333 |
int nInstBytes; |
|
1334 |
OBJ cls; |
|
10603 | 1335 |
|
19311 | 1336 |
if (async == true) goto badArgForAsyncCall; |
1337 |
cls = __qClass(arg); |
|
1338 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1339 |
p = p + nInstBytes; |
|
1340 |
__argValues[i].pointerVal = p; |
|
1341 |
} else { |
|
1342 |
if (arg == nil) { |
|
1343 |
__argValues[i].pointerVal = (void *)0; |
|
1344 |
} else { |
|
1345 |
__FAIL__(@symbol(InvalidArgument)) |
|
1346 |
} |
|
1347 |
} |
|
1348 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
10603 | 1349 |
|
19311 | 1350 |
} else if (typeSymbol == @symbol(doublePointer)) { |
1351 |
thisType = __get_ffi_type_pointer(); |
|
1352 |
if (__isBytes(arg)) { |
|
1353 |
if (async == true) goto badArgForAsyncCall; |
|
1354 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1355 |
} else if (__isExternalAddressLike(arg)) { |
|
1356 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1357 |
} else if (__isExternalBytesLike(arg)) { |
|
1358 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1359 |
} else if (__isDoubles(arg)) { |
|
1360 |
char *p = (char *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1361 |
int nInstBytes; |
|
1362 |
OBJ cls; |
|
10603 | 1363 |
|
19311 | 1364 |
if (async == true) goto badArgForAsyncCall; |
1365 |
cls = __qClass(arg); |
|
1366 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1367 |
p = p + nInstBytes; |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1368 |
# ifdef __NEED_DOUBLE_ALIGN |
19311 | 1369 |
if ((INT)(__DoubleArrayInstPtr(arg)->d_element) & (__DOUBLE_ALIGN-1)) { |
1370 |
int delta = __DOUBLE_ALIGN - ((INT)p & (__DOUBLE_ALIGN-1)); |
|
10603 | 1371 |
|
19311 | 1372 |
p += delta; |
1373 |
} |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1374 |
# endif |
19311 | 1375 |
__argValues[i].pointerVal = p; |
1376 |
} else { |
|
1377 |
if (arg == nil) { |
|
1378 |
__argValues[i].pointerVal = (void *)0; |
|
1379 |
} else { |
|
1380 |
__FAIL__(@symbol(InvalidArgument)) |
|
1381 |
} |
|
1382 |
} |
|
1383 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1384 |
|
19311 | 1385 |
} else if (typeSymbol == @symbol(pointer)) { |
9524 | 1386 |
commonPointerTypeArg: ; |
19311 | 1387 |
thisType = __get_ffi_type_pointer(); |
1388 |
if (arg == nil) { |
|
1389 |
__argValues[i].pointerVal = NULL; |
|
1390 |
} else if (__isExternalAddressLike(arg)) { |
|
1391 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
1392 |
} else if (__isExternalBytesLike(arg)) { |
|
1393 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
1394 |
} else if (__isByteArrayLike(arg)) { |
|
1395 |
if (async == true) goto badArgForAsyncCall; |
|
1396 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
1397 |
} else if (__isWordArray(arg) || __isSignedWordArray(arg) |
|
1398 |
|| __isIntegerArray(arg) || __isSignedIntegerArray(arg)) { |
|
1399 |
if (async == true) goto badArgForAsyncCall; |
|
1400 |
__argValues[i].pointerVal = (void *)(__integerArrayVal(arg)); |
|
1401 |
} else if (__isFloatArray(arg)) { |
|
1402 |
if (async == true) goto badArgForAsyncCall; |
|
1403 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
|
1404 |
} else if (__isDoubleArray(arg)) { |
|
1405 |
if (async == true) goto badArgForAsyncCall; |
|
1406 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
|
1407 |
} else if (__isStringLike(arg)) { |
|
1408 |
if (async == true) { |
|
9435 | 1409 |
badArgForAsyncCall: ; |
19311 | 1410 |
__FAIL__(@symbol(BadArgForAsyncCall)) |
1411 |
} |
|
1412 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
1413 |
} else if (__isBytes(arg) || __isWords(arg) || __isLongs(arg)) { |
|
1414 |
char *p = (char *)(__byteArrayVal(arg)); |
|
1415 |
int nInstBytes; |
|
1416 |
OBJ cls; |
|
9483 | 1417 |
|
19311 | 1418 |
if (async == true) goto badArgForAsyncCall; |
1419 |
cls = __qClass(arg); |
|
1420 |
nInstBytes = __OBJS2BYTES__(__intVal(__ClassInstPtr(cls)->c_ninstvars)); |
|
1421 |
__argValues[i].pointerVal = p + nInstBytes; |
|
1422 |
} else { |
|
1423 |
__FAIL__(@symbol(InvalidArgument)) |
|
1424 |
} |
|
1425 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
9479 | 1426 |
|
19311 | 1427 |
} else if (typeSymbol == @symbol(bool)) { |
1428 |
thisType = __get_ffi_type_uint(); |
|
9342 | 1429 |
|
19311 | 1430 |
if (arg == true) { |
1431 |
__argValues[i].iVal = 1; |
|
1432 |
} else if (arg == false) { |
|
1433 |
__argValues[i].iVal = 0; |
|
1434 |
} else if (__isSmallInteger(arg)) { |
|
1435 |
__argValues[i].iVal = __intVal(arg); |
|
1436 |
} else { |
|
1437 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
1438 |
if (__argValues[i].iVal == 0) { |
|
1439 |
__FAIL__(@symbol(InvalidArgument)) |
|
1440 |
} |
|
1441 |
} |
|
1442 |
argValuePtr = &(__argValues[i].iVal); |
|
1443 |
} else { |
|
1444 |
if (__isSymbol(typeSymbol) |
|
1445 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
1446 |
if (! __isBehaviorLike(argValueClass)) { |
|
1447 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
1448 |
} |
|
1449 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
1450 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
1451 |
} |
|
1452 |
goto commonPointerTypeArg; /* sorry */ |
|
1453 |
} else { |
|
1454 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
1455 |
} |
|
1456 |
} |
|
9342 | 1457 |
|
19311 | 1458 |
__argTypes[i] = thisType; |
1459 |
__argValuePointers[i] = argValuePtr; |
|
9483 | 1460 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1461 |
# ifdef VERBOSE |
19311 | 1462 |
printf("arg%d: %x\n", i, __argValues[i].iVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1463 |
# endif |
9342 | 1464 |
} |
9465 | 1465 |
failureInfo = nil; |
9342 | 1466 |
|
1467 |
__callType = FFI_DEFAULT_ABI; |
|
1468 |
||
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1469 |
# ifdef CALLTYPE_FFI_STDCALL |
9465 | 1470 |
if (callTypeNumber == @global(CALLTYPE_API)) { |
19311 | 1471 |
__callType = CALLTYPE_FFI_STDCALL; |
9342 | 1472 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1473 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1474 |
# ifdef CALLTYPE_FFI_V8 |
9465 | 1475 |
if (callTypeNumber == @global(CALLTYPE_V8)) { |
19311 | 1476 |
__callType = CALLTYPE_FFI_V8; |
9342 | 1477 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1478 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1479 |
# ifdef CALLTYPE_FFI_V9 |
9465 | 1480 |
if (callTypeNumber == @global(CALLTYPE_V9)) { |
19311 | 1481 |
__callType = CALLTYPE_FFI_V9; |
9342 | 1482 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1483 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1484 |
# ifdef CALLTYPE_FFI_UNIX64 |
9465 | 1485 |
if (callTypeNumber == @global(CALLTYPE_UNIX64)) { |
19311 | 1486 |
__callType = CALLTYPE_FFI_UNIX64; |
9342 | 1487 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1488 |
# endif |
9342 | 1489 |
|
1490 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
19311 | 1491 |
__FAIL__(@symbol(FFIPrepareFailed)) |
9342 | 1492 |
} |
9463 | 1493 |
if (async == true) { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1494 |
# ifdef VERBOSE |
19311 | 1495 |
printf("async call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1496 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1497 |
# ifdef WIN32 |
19311 | 1498 |
__STX_C_CALL4( "ffi_call", ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1499 |
# else |
19311 | 1500 |
__BEGIN_INTERRUPTABLE__ |
1501 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
|
1502 |
__END_INTERRUPTABLE__ |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1503 |
# endif |
9435 | 1504 |
} else { |
19311 | 1505 |
if (unlimitedStack == true) { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1506 |
# ifdef VERBOSE |
19311 | 1507 |
printf("UNLIMITEDSTACKCALL call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1508 |
# endif |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1509 |
# if 0 |
19311 | 1510 |
__UNLIMITEDSTACKCALL__(ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1511 |
# endif |
19311 | 1512 |
} else { |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1513 |
# ifdef VERBOSE |
19311 | 1514 |
printf("call 0x%x\n", codeAddress); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1515 |
# endif |
19311 | 1516 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
1517 |
} |
|
9435 | 1518 |
} |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1519 |
# ifdef VERBOSE |
9524 | 1520 |
printf("retval is %d (0x%x)\n", __returnValue.iVal, __returnValue.iVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1521 |
# endif |
11586 | 1522 |
if ((returnTypeSymbol == @symbol(int)) |
1523 |
|| (returnTypeSymbol == @symbol(sint)) |
|
9479 | 1524 |
|| (returnTypeSymbol == @symbol(sint8)) |
1525 |
|| (returnTypeSymbol == @symbol(sint16)) |
|
1526 |
|| (returnTypeSymbol == @symbol(sint32))) { |
|
19311 | 1527 |
RETURN ( __MKINT(__returnValue.iVal) ); |
9342 | 1528 |
} |
1529 |
if ((returnTypeSymbol == @symbol(uint)) |
|
1530 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
1531 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
1532 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
19311 | 1533 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
9342 | 1534 |
} |
10025
053904a63549
stc-compiled ExternalLibraryFunctions
Claus Gittinger <cg@exept.de>
parents:
9981
diff
changeset
|
1535 |
if (returnTypeSymbol == @symbol(bool)) { |
19311 | 1536 |
RETURN ( __returnValue.iVal ? true : false ); |
9342 | 1537 |
} |
1538 |
if (returnTypeSymbol == @symbol(float)) { |
|
19311 | 1539 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
9342 | 1540 |
} |
1541 |
if (returnTypeSymbol == @symbol(double)) { |
|
19311 | 1542 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
9342 | 1543 |
} |
1544 |
if (returnTypeSymbol == @symbol(void)) { |
|
19311 | 1545 |
RETURN ( nil ); |
9342 | 1546 |
} |
9463 | 1547 |
if (returnTypeSymbol == @symbol(char)) { |
19311 | 1548 |
RETURN ( __MKCHARACTER(__returnValue.iVal & 0xFF) ); |
9463 | 1549 |
} |
1550 |
if (returnTypeSymbol == @symbol(wchar)) { |
|
19311 | 1551 |
RETURN ( __MKUCHARACTER(__returnValue.iVal & 0xFFFF) ); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1552 |
} |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1553 |
if (returnTypeSymbol == @symbol(sint64)) { |
19311 | 1554 |
RETURN ( __MKINT64(&__returnValue.longLongVal) ); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1555 |
} |
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1556 |
if (returnTypeSymbol == @symbol(uint64)) { |
19311 | 1557 |
RETURN ( __MKUINT64(&__returnValue.longLongVal) ); |
9463 | 1558 |
} |
11586 | 1559 |
|
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1560 |
# ifdef VERBOSE |
11586 | 1561 |
printf("%x\n", __returnValue.pointerVal); |
12504
c9987bb76eb6
untested long long return value
Claus Gittinger <cg@exept.de>
parents:
12503
diff
changeset
|
1562 |
# endif |
9346 | 1563 |
if (returnTypeSymbol == @symbol(handle)) { |
19311 | 1564 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 1565 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
19311 | 1566 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
10603 | 1567 |
} else if (returnTypeSymbol == @symbol(bytePointer)) { |
19311 | 1568 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
9463 | 1569 |
} else if (returnTypeSymbol == @symbol(charPointer)) { |
19311 | 1570 |
returnValue = __MKSTRING(__returnValue.pointerVal); |
9463 | 1571 |
} else if (returnTypeSymbol == @symbol(wcharPointer)) { |
19311 | 1572 |
returnValue = __MKU16STRING(__returnValue.pointerVal); |
9342 | 1573 |
} else { |
19311 | 1574 |
__FAIL__(@symbol(UnknownReturnType2)) |
9342 | 1575 |
} |
10603 | 1576 |
#else /* no FFI support */ |
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10614
diff
changeset
|
1577 |
failureCode = @symbol(FFINotSupported); |
10603 | 1578 |
#endif /* HAVE_FFI */ |
9342 | 1579 |
getOutOfHere: ; |
1580 |
%}. |
|
1581 |
failureCode notNil ifTrue:[ |
|
19311 | 1582 |
(failureCode == #UnknownReturnType or:[ failureCode == #UnknownArgumentType ]) ifTrue:[ |
1583 |
oldReturnType := returnType. |
|
1584 |
oldArgumentTypes := argumentTypes. |
|
1585 |
self adjustTypes. |
|
1586 |
((oldReturnType ~= returnType) or:[oldArgumentTypes ~= argumentTypes]) ifTrue:[ |
|
1587 |
thisContext restart |
|
1588 |
]. |
|
1589 |
]. |
|
1590 |
(failureCode == #BadArgForAsyncCall) ifTrue:[ |
|
1591 |
^ self tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aReceiverOrNil |
|
1592 |
]. |
|
1593 |
(failureCode == #FFINotSupported) ifTrue:[ |
|
1594 |
self primitiveFailed:'FFI support missing in this build'. |
|
1595 |
]. |
|
12579
516bba5b3e57
changed: #ffiTypeSymbolForType:
Claus Gittinger <cg@exept.de>
parents:
12504
diff
changeset
|
1596 |
|
19311 | 1597 |
self primitiveFailed. "see failureCode and failureInfo for details" |
1598 |
^ nil |
|
9342 | 1599 |
]. |
1600 |
||
9483 | 1601 |
returnType isSymbol ifTrue:[ |
19311 | 1602 |
returnValueClass notNil ifTrue:[ |
1603 |
self isConstReturnValue ifTrue:[ |
|
1604 |
returnValue changeClassTo:returnValueClass. |
|
1605 |
^ returnValue |
|
1606 |
]. |
|
1607 |
^ returnValueClass fromExternalAddress:returnValue. |
|
1608 |
]. |
|
9483 | 1609 |
] ifFalse:[ |
19311 | 1610 |
returnType isCPointer ifTrue:[ |
1611 |
returnType baseType isCStruct ifTrue:[ |
|
1612 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
1613 |
stClass notNil ifTrue:[ |
|
1614 |
self isConstReturnValue ifTrue:[ |
|
1615 |
returnValue changeClassTo:returnValueClass. |
|
1616 |
^ returnValue |
|
1617 |
]. |
|
1618 |
^ stClass fromExternalAddress:returnValue. |
|
1619 |
]. |
|
1620 |
]. |
|
1621 |
returnType baseType isCChar ifTrue:[ |
|
1622 |
^ returnValue stringAt:1 |
|
1623 |
]. |
|
1624 |
]. |
|
9342 | 1625 |
]. |
1626 |
||
1627 |
^ returnValue |
|
9463 | 1628 |
|
1629 |
"Created: / 01-08-2006 / 13:56:23 / cg" |
|
10603 | 1630 |
"Modified: / 11-06-2007 / 01:50:36 / cg" |
14037 | 1631 |
! |
1632 |
||
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
1633 |
tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aReceiverOrNil |
14037 | 1634 |
"invoked by the call primitive, iff GC-unsave arguments where passed to the call. |
1635 |
Here, allocate non-movable blocks of memory and copy the arguments into them, |
|
1636 |
then try the call again, copy changed values back, and release the memeory." |
|
1637 |
||
1638 |
|saveArguments anyBadArg result originalToSaveArgMapping| |
|
1639 |
||
1640 |
argumentsOrNil isNil ifTrue:[ |
|
14625 | 1641 |
^ self primitiveFailed |
14037 | 1642 |
]. |
14506
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1643 |
thisContext isRecursive ifTrue: [^self primitiveFailed]. |
14037 | 1644 |
|
1645 |
anyBadArg := false. |
|
1646 |
originalToSaveArgMapping := IdentityDictionary new. |
|
1647 |
||
14625 | 1648 |
saveArguments := argumentsOrNil |
1649 |
collect:[:eachArg | |
|
1650 |
|saveArg| |
|
14037 | 1651 |
|
14625 | 1652 |
(originalToSaveArgMapping includesKey:eachArg) ifTrue:[ |
1653 |
saveArg := originalToSaveArgMapping at:eachArg |
|
1654 |
] ifFalse:[ |
|
1655 |
eachArg isString ifTrue:[ |
|
1656 |
saveArg := (ExternalBytes fromString:eachArg) register. |
|
1657 |
anyBadArg := true. |
|
1658 |
originalToSaveArgMapping at:eachArg put:saveArg. |
|
1659 |
] ifFalse:[ |
|
1660 |
eachArg isByteCollection ifTrue:[ |
|
1661 |
saveArg := (ExternalBytes from:eachArg) register. |
|
1662 |
originalToSaveArgMapping at:eachArg put:saveArg. |
|
1663 |
anyBadArg := true. |
|
1664 |
] ifFalse:[ |
|
1665 |
saveArg := eachArg |
|
1666 |
] |
|
1667 |
]. |
|
1668 |
]. |
|
1669 |
saveArg |
|
1670 |
]. |
|
14037 | 1671 |
|
1672 |
anyBadArg ifFalse:[ |
|
14625 | 1673 |
"avoid recursion..." |
1674 |
^ self primitiveFailed |
|
14037 | 1675 |
]. |
1676 |
||
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
1677 |
result := self invokeFFIwithArguments:saveArguments forCPPInstance:aReceiverOrNil. |
14037 | 1678 |
|
1679 |
"/ copy back !! |
|
1680 |
originalToSaveArgMapping keysAndValuesDo:[:arg :saveArg | |
|
14625 | 1681 |
arg isSymbol ifFalse:[ |
1682 |
arg replaceFrom:1 to:(arg size) with:saveArg startingAt:1. |
|
1683 |
]. |
|
1684 |
saveArg free. |
|
14037 | 1685 |
]. |
1686 |
^ result. |
|
14506
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1687 |
|
e53548cdc24d
changed: #tryAgainWithAsyncSafeArguments:forCPPInstance:
anwild
parents:
14412
diff
changeset
|
1688 |
"Modified (format): / 06-11-2012 / 10:52:41 / anwild" |
9342 | 1689 |
! ! |
1690 |
||
10481 | 1691 |
!ExternalLibraryFunction methodsFor:'testing'! |
1692 |
||
10603 | 1693 |
isExternalLibraryFunction |
1694 |
"return true, if the receiver is some kind of externalLibrary function; |
|
1695 |
true is returned here" |
|
1696 |
||
1697 |
^true |
|
1698 |
||
1699 |
"Created: / 07-06-2007 / 10:36:40 / cg" |
|
10481 | 1700 |
! ! |
1701 |
||
8533 | 1702 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
1703 |
||
13412 | 1704 |
version_CVS |
19311 | 1705 |
^ '$Header$' |
12436 | 1706 |
! |
1707 |
||
13412 | 1708 |
version_SVN |
19311 | 1709 |
^ '$ Id: ExternalLibraryFunction.st 10643 2011-06-08 21:53:07Z vranyj1 $' |
8533 | 1710 |
! ! |
9463 | 1711 |
|
19311 | 1712 |
|
9463 | 1713 |
ExternalLibraryFunction initialize! |