ExternalFunctionCallback.st
author Claus Gittinger <cg@exept.de>
Mon, 19 Feb 2018 14:48:01 +0100
changeset 22545 d5b6e2585870
parent 21623 0fd2de531f9a
child 22546 4f467704d94e
permissions -rw-r--r--
#FEATURE by cg class: ExternalFunctionCallback added: #beCallTypeUNIX64 changed: #generateClosure class: ExternalFunctionCallback class added: #callTypeUNIX64
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].
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   302
    cb code.  'can be passed to C'.
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   303
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   304
    ExternalFunctionCallback testCall:cb withArgument:123.
10611
0ca921c0a7a1 more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents: 10610
diff changeset
   305
0ca921c0a7a1 more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents: 10610
diff changeset
   306
    cb action:[:args | Transcript show:'hello '; showCR:args. true].
0ca921c0a7a1 more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents: 10610
diff changeset
   307
0ca921c0a7a1 more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents: 10610
diff changeset
   308
    ExternalFunctionCallback testCall:cb withArgument:123.
0ca921c0a7a1 more documentation; removed leftover halt.
Claus Gittinger <cg@exept.de>
parents: 10610
diff changeset
   309
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   310
    cb release
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
"
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
13867
ed5b71c6e589 category of:
Claus Gittinger <cg@exept.de>
parents: 13574
diff changeset
   314
!ExternalFunctionCallback class methodsFor:'instance creation'!
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   315
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   316
callbackFor:aBlock returnType:returnType argumentTypes:argumentTypes
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   317
    "generate a callback for the ErrorCallbackProc signature:
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   318
	ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText)
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   319
     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
   320
     three arg block when clled.
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   321
     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
   322
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   323
    |cb|
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   324
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   325
    self assert:(aBlock numArgs == argumentTypes size).
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   326
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   327
    cb := ExternalFunctionCallback new.
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   328
    cb returnType:returnType argumentTypes:argumentTypes.
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   329
    cb beCallTypeWINAPI.
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   330
    cb generateClosure.
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   331
    cb action:aBlock.
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   332
    "/ ^ cb code.  'can be passed to C'.
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   333
    ^ cb
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   334
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
     |cb|
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   337
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   338
     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
   339
     ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello').
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   340
     cb release
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   341
    "
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   342
!
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
errorCallbackProcFor:aThreeArgBlock
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   345
    "generate a callback for the ErrorCallbackProc signature:
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   346
	ErrorCallbackProc(HWND hWnd, int nErrID, LPTSTR lpErrorText)
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   347
     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
   348
     three arg block when clled.
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   349
     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
   350
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   351
    ^ self
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   352
	callbackFor:aThreeArgBlock
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   353
	returnType:#long
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   354
	argumentTypes:#(handle int charPointer)
11851
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   355
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
     |cb|
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   358
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   359
     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
   360
     ExternalFunctionCallback testCall:cb withArguments:#(#[1 2 3] 456 'hello').
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   361
     cb release
adda395bb92e *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 11849
diff changeset
   362
    "
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   363
! !
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   364
10620
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   365
!ExternalFunctionCallback class methodsFor:'constants'!
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   366
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   367
callTypeAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   368
    ^ ExternalLibraryFunction callTypeAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   369
!
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
callTypeC
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   372
    ^ ExternalLibraryFunction callTypeC
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   373
!
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
callTypeCDecl
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   376
    ^ ExternalLibraryFunction callTypeCDecl
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   377
!
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
callTypeMASK
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   380
    ^ ExternalLibraryFunction callTypeMASK
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   381
!
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
callTypeOLE
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   384
    ^ ExternalLibraryFunction callTypeOLE
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   385
!
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
callTypeUNIX64
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   388
    ^ ExternalLibraryFunction callTypeUNIX64
10620
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   389
! !
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   390
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   391
!ExternalFunctionCallback class methodsFor:'helpers'!
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   392
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   393
closureIndexFor:aCallBack
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   394
    CallBackRegistry isNil ifTrue:[
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   395
	CallBackRegistry := WeakArray with:aCallBack.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   396
    ] ifFalse:[
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   397
	CallBackRegistry := CallBackRegistry copyWith:aCallBack.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   398
    ].
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   399
    ^ CallBackRegistry size.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   400
