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