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