!
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
testCall:aCallback withArgument:arg
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   403
    "a simple test, if I can be called"
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   404
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   405
    self testCall:aCallback withArguments:(Array with:arg)
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   406
!
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
testCall:aCallback withArguments:args
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   409
    "a simple test, if I can be called"
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   410
%{
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   411
#   define MAX_CALLBACK_ARGS 5
20307
678da26adf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 20291
diff changeset
   412
    INTLFUNC f = __externalAddressVal(aCallback);
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   413
    INT result;
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   414
    int i;
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   415
    void *c_args[MAX_CALLBACK_ARGS];
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   416
21623
0fd2de531f9a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 20307
diff changeset
   417
    if (! __isArrayLike(args))
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   418
	goto badArg;
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   419
    if (__arraySize(args) > MAX_CALLBACK_ARGS)
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   420
	goto badArg;
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   421
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   422
    for (i=0; i < __arraySize(args); i++) {
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   423
	OBJ arg = __ArrayInstPtr(args)->a_element[i];
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   424
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   425
	if (__isSmallInteger(arg)) {
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   426
	    c_args[i] = (void *)(__intVal(arg));
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   427
	} else if (arg == true) {
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   428
	    c_args[i] = (void *)1;
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   429
	} else if (arg == false) {
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   430
	    c_args[i] = (void *)0;
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   431
	} else if (__isStringLike(arg)) {
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   432
	    c_args[i] = (void *)__stringVal(arg);
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   433
	} else if (__isByteArrayLike(arg)) {
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   434
	    c_args[i] = (void *)__byteArrayVal(arg);
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   435
	} else
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   436
	    goto badArg;
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   437
    }
14659
410089913ca1 allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   438
    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
   439
    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
   440
    fprintf(stderr, "ExternalFunctionCallback: result from callBack is %"_lx_"\n", (INT)result);
11849
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   441
    RETURN(true);
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   442
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   443
badArg: ;
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   444
%}.
9df0a07fbd13 fixed #release
Claus Gittinger <cg@exept.de>
parents: 10722
diff changeset
   445
    self error:'bad argument'
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   446
! !
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   447
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
!ExternalFunctionCallback methodsFor:'accessing'!
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   450
action:aBlock
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   451
    "set the action-block, to be evaluated when C calls me.
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   452
     The C-arguments will be passed as arguments to the block.
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   453
     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
   454
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   455
    action := aBlock.
10620
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   456
!
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
beCallTypeAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   459
    flags := (flags ? 0) bitOr: (self class callTypeAPI).
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   460
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   461
    "Created: / 01-08-2006 / 15:12:40 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   462
!
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
beCallTypeC
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   465
    flags := (flags ? 0) bitOr: (self class callTypeC).
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   466
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   467
    "Created: / 01-08-2006 / 15:12:40 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   468
!
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
beCallTypeOLE
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   471
    flags := (flags ? 0) bitOr: (self class callTypeOLE).
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   472
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   473
    "Created: / 01-08-2006 / 15:12:40 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   474
!
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   475
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   476
beCallTypeUNIX64
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   477
    flags := (flags ? 0) bitOr: (self class callTypeUNIX64).
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   478
!
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   479
10620
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   480
beCallTypeWINAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   481
    self beCallTypeAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   482
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   483
    "Modified: / 01-08-2006 / 15:14:02 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   484
!
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
callTypeNumber
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   487
    ^ (flags ? 0) bitAnd: (self class callTypeMASK)
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   488
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   489
    "Created: / 01-08-2006 / 15:12:10 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   490
