author | Claus Gittinger <cg@exept.de> |
Thu, 22 Jun 2006 16:49:05 +0200 | |
changeset 9393 | 6764de553db1 |
parent 9392 | 11914531960a |
child 9396 | 7b93396c30c1 |
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 |
||
122 |
callTypeC |
|
123 |
^ #callTypeC |
|
124 |
! |
|
125 |
||
126 |
callTypeCDecl |
|
127 |
^ #callTypeCDecl |
|
128 |
! |
|
129 |
||
130 |
callTypeOLE |
|
131 |
^ #callTypeOLE |
|
132 |
! ! |
|
133 |
||
134 |
!ExternalLibraryFunction methodsFor:'accessing'! |
|
135 |
||
136 |
argumentTypes |
|
137 |
^ argumentTypes |
|
138 |
! ! |
|
139 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
140 |
!ExternalLibraryFunction methodsFor:'invoking'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
141 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
142 |
invoke |
9321 | 143 |
self hasCode ifFalse:[ |
9322 | 144 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
145 |
]. |
9321 | 146 |
^ self invokeFFIWithArguments:#() |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
147 |
! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
148 |
|
9347 | 149 |
invokeCPPVirtualOn:anInstance |
9341 | 150 |
self hasCode ifFalse:[ |
151 |
self prepareInvoke. |
|
152 |
]. |
|
9347 | 153 |
^ self invokeCPPVirtualFFIOn:anInstance withArguments:#() |
9341 | 154 |
! |
155 |
||
9347 | 156 |
invokeCPPVirtualOn:instance with:arg |
9341 | 157 |
self hasCode ifFalse:[ |
158 |
self prepareInvoke. |
|
159 |
]. |
|
9347 | 160 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg) |
9341 | 161 |
! |
162 |
||
9347 | 163 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 |
9341 | 164 |
self hasCode ifFalse:[ |
165 |
self prepareInvoke. |
|
166 |
]. |
|
9347 | 167 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2) |
9341 | 168 |
! |
169 |
||
9347 | 170 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 |
9341 | 171 |
self hasCode ifFalse:[ |
172 |
self prepareInvoke. |
|
173 |
]. |
|
9347 | 174 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3) |
9341 | 175 |
! |
176 |
||
9347 | 177 |
invokeCPPVirtualOn:instance with:arg1 with:arg2 with:arg3 with:arg4 |
9341 | 178 |
self hasCode ifFalse:[ |
179 |
self prepareInvoke. |
|
180 |
]. |
|
9347 | 181 |
^ self invokeCPPVirtualFFIOn:instance withArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
9341 | 182 |
! |
183 |
||
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
184 |
invokeWith:arg |
9321 | 185 |
self hasCode ifFalse:[ |
9322 | 186 |
self prepareInvoke. |
9321 | 187 |
]. |
188 |
^ self invokeFFIWithArguments:(Array with:arg) |
|
189 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
190 |
|
9321 | 191 |
invokeWith:arg1 with:arg2 |
192 |
self hasCode ifFalse:[ |
|
9322 | 193 |
self prepareInvoke. |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
194 |
]. |
9321 | 195 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2) |
196 |
! |
|
197 |
||
198 |
invokeWith:arg1 with:arg2 with:arg3 |
|
199 |
self hasCode ifFalse:[ |
|
9322 | 200 |
self prepareInvoke. |
9321 | 201 |
]. |
202 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3) |
|
203 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
204 |
|
9321 | 205 |
invokeWith:arg1 with:arg2 with:arg3 with:arg4 |
206 |
self hasCode ifFalse:[ |
|
9322 | 207 |
self prepareInvoke. |
9321 | 208 |
]. |
209 |
^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) |
|
210 |
! |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
211 |
|
9321 | 212 |
invokeWithArguments:argArray |
213 |
self hasCode ifFalse:[ |
|
9322 | 214 |
self prepareInvoke. |
9321 | 215 |
]. |
216 |
^ self invokeFFIWithArguments:argArray |
|
217 |
! ! |
|
218 |
||
219 |
!ExternalLibraryFunction methodsFor:'printing'! |
|
220 |
||
221 |
printOn:aStream |
|
222 |
aStream nextPutAll:'<'. |
|
223 |
callType printOn:aStream. |
|
224 |
aStream nextPutAll:' '. |
|
225 |
name printOn:aStream. |
|
226 |
aStream nextPutAll:' module:'. |
|
227 |
moduleName printOn:aStream. |
|
228 |
aStream nextPutAll:'>'. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
229 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
230 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
231 |
!ExternalLibraryFunction methodsFor:'private'! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
232 |
|
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
233 |
linkToModule |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
234 |
"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
|
235 |
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
|
236 |
|
9392 | 237 |
|handle functionName| |
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
238 |
|
9321 | 239 |
moduleName isNil ifTrue:[ |
9340 | 240 |
self error:'Missing moduleName'. |
9321 | 241 |
]. |
9336 | 242 |
moduleHandle isNil ifTrue:[ |
9340 | 243 |
handle := ObjectFileLoader loadDynamicObject:moduleName. |
244 |
handle isNil ifTrue:[ |
|
245 |
handle := ObjectFileLoader |
|
246 |
loadDynamicObject:(Filename currentDirectory construct:moduleName) pathName. |
|
247 |
handle isNil ifTrue:[ |
|
248 |
self error:'Cannot load module: ', moduleName. |
|
249 |
]. |
|
250 |
]. |
|
251 |
moduleHandle := handle. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
252 |
]. |
9341 | 253 |
name isNumber ifFalse:[ |
9393 | 254 |
functionName := name. |
255 |
OperatingSystem isUNIXlike ifTrue:[ |
|
256 |
functionName := '_' |
|
257 |
]. |
|
258 |
(moduleHandle getFunctionAddress:functionName into:self) isNil ifTrue:[ |
|
9392 | 259 |
moduleHandle := nil. |
9341 | 260 |
self error:'Missing function: ', name, ' in module: ', moduleName. |
261 |
] |
|
9321 | 262 |
]. |
9392 | 263 |
|
9393 | 264 |
"Modified: / 22-06-2006 / 16:48:57 / cg" |
9321 | 265 |
! |
266 |
||
267 |
prepareInvoke |
|
268 |
self hasCode ifFalse:[ |
|
9392 | 269 |
moduleHandle isNil ifTrue:[ |
270 |
self linkToModule. |
|
271 |
]. |
|
8891
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
272 |
]. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
273 |
! ! |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
274 |
|
8550 | 275 |
!ExternalLibraryFunction methodsFor:'private-accessing'! |
276 |
||
9327 | 277 |
ffiTypeSymbolForType:aType |
9340 | 278 |
|t| |
9322 | 279 |
|
280 |
"/ kludge for those who do not have the CType package... |
|
9340 | 281 |
t := aType. |
282 |
t isSymbol ifFalse:[ |
|
283 |
aType isString ifFalse:[ |
|
284 |
CType isNil ifTrue:[ |
|
285 |
self error:'unknown type'. |
|
286 |
]. |
|
287 |
t := aType typeSymbol. |
|
288 |
]. |
|
289 |
aType isString ifTrue:[ |
|
290 |
self halt |
|
291 |
]. |
|
292 |
t isSymbol ifFalse:[ |
|
293 |
self error:'unknown type'. |
|
294 |
]. |
|
295 |
]. |
|
9327 | 296 |
|
9340 | 297 |
^ t |
9322 | 298 |
! |
299 |
||
300 |
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
|
301 |
name := functionName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
302 |
moduleName := aModuleName. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
303 |
callType := aCallType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
304 |
returnType := aReturnType. |
c30a030ff5ec
more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents:
8728
diff
changeset
|
305 |
argumentTypes := argTypes. |
8550 | 306 |
! ! |
307 |
||
9342 | 308 |
!ExternalLibraryFunction methodsFor:'private-invoking'! |
309 |
||
9347 | 310 |
invokeCPPVirtualFFIOn:instance withArguments:arguments |
311 |
^ self invokeFFIwithArguments:arguments forCPPInstance:instance virtual:true |
|
9342 | 312 |
! |
313 |
||
9347 | 314 |
invokeFFIWithArguments:arguments |
315 |
^ self invokeFFIwithArguments:arguments forCPPInstance:nil virtual:false |
|
316 |
! |
|
317 |
||
318 |
invokeFFIwithArguments:arguments forCPPInstance:aCPlusPlusObjectOrNil virtual:virtual |
|
9342 | 319 |
|argTypeSymbols returnTypeSymbol failureCode returnValue stClass vtOffset| |
320 |
||
321 |
argumentTypes notNil ifTrue:[ |
|
322 |
argTypeSymbols := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType]. |
|
323 |
]. |
|
324 |
returnTypeSymbol := self ffiTypeSymbolForType:returnType. |
|
9346 | 325 |
|
9342 | 326 |
aCPlusPlusObjectOrNil notNil ifTrue:[ |
327 |
"/ it must be a kind of ExternalStructure !! |
|
328 |
(aCPlusPlusObjectOrNil isKindOf:ExternalStructure) ifFalse:[ |
|
329 |
self primitiveFailed. |
|
330 |
]. |
|
9347 | 331 |
virtual ifTrue:[ |
332 |
vtOffset := name. |
|
333 |
(vtOffset between:0 and:10000) ifFalse:[ |
|
334 |
self primitiveFailed. |
|
335 |
] |
|
9342 | 336 |
]. |
337 |
]. |
|
338 |
||
339 |
%{ /* UNLIMITEDSTACK */ |
|
340 |
#ifdef HAVE_FFI |
|
341 |
ffi_cif __cif; |
|
342 |
ffi_type *__argTypesIncludingThis[MAX_ARGS+1]; |
|
343 |
ffi_type **__argTypes = __argTypesIncludingThis; |
|
344 |
ffi_type *__returnType = NULL; |
|
345 |
union u { |
|
346 |
int iVal; |
|
347 |
float fVal; |
|
348 |
double dVal; |
|
349 |
void *pointerVal; |
|
350 |
}; |
|
351 |
union u __argValuesIncludingThis[MAX_ARGS+1]; |
|
352 |
union u *__argValues = __argValuesIncludingThis; |
|
353 |
union u __returnValue; |
|
354 |
void *__argValuePointersIncludingThis[MAX_ARGS+1]; |
|
355 |
void **__argValuePointers = __argValuePointersIncludingThis; |
|
356 |
void *__returnValuePointer; |
|
357 |
int __numArgs, __numArgsIncludingThis; |
|
358 |
static int null = 0; |
|
359 |
int i; |
|
360 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
361 |
VOIDFUNC codeAddress = (VOIDFUNC)__INST(code_); |
|
362 |
||
363 |
if (arguments == nil) { |
|
364 |
__numArgs = 0; |
|
365 |
if (argTypeSymbols != nil) { |
|
366 |
if (! __isArray(argTypeSymbols) |
|
367 |
|| (__arraySize(argTypeSymbols) != __numArgs)) { |
|
368 |
failureCode = @symbol(ArgumentCountMismatch); |
|
369 |
goto getOutOfHere; |
|
370 |
} |
|
371 |
} |
|
372 |
} else { |
|
373 |
if (! __isArray(arguments) |
|
374 |
|| ! __isArray(argTypeSymbols) |
|
375 |
|| (__arraySize(argTypeSymbols) != (__numArgs = __arraySize(arguments)))) { |
|
376 |
failureCode = @symbol(ArgumentCountMismatch); |
|
377 |
goto getOutOfHere; |
|
378 |
} |
|
379 |
} |
|
380 |
if (__numArgs > MAX_ARGS) { |
|
381 |
failureCode = @symbol(TooManyArguments); |
|
382 |
goto getOutOfHere; |
|
383 |
} |
|
384 |
||
385 |
/* |
|
386 |
* validate the return type |
|
387 |
*/ |
|
388 |
__returnValuePointer = &__returnValue; |
|
389 |
if (returnTypeSymbol == @symbol(int)) { |
|
390 |
__returnType = __get_ffi_type_sint(); |
|
391 |
} else if (returnTypeSymbol == @symbol(uint)) { |
|
392 |
__returnType = __get_ffi_type_uint(); |
|
393 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
394 |
if (sizeof(long) == 4) { |
|
395 |
__returnType = __get_ffi_type_sint32(); |
|
396 |
} else if (sizeof(long) == 8) { |
|
397 |
__returnType = __get_ffi_type_sint64(); |
|
398 |
} else { |
|
399 |
failureCode = @symbol(UnknownReturnType); |
|
400 |
goto getOutOfHere; |
|
401 |
} |
|
402 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
403 |
if (sizeof(long) == 4) { |
|
404 |
__returnType = __get_ffi_type_uint32(); |
|
405 |
}else if (sizeof(long) == 8) { |
|
406 |
__returnType = __get_ffi_type_uint64(); |
|
407 |
} else { |
|
408 |
failureCode = @symbol(UnknownReturnType); |
|
409 |
goto getOutOfHere; |
|
410 |
} |
|
411 |
} else if (returnTypeSymbol == @symbol(boolean)) { |
|
412 |
__returnType = __get_ffi_type_uint(); |
|
413 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
|
414 |
__returnType = __get_ffi_type_uint8(); |
|
415 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
|
416 |
__returnType = __get_ffi_type_sint8(); |
|
417 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
|
418 |
__returnType = __get_ffi_type_uint16(); |
|
419 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
|
420 |
__returnType = __get_ffi_type_sint16(); |
|
421 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
422 |
__returnType = __get_ffi_type_float(); |
|
423 |
} else if (returnTypeSymbol == @symbol(double)) { |
|
424 |
__returnType = __get_ffi_type_double(); |
|
425 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
426 |
__returnType = __get_ffi_type_void(); |
|
427 |
__returnValuePointer = NULL; |
|
428 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
|
429 |
__returnType = __get_ffi_type_pointer(); |
|
9346 | 430 |
} else if (returnTypeSymbol == @symbol(handle)) { |
431 |
__returnType = __get_ffi_type_pointer(); |
|
9342 | 432 |
} else { |
433 |
failureCode = @symbol(UnknownReturnType); |
|
434 |
goto getOutOfHere; |
|
435 |
} |
|
436 |
||
437 |
/* |
|
438 |
* validate the c++ object |
|
439 |
*/ |
|
440 |
if (aCPlusPlusObjectOrNil != nil) { |
|
441 |
struct cPlusPlusInstance { |
|
442 |
void **vTable; |
|
443 |
}; |
|
444 |
struct cPlusPlusInstance *inst; |
|
445 |
||
446 |
if (__isExternalAddressLike(aCPlusPlusObjectOrNil)) { |
|
447 |
inst = (void *)(__externalAddressVal(aCPlusPlusObjectOrNil)); |
|
448 |
} else if (__isExternalBytesLike(aCPlusPlusObjectOrNil)) { |
|
449 |
inst = (void *)(__externalBytesVal(aCPlusPlusObjectOrNil)); |
|
450 |
} else { |
|
451 |
failureCode = @symbol(InvalidInstance); |
|
452 |
goto getOutOfHere; |
|
453 |
} |
|
454 |
__argValues[0].pointerVal = inst; |
|
455 |
__argValuePointersIncludingThis[0] = &(__argValues[0]); |
|
456 |
__argTypes[0] = __get_ffi_type_pointer(); |
|
457 |
||
458 |
__argValuePointers = &__argValuePointersIncludingThis[1]; |
|
459 |
__argTypes = &__argTypesIncludingThis[1]; |
|
460 |
__argValues = &__argValuesIncludingThis[1]; |
|
461 |
__numArgsIncludingThis = __numArgs + 1; |
|
9347 | 462 |
|
463 |
if (virtual == true) { |
|
464 |
if (! __isSmallInteger(vtOffset)) { |
|
465 |
failureCode = @symbol(InvalidVTableIndex); |
|
466 |
goto getOutOfHere; |
|
467 |
} |
|
468 |
codeAddress = inst->vTable[__intVal(vtOffset)]; |
|
469 |
printf("codeAddress: %x\n", codeAddress); |
|
470 |
} |
|
9342 | 471 |
} else { |
472 |
__numArgsIncludingThis = __numArgs; |
|
473 |
} |
|
474 |
||
475 |
/* |
|
476 |
* validate all arg types and setup arg-buffers |
|
477 |
*/ |
|
478 |
for (i=0; i<__numArgs; i++) { |
|
479 |
ffi_type *thisType; |
|
480 |
void *argValuePtr; |
|
481 |
OBJ typeSymbol; |
|
482 |
OBJ arg; |
|
483 |
||
484 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
|
485 |
arg = __ArrayInstPtr(arguments)->a_element[i]; |
|
486 |
||
487 |
if (typeSymbol == @symbol(long)) { |
|
488 |
if (sizeof(long) == sizeof(int)) { |
|
489 |
typeSymbol = @symbol(sint); |
|
490 |
} else { |
|
491 |
if (sizeof(long) == 4) { |
|
492 |
typeSymbol = @symbol(sint32); |
|
493 |
} else if (sizeof(long) == 8) { |
|
494 |
typeSymbol = @symbol(sint64); |
|
495 |
} |
|
496 |
} |
|
497 |
} |
|
498 |
if (typeSymbol == @symbol(ulong)) { |
|
499 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
500 |
typeSymbol = @symbol(uint); |
|
501 |
} else { |
|
502 |
if (sizeof(long) == 4) { |
|
503 |
typeSymbol = @symbol(uint32); |
|
504 |
} else if (sizeof(long) == 8) { |
|
505 |
typeSymbol = @symbol(uint64); |
|
506 |
} |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
if (typeSymbol == @symbol(int)) { |
|
511 |
thisType = __get_ffi_type_sint(); |
|
512 |
if (__isSmallInteger(arg)) { |
|
513 |
__argValues[i].iVal = __intVal(arg); |
|
514 |
} else { |
|
515 |
__argValues[i].iVal = __signedLongIntVal(arg); |
|
516 |
if (__argValues[i].iVal == 0) { |
|
517 |
failureCode = @symbol(InvalidArgument); |
|
518 |
goto getOutOfHere; |
|
519 |
} |
|
520 |
} |
|
521 |
argValuePtr = &(__argValues[i].iVal); |
|
522 |
} else if (typeSymbol == @symbol(uint)) { |
|
523 |
thisType = __get_ffi_type_uint(); |
|
524 |
||
525 |
if (__isSmallInteger(arg)) { |
|
526 |
__argValues[i].iVal = __intVal(arg); |
|
527 |
} else { |
|
528 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
529 |
if (__argValues[i].iVal == 0) { |
|
530 |
failureCode = @symbol(InvalidArgument); |
|
531 |
goto getOutOfHere; |
|
532 |
} |
|
533 |
} |
|
534 |
argValuePtr = &(__argValues[i].iVal); |
|
535 |
} else if (typeSymbol == @symbol(uint8)) { |
|
536 |
thisType = __get_ffi_type_uint8(); |
|
537 |
if (! __isSmallInteger(arg)) { |
|
538 |
failureCode = @symbol(InvalidArgument); |
|
539 |
goto getOutOfHere; |
|
540 |
} |
|
541 |
__argValues[i].iVal = __intVal(arg); |
|
542 |
if (((unsigned)(__argValues[i].iVal)) > 0xFF) { |
|
543 |
failureCode = @symbol(InvalidArgument); |
|
544 |
goto getOutOfHere; |
|
545 |
} |
|
546 |
argValuePtr = &(__argValues[i].iVal); |
|
547 |
} else if (typeSymbol == @symbol(sint8)) { |
|
548 |
thisType = __get_ffi_type_sint8(); |
|
549 |
if (! __isSmallInteger(arg)) { |
|
550 |
failureCode = @symbol(InvalidArgument); |
|
551 |
goto getOutOfHere; |
|
552 |
} |
|
553 |
__argValues[i].iVal = __intVal(arg); |
|
554 |
if (((__argValues[i].iVal) < -0x80) || ((__argValues[i].iVal) > 0x7F)) { |
|
555 |
failureCode = @symbol(InvalidArgument); |
|
556 |
goto getOutOfHere; |
|
557 |
} |
|
558 |
argValuePtr = &(__argValues[i].iVal); |
|
559 |
} else if (typeSymbol == @symbol(uint16)) { |
|
560 |
thisType = __get_ffi_type_uint16(); |
|
561 |
if (! __isSmallInteger(arg)) { |
|
562 |
failureCode = @symbol(InvalidArgument); |
|
563 |
goto getOutOfHere; |
|
564 |
} |
|
565 |
__argValues[i].iVal = __intVal(arg); |
|
566 |
if (((unsigned)(__argValues[i].iVal)) > 0xFFFF) { |
|
567 |
failureCode = @symbol(InvalidArgument); |
|
568 |
goto getOutOfHere; |
|
569 |
} |
|
570 |
argValuePtr = &(__argValues[i].iVal); |
|
571 |
} else if (typeSymbol == @symbol(sint16)) { |
|
572 |
thisType = __get_ffi_type_sint16(); |
|
573 |
if (! __isSmallInteger(arg)) { |
|
574 |
failureCode = @symbol(InvalidArgument); |
|
575 |
goto getOutOfHere; |
|
576 |
} |
|
577 |
__argValues[i].iVal = __intVal(arg); |
|
578 |
if (((__argValues[i].iVal) < -0x8000) || ((__argValues[i].iVal) > 0x7FFF)) { |
|
579 |
failureCode = @symbol(InvalidArgument); |
|
580 |
goto getOutOfHere; |
|
581 |
} |
|
582 |
argValuePtr = &(__argValues[i].iVal); |
|
583 |
} else if (typeSymbol == @symbol(float)) { |
|
584 |
thisType = __get_ffi_type_float(); |
|
585 |
if (__isSmallInteger(arg)) { |
|
586 |
__argValues[i].fVal = (float)(__intVal(arg)); |
|
587 |
} else if (__isFloat(arg)) { |
|
588 |
__argValues[i].fVal = (float)(__floatVal(arg)); |
|
589 |
} else if (__isShortFloat(arg)) { |
|
590 |
__argValues[i].fVal = (float)(__shortFloatVal(arg)); |
|
591 |
} else { |
|
592 |
failureCode = @symbol(InvalidArgument); |
|
593 |
goto getOutOfHere; |
|
594 |
} |
|
595 |
argValuePtr = &(__argValues[i].fVal); |
|
596 |
} else if (typeSymbol == @symbol(double)) { |
|
597 |
thisType = __get_ffi_type_double(); |
|
598 |
if (__isSmallInteger(arg)) { |
|
599 |
__argValues[i].dVal = (double)(__intVal(arg)); |
|
600 |
} else if (__isFloat(arg)) { |
|
601 |
__argValues[i].dVal = (double)(__floatVal(arg)); |
|
602 |
} else if (__isShortFloat(arg)) { |
|
603 |
__argValues[i].dVal = (double)(__shortFloatVal(arg)); |
|
604 |
} else { |
|
605 |
failureCode = @symbol(InvalidArgument); |
|
606 |
goto getOutOfHere; |
|
607 |
} |
|
608 |
argValuePtr = &(__argValues[i].dVal); |
|
609 |
} else if (typeSymbol == @symbol(void)) { |
|
610 |
thisType = __get_ffi_type_void(); |
|
611 |
argValuePtr = &null; |
|
612 |
} else if (typeSymbol == @symbol(pointer)) { |
|
613 |
thisType = __get_ffi_type_pointer(); |
|
614 |
if (__isExternalAddressLike(arg)) { |
|
615 |
__argValues[i].pointerVal = (void *)(__externalAddressVal(arg)); |
|
616 |
} else if (__isExternalBytesLike(arg)) { |
|
617 |
__argValues[i].pointerVal = (void *)(__externalBytesVal(arg)); |
|
618 |
} else if (__isByteArray(arg)) { |
|
619 |
__argValues[i].pointerVal = (void *)(__byteArrayVal(arg)); |
|
620 |
} else if (__isFloatArray(arg)) { |
|
621 |
__argValues[i].pointerVal = (void *)(__FloatArrayInstPtr(arg)->f_element); |
|
622 |
} else if (__isDoubleArray(arg)) { |
|
623 |
__argValues[i].pointerVal = (void *)(__DoubleArrayInstPtr(arg)->d_element); |
|
624 |
} else if (__isString(arg) || __isSymbol(arg)) { |
|
625 |
__argValues[i].pointerVal = (void *)(__stringVal(arg)); |
|
626 |
} else { |
|
627 |
failureCode = @symbol(InvalidArgument); |
|
628 |
goto getOutOfHere; |
|
629 |
} |
|
630 |
argValuePtr = &(__argValues[i].pointerVal);; |
|
631 |
} else if (typeSymbol == @symbol(boolean)) { |
|
632 |
thisType = __get_ffi_type_uint(); |
|
633 |
||
634 |
if (arg == true) { |
|
635 |
__argValues[i].iVal = 1; |
|
636 |
} else if (arg == false) { |
|
637 |
__argValues[i].iVal = 0; |
|
638 |
} else if (__isSmallInteger(arg)) { |
|
639 |
__argValues[i].iVal = __intVal(arg); |
|
640 |
} else { |
|
641 |
__argValues[i].iVal = __unsignedLongIntVal(arg); |
|
642 |
if (__argValues[i].iVal == 0) { |
|
643 |
failureCode = @symbol(InvalidArgument); |
|
644 |
goto getOutOfHere; |
|
645 |
} |
|
646 |
} |
|
647 |
argValuePtr = &(__argValues[i].iVal); |
|
648 |
} else { |
|
649 |
failureCode = @symbol(UnknownArgumentType); |
|
650 |
goto getOutOfHere; |
|
651 |
} |
|
652 |
||
653 |
__argTypes[i] = thisType; |
|
654 |
__argValuePointers[i] = argValuePtr; |
|
655 |
} |
|
656 |
||
657 |
__callType = FFI_DEFAULT_ABI; |
|
658 |
||
659 |
#ifdef CALLTYPE_FFI_STDCALL |
|
660 |
if ((__INST(callType) == @symbol(callTypeAPI)) |
|
661 |
|| (__INST(callType) == @symbol(WINAPI)) |
|
662 |
|| (__INST(callType) == @symbol(STDCALL))) { |
|
663 |
__callType = CALLTYPE_FFI_STDCALL; |
|
664 |
} |
|
665 |
#endif |
|
666 |
#ifdef CALLTYPE_FFI_V8 |
|
667 |
if ((__INST(callType) == @symbol(callTypeV8)) |
|
668 |
|| (__INST(callType) == @symbol(V8))) { |
|
669 |
__callType = CALLTYPE_FFI_V8; |
|
670 |
} |
|
671 |
#endif |
|
672 |
#ifdef CALLTYPE_FFI_V9 |
|
673 |
if ((__INST(callType) == @symbol(callTypeV9)) |
|
674 |
|| (__INST(callType) == @symbol(V9))) { |
|
675 |
__callType = CALLTYPE_FFI_V9; |
|
676 |
} |
|
677 |
#endif |
|
678 |
#ifdef CALLTYPE_FFI_UNIX64 |
|
679 |
if ((__INST(callType) == @symbol(callTypeUnix64)) |
|
680 |
|| (__INST(callType) == @symbol(UNIX64))) { |
|
681 |
__callType = CALLTYPE_FFI_UNIX64; |
|
682 |
} |
|
683 |
#endif |
|
684 |
||
685 |
if (ffi_prep_cif(&__cif, __callType, __numArgsIncludingThis, __returnType, __argTypesIncludingThis) != FFI_OK) { |
|
686 |
failureCode = @symbol(FFIPrepareFailed); |
|
687 |
goto getOutOfHere; |
|
688 |
} |
|
689 |
ffi_call(&__cif, codeAddress, __returnValuePointer, __argValuePointersIncludingThis); |
|
690 |
||
691 |
if ((returnTypeSymbol == @symbol(int)) |
|
692 |
|| (returnTypeSymbol == @symbol(int8)) |
|
693 |
|| (returnTypeSymbol == @symbol(int16)) |
|
694 |
|| (returnTypeSymbol == @symbol(int32))) { |
|
695 |
RETURN ( __MKINT(__returnValue.iVal) ); |
|
696 |
} |
|
697 |
if ((returnTypeSymbol == @symbol(uint)) |
|
698 |
|| (returnTypeSymbol == @symbol(uint8)) |
|
699 |
|| (returnTypeSymbol == @symbol(uint16)) |
|
700 |
|| (returnTypeSymbol == @symbol(uint32))) { |
|
701 |
RETURN ( __MKUINT(__returnValue.iVal) ); |
|
702 |
} |
|
703 |
if (returnTypeSymbol == @symbol(boolean)) { |
|
704 |
RETURN ( __returnValue.iVal ? true : false ); |
|
705 |
} |
|
706 |
if (returnTypeSymbol == @symbol(float)) { |
|
707 |
RETURN ( __MKFLOAT(__returnValue.fVal )); |
|
708 |
} |
|
709 |
if (returnTypeSymbol == @symbol(double)) { |
|
710 |
RETURN ( __MKFLOAT(__returnValue.dVal )); |
|
711 |
} |
|
712 |
if (returnTypeSymbol == @symbol(void)) { |
|
713 |
RETURN ( nil ); |
|
714 |
} |
|
9346 | 715 |
if (returnTypeSymbol == @symbol(handle)) { |
9342 | 716 |
returnValue = __MKEXTERNALADDRESS(__returnValue.pointerVal); |
9346 | 717 |
} else if (returnTypeSymbol == @symbol(pointer)) { |
718 |
returnValue = __MKEXTERNALBYTES(__returnValue.pointerVal); |
|
9342 | 719 |
} else { |
720 |
failureCode = @symbol(UnknownReturnType2); |
|
721 |
} |
|
722 |
getOutOfHere: ; |
|
723 |
||
724 |
#else /* no FFI support */ |
|
725 |
failureCode = @symbol(FFINotSupported); |
|
726 |
#endif /* HAVE_FFI */ |
|
727 |
%}. |
|
728 |
failureCode notNil ifTrue:[ |
|
729 |
self primitiveFailed. |
|
730 |
^ nil |
|
731 |
]. |
|
732 |
||
733 |
returnType isCPointer ifTrue:[ |
|
734 |
returnType baseType isCStruct ifTrue:[ |
|
735 |
stClass := Smalltalk classNamed:returnType baseType name. |
|
9386 | 736 |
stClass notNil ifTrue:[ |
737 |
^ stClass fromExternalAddress:returnValue. |
|
9342 | 738 |
]. |
9346 | 739 |
]. |
740 |
returnType baseType isCChar ifTrue:[ |
|
741 |
^ returnValue stringAt:1 |
|
742 |
]. |
|
9342 | 743 |
]. |
744 |
||
745 |
^ returnValue |
|
746 |
! ! |
|
747 |
||
8533 | 748 |
!ExternalLibraryFunction class methodsFor:'documentation'! |
749 |
||
750 |
version |
|
9393 | 751 |
^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.25 2006-06-22 14:49:05 cg Exp $' |
8533 | 752 |
! ! |