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