!
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
isCallTypeAPI
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   493
    ^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeAPI)
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   494
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   495
    "Created: / 01-08-2006 / 15:21:16 / cg"
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   496
!
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
isCallTypeC
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   499
    ^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeC)
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   500
!
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
isCallTypeOLE
c30c866d4b3d *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10618
diff changeset
   503
    ^ ((flags ? 0) bitAnd: (self class callTypeMASK)) == (self class callTypeOLE)
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   504
! !
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
!ExternalFunctionCallback methodsFor:'callback'!
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   507
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   508
callFromCWith:argList
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   509
    "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
   510
     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
   511
     any other things which confuse C
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   512
     (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
   513
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   514
    action notNil ifTrue:[
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   515
	^ action valueWithArguments:argList
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   516
    ].
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   517
    ^ nil
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
! !
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
!ExternalFunctionCallback methodsFor:'generation'!
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   522
code
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   523
    self hasCode ifFalse:[
14626
7dc0ee28824f 64bit fixes
Claus Gittinger <cg@exept.de>
parents: 13868
diff changeset
   524
	self generateClosure
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
    ].
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   526
    ^ super code
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   528
    "Created: / 11-06-2007 / 15:53:00 / cg"
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   529
!
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
getCode
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   532
    ^ super code
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   533
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   534
    "Created: / 03-03-2017 / 13:55:00 / cg"
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
! !
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
!ExternalFunctionCallback methodsFor:'private-accessing'!
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   539
returnType:aReturnType argumentTypes:argTypes
10722
ab6fbe61f116 changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents: 10621
diff changeset
   540
    "see generateClosure for valid return types"
ab6fbe61f116 changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents: 10621
diff changeset
   541
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
    returnType := aReturnType.
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
    argumentTypes := argTypes.
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
    "Created: / 11-06-2007 / 15:52:01 / cg"
10722
ab6fbe61f116 changed #returnType:argumentTypes:
Claus Gittinger <cg@exept.de>
parents: 10621
diff changeset
   546
    "Modified: / 19-09-2007 / 18:14:59 / cg"
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   547
! !
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
!ExternalFunctionCallback methodsFor:'private-generation'!
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
generateClosure
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   552
    |argTypeSymbols returnTypeSymbol failureCode failureInfo
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   553
     callTypeNumber returnValueClass argValueClass callBackIndex|
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
    argTypeSymbols := argumentTypes.
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
    returnTypeSymbol := returnType.
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   557
    callBackIndex := self class closureIndexFor:self.
10621
5e6966b208f5 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10620
diff changeset
   558
    callTypeNumber := self callTypeNumber.
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   559
%{
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
#ifdef HAVE_FFI
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   561
    ffi_cif *pcif;
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
    ffi_type *__returnType = NULL;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
    static int null = 0;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
    int i;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
    ffi_abi __callType = FFI_DEFAULT_ABI;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
    int __numArgsWanted;
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   567
    struct closurePlusCIF {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   568
        ffi_closure closure;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   569
        ffi_cif cif;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   570
        ffi_type *argTypes[MAX_ARGS];
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   571
    } *closurePlusCIFp;
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
    ffi_closure *pcl;
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   573
    ffi_cif *cif;
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   574
    ffi_type **argTypePtrs;
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   575
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   576
    closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF));
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   577
    cif = &(closurePlusCIFp->cif);
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   578
    argTypePtrs = closurePlusCIFp->argTypes;
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   579
    pcl = &(closurePlusCIFp->closure);
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
21623
0fd2de531f9a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 20307
diff changeset
   581
