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