author | ca |
Wed, 26 Jul 2006 15:37:09 +0200 | |
changeset 9459 | 6cd520c582b3 |
parent 9436 | 388724e908e3 |
child 9460 | 812f48058d90 |
permissions | -rw-r--r-- |
8728 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9322 | 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 |
" |
|
12 |
||
8533 | 13 |
"{ Package: 'stx:libbasic' }" |
14 |
||
15 |
ExternalFunction subclass:#ExternalLibraryFunction |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
16 |
instanceVariableNames:'flags moduleName callType returnType argumentTypes' |
8533 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'System-Support' |
|
20 |
! |
|
21 |
||
9322 | 22 |
!ExternalLibraryFunction primitiveDefinitions! |
23 |
%{ |
|
24 |
||
9337 | 25 |
/* |
26 |
* does this architecture support FFI ? |
|
9365 | 27 |
* NOTICE: this is now defined in the architecture-specific configuration file. |
9337 | 28 |
*/ |
9365 | 29 |
#if defined(WIN32) || defined(LINUX) |
30 |
# ifndef HAVE_FFI |
|
31 |
# define HAVE_FFI |
|
32 |
# endif |
|
9337 | 33 |
#endif |
34 |
||
35 |
||
36 |
#ifdef HAVE_FFI |
|
37 |
# include <ffi.h> |
|
9322 | 38 |
# define MAX_ARGS 128 |
9365 | 39 |
|
40 |
extern ffi_type *__get_ffi_type_sint(); |
|
41 |
extern ffi_type *__get_ffi_type_sint8(); |
|
42 |
extern ffi_type *__get_ffi_type_sint16(); |
|
43 |
extern ffi_type *__get_ffi_type_sint32(); |
|
44 |
extern ffi_type *__get_ffi_type_sint64(); |
|
45 |
extern ffi_type *__get_ffi_type_uint(); |
|
46 |
extern ffi_type *__get_ffi_type_uint8(); |
|
47 |
extern ffi_type *__get_ffi_type_uint16(); |
|
48 |
extern ffi_type *__get_ffi_type_uint32(); |
|
49 |
extern ffi_type *__get_ffi_type_uint64(); |
|
50 |
extern ffi_type *__get_ffi_type_float(); |
|
51 |
extern ffi_type *__get_ffi_type_double(); |
|
52 |
extern ffi_type *__get_ffi_type_void(); |
|
53 |
extern ffi_type *__get_ffi_type_pointer(); |
|
54 |
||
9337 | 55 |
#endif |
9322 | 56 |
|
57 |
%} |
|
58 |
! ! |
|
59 |
||
8728 | 60 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
61 |
||
62 |
copyright |
|
63 |
" |
|
64 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
9322 | 65 |
All Rights Reserved |
8728 | 66 |
|
67 |
This software is furnished under a license and may be used |
|
68 |
only in accordance with the terms of that license and with the |
|
69 |
inclusion of the above copyright notice. This software may not |
|
70 |
be provided or otherwise made available to, or used by, any |
|
71 |
other person. No title to or ownership of the software is |
|
72 |
hereby transferred. |
|
73 |
" |
|
9322 | 74 |
! |
75 |
||
9331 | 76 |
documentation |
77 |
" |
|
78 |
instances of me are used to interface to external library functions (as found in a dll/shared object). |
|
79 |
When a special external-call pragma such as: |
|
9336 | 80 |
<api: bool MessageBeep(uint)> |
9331 | 81 |
|
82 |
is encountered by the parser in a method, the compiler generates a call via |
|
9336 | 83 |
<correspondingExternalLibraryFunctionObject> invokeWithArguments: argumentArray. |
9331 | 84 |
|
85 |
In the invoke method, the library is checked to be loaded (and loaded if not already), |
|
9336 | 86 |
the arguments are converted to C and pushed onto the C-stack, the function is called, |
9331 | 87 |
and finally, the return value is converted back from C to a smalltalk object. |
88 |
" |
|
89 |
! |
|
90 |
||
9322 | 91 |
example |
92 |
" |
|
9336 | 93 |
[exBegin] |
94 |
|f| |
|
9322 | 95 |
|
9336 | 96 |
f := ExternalLibraryFunction new. |
97 |
f name:'MessageBeep' |
|
98 |
module:'user32.dll' |
|
99 |
callType:#WINAPI |
|
100 |
returnType:#boolean |
|
101 |
argumentTypes:#(uint). |
|
9322 | 102 |
|
9336 | 103 |
f invokeWith:1. |
104 |
[exEnd] |
|
9322 | 105 |
" |
8728 | 106 |
! ! |
8533 | 107 |
|
8550 | 108 |
!ExternalLibraryFunction class methodsFor:'instance creation'! |
109 |
||
9331 | 110 |
name:functionName module:moduleName callType:callType returnType:returnType argumentTypes:argTypes |
8550 | 111 |
^ self new |
9336 | 112 |
name:functionName module:moduleName callType:callType |
113 |
returnType:returnType argumentTypes:argTypes |
|
8550 | 114 |
! ! |
115 |
||
9321 | 116 |
!ExternalLibraryFunction class methodsFor:'constants'! |
117 |
||
118 |
callTypeAPI |
|
119 |
^ #callTypeAPI |
|
120 |
! |
|
121 |
||
9435 | 122 |
callTypeAPIAsync |
123 |
^ #callTypeAPIAsync |
|
124 |
! |
|
125 |
||
9321 | 126 |
callTypeC |
127 |
^ #callTypeC |
|
128 |
! |
|
129 |
||
9435 | 130 |
callTypeCAsync |
131 |
^ #callTypeCAsync |
|
132 |
! |
|
133 |
||
9321 | 134 |
callTypeCDecl |
135 |
^ #callTypeCDecl |
|
136 |
! |
|
137 |
||
9435 | 138 |
callTypeCDeclAsync |
139 |
^ #callTypeCDeclAsync |
|
140 |
! |
|
141 |
||
9321 | 142 |
callTypeOLE |
143 |
^ #callTypeOLE |
|
9435 | 144 |
! |
145 |
||
146 |
callTypeOLEAsync |
|
147 |
^ #callTypeOLEAsync |
|
9321 | 148 |
! ! |
149 |
||
150 |
!ExternalLibraryFunction methodsFor:'accessing'! |
|
151 |
||
152 |
argumentTypes |
|
153 |
^ argumentTypes |
|
154 |
! ! |
|
155 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
156 |
!ExternalLibraryFunction methodsFor:'invoking'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
157 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
158 |
invoke |
9321 | 159 |
self hasCode ifFalse:[ |
9322 | 160 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
161 |
]. |
9321 | 162 |
^ self invokeFFIWithArguments:#() |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
163 |
! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
164 |
|
9347 | 165 |
invokeCPPVirtualOn:anInstance |
9341 | 166 |
self hasCode ifFalse:[ |
167 |
self prepareInvoke. |
|
168 |
]. |
|
9347 | 169 |
^ self invokeCPPVirtualFFIOn:anInstance withArguments:#() |
9341 | 170 |
! |
171 |
||
9347 | 172 |
invokeCPPVirtualOn:instance with:arg |
9341 | 173 |
self hasCode ifFalse:[ |
174 |
self prepareInvoke. |
|
175 |
]. |
|
9347 | 176 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg) |
9341 | 177 |
! |
178 |
||
9347 | 179 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 |
9341 | 180 |
self hasCode ifFalse:[ |
181 |
self prepareInvoke. |
|
182 |
]. |
|
9347 | 183 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2) |
9341 | 184 |
! |
185 |
||
9347 | 186 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 |
9341 | 187 |
self hasCode ifFalse:[ |
188 |
self prepareInvoke. |
|
189 |
]. |
|
9347 | 190 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3) |
9341 | 191 |
! |
192 |
||
9347 | 193 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 with:arg4 |
9341 | 194 |
self hasCode ifFalse:[ |
195 |
self prepareInvoke. |
|
196 |
]. |
|
9347 | 197 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
9341 | 198 |
! |
199 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
200 |
invokeWith:arg |
9321 | 201 |
self hasCode ifFalse:[ |
9322 | 202 |
self prepareInvoke. |
9321 | 203 |
]. |
204 |
^ self invokeFFIWithArguments:(Array with:arg) |
|
205 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
206 |
|
9321 | 207 |
invokeWith:arg1 with:arg2 |
208 |
self hasCode ifFalse:[ |
|
9322 | 209 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
210 |
]. |
9321 | 211 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2) |
212 |
! |
|
213 |
||
214 |
invokeWith:arg1 with:arg2 with:arg3 |
|
215 |
self hasCode ifFalse:[ |
|
9322 | 216 |
self prepareInvoke. |
9321 | 217 |
]. |
218 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
219 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
220 |
|
9321 | 221 |
invokeWith:arg1 with:arg2 with:arg3 with:arg4 |
222 |
self hasCode ifFalse:[ |
|
9322 | 223 |
self prepareInvoke. |
9321 | 224 |
]. |
225 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
226 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
227 |
|
9321 | 228 |
invokeWithArguments:argArray |
229 |
self hasCode ifFalse:[ |
|
9322 | 230 |
self prepareInvoke. |
9321 | 231 |
]. |
232 |
^ self invokeFFIWithArguments:argArray |
|
233 |
! ! |
|
234 |
||
235 |
!ExternalLibraryFunction methodsFor:'printing'! |
|
236 |
||
237 |
printOn:aStream |
|
238 |
aStream nextPutAll:'<'. |
|
239 |
callType printOn:aStream. |
|
240 |
aStream nextPutAll:' '. |
|
241 |
name printOn:aStream. |
|
242 |
aStream nextPutAll:' module:'. |
|
243 |
moduleName printOn:aStream. |
|
244 |
aStream nextPutAll:'>'. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
245 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
246 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
247 |
!ExternalLibraryFunction methodsFor:'private'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
248 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
249 |
linkToModule |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
250 |
"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
|
251 |
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
|
252 |
|
9392 | 253 |
|handle functionName| |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
254 |
|
9321 | 255 |
moduleName isNil ifTrue:[ |
9340 | 256 |
self error:'Missing moduleName'. |
9321 | 257 |
]. |
9336 | 258 |
moduleHandle isNil ifTrue:[ |
9399 | 259 |
handle := ObjectFileLoader loadDynamicObject:(moduleName asString). |
9340 | 260 |
handle isNil ifTrue:[ |
261 |
handle := ObjectFileLoader |
|
262 |
loadDynamicObject:(Filename currentDirectory construct:moduleName) pathName. |
|
263 |
handle isNil ifTrue:[ |
|
264 |
self error:'Cannot load module: ', moduleName. |
|
265 |
]. |
|
266 |
]. |
|
267 |
moduleHandle := handle. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
268 |
]. |
9341 | 269 |
name isNumber ifFalse:[ |
9393 | 270 |
functionName := name. |
271 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
|
9418
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
272 |
functionName := ('_', functionName) asSymbol. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
273 |
|
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
274 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
275 |
moduleHandle := nil. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
276 |
self error:'Missing function: ', name, ' in module: ', moduleName. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
277 |
]. |
df5d2576402d
resolve function - if function with name not exists add an underscore in front and retry
ca
parents:
9399
diff
changeset
|
278 |
]. |
9321 | 279 |
]. |
9392 | 280 |
|
9393 | 281 |
"Modified: / 22-06-2006 / 16:48:57 / cg" |
9321 | 282 |
! |
283 |
||
284 |
prepareInvoke |
|
285 |
self hasCode ifFalse:[ |
|
9392 | 286 |
moduleHandle isNil ifTrue:[ |
287 |
self linkToModule. |
|
288 |
]. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
289 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
290 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
291 |
|
8550 | 292 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
293 |
||
9327 | 294 |
ffiTypeSymbolForType:aType |
9340 | 295 |
|t| |
9322 | 296 |
|
297 |
"/ kludge for those who do not have the CType package... |
|
9340 | 298 |
t := aType. |
299 |
t isSymbol ifFalse:[ |
|
300 |
aType isString ifFalse:[ |
|
301 |
CType isNil ifTrue:[ |
|
302 |
self error:'unknown type'. |
|
303 |
]. |
|
304 |
t := aType typeSymbol. |
|
305 |
]. |
|
306 |
aType isString ifTrue:[ |
|
307 |
self halt |
|
308 |
]. |
|
309 |
t isSymbol ifFalse:[ |
|
310 |
self error:'unknown type'. |
|
311 |
]. |
|
312 |
]. |
|
9327 | 313 |
|
9340 | 314 |
^ t |
9322 | 315 |
! |
316 |
||
317 |
name:functionName module:aModuleName callType:aCallType returnType:aReturnType argumentTypes:argTypes |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
318 |
name := functionName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
319 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
320 |
callType := aCallType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
321 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
322 |
argumentTypes := argTypes. |
8550 | 323 |
! ! |
324 |
||
9342 | 325 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
326 |
||
9347 | 327 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
328 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance virtual:true |
|
9342 | 329 |
! |
330 |
||
9347 | 331 |
invokeFFIWithArguments:arguments |
332 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil virtual:false |
|
333 |
! |
|
334 |
||
335 |
invokeFFIwithArguments:arguments forCPPInstance:aCPlusPlusObjectOrNil virtual:virtual |
|
9342 | 336 |
|argTypeSymbols returnTypeSymbol failureCode returnValue stClass vtOffset| |
337 |
||
338 |
argumentTypes notNil ifTrue:[ |
|
339 |
argTypeSymbols := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType]. |
|
340 |
]. |
|
341 |
returnTypeSymbol := self ffiTypeSymbolForType:returnType. |
|
9346 | 342 |
|
9342 | 343 |
aCPlusPlusObjectOrNil notNil ifTrue:[ |
344 |
"/ it must be a kind of ExternalStructure !! |
|
345 |
(aCPlusPlusObjectOrNil isKindOf:ExternalStructure) ifFalse:[ |
|
346 |
self primitiveFailed. |
|
347 |
]. |
|
9347 | 348 |
virtual ifTrue:[ |
349 |
vtOffset := name. |
|
350 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
351 |
self primitiveFailed. |
|
352 |
] |
|
9342 | 353 |
]. |
354 |
]. |
|
355 |
||
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
356 |
%{ /* STACK: 100000 */ |
9342 | 357 |
#ifdef HAVE_FFI |
358 |
ffi_cif __cif; |
|
359 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
360 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
361 |
ffi_type *__returnType = NULL; |
|
362 |
union u { |
|
363 |
int iVal; |
|
364 |
float fVal; |
|
365 |
double dVal; |
|
366 |
void *pointerVal; |
|
367 |
}; |
|
368 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
369 |
union u *__argValues = __argValuesIncludingThis; |
|
370 |
union u __returnValue; |
|
371 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
372 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
373 |
void *__returnValuePointer; |
|
374 |
int __numArgs, __numArgsIncludingThis; |
|
375 |
static int null = 0; |
|
376 |
int i; |
|
377 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
9435 | 378 |
int __asyncCall = 0; |
9342 | 379 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
380 |
||
381 |
if (arguments == nil) { |
|
382 |
__numArgs = 0; |
|
383 |
if (argTypeSymbols != nil) { |
|
384 |
if (! __isArray(argTypeSymbols) |
|
385 |
|| (__arraySize(argTypeSymbols) != __numArgs)) { |
|
386 |
failureCode = @symbol(ArgumentCountMismatch); |
|
387 |
goto getOutOfHere; |
|
388 |
} |
|
389 |
} |
|
390 |
} else { |
|
391 |
if (! __isArray(arguments) |
|
392 |
|| ! __isArray(argTypeSymbols) |
|
393 |
|| (__arraySize(argTypeSymbols) != (__numArgs = __arraySize(arguments)))) { |
|
394 |
failureCode = @symbol(ArgumentCountMismatch); |
|
395 |
goto getOutOfHere; |
|
396 |
} |
|
397 |
} |
|
398 |
if (__numArgs > MAX_ARGS) { |
|
399 |
failureCode = @symbol(TooManyArguments); |
|
400 |
goto getOutOfHere; |
|
401 |
} |
|
402 |
||
9435 | 403 |
if ((__INST(callType) == @symbol(callTypeCAsync)) |
404 |
|| (__INST(callType) == @symbol(callTypeAPIAsync)) |
|
405 |
|| (__INST(callType) == @symbol(callTypeCDeclAsync)) |
|
406 |
|| (__INST(callType) == @symbol(callTypeOLEAsync))) { |
|
407 |
__asyncCall = 1; |
|
408 |
} |
|
409 |
||
9342 | 410 |
/* |
411 |
* validate the return type |
|
412 |
*/ |
|
413 |
__returnValuePointer = &__returnValue; |
|
414 |
if (returnTypeSymbol == @symbol(int)) { |
|
415 |
__returnType = __get_ffi_type_sint(); |
|
416 |
} else if (returnTypeSymbol == @symbol(uint)) { |
|
417 |
__returnType = __get_ffi_type_uint(); |
|
418 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
419 |
if (sizeof(long) == 4) { |
|
420 |
__returnType = __get_ffi_type_sint32(); |
|
421 |
} else if (sizeof(long) == 8) { |
|
422 |
__returnType = __get_ffi_type_sint64(); |
|
423 |
} else { |
|
424 |
failureCode = @symbol(UnknownReturnType); |
|
425 |
goto getOutOfHere; |
|
426 |
} |
|
427 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
428 |
if (sizeof(long) == 4) { |
|
429 |
__returnType = __get_ffi_type_uint32(); |
|
430 |
}else if (sizeof(long) == 8) { |
|
431 |
__returnType = __get_ffi_type_uint64(); |
|
432 |
} else { |
|
433 |
failureCode = @symbol(UnknownReturnType); |
|
434 |
goto getOutOfHere; |
|
435 |
} |
|
436 |
} else if (returnTypeSymbol == @symbol(boolean)) { |
|
437 |
__returnType = __get_ffi_type_uint(); |
|
438 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
|
439 |
__returnType = __get_ffi_type_uint8(); |
|
440 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
|
441 |
__returnType = __get_ffi_type_sint8(); |
|
442 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
|
443 |
__returnType = __get_ffi_type_uint16(); |
|
444 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
|
445 |
__returnType = __get_ffi_type_sint16(); |
|
446 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
447 |
__returnType = __get_ffi_type_float(); |
|
448 |
} else if (returnTypeSymbol == @symbol(double)) { |
|
449 |
__returnType = __get_ffi_type_double(); |
|
450 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
451 |
__returnType = __get_ffi_type_void(); |
|
452 |
__returnValuePointer = NULL; |
|
453 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
|
454 |
__returnType = __get_ffi_type_pointer(); |
|
9346 | 455 |
} else if (returnTypeSymbol == @symbol(handle)) { |
456 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 457 |
} else { |
458 |
failureCode = @symbol(UnknownReturnType); |
|
459 |
goto getOutOfHere; |
|
460 |
} |
|
461 |
||
462 |
/* |
|
463 |
* validate the c++ object |
|
464 |
*/ |
|
465 |
if (aCPlusPlusObjectOrNil != nil) { |
|
466 |
struct cPlusPlusInstance { |
|
467 |
void **vTable; |
|
468 |
}; |
|
469 |
struct cPlusPlusInstance *inst; |
|
470 |
||
471 |
if (__isExternalAddressLike(aCPlusPlusObjectOrNil)) { |
|
472 |
inst = (void *)(__externalAddressVal(aCPlusPlusObjectOrNil)); |
|
473 |
} else if (__isExternalBytesLike(aCPlusPlusObjectOrNil)) { |
|
474 |
inst = (void *)(__externalBytesVal(aCPlusPlusObjectOrNil)); |
|
475 |
} else { |
|
476 |
failureCode = @symbol(InvalidInstance); |
|
477 |
goto getOutOfHere; |
|
478 |
} |
|
479 |
__argValues[0].pointerVal = inst; |
|
480 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
481 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
482 |
||
483 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
|
484 |
__argTypes = &__argTypesIncludingThis[1]; |
|
485 |
__argValues = &__argValuesIncludingThis[1]; |
|
486 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 487 |
|
488 |
if (virtual == true) { |
|
489 |
if (! __isSmallInteger(vtOffset)) { |
|
490 |
failureCode = @symbol(InvalidVTableIndex); |
|
491 |
goto getOutOfHere; |
|
492 |
} |
|
493 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
494 |
printf("codeAddress: %x\n", codeAddress); |
|
495 |
} |
|
9342 | 496 |
} else { |
497 |
__numArgsIncludingThis = __numArgs; |
|
498 |
} |
|
499 |
||
500 |
/* |
|
501 |
* validate all arg types and setup arg-buffers |
|
502 |
*/ |
|
503 |
for (i=0; i<__numArgs; i++) { |
|
504 |
ffi_type *thisType; |
|
505 |
void *argValuePtr; |
|
506 |
OBJ typeSymbol; |
|
507 |
OBJ arg; |
|
508 |
||
509 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
|
510 |
arg = __ArrayInstPtr(arguments)->a_element[i]; |
|
511 |
||
512 |
if (typeSymbol == @symbol(long)) { |
|
513 |
if (sizeof(long) == sizeof(int)) { |
|
514 |
typeSymbol = @symbol(sint); |
|
515 |
} else { |
|
516 |
if (sizeof(long) == 4) { |
|
517 |
typeSymbol = @symbol(sint32); |
|
518 |
} else if (sizeof(long) == 8) { |
|
519 |
typeSymbol = @symbol(sint64); |
|
520 |
} |
|
521 |
} |
|
522 |
} |
|
523 |
if (typeSymbol == @symbol(ulong)) { |
|
524 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
525 |
typeSymbol = @symbol(uint); |
|
526 |
} else { |
|
527 |
if (sizeof(long) == 4) { |
|
528 |
typeSymbol = @symbol(uint32); |
|
529 |
} else if (sizeof(long) == 8) { |
|
530 |
typeSymbol = @symbol(uint64); |
|
531 |
} |
|
532 |
} |
|
533 |
} |
|
534 |
||
535 |
if (typeSymbol == @symbol(int)) { |
|
536 |
thisType = __get_ffi_type_sint(); |
|
537 |
if (__isSmallInteger(arg)) { |
|
538 |
__argValues[i].iVal = __intVal(arg); |
|
539 |
} else { |
|
540 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
541 |
if (__argValues[i].iVal == 0) { |
|
542 |
failureCode = @symbol(InvalidArgument); |
|
543 |
goto getOutOfHere; |
|
544 |
} |
|
545 |
} |
|
546 |
argValuePtr = &(__argValues[i].iVal); |
|
547 |
} else if (typeSymbol == @symbol(uint)) { |
|
548 |
thisType = __get_ffi_type_uint(); |
|
549 |
||
550 |
if (__isSmallInteger(arg)) { |
|
551 |
__argValues[i].iVal = __intVal(arg); |
|
552 |
} else { |
|
553 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
554 |
if (__argValues[i].iVal == 0) { |
|
555 |
failureCode = @symbol(InvalidArgument); |
|
556 |
goto getOutOfHere; |
|
557 |
} |
|
558 |
} |
|
559 |
argValuePtr = &(__argValues[i].iVal); |
|
560 |
} else if (typeSymbol == @symbol(uint8)) { |
|
561 |
thisType = __get_ffi_type_uint8(); |
|
562 |
if (! __isSmallInteger(arg)) { |
|
563 |
failureCode = @symbol(InvalidArgument); |
|
564 |
goto getOutOfHere; |
|
565 |
} |
|
566 |
__argValues[i].iVal = __intVal(arg); |
|
567 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
568 |
failureCode = @symbol(InvalidArgument); |
|
569 |
goto getOutOfHere; |
|
570 |
} |
|
571 |
argValuePtr = &(__argValues[i].iVal); |
|
572 |
} else if (typeSymbol == @symbol(sint8)) { |
|
573 |
thisType = __get_ffi_type_sint8(); |
|
574 |
if (! __isSmallInteger(arg)) { |
|
575 |
failureCode = @symbol(InvalidArgument); |
|
576 |
goto getOutOfHere; |
|
577 |
} |
|
578 |
__argValues[i].iVal = __intVal(arg); |
|
579 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
580 |
failureCode = @symbol(InvalidArgument); |
|
581 |
goto getOutOfHere; |
|
582 |
} |
|
583 |
argValuePtr = &(__argValues[i].iVal); |
|
584 |
} else if (typeSymbol == @symbol(uint16)) { |
|
585 |
thisType = __get_ffi_type_uint16(); |
|
586 |
if (! __isSmallInteger(arg)) { |
|
587 |
failureCode = @symbol(InvalidArgument); |
|
588 |
goto getOutOfHere; |
|
589 |
} |
|
590 |
__argValues[i].iVal = __intVal(arg); |
|
591 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
592 |
failureCode = @symbol(InvalidArgument); |
|
593 |
goto getOutOfHere; |
|
594 |
} |
|
595 |
argValuePtr = &(__argValues[i].iVal); |
|
596 |
} else if (typeSymbol == @symbol(sint16)) { |
|
597 |
thisType = __get_ffi_type_sint16(); |
|
598 |
if (! __isSmallInteger(arg)) { |
|
599 |
failureCode = @symbol(InvalidArgument); |
|
600 |
goto getOutOfHere; |
|
601 |
} |
|
602 |
__argValues[i].iVal = __intVal(arg); |
|
603 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
604 |
failureCode = @symbol(InvalidArgument); |
|
605 |
goto getOutOfHere; |
|
606 |
} |
|
607 |
argValuePtr = &(__argValues[i].iVal); |
|
608 |
} else if (typeSymbol == @symbol(float)) { |
|
609 |
thisType = __get_ffi_type_float(); |
|
610 |
if (__isSmallInteger(arg)) { |
|
611 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
612 |
} else if (__isFloat(arg)) { |
|
613 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
614 |
} else if (__isShortFloat(arg)) { |
|
615 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
616 |
} else { |
|
617 |
failureCode = @symbol(InvalidArgument); |
|
618 |
goto getOutOfHere; |
|
619 |
} |
|
620 |
argValuePtr = &(__argValues[i].fVal); |
|
621 |
} else if (typeSymbol == @symbol(double)) { |
|
622 |
thisType = __get_ffi_type_double(); |
|
623 |
if (__isSmallInteger(arg)) { |
|
624 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
625 |
} else if (__isFloat(arg)) { |
|
626 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
627 |
} else if (__isShortFloat(arg)) { |
|
628 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
629 |
} else { |
|
630 |
failureCode = @symbol(InvalidArgument); |
|
631 |
goto getOutOfHere; |
|
632 |
} |
|
633 |
argValuePtr = &(__argValues[i].dVal); |
|
634 |
} else if (typeSymbol == @symbol(void)) { |
|
635 |
thisType = __get_ffi_type_void(); |
|
636 |
argValuePtr = &null; |
|
637 |
} else if (typeSymbol == @symbol(pointer)) { |
|
638 |
thisType = __get_ffi_type_pointer(); |
|
639 |
if (__isExternalAddressLike(arg)) { |
|
640 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
641 |
} else if (__isExternalBytesLike(arg)) { |
|
642 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
643 |
} else if (__isByteArray(arg)) { |
|
9435 | 644 |
if (__asyncCall) goto badArgForAsyncCall; |
9342 | 645 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
646 |
} else if (__isFloatArray(arg)) { |
|
9435 | 647 |
if (__asyncCall) goto badArgForAsyncCall; |
9342 | 648 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
649 |
} else if (__isDoubleArray(arg)) { |
|
9435 | 650 |
if (__asyncCall) goto badArgForAsyncCall; |
9342 | 651 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
652 |
} else if (__isString(arg) || __isSymbol(arg)) { |
|
9435 | 653 |
if (__asyncCall) { |
654 |
badArgForAsyncCall: ; |
|
655 |
failureCode = @symbol(BadArgForAsyncCall); |
|
656 |
goto getOutOfHere; |
|
657 |
} |
|
9342 | 658 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
659 |
} else { |
|
660 |
failureCode = @symbol(InvalidArgument); |
|
661 |
goto getOutOfHere; |
|
662 |
} |
|
663 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
664 |
} else if (typeSymbol == @symbol(boolean)) { |
|
665 |
thisType = __get_ffi_type_uint(); |
|
666 |
||
667 |
if (arg == true) { |
|
668 |
__argValues[i].iVal = 1; |
|
669 |
} else if (arg == false) { |
|
670 |
__argValues[i].iVal = 0; |
|
671 |
} else if (__isSmallInteger(arg)) { |
|
672 |
__argValues[i].iVal = __intVal(arg); |
|
673 |
} else { |
|
674 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
675 |
if (__argValues[i].iVal == 0) { |
|
676 |
failureCode = @symbol(InvalidArgument); |
|
677 |
goto getOutOfHere; |
|
678 |
} |
|
679 |
} |
|
680 |
argValuePtr = &(__argValues[i].iVal); |
|
681 |
} else { |
|
682 |
failureCode = @symbol(UnknownArgumentType); |
|
683 |
goto getOutOfHere; |
|
684 |
} |
|
685 |
||
686 |
__argTypes[i] = thisType; |
|
687 |
__argValuePointers[i] = argValuePtr; |
|
688 |
} |
|
689 |
||
690 |
__callType = FFI_DEFAULT_ABI; |
|
691 |
||
692 |
#ifdef CALLTYPE_FFI_STDCALL |
|
693 |
if ((__INST(callType) == @symbol(callTypeAPI)) |
|
694 |
|| (__INST(callType) == @symbol(WINAPI)) |
|
9435 | 695 |
|| (__INST(callType) == @symbol(STDCALL)) |
696 |
|| (__INST(callType) == @symbol(callTypeAPIAsync))) { |
|
9342 | 697 |
__callType = CALLTYPE_FFI_STDCALL; |
698 |
} |
|
699 |
#endif |
|
700 |
#ifdef CALLTYPE_FFI_V8 |
|
701 |
if ((__INST(callType) == @symbol(callTypeV8)) |
|
702 |
|| (__INST(callType) == @symbol(V8))) { |
|
703 |
__callType = CALLTYPE_FFI_V8; |
|
704 |
} |
|
705 |
#endif |
|
706 |
#ifdef CALLTYPE_FFI_V9 |
|
707 |
if ((__INST(callType) == @symbol(callTypeV9)) |
|
708 |
|| (__INST(callType) == @symbol(V9))) { |
|
709 |
__callType = CALLTYPE_FFI_V9; |
|
710 |
} |
|
711 |
#endif |
|
712 |
#ifdef CALLTYPE_FFI_UNIX64 |
|
713 |
if ((__INST(callType) == @symbol(callTypeUnix64)) |
|
714 |
|| (__INST(callType) == @symbol(UNIX64))) { |
|
715 |
__callType = CALLTYPE_FFI_UNIX64; |
|
716 |
} |
|
717 |
#endif |
|
718 |
||
719 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
720 |
failureCode = @symbol(FFIPrepareFailed); |
|
721 |
goto getOutOfHere; |
|
722 |
} |
|
9435 | 723 |
if (__asyncCall) { |
9436 | 724 |
#ifdef WIN32 |
9435 | 725 |
__STX_C_CALL4( "ffi_call", ffi_call, &__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9436 | 726 |
#else |
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
727 |
fprintf(stderr, 'async\n'); |
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
728 |
__BEGIN_INTERRUPTABLE__ |
9436 | 729 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
730 |
__END_INTERRUPTABLE__ |
9436 | 731 |
#endif |
9435 | 732 |
} else { |
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
733 |
fprintf(stderr, 'sync\n'); |
9435 | 734 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
735 |
} |
|
9342 | 736 |
|
737 |
if ((returnTypeSymbol == @symbol(int)) |
|
738 |
|| (returnTypeSymbol == @symbol(int8)) |
|
739 |
|| (returnTypeSymbol == @symbol(int16)) |
|
740 |
|| (returnTypeSymbol == @symbol(int32))) { |
|
741 |
RETURN ( __MKINT(__returnValue.iVal) ); |
|
742 |
} |
|
743 |
if ((returnTypeSymbol == @symbol(uint)) |
|
744 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
745 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
746 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
747 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
|
748 |
} |
|
749 |
if (returnTypeSymbol == @symbol(boolean)) { |
|
750 |
RETURN ( __returnValue.iVal ? true : false ); |
|
751 |
} |
|
752 |
if (returnTypeSymbol == @symbol(float)) { |
|
753 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
|
754 |
} |
|
755 |
if (returnTypeSymbol == @symbol(double)) { |
|
756 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
|
757 |
} |
|
758 |
if (returnTypeSymbol == @symbol(void)) { |
|
759 |
RETURN ( nil ); |
|
760 |
} |
|
9346 | 761 |
if (returnTypeSymbol == @symbol(handle)) { |
9342 | 762 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 763 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
764 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
|
9342 | 765 |
} else { |
766 |
failureCode = @symbol(UnknownReturnType2); |
|
767 |
} |
|
768 |
getOutOfHere: ; |
|
769 |
||
770 |
#else /* no FFI support */ |
|
771 |
failureCode = @symbol(FFINotSupported); |
|
772 |
#endif /* HAVE_FFI */ |
|
773 |
%}. |
|
774 |
failureCode notNil ifTrue:[ |
|
775 |
self primitiveFailed. |
|
776 |
^ nil |
|
777 |
]. |
|
778 |
||
779 |
returnType isCPointer ifTrue:[ |
|
780 |
returnType baseType isCStruct ifTrue:[ |
|
781 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
9386 | 782 |
stClass notNil ifTrue:[ |
783 |
^ stClass fromExternalAddress:returnValue. |
|
9342 | 784 |
]. |
9346 | 785 |
]. |
786 |
returnType baseType isCChar ifTrue:[ |
|
787 |
^ returnValue stringAt:1 |
|
788 |
]. |
|
9342 | 789 |
]. |
790 |
||
791 |
^ returnValue |
|
792 |
! ! |
|
793 |
||
8533 | 794 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
795 |
||
796 |
version |
|
9459
6cd520c582b3
change from unlimited to big stack, to allow for interrupts to be handled
ca
parents:
9436
diff
changeset
|
797 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.32 2006-07-26 13:37:09 ca Exp $' |
8533 | 798 |
! ! |