#   define __FAIL__(fcode) \
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   582
        { \
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   583
            failureCode = fcode; free(closurePlusCIFp); goto getOutOfHere; \
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   584
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
    if (argTypeSymbols == nil) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   587
        __numArgsWanted = 0;
21623
0fd2de531f9a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 20307
diff changeset
   588
    } else if (__isArrayLike(argTypeSymbols)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   589
        __numArgsWanted = __arraySize(argTypeSymbols);
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
    } else {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   591
        __FAIL__(@symbol(BadArgumentTypeVector))
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
    if (__numArgsWanted > MAX_ARGS) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   595
        __FAIL__(@symbol(TooManyArguments))
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
    /*
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
     * validate the return type
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
     */
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
    if (returnTypeSymbol == @symbol(voidPointer)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   602
        returnTypeSymbol = @symbol(handle);
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
    if (returnTypeSymbol == @symbol(int)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   606
        __returnType = __get_ffi_type_sint();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
    } else if (returnTypeSymbol == @symbol(uint)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   608
        __returnType = __get_ffi_type_uint();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
    } else if (returnTypeSymbol == @symbol(uint8)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   610
        __returnType = __get_ffi_type_uint8();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
    } else if (returnTypeSymbol == @symbol(uint16)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   612
        __returnType = __get_ffi_type_uint16();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
    } else if (returnTypeSymbol == @symbol(uint32)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   614
        __returnType = __get_ffi_type_uint32();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
    } else if (returnTypeSymbol == @symbol(uint64)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   616
        __returnType = __get_ffi_type_uint64();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
    } else if (returnTypeSymbol == @symbol(sint)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   619
        __returnType = __get_ffi_type_sint();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
    } else if (returnTypeSymbol == @symbol(sint8)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   621
        __returnType = __get_ffi_type_sint8();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   622
    } else if (returnTypeSymbol == @symbol(sint16)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   623
        __returnType = __get_ffi_type_sint16();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   624
    } else if (returnTypeSymbol == @symbol(sint32)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   625
        __returnType = __get_ffi_type_sint32();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
    } else if (returnTypeSymbol == @symbol(sint64)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   627
        __returnType = __get_ffi_type_sint64();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   628
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   629
    } else if (returnTypeSymbol == @symbol(long)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   630
        if (sizeof(long) == 4) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   631
           returnTypeSymbol = @symbol(sint32);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   632
           __returnType = __get_ffi_type_sint32();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   633
        } else if (sizeof(long) == 8) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   634
           returnTypeSymbol = @symbol(sint64);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   635
           __returnType = __get_ffi_type_sint64();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   636
        } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   637
            __FAIL__(@symbol(UnknownReturnType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   638
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   639
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   640
    } else if (returnTypeSymbol == @symbol(ulong)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   641
        if (sizeof(long) == 4) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   642
           returnTypeSymbol = @symbol(uint32);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   643
           __returnType = __get_ffi_type_uint32();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   644
        }else if (sizeof(long) == 8) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   645
           returnTypeSymbol = @symbol(uint64);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   646
           __returnType = __get_ffi_type_uint64();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   647
        } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   648
            __FAIL__(@symbol(UnknownReturnType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   649
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   650
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   651
    } else if (returnTypeSymbol == @symbol(bool)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   652
        __returnType = __get_ffi_type_uint();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   653
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   654
    } else if (returnTypeSymbol == @symbol(float)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   655
        __returnType = __get_ffi_type_float();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   656
    } else if (returnTypeSymbol == @symbol(double)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   657
        __returnType = __get_ffi_type_double();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   658
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   659
    } else if (returnTypeSymbol == @symbol(void)) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   660
        __returnType = __get_ffi_type_void();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   661
    } else if ((returnTypeSymbol == @symbol(pointer))
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   662
               || (returnTypeSymbol == @symbol(handle))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   663
               || (returnTypeSymbol == @symbol(charPointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   664
               || (returnTypeSymbol == @symbol(bytePointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   665
               || (returnTypeSymbol == @symbol(floatPointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   666
               || (returnTypeSymbol == @symbol(doublePointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   667
               || (returnTypeSymbol == @symbol(intPointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   668
               || (returnTypeSymbol == @symbol(shortPointer))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   669
               || (returnTypeSymbol == @symbol(wcharPointer))) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   670
        __returnType = __get_ffi_type_pointer();
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   671
    } else {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   672
        if (__isSymbol(returnTypeSymbol)
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   673
         && ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   674
            if (! __isBehaviorLike(returnValueClass)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   675
                __FAIL__(@symbol(NonBehaviorReturnType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   676
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   677
            if (! __qIsSubclassOfExternalAddress(returnValueClass)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   678
                __FAIL__(@symbol(NonExternalAddressReturnType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   679
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   680
            __returnType = __get_ffi_type_pointer();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   681
            returnTypeSymbol = @symbol(pointer);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   682
        } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   683
            __FAIL__(@symbol(UnknownReturnType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   684
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
    /*
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
     * setup arg-buffers
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
     */
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
    for (i=0; i<__numArgsWanted; i++) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   691
        ffi_type *thisType;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   692
        void *argValuePtr;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   693
        OBJ typeSymbol;
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   695
        failureInfo = __mkSmallInteger(i+1);   /* in case there is one */
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   697
        typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i];
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   699
        if (typeSymbol == @symbol(handle)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   700
            typeSymbol = @symbol(pointer);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   701
        } else if (typeSymbol == @symbol(voidPointer)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   702
            typeSymbol = @symbol(pointer);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   703
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   705
        if (typeSymbol == @symbol(long)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   706
            if (sizeof(long) == sizeof(int)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   707
                typeSymbol = @symbol(sint);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   708
            } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   709
                if (sizeof(long) == 4) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   710
                    typeSymbol = @symbol(sint32);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   711
                } else if (sizeof(long) == 8) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   712
                    typeSymbol = @symbol(sint64);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   713
                }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   714
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   715
        }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   716
        if (typeSymbol == @symbol(ulong)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   717
            if (sizeof(unsigned long) == sizeof(unsigned int)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   718
                typeSymbol = @symbol(uint);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   719
            } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   720
                if (sizeof(long) == 4) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   721
                    typeSymbol = @symbol(uint32);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   722
                } else if (sizeof(long) == 8) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   723
                    typeSymbol = @symbol(uint64);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   724
                }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   725
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   726
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   727
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   728
        if (typeSymbol == @symbol(int)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   729
            thisType = __get_ffi_type_sint();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   730
        } else if (typeSymbol == @symbol(uint)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   731
            thisType = __get_ffi_type_uint();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   732
        } else if (typeSymbol == @symbol(uint8)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   733
            thisType = __get_ffi_type_uint8();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   734
        } else if (typeSymbol == @symbol(sint8)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   735
            thisType = __get_ffi_type_sint8();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   736
        } else if (typeSymbol == @symbol(uint16)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   737
            thisType = __get_ffi_type_uint16();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   738
        } else if (typeSymbol == @symbol(sint16)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   739
            thisType = __get_ffi_type_sint16();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   740
        } else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   741
            thisType = __get_ffi_type_uint32();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   742
        } else if (typeSymbol == @symbol(float)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   743
            thisType = __get_ffi_type_float();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   744
        } else if (typeSymbol == @symbol(double)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   745
            thisType = __get_ffi_type_double();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   746
        } else if (typeSymbol == @symbol(void)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   747
            thisType = __get_ffi_type_void();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   748
        } else if (typeSymbol == @symbol(charPointer)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   749
            thisType = __get_ffi_type_pointer();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   750
        } else if (typeSymbol == @symbol(floatPointer)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   751
            thisType = __get_ffi_type_pointer();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   752
        } else if (typeSymbol == @symbol(doublePointer)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   753
            thisType = __get_ffi_type_pointer();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   754
        } else if (typeSymbol == @symbol(pointer)) {
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   755
commonPointerTypeArg: ;
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   756
            thisType = __get_ffi_type_pointer();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   757
        } else if (typeSymbol == @symbol(bool)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   758
            thisType = __get_ffi_type_uint();
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   759
        } else if (__isSymbol(typeSymbol)
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   760
             && ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   761
            if (! __isBehaviorLike(argValueClass)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   762
                __FAIL__(@symbol(NonBehaviorArgumentType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   763
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   764
            if (! __qIsSubclassOfExternalAddress(argValueClass)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   765
                __FAIL__(@symbol(NonExternalAddressArgumentType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   766
            }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   767
            goto commonPointerTypeArg; /* sorry */
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   768
        } else {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   769
            __FAIL__(@symbol(UnknownArgumentType))
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   770
        }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   772
        closurePlusCIFp->argTypes[i] = thisType;
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   774
    failureInfo = nil;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
    __callType = FFI_DEFAULT_ABI;
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   777
#ifdef __osx__
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   778
    __callType = CALLTYPE_FFI_UNIX64;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   779
#else
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   780
    if (callTypeNumber != nil) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   781
# ifdef CALLTYPE_FFI_STDCALL
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   782
        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   783
            __callType = CALLTYPE_FFI_STDCALL;
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
# endif
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   786
# ifdef CALLTYPE_FFI_V8
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   787
        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   788
            __callType = CALLTYPE_FFI_V8;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   789
        }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   790
# endif
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   791
# ifdef CALLTYPE_FFI_V9
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   792
        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   793
            __callType = CALLTYPE_FFI_V9;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   794
        }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   795
# endif
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   796
# ifdef CALLTYPE_FFI_UNIX64
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   797
        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   798
            __callType = CALLTYPE_FFI_UNIX64;
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   799
        }
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   800
# endif
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   801
    }
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
#endif
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   803
    if (@global(ExternalFunctionCallback:Verbose) == true) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   804
        printf("prep_cif cif-ptr=%"_lx_"\n", (INT)cif);
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
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   807
    if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) {
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   808
        __FAIL__(@symbol(FFIPrepareFailed))
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   809
    }
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   810
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   811
    if (@global(ExternalFunctionCallback:Verbose) == true) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   812
        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
   813
        printf("index is %"_ld_"\n", __intVal(callBackIndex));
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   814
    }
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   815
    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
   816
        __FAIL__(@symbol(FFIPrepareClosureFailed))
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   817
    }
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   818
    if (@global(ExternalFunctionCallback:Verbose) == true) {
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   819
        printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif));
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   820
        printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun));
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   821
        printf("pcl code at %"_lx_" is:\n", (INT)pcl);
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   822
        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
   823
        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
   824
        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
   825
        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
   826
    }
14659
410089913ca1 allow use of the standard (system) ffi
Claus Gittinger <cg@exept.de>
parents: 14632
diff changeset
   827
    __INST(code_) = (OBJ)pcl;
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   828
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   829
#if 0
20307
678da26adf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 20291
diff changeset
   830
    ExternalFunctionCallback__test_call_closure((INTLFUNC)pcl);
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   831
#endif
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   832
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   833
#else /* no FFI support */
10618
343d0bf4646a Now can compile even #ifndef HAVE_FFI
Stefan Vogel <sv@exept.de>
parents: 10617
diff changeset
   834
    failureCode = @symbol(FFINotSupported);
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   835
#endif /* HAVE_FFI */
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   836
getOutOfHere: ;
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   837
%}.
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   838
    failureCode notNil ifTrue:[
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   839
        self primitiveFailed:(failureCode->failureInfo).   "see failureCode and failureInfo for details"
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   840
        ^ nil
10607
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   841
    ].
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   842
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   843
    "Created: / 11-06-2007 / 21:53:02 / cg"
9f42b83e653a *** empty log message ***
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   844
! !
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   845
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   846
!ExternalFunctionCallback methodsFor:'private-releasing'!
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   847
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   848
release
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   849
    |idx|
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
    idx := CallBackRegistry identityIndexOf:self.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   852
    CallBackRegistry at:idx put:nil.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   853
%{
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   854
    void *pcl = (void *)__INST(code_);
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   855
10613
12d012eeb755 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10611
diff changeset
   856
    __INST(code_) = 0;
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   857
    if (pcl) {
13574
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   858
	free(pcl);
58e5a69da7aa MAX_ARGS confusion
Claus Gittinger <cg@exept.de>
parents: 12475
diff changeset
   859
	RETURN(self);
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   860
    }
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   861
%}.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   862
    self invalidateReference.
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   863
! !
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   864
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   865
!ExternalFunctionCallback class methodsFor:'documentation'!
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   866
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   867
version
20123
0b0dd12ddb6d #OTHER by mawalch
mawalch
parents: 14754
diff changeset
   868
    ^ '$Header$'
12461
8dc128473734 __isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents: 11851
diff changeset
   869
!
8dc128473734 __isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents: 11851
diff changeset
   870
8dc128473734 __isByteArray() to __isByteArrayLike() in primitive code
Stefan Vogel <sv@exept.de>
parents: 11851
diff changeset
   871
version_CVS
20123
0b0dd12ddb6d #OTHER by mawalch
mawalch
parents: 14754
diff changeset
   872
    ^ '$Header$'
10609
fa629d528330 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 10607
diff changeset
   873
! !
22545
d5b6e2585870 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 21623
diff changeset
   874