author | Stefan Vogel <sv@exept.de> |
Wed, 11 Mar 2020 10:09:01 +0100 | |
changeset 25334 | 8f44c9352333 |
parent 25087 | 6be980cf75e3 |
permissions | -rw-r--r-- |
24756 | 1 |
"{ Encoding: utf8 }" |
2 |
||
10609 | 3 |
" |
4 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
10610 | 5 |
All Rights Reserved |
10609 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic' }" |
|
10607 | 15 |
|
20123 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
10613 | 18 |
ExternalFunction subclass:#ExternalFunctionCallback |
10620 | 19 |
instanceVariableNames:'returnType argumentTypes flags action' |
24070 | 20 |
classVariableNames:'CallBackRegistry Verbose' |
10607 | 21 |
poolDictionaries:'' |
22 |
category:'System-Support' |
|
23 |
! |
|
24 |
||
25 |
!ExternalFunctionCallback primitiveDefinitions! |
|
26 |
%{ |
|
14754
f2511581c8a1
class: ExternalFunctionCallback
Stefan Vogel <sv@exept.de>
parents:
14659
diff
changeset
|
27 |
#include <stdlib.h> |
10616 | 28 |
#include <stdio.h> |
29 |
||
24068 | 30 |
#define VERBOSE |
31 |
||
32 |
#ifdef VERBOSE |
|
33 |
# define DEBUGCODE_IF(flag, code) if ((flag) == true) { code } |
|
34 |
# else |
|
35 |
# define DEBUG_IF(flag, code) /* nothing */ |
|
36 |
# endif |
|
37 |
||
10607 | 38 |
#ifdef HAVE_FFI |
25087 | 39 |
# ifdef __osx__ |
40 |
# include <ffi/ffi.h> |
|
41 |
# else |
|
42 |
# include <ffi.h> |
|
43 |
# endif |
|
10607 | 44 |
# define MAX_ARGS 128 |
45 |
||
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
46 |
# ifdef USE_STANDARD_FFI |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
47 |
# define __get_ffi_type_sint() &ffi_type_sint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
48 |
# define __get_ffi_type_sint8() &ffi_type_sint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
49 |
# define __get_ffi_type_sint16() &ffi_type_sint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
50 |
# define __get_ffi_type_sint32() &ffi_type_sint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
51 |
# define __get_ffi_type_sint64() &ffi_type_sint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
52 |
# define __get_ffi_type_uint() &ffi_type_uint |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
53 |
# define __get_ffi_type_uint8() &ffi_type_uint8 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
54 |
# define __get_ffi_type_uint16() &ffi_type_uint16 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
55 |
# define __get_ffi_type_uint32() &ffi_type_uint32 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
56 |
# define __get_ffi_type_uint64() &ffi_type_uint64 |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
57 |
# define __get_ffi_type_float() &ffi_type_float |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
58 |
# define __get_ffi_type_double() &ffi_type_double |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
59 |
# define __get_ffi_type_void() &ffi_type_void |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
60 |
# define __get_ffi_type_pointer() &ffi_type_pointer |
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
61 |
# else |
24068 | 62 |
extern ffi_type *__get_ffi_type_sint(); |
63 |
extern ffi_type *__get_ffi_type_sint8(); |
|
64 |
extern ffi_type *__get_ffi_type_sint16(); |
|
65 |
extern ffi_type *__get_ffi_type_sint32(); |
|
66 |
extern ffi_type *__get_ffi_type_sint64(); |
|
67 |
extern ffi_type *__get_ffi_type_uint(); |
|
68 |
extern ffi_type *__get_ffi_type_uint8(); |
|
69 |
extern ffi_type *__get_ffi_type_uint16(); |
|
70 |
extern ffi_type *__get_ffi_type_uint32(); |
|
71 |
extern ffi_type *__get_ffi_type_uint64(); |
|
72 |
extern ffi_type *__get_ffi_type_float(); |
|
73 |
extern ffi_type *__get_ffi_type_double(); |
|
74 |
extern ffi_type *__get_ffi_type_void(); |
|
75 |
extern ffi_type *__get_ffi_type_pointer(); |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
76 |
# endif |
10607 | 77 |
|
78 |
#endif |
|
79 |
||
22551 | 80 |
#ifdef __osx__ |
81 |
# define NEW_FFI |
|
82 |
#endif |
|
83 |
||
84 |
#ifndef NEW_FFI |
|
85 |
# define ffi_closure_alloc malloc |
|
86 |
# define ffi_closure_free free |
|
87 |
#endif |
|
88 |
||
10607 | 89 |
%} |
90 |
! ! |
|
91 |
||
92 |
!ExternalFunctionCallback primitiveFunctions! |
|
93 |
%{ |
|
94 |
||
10617
a9dc57b77568
Compile even if !defined(HAVE_FFI)
Stefan Vogel <sv@exept.de>
parents:
10616
diff
changeset
|
95 |
#ifdef HAVE_FFI |
10607 | 96 |
|
97 |
void |
|
10609 | 98 |
ExternalFunctionCallback__closure_wrapper_fn(ffi_cif* cif, void* resp, void** args, void* userdata) |
10607 | 99 |
{ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
100 |
INT actionIndex = (INT)userdata; |
10609 | 101 |
int i; |
102 |
OBJ st_argVector = nil; |
|
103 |
OBJ st_actionVector, st_callBack = nil, st_result; |
|
104 |
OBJFUNC code; |
|
105 |
ffi_type *retType; |
|
106 |
INT sintResult; |
|
107 |
unsigned INT uintResult; |
|
108 |
float floatResult; |
|
109 |
double doubleResult; |
|
110 |
||
111 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
112 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): actionIndex=%"_ld_" resp*=%"_lx_"\n", actionIndex, (INT)resp); |
10609 | 113 |
fflush(stderr); |
114 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): nargs=%d\n", cif->nargs); |
|
115 |
fflush(stderr); |
|
116 |
} |
|
117 |
||
118 |
st_argVector = __ARRAY_NEW_INT(cif->nargs); |
|
119 |
||
120 |
for (i=0; i<cif->nargs; i++) { |
|
121 |
ffi_type *argType; |
|
122 |
OBJ st_arg = nil; |
|
123 |
||
124 |
__PROTECT__(st_argVector); |
|
125 |
||
126 |
argType = cif->arg_types[i]; |
|
127 |
if (argType == __get_ffi_type_sint()) { |
|
128 |
st_arg = __MKINT( *(int *)(args[i]) ); |
|
22552 | 129 |
|
10609 | 130 |
} else if (argType == __get_ffi_type_uint()) { |
131 |
st_arg = __MKUINT( *(unsigned int *)(args[i]) ); |
|
22552 | 132 |
|
10609 | 133 |
} else if (argType == __get_ffi_type_uint8()) { |
134 |
st_arg = __MKSMALLINT( *(unsigned char *)(args[i]) ); |
|
22552 | 135 |
|
10609 | 136 |
} else if (argType == __get_ffi_type_sint8()) { |
137 |
st_arg = __MKSMALLINT( *(char *)(args[i]) ); |
|
22552 | 138 |
|
10609 | 139 |
} else if (argType == __get_ffi_type_uint16()) { |
140 |
st_arg = __MKSMALLINT( *(unsigned short *)(args[i]) ); |
|
22552 | 141 |
|
10609 | 142 |
} else if (argType == __get_ffi_type_sint16()) { |
143 |
st_arg = __MKSMALLINT( *(short *)(args[i]) ); |
|
22552 | 144 |
|
10609 | 145 |
} else if (argType == __get_ffi_type_uint32()) { |
146 |
st_arg = __MKUINT( *(unsigned int *)(args[i]) ); |
|
22552 | 147 |
|
10609 | 148 |
} else if (argType == __get_ffi_type_sint32()) { |
149 |
st_arg = __MKINT( *(int *)(args[i]) ); |
|
24072 | 150 |
#ifndef __BORLANDC__ |
22552 | 151 |
} else if (argType == __get_ffi_type_uint64()) { |
24073 | 152 |
st_arg = __MKUINT( *(unsigned long long *)(args[i]) ); |
22552 | 153 |
} else if (argType == __get_ffi_type_sint64()) { |
24068 | 154 |
st_arg = __MKINT( *(long long *)(args[i]) ); |
24072 | 155 |
#endif |
10609 | 156 |
} else if (argType == __get_ffi_type_float()) { |
157 |
st_arg = __MKSFLOAT( *(float *)(args[i]) ); |
|
22552 | 158 |
|
10609 | 159 |
} else if (argType == __get_ffi_type_double()) { |
160 |
st_arg = __MKFLOAT( *(double *)(args[i]) ); |
|
22552 | 161 |
|
10609 | 162 |
} else if (argType == __get_ffi_type_pointer()) { |
163 |
st_arg = __MKEXTERNALADDRESS( *(void **)(args[i]) ); |
|
22552 | 164 |
|
10609 | 165 |
} else { |
166 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
167 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid argument type %"_lx_" - arg %d\n", (INT)argType, i); |
10609 | 168 |
} |
169 |
} |
|
170 |
||
171 |
__UNPROTECT__(st_argVector); |
|
172 |
||
173 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
174 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): st-arg for %"_lx_" is %"_lx_"\n", *(unsigned INT *)(args[i]), (INT)st_arg); |
10609 | 175 |
} |
176 |
__ArrayInstPtr(st_argVector)->a_element[i] = st_arg; __STORE(st_argVector, st_arg); |
|
177 |
} |
|
178 |
||
179 |
/* the action ... */ |
|
180 |
st_actionVector = @global(ExternalFunctionCallback:CallBackRegistry); |
|
181 |
if (st_actionVector != nil) { |
|
182 |
OBJ cls = __Class(st_actionVector); |
|
183 |
||
184 |
if ((cls == Array) || (cls==WeakArray)) { |
|
185 |
actionIndex += /* nInstVars */ __intVal(__ClassInstPtr(cls)->c_ninstvars); |
|
10607 | 186 |
|
10609 | 187 |
if (__arraySize(st_actionVector) <= actionIndex) { |
188 |
st_callBack = __ArrayInstPtr(st_actionVector)->a_element[actionIndex-1]; |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
if (st_callBack == nil) { |
|
193 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
194 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): ignored nil callback\n"); |
|
195 |
} |
|
196 |
*(void **)resp = 0; |
|
197 |
return; |
|
198 |
} |
|
199 |
||
20291 | 200 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
201 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): sending value: to %"_lx_"..\n", (INT)st_callBack); |
|
202 |
} |
|
10609 | 203 |
{ |
20291 | 204 |
static struct inlineCache value_snd = __DUMMYILC1((@line+1)); |
10613 | 205 |
st_result = _SEND1(st_callBack, @symbol(callFromCWith:), nil, &value_snd, st_argVector); |
20291 | 206 |
} |
207 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
208 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): result is %"_lx_"\n", (INT)st_result); |
|
10609 | 209 |
} |
210 |
||
211 |
retType = cif->rtype; |
|
212 |
||
213 |
if (st_result == true) { |
|
214 |
sintResult = uintResult = 1; |
|
215 |
} else if (st_result == false) { |
|
216 |
sintResult = uintResult = 0; |
|
217 |
} else if (st_result == nil) { |
|
218 |
sintResult = uintResult = 0; |
|
219 |
} else { |
|
220 |
sintResult = __signedLongIntVal(st_result); |
|
221 |
uintResult = __unsignedLongIntVal(st_result); |
|
222 |
} |
|
223 |
||
224 |
if (retType == __get_ffi_type_sint()) { |
|
225 |
*(int *)resp = sintResult; |
|
22552 | 226 |
|
10609 | 227 |
} else if (retType == __get_ffi_type_uint()) { |
228 |
*(int *)resp = uintResult; |
|
22552 | 229 |
|
10609 | 230 |
} else if (retType == __get_ffi_type_uint8()) { |
231 |
*(unsigned char *)resp = uintResult; |
|
22552 | 232 |
|
10609 | 233 |
} else if (retType == __get_ffi_type_sint8()) { |
234 |
*(char *)resp = sintResult; |
|
22552 | 235 |
|
10609 | 236 |
} else if (retType == __get_ffi_type_uint16()) { |
237 |
*(unsigned short *)resp = uintResult; |
|
22552 | 238 |
|
10609 | 239 |
} else if (retType == __get_ffi_type_sint16()) { |
240 |
*(short *)resp = sintResult; |
|
22552 | 241 |
|
10609 | 242 |
} else if (retType == __get_ffi_type_uint32()) { |
243 |
*(int *)resp = uintResult; |
|
22552 | 244 |
|
10609 | 245 |
} else if (retType == __get_ffi_type_sint32()) { |
246 |
*(int *)resp = sintResult; |
|
22552 | 247 |
|
10609 | 248 |
} else if (retType == __get_ffi_type_float()) { |
249 |
if (__isFloat(st_result)) { |
|
250 |
*(float *)resp = (float)__floatVal(st_result); |
|
251 |
} else { |
|
252 |
if (__isShortFloat(st_result)) { |
|
253 |
*(float *)resp = __shortFloatVal(st_result); |
|
254 |
} else { |
|
255 |
*(float *)resp = (float)sintResult; |
|
256 |
} |
|
257 |
} |
|
258 |
} else if (retType == __get_ffi_type_double()) { |
|
259 |
if (__isFloat(st_result)) { |
|
260 |
*(double *)resp = __floatVal(st_result); |
|
261 |
} else { |
|
262 |
if (__isShortFloat(st_result)) { |
|
263 |
*(double *)resp = (double)__shortFloatVal(st_result); |
|
264 |
} else { |
|
265 |
*(double *)resp = (double)sintResult; |
|
266 |
} |
|
267 |
} |
|
22552 | 268 |
|
10609 | 269 |
} else if (retType == __get_ffi_type_pointer()) { |
270 |
*(void **)resp = (void *)__externalAddressVal( st_result ); |
|
22552 | 271 |
|
10609 | 272 |
} else { |
273 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
274 |
fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid result type %"_ld_"\n", (INT)retType); |
10609 | 275 |
} |
276 |
*(void **)resp = 0; |
|
277 |
} |
|
10607 | 278 |
} |
279 |
||
280 |
void |
|
20307 | 281 |
ExternalFunctionCallback__test_call_closure(INTLFUNC f) |
10607 | 282 |
{ |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
283 |
INT result = 0; |
10607 | 284 |
|
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
285 |
printf("doCall_closure: calling closure %"_lx_"(123)...\n", (INT)f); |
10607 | 286 |
result = (*f)(123); |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
287 |
printf("doCall_closure: back; result is %"_lx_"...\n", (INT)result); |
10607 | 288 |
} |
10617
a9dc57b77568
Compile even if !defined(HAVE_FFI)
Stefan Vogel <sv@exept.de>
parents:
10616
diff
changeset
|
289 |
#endif /* HAVE_FFI */ |
10607 | 290 |
|
291 |
%} |
|
292 |
! ! |
|
293 |
||
10609 | 294 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
295 |
||
296 |
copyright |
|
297 |
" |
|
298 |
COPYRIGHT (c) 2007 by eXept Software AG |
|
10610 | 299 |
All Rights Reserved |
10609 | 300 |
|
301 |
This software is furnished under a license and may be used |
|
302 |
only in accordance with the terms of that license and with the |
|
303 |
inclusion of the above copyright notice. This software may not |
|
304 |
be provided or otherwise made available to, or used by, any |
|
305 |
other person. No title to or ownership of the software is |
|
306 |
hereby transferred. |
|
307 |
" |
|
308 |
! |
|
309 |
||
310 |
documentation |
|
311 |
" |
|
312 |
an ExternalFunctionCallback wraps a block into a C-callable function; |
|
313 |
i.e. it creates a closure, which as seen from C-code looks like an ordinary |
|
314 |
function pointer, but when invoked evaluates a smalltalk block. |
|
315 |
||
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
316 |
A callback is created with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
317 |
cb := ExternalFunctionCallback new. |
10613 | 318 |
the arguments (as passed from the C-caller into ST) |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
319 |
and the returnValue (from ST to the C-caller) are specified with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
320 |
cb returnType:#bool argumentTypes:#(uint). |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
321 |
Then, the code is generated with: |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
322 |
cb generateClosure. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
323 |
|
20123 | 324 |
After that, the callBack-functions address can be acquired with: |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
325 |
cb address. 'can be passed to C'. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
326 |
and handed out to C. (you can also hand out the callBack directly - as it is a subclass of |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
327 |
ExternalBytes. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
328 |
The actual action of the callback can be changed (at any time later) with: |
24068 | 329 |
cb action:[:args | Transcript showCR:args. true]. |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
330 |
|
11849 | 331 |
Eventually, the callback MUST be released: |
24068 | 332 |
cb release. |
22551 | 333 |
|
334 |
[supported returnTypes:] |
|
24068 | 335 |
int |
336 |
uint |
|
337 |
uint8 |
|
338 |
uint16 |
|
339 |
uint32 |
|
340 |
uint64 |
|
341 |
sint |
|
342 |
sint8 |
|
343 |
sint16 |
|
344 |
sint32 |
|
345 |
sint64 |
|
346 |
long system dependent sint32 or sint64 |
|
347 |
ulong system dependent uint32 or uint64 |
|
22551 | 348 |
|
24068 | 349 |
bool |
350 |
float |
|
351 |
double |
|
352 |
void |
|
22551 | 353 |
|
24068 | 354 |
pointer, |
355 |
handle, |
|
356 |
charPointer, |
|
357 |
bytePointer, |
|
358 |
floatPointer |
|
359 |
doublePointer |
|
360 |
intPointer |
|
361 |
shortPointer |
|
362 |
wcharPointer |
|
22551 | 363 |
|
24068 | 364 |
<name of subclass of ExternalAddress> |
22551 | 365 |
|
366 |
[supported argumentTypes:] |
|
24068 | 367 |
handle |
368 |
pointer |
|
369 |
voidPointer |
|
22551 | 370 |
|
24068 | 371 |
long, ulong - system dependent |
22551 | 372 |
|
24068 | 373 |
int, |
374 |
uint8, sint8, uint16, sint16, |
|
375 |
uint32, sint32, |
|
376 |
float, double |
|
377 |
void |
|
22551 | 378 |
|
24068 | 379 |
charPointer, floatPointer, doublePointer, |
22551 | 380 |
|
24068 | 381 |
<name of subclass of ExternalAddress> |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
382 |
|
10609 | 383 |
[author:] |
24068 | 384 |
Claus Gittinger |
10609 | 385 |
" |
386 |
! |
|
10607 | 387 |
|
388 |
examples |
|
389 |
" |
|
390 |
|cb| |
|
391 |
||
392 |
cb := ExternalFunctionCallback new. |
|
393 |
cb returnType:#bool argumentTypes:#(uint). |
|
10620 | 394 |
cb beCallTypeWINAPI. |
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
395 |
cb generateClosure. |
10609 | 396 |
cb action:[:args | Transcript showCR:args. true]. |
22547 | 397 |
cb code. 'address can be passed to C'. |
398 |
Transcript showCR:cb code. |
|
10609 | 399 |
|
400 |
ExternalFunctionCallback testCall:cb withArgument:123. |
|
10611
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
401 |
|
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
402 |
cb action:[:args | Transcript show:'hello '; showCR:args. true]. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
403 |
|
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
404 |
ExternalFunctionCallback testCall:cb withArgument:123. |
0ca921c0a7a1
more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents:
10610
diff
changeset
|
405 |
|
10609 | 406 |
cb release |
10607 | 407 |
" |
408 |
! ! |
|
409 |
||
13867 | 410 |
!ExternalFunctionCallback class methodsFor:'instance creation'! |
11849 | 411 |
|
11851 | 412 |
callbackFor:aBlock returnType:returnType argumentTypes:argumentTypes |
11849 | 413 |
"generate a callback for the ErrorCallbackProc signature: |
25087 | 414 |
ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText) |
11849 | 415 |
which, can be given to an external API call and which invokes the |
416 |
three arg block when clled. |
|
417 |
Do not forget to eventually release the callback to avoid a memory leak." |
|
418 |
||
419 |
|cb| |
|
420 |
||
24756 | 421 |
self assert:(aBlock argumentCount == argumentTypes size). |
11851 | 422 |
|
11849 | 423 |
cb := ExternalFunctionCallback new. |
11851 | 424 |
cb returnType:returnType argumentTypes:argumentTypes. |
11849 | 425 |
cb beCallTypeWINAPI. |
426 |
cb generateClosure. |
|
11851 | 427 |
cb action:aBlock. |
11849 | 428 |
"/ ^ cb code. 'can be passed to C'. |
429 |
^ cb |
|
430 |
||
431 |
" |
|
432 |
|cb| |
|
433 |
||
434 |
cb := self errorCallbackProcFor:[:a1 :a2 :a3 | Transcript showCR:('%1 %2 %3' bindWith:a1 with:a2 with:a3)]. |
|
435 |
ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello'). |
|
436 |
cb release |
|
437 |
" |
|
11851 | 438 |
! |
439 |
||
440 |
errorCallbackProcFor:aThreeArgBlock |
|
441 |
"generate a callback for the ErrorCallbackProc signature: |
|
13574 | 442 |
ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText) |
11851 | 443 |
which, can be given to an external API call and which invokes the |
444 |
three arg block when clled. |
|
445 |
Do not forget to eventually release the callback to avoid a memory leak." |
|
446 |
||
13574 | 447 |
^ self |
448 |
callbackFor:aThreeArgBlock |
|
449 |
returnType:#long |
|
450 |
argumentTypes:#(handle int charPointer) |
|
11851 | 451 |
|
452 |
" |
|
453 |
|cb| |
|
454 |
||
455 |
cb := self errorCallbackProcFor:[:a1 :a2 :a3 | Transcript showCR:('%1 %2 %3' bindWith:a1 with:a2 with:a3)]. |
|
456 |
ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello'). |
|
457 |
cb release |
|
458 |
" |
|
11849 | 459 |
! ! |
460 |
||
10620 | 461 |
!ExternalFunctionCallback class methodsFor:'constants'! |
462 |
||
463 |
callTypeAPI |
|
464 |
^ ExternalLibraryFunction callTypeAPI |
|
465 |
! |
|
466 |
||
467 |
callTypeC |
|
468 |
^ ExternalLibraryFunction callTypeC |
|
469 |
! |
|
470 |
||
471 |
callTypeCDecl |
|
472 |
^ ExternalLibraryFunction callTypeCDecl |
|
473 |
! |
|
474 |
||
475 |
callTypeMASK |
|
476 |
^ ExternalLibraryFunction callTypeMASK |
|
477 |
! |
|
478 |
||
479 |
callTypeOLE |
|
480 |
^ ExternalLibraryFunction callTypeOLE |
|
22545 | 481 |
! |
482 |
||
483 |
callTypeUNIX64 |
|
484 |
^ ExternalLibraryFunction callTypeUNIX64 |
|
10620 | 485 |
! ! |
486 |
||
10609 | 487 |
!ExternalFunctionCallback class methodsFor:'helpers'! |
488 |
||
489 |
closureIndexFor:aCallBack |
|
490 |
CallBackRegistry isNil ifTrue:[ |
|
491 |
CallBackRegistry := WeakArray with:aCallBack. |
|
492 |
] ifFalse:[ |
|
493 |
CallBackRegistry := CallBackRegistry copyWith:aCallBack. |
|
494 |
]. |
|
495 |
^ CallBackRegistry size. |
|
496 |
! |
|
497 |
||
498 |
testCall:aCallback withArgument:arg |
|
10613 | 499 |
"a simple test, if I can be called" |
11849 | 500 |
|
501 |
self testCall:aCallback withArguments:(Array with:arg) |
|
502 |
! |
|
503 |
||
504 |
testCall:aCallback withArguments:args |
|
505 |
"a simple test, if I can be called" |
|
10609 | 506 |
%{ |
13574 | 507 |
# define MAX_CALLBACK_ARGS 5 |
20307 | 508 |
INTLFUNC f = __externalAddressVal(aCallback); |
10609 | 509 |
INT result; |
11849 | 510 |
int i; |
13574 | 511 |
void *c_args[MAX_CALLBACK_ARGS]; |
11849 | 512 |
|
21623 | 513 |
if (! __isArrayLike(args)) |
13574 | 514 |
goto badArg; |
515 |
if (__arraySize(args) > MAX_CALLBACK_ARGS) |
|
516 |
goto badArg; |
|
11849 | 517 |
|
518 |
for (i=0; i < __arraySize(args); i++) { |
|
13574 | 519 |
OBJ arg = __ArrayInstPtr(args)->a_element[i]; |
10609 | 520 |
|
13574 | 521 |
if (__isSmallInteger(arg)) { |
522 |
c_args[i] = (void *)(__intVal(arg)); |
|
523 |
} else if (arg == true) { |
|
524 |
c_args[i] = (void *)1; |
|
525 |
} else if (arg == false) { |
|
526 |
c_args[i] = (void *)0; |
|
527 |
} else if (__isStringLike(arg)) { |
|
528 |
c_args[i] = (void *)__stringVal(arg); |
|
529 |
} else if (__isByteArrayLike(arg)) { |
|
530 |
c_args[i] = (void *)__byteArrayVal(arg); |
|
531 |
} else |
|
532 |
goto badArg; |
|
10613 | 533 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
534 |
fprintf(stderr, "ExternalFunctionCallback: calling callBack %"_lx_"(%"_lx_", %"_lx_")\n", (INT)f, (INT)(c_args[0]), (INT)(c_args[1])); |
11849 | 535 |
result = (*f)(c_args[0], c_args[1], c_args[2], c_args[3], c_args[4]); |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
536 |
fprintf(stderr, "ExternalFunctionCallback: result from callBack is %"_lx_"\n", (INT)result); |
11849 | 537 |
RETURN(true); |
538 |
||
539 |
badArg: ; |
|
540 |
%}. |
|
541 |
self error:'bad argument' |
|
10609 | 542 |
! ! |
543 |
||
10607 | 544 |
!ExternalFunctionCallback methodsFor:'accessing'! |
545 |
||
10613 | 546 |
action:aBlock |
547 |
"set the action-block, to be evaluated when C calls me. |
|
548 |
The C-arguments will be passed as arguments to the block. |
|
549 |
The value returned by the block will be returned to the C-caller." |
|
550 |
||
551 |
action := aBlock. |
|
10620 | 552 |
! |
553 |
||
554 |
beCallTypeAPI |
|
555 |
flags := (flags ? 0) bitOr: (self class callTypeAPI). |
|
556 |
||
557 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
558 |
! |
|
559 |
||
560 |
beCallTypeC |
|
561 |
flags := (flags ? 0) bitOr: (self class callTypeC). |
|
562 |
||
563 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
564 |
! |
|
565 |
||
566 |
beCallTypeOLE |
|
567 |
flags := (flags ? 0) bitOr: (self class callTypeOLE). |
|
568 |
||
569 |
"Created: / 01-08-2006 / 15:12:40 / cg" |
|
570 |
! |
|
571 |
||
22545 | 572 |
beCallTypeUNIX64 |
573 |
flags := (flags ? 0) bitOr: (self class callTypeUNIX64). |
|
574 |
! |
|
575 |
||
10620 | 576 |
beCallTypeWINAPI |
577 |
self beCallTypeAPI |
|
578 |
||
579 |
"Modified: / 01-08-2006 / 15:14:02 / cg" |
|
580 |
! |
|
581 |
||
582 |
callTypeNumber |
|
583 |
^ (flags ? 0) bitAnd: (self class callTypeMASK) |
|
584 |
||
585 |
"Created: / 01-08-2006 / 15:12:10 / cg" |
|
586 |
! |
|
587 |
||
588 |
isCallTypeAPI |
|
589 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeAPI) |
|
590 |
||
591 |
"Created: / 01-08-2006 / 15:21:16 / cg" |
|
592 |
! |
|
593 |
||
594 |
isCallTypeC |
|
595 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeC) |
|
596 |
! |
|
597 |
||
598 |
isCallTypeOLE |
|
599 |
^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeOLE) |
|
10609 | 600 |
! ! |
601 |
||
602 |
!ExternalFunctionCallback methodsFor:'callback'! |
|
603 |
||
10613 | 604 |
callFromCWith:argList |
605 |
"invoked by the C-code, to which we have given out the code-ptr. |
|
606 |
Because this is evaluated from C, we probably should not block or abort or do |
|
10609 | 607 |
any other things which confuse C |
608 |
(its probably a good idea to write something into a queue here)" |
|
609 |
||
610 |
action notNil ifTrue:[ |
|
10613 | 611 |
^ action valueWithArguments:argList |
10609 | 612 |
]. |
613 |
^ nil |
|
10607 | 614 |
! ! |
615 |
||
616 |
!ExternalFunctionCallback methodsFor:'generation'! |
|
617 |
||
10613 | 618 |
code |
619 |
self hasCode ifFalse:[ |
|
14626 | 620 |
self generateClosure |
10607 | 621 |
]. |
10613 | 622 |
^ super code |
10607 | 623 |
|
624 |
"Created: / 11-06-2007 / 15:53:00 / cg" |
|
22545 | 625 |
! |
626 |
||
627 |
getCode |
|
628 |
^ super code |
|
629 |
||
630 |
"Created: / 03-03-2017 / 13:55:00 / cg" |
|
10607 | 631 |
! ! |
632 |
||
633 |
!ExternalFunctionCallback methodsFor:'private-accessing'! |
|
634 |
||
635 |
returnType:aReturnType argumentTypes:argTypes |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
636 |
"see generateClosure for valid return types" |
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
637 |
|
10607 | 638 |
returnType := aReturnType. |
639 |
argumentTypes := argTypes. |
|
640 |
||
641 |
"Created: / 11-06-2007 / 15:52:01 / cg" |
|
10722
ab6fbe61f116
changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents:
10621
diff
changeset
|
642 |
"Modified: / 19-09-2007 / 18:14:59 / cg" |
10607 | 643 |
! ! |
644 |
||
645 |
!ExternalFunctionCallback methodsFor:'private-generation'! |
|
646 |
||
647 |
generateClosure |
|
10609 | 648 |
|argTypeSymbols returnTypeSymbol failureCode failureInfo |
649 |
callTypeNumber returnValueClass argValueClass callBackIndex| |
|
10607 | 650 |
|
651 |
argTypeSymbols := argumentTypes. |
|
652 |
returnTypeSymbol := returnType. |
|
10609 | 653 |
callBackIndex := self class closureIndexFor:self. |
10621 | 654 |
callTypeNumber := self callTypeNumber. |
10609 | 655 |
%{ |
10607 | 656 |
#ifdef HAVE_FFI |
10609 | 657 |
ffi_cif *pcif; |
10607 | 658 |
ffi_type *__returnType = NULL; |
659 |
static int null = 0; |
|
660 |
int i; |
|
661 |
ffi_abi __callType = FFI_DEFAULT_ABI; |
|
662 |
int __numArgsWanted; |
|
22551 | 663 |
|
10609 | 664 |
struct closurePlusCIF { |
22552 | 665 |
ffi_closure closure; |
666 |
ffi_cif cif; |
|
667 |
ffi_type *argTypes[MAX_ARGS]; |
|
10609 | 668 |
} *closurePlusCIFp; |
10607 | 669 |
ffi_closure *pcl; |
10609 | 670 |
ffi_cif *cif; |
671 |
ffi_type **argTypePtrs; |
|
22551 | 672 |
|
22548 | 673 |
void* codePtr; |
10609 | 674 |
|
24068 | 675 |
# ifndef NEW_FFI |
10609 | 676 |
closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF)); |
22548 | 677 |
codePtr = &(closurePlusCIFp->closure); |
24068 | 678 |
DEBUGCODE_IF( @global(Verbose), { |
679 |
printf("old ffi\n"); |
|
680 |
}) |
|
681 |
# else |
|
22548 | 682 |
closurePlusCIFp = (struct closurePlusCIF *) ffi_closure_alloc(sizeof(struct closurePlusCIF), &codePtr); |
24068 | 683 |
DEBUGCODE_IF( @global(Verbose), { |
684 |
printf("new ffi\n"); |
|
685 |
}) |
|
686 |
# endif |
|
22548 | 687 |
pcl = &(closurePlusCIFp->closure); |
688 |
cif = &(closurePlusCIFp->cif); |
|
689 |
argTypePtrs = closurePlusCIFp->argTypes; |
|
690 |
||
22551 | 691 |
# define __FAIL__(fcode) \ |
22552 | 692 |
{ \ |
693 |
failureCode = fcode; ffi_closure_free(closurePlusCIFp); goto getOutOfHere; \ |
|
694 |
} |
|
22551 | 695 |
|
10607 | 696 |
if (argTypeSymbols == nil) { |
22552 | 697 |
__numArgsWanted = 0; |
21623 | 698 |
} else if (__isArrayLike(argTypeSymbols)) { |
22552 | 699 |
__numArgsWanted = __arraySize(argTypeSymbols); |
10607 | 700 |
} else { |
22552 | 701 |
__FAIL__(@symbol(BadArgumentTypeVector)) |
10607 | 702 |
} |
703 |
||
704 |
if (__numArgsWanted > MAX_ARGS) { |
|
22552 | 705 |
__FAIL__(@symbol(TooManyArguments)) |
10607 | 706 |
} |
707 |
||
708 |
/* |
|
709 |
* validate the return type |
|
710 |
*/ |
|
711 |
if (returnTypeSymbol == @symbol(voidPointer)) { |
|
22552 | 712 |
returnTypeSymbol = @symbol(handle); |
10607 | 713 |
} |
714 |
||
715 |
if (returnTypeSymbol == @symbol(int)) { |
|
22552 | 716 |
__returnType = __get_ffi_type_sint(); |
10607 | 717 |
} else if (returnTypeSymbol == @symbol(uint)) { |
22552 | 718 |
__returnType = __get_ffi_type_uint(); |
10607 | 719 |
} else if (returnTypeSymbol == @symbol(uint8)) { |
22552 | 720 |
__returnType = __get_ffi_type_uint8(); |
10607 | 721 |
} else if (returnTypeSymbol == @symbol(uint16)) { |
22552 | 722 |
__returnType = __get_ffi_type_uint16(); |
10607 | 723 |
} else if (returnTypeSymbol == @symbol(uint32)) { |
22552 | 724 |
__returnType = __get_ffi_type_uint32(); |
10607 | 725 |
} else if (returnTypeSymbol == @symbol(uint64)) { |
22552 | 726 |
__returnType = __get_ffi_type_uint64(); |
10607 | 727 |
|
728 |
} else if (returnTypeSymbol == @symbol(sint)) { |
|
22552 | 729 |
__returnType = __get_ffi_type_sint(); |
10607 | 730 |
} else if (returnTypeSymbol == @symbol(sint8)) { |
22552 | 731 |
__returnType = __get_ffi_type_sint8(); |
10607 | 732 |
} else if (returnTypeSymbol == @symbol(sint16)) { |
22552 | 733 |
__returnType = __get_ffi_type_sint16(); |
10607 | 734 |
} else if (returnTypeSymbol == @symbol(sint32)) { |
22552 | 735 |
__returnType = __get_ffi_type_sint32(); |
10607 | 736 |
} else if (returnTypeSymbol == @symbol(sint64)) { |
22552 | 737 |
__returnType = __get_ffi_type_sint64(); |
10607 | 738 |
|
739 |
} else if (returnTypeSymbol == @symbol(long)) { |
|
22552 | 740 |
if (sizeof(long) == 4) { |
741 |
returnTypeSymbol = @symbol(sint32); |
|
742 |
__returnType = __get_ffi_type_sint32(); |
|
743 |
} else if (sizeof(long) == 8) { |
|
744 |
returnTypeSymbol = @symbol(sint64); |
|
745 |
__returnType = __get_ffi_type_sint64(); |
|
746 |
} else { |
|
747 |
__FAIL__(@symbol(UnknownReturnType)) |
|
748 |
} |
|
10607 | 749 |
|
750 |
} else if (returnTypeSymbol == @symbol(ulong)) { |
|
22552 | 751 |
if (sizeof(long) == 4) { |
752 |
returnTypeSymbol = @symbol(uint32); |
|
753 |
__returnType = __get_ffi_type_uint32(); |
|
754 |
}else if (sizeof(long) == 8) { |
|
755 |
returnTypeSymbol = @symbol(uint64); |
|
756 |
__returnType = __get_ffi_type_uint64(); |
|
757 |
} else { |
|
758 |
__FAIL__(@symbol(UnknownReturnType)) |
|
759 |
} |
|
10607 | 760 |
|
761 |
} else if (returnTypeSymbol == @symbol(bool)) { |
|
22552 | 762 |
__returnType = __get_ffi_type_uint(); |
10607 | 763 |
|
764 |
} else if (returnTypeSymbol == @symbol(float)) { |
|
22552 | 765 |
__returnType = __get_ffi_type_float(); |
10607 | 766 |
} else if (returnTypeSymbol == @symbol(double)) { |
22552 | 767 |
__returnType = __get_ffi_type_double(); |
10607 | 768 |
|
769 |
} else if (returnTypeSymbol == @symbol(void)) { |
|
22552 | 770 |
__returnType = __get_ffi_type_void(); |
10607 | 771 |
} else if ((returnTypeSymbol == @symbol(pointer)) |
22552 | 772 |
|| (returnTypeSymbol == @symbol(handle)) |
24071 | 773 |
|| (returnTypeSymbol == @symbol(voidPointer)) |
22552 | 774 |
|| (returnTypeSymbol == @symbol(charPointer)) |
775 |
|| (returnTypeSymbol == @symbol(bytePointer)) |
|
776 |
|| (returnTypeSymbol == @symbol(floatPointer)) |
|
777 |
|| (returnTypeSymbol == @symbol(doublePointer)) |
|
778 |
|| (returnTypeSymbol == @symbol(intPointer)) |
|
779 |
|| (returnTypeSymbol == @symbol(shortPointer)) |
|
780 |
|| (returnTypeSymbol == @symbol(wcharPointer))) { |
|
781 |
__returnType = __get_ffi_type_pointer(); |
|
10607 | 782 |
} else { |
22552 | 783 |
if (__isSymbol(returnTypeSymbol) |
784 |
&& ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) { |
|
785 |
if (! __isBehaviorLike(returnValueClass)) { |
|
786 |
__FAIL__(@symbol(NonBehaviorReturnType)) |
|
787 |
} |
|
788 |
if (! __qIsSubclassOfExternalAddress(returnValueClass)) { |
|
789 |
__FAIL__(@symbol(NonExternalAddressReturnType)) |
|
790 |
} |
|
791 |
__returnType = __get_ffi_type_pointer(); |
|
792 |
returnTypeSymbol = @symbol(pointer); |
|
793 |
} else { |
|
794 |
__FAIL__(@symbol(UnknownReturnType)) |
|
795 |
} |
|
10607 | 796 |
} |
24068 | 797 |
DEBUGCODE_IF( @global(Verbose), { |
798 |
printf("__returnType: %p\n", __returnType); |
|
799 |
}) |
|
10607 | 800 |
|
801 |
/* |
|
802 |
* setup arg-buffers |
|
803 |
*/ |
|
804 |
for (i=0; i<__numArgsWanted; i++) { |
|
24068 | 805 |
ffi_type *thisType; |
806 |
void *argValuePtr; |
|
807 |
OBJ typeSymbol; |
|
10607 | 808 |
|
24068 | 809 |
failureInfo = __mkSmallInteger(i+1); /* in case there is one */ |
10607 | 810 |
|
24068 | 811 |
typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i]; |
10607 | 812 |
|
24068 | 813 |
if (typeSymbol == @symbol(handle)) { |
814 |
typeSymbol = @symbol(pointer); |
|
815 |
} else if (typeSymbol == @symbol(voidPointer)) { |
|
816 |
typeSymbol = @symbol(pointer); |
|
817 |
} |
|
10607 | 818 |
|
24068 | 819 |
if (typeSymbol == @symbol(long)) { |
820 |
if (sizeof(long) == sizeof(int)) { |
|
821 |
typeSymbol = @symbol(sint); |
|
822 |
} else { |
|
823 |
if (sizeof(long) == 4) { |
|
824 |
typeSymbol = @symbol(sint32); |
|
825 |
} else if (sizeof(long) == 8) { |
|
826 |
typeSymbol = @symbol(sint64); |
|
827 |
} |
|
828 |
} |
|
829 |
} |
|
830 |
if (typeSymbol == @symbol(ulong)) { |
|
831 |
if (sizeof(unsigned long) == sizeof(unsigned int)) { |
|
832 |
typeSymbol = @symbol(uint); |
|
833 |
} else { |
|
834 |
if (sizeof(long) == 4) { |
|
835 |
typeSymbol = @symbol(uint32); |
|
836 |
} else if (sizeof(long) == 8) { |
|
837 |
typeSymbol = @symbol(uint64); |
|
838 |
} |
|
839 |
} |
|
840 |
} |
|
10607 | 841 |
|
24068 | 842 |
if (typeSymbol == @symbol(int)) { |
843 |
thisType = __get_ffi_type_sint(); |
|
844 |
} else if (typeSymbol == @symbol(uint)) { |
|
845 |
thisType = __get_ffi_type_uint(); |
|
846 |
} else if (typeSymbol == @symbol(uint8)) { |
|
847 |
thisType = __get_ffi_type_uint8(); |
|
848 |
} else if (typeSymbol == @symbol(sint8)) { |
|
849 |
thisType = __get_ffi_type_sint8(); |
|
850 |
} else if (typeSymbol == @symbol(uint16)) { |
|
851 |
thisType = __get_ffi_type_uint16(); |
|
852 |
} else if (typeSymbol == @symbol(sint16)) { |
|
853 |
thisType = __get_ffi_type_sint16(); |
|
854 |
} else if (typeSymbol == @symbol(uint32)) { |
|
855 |
thisType = __get_ffi_type_uint32(); |
|
856 |
} else if (typeSymbol == @symbol(sint32)) { |
|
857 |
thisType = __get_ffi_type_sint32(); |
|
858 |
} else if (typeSymbol == @symbol(uint64)) { |
|
859 |
thisType = __get_ffi_type_uint64(); |
|
860 |
} else if (typeSymbol == @symbol(sint64)) { |
|
861 |
thisType = __get_ffi_type_sint64(); |
|
22551 | 862 |
|
24068 | 863 |
} else if (typeSymbol == @symbol(float)) { |
864 |
thisType = __get_ffi_type_float(); |
|
865 |
} else if (typeSymbol == @symbol(double)) { |
|
866 |
thisType = __get_ffi_type_double(); |
|
867 |
} else if (typeSymbol == @symbol(void)) { |
|
868 |
thisType = __get_ffi_type_void(); |
|
24071 | 869 |
} else if (typeSymbol == @symbol(voidPointer)) { |
870 |
thisType = __get_ffi_type_pointer(); |
|
24068 | 871 |
} else if (typeSymbol == @symbol(charPointer)) { |
872 |
thisType = __get_ffi_type_pointer(); |
|
873 |
} else if (typeSymbol == @symbol(floatPointer)) { |
|
874 |
thisType = __get_ffi_type_pointer(); |
|
875 |
} else if (typeSymbol == @symbol(doublePointer)) { |
|
876 |
thisType = __get_ffi_type_pointer(); |
|
877 |
} else if (typeSymbol == @symbol(intPointer)) { |
|
878 |
thisType = __get_ffi_type_pointer(); |
|
879 |
} else if (typeSymbol == @symbol(bytePointer)) { |
|
880 |
thisType = __get_ffi_type_pointer(); |
|
881 |
} else if (typeSymbol == @symbol(shortPointer)) { |
|
882 |
thisType = __get_ffi_type_pointer(); |
|
883 |
} else if (typeSymbol == @symbol(wcharPointer)) { |
|
884 |
thisType = __get_ffi_type_pointer(); |
|
885 |
} else if (typeSymbol == @symbol(pointer)) { |
|
10607 | 886 |
commonPointerTypeArg: ; |
22552 | 887 |
thisType = __get_ffi_type_pointer(); |
888 |
} else if (typeSymbol == @symbol(bool)) { |
|
889 |
thisType = __get_ffi_type_uint(); |
|
890 |
} else if (__isSymbol(typeSymbol) |
|
891 |
&& ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) { |
|
892 |
if (! __isBehaviorLike(argValueClass)) { |
|
893 |
__FAIL__(@symbol(NonBehaviorArgumentType)) |
|
894 |
} |
|
895 |
if (! __qIsSubclassOfExternalAddress(argValueClass)) { |
|
896 |
__FAIL__(@symbol(NonExternalAddressArgumentType)) |
|
897 |
} |
|
898 |
goto commonPointerTypeArg; /* sorry */ |
|
899 |
} else { |
|
900 |
__FAIL__(@symbol(UnknownArgumentType)) |
|
901 |
} |
|
10607 | 902 |
|
22552 | 903 |
argTypePtrs[i] = thisType; |
10607 | 904 |
} |
905 |
failureInfo = nil; |
|
906 |
||
24068 | 907 |
DEBUGCODE_IF( @global(Verbose), { |
908 |
printf("got argTypes\n"); |
|
909 |
}) |
|
910 |
||
10607 | 911 |
__callType = FFI_DEFAULT_ABI; |
22546 | 912 |
#ifndef __osx__ |
10607 | 913 |
if (callTypeNumber != nil) { |
22545 | 914 |
# ifdef CALLTYPE_FFI_STDCALL |
22552 | 915 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) { |
916 |
__callType = CALLTYPE_FFI_STDCALL; |
|
917 |
} |
|
22545 | 918 |
# endif |
919 |
# ifdef CALLTYPE_FFI_V8 |
|
22552 | 920 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) { |
921 |
__callType = CALLTYPE_FFI_V8; |
|
922 |
} |
|
22545 | 923 |
# endif |
924 |
# ifdef CALLTYPE_FFI_V9 |
|
22552 | 925 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) { |
926 |
__callType = CALLTYPE_FFI_V9; |
|
927 |
} |
|
22545 | 928 |
# endif |
929 |
# ifdef CALLTYPE_FFI_UNIX64 |
|
22552 | 930 |
if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) { |
931 |
__callType = CALLTYPE_FFI_UNIX64; |
|
932 |
} |
|
22545 | 933 |
# endif |
934 |
} |
|
10607 | 935 |
#endif |
22545 | 936 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
24068 | 937 |
printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (void*)cif); |
22545 | 938 |
} |
939 |
||
940 |
if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) { |
|
22552 | 941 |
__FAIL__(@symbol(FFIPrepareFailed)) |
10607 | 942 |
} |
943 |
||
10609 | 944 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22552 | 945 |
printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure)); |
946 |
printf("index is %"_ld_"\n", __intVal(callBackIndex)); |
|
10609 | 947 |
} |
22551 | 948 |
#ifndef NEW_FFI |
10609 | 949 |
if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) { |
22552 | 950 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
10607 | 951 |
} |
22548 | 952 |
#else |
22551 | 953 |
if (ffi_prep_closure_loc(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)), codePtr) != FFI_OK) { |
22552 | 954 |
__FAIL__(@symbol(FFIPrepareClosureFailed)) |
22548 | 955 |
} |
956 |
#endif |
|
10609 | 957 |
if (@global(ExternalFunctionCallback:Verbose) == true) { |
22552 | 958 |
printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif)); |
959 |
printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun)); |
|
960 |
printf("pcl code at %"_lx_" is:\n", (INT)pcl); |
|
961 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]); |
|
962 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]); |
|
963 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]); |
|
964 |
printf(" %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]); |
|
10609 | 965 |
} |
14659
410089913ca1
allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents:
14632
diff
changeset
|
966 |
__INST(code_) = (OBJ)pcl; |
10609 | 967 |
|
968 |
#if 0 |
|
20307 | 969 |
ExternalFunctionCallback__test_call_closure((INTLFUNC)pcl); |
10607 | 970 |
#endif |
971 |
||
972 |
#else /* no FFI support */ |
|
10618
343d0bf4646a
Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents:
10617
diff
changeset
|
973 |
failureCode = @symbol(FFINotSupported); |
10607 | 974 |
#endif /* HAVE_FFI */ |
975 |
getOutOfHere: ; |
|
976 |
%}. |
|
977 |
failureCode notNil ifTrue:[ |
|
22552 | 978 |
self primitiveFailed:(failureCode->failureInfo). "see failureCode and failureInfo for details" |
979 |
^ nil |
|
10607 | 980 |
]. |
981 |
||
982 |
"Created: / 11-06-2007 / 21:53:02 / cg" |
|
24068 | 983 |
"Modified: / 12-04-2019 / 11:26:53 / Claus Gittinger" |
10607 | 984 |
! ! |
10609 | 985 |
|
986 |
!ExternalFunctionCallback methodsFor:'private-releasing'! |
|
987 |
||
988 |
release |
|
989 |
|idx| |
|
990 |
||
991 |
idx := CallBackRegistry identityIndexOf:self. |
|
992 |
CallBackRegistry at:idx put:nil. |
|
993 |
%{ |
|
10613 | 994 |
void *pcl = (void *)__INST(code_); |
10609 | 995 |
|
10613 | 996 |
__INST(code_) = 0; |
10609 | 997 |
if (pcl) { |
22551 | 998 |
ffi_closure_free(pcl); |
13574 | 999 |
RETURN(self); |
10609 | 1000 |
} |
1001 |
%}. |
|
1002 |
self invalidateReference. |
|
1003 |
! ! |
|
1004 |
||
1005 |
!ExternalFunctionCallback class methodsFor:'documentation'! |
|
1006 |
||
1007 |
version |
|
20123 | 1008 |
^ '$Header$' |
12461
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1009 |
! |
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1010 |
|
8dc128473734
__isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents:
11851
diff
changeset
|
1011 |
version_CVS |
20123 | 1012 |
^ '$Header$' |
10609 | 1013 |
! ! |