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