ExternalFunctionCallback.st
changeset 24068 3add4c42c177
parent 22552 b14b3b47197e
child 24070 85b7952e1260
--- a/ExternalFunctionCallback.st	Fri Apr 12 12:11:46 2019 +0200
+++ b/ExternalFunctionCallback.st	Sat Apr 13 06:26:09 2019 +0200
@@ -27,6 +27,14 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#define VERBOSE
+
+#ifdef VERBOSE
+#  define DEBUGCODE_IF(flag, code) if ((flag) == true) {  code }
+# else
+#  define DEBUG_IF(flag, code) /* nothing */
+# endif
+
 #ifdef HAVE_FFI
 # include <ffi.h>
 # define MAX_ARGS    128
@@ -47,20 +55,20 @@
 #  define __get_ffi_type_void() &ffi_type_void
 #  define __get_ffi_type_pointer() &ffi_type_pointer
 # else
-extern ffi_type *__get_ffi_type_sint();
-extern ffi_type *__get_ffi_type_sint8();
-extern ffi_type *__get_ffi_type_sint16();
-extern ffi_type *__get_ffi_type_sint32();
-extern ffi_type *__get_ffi_type_sint64();
-extern ffi_type *__get_ffi_type_uint();
-extern ffi_type *__get_ffi_type_uint8();
-extern ffi_type *__get_ffi_type_uint16();
-extern ffi_type *__get_ffi_type_uint32();
-extern ffi_type *__get_ffi_type_uint64();
-extern ffi_type *__get_ffi_type_float();
-extern ffi_type *__get_ffi_type_double();
-extern ffi_type *__get_ffi_type_void();
-extern ffi_type *__get_ffi_type_pointer();
+   extern ffi_type *__get_ffi_type_sint();
+   extern ffi_type *__get_ffi_type_sint8();
+   extern ffi_type *__get_ffi_type_sint16();
+   extern ffi_type *__get_ffi_type_sint32();
+   extern ffi_type *__get_ffi_type_sint64();
+   extern ffi_type *__get_ffi_type_uint();
+   extern ffi_type *__get_ffi_type_uint8();
+   extern ffi_type *__get_ffi_type_uint16();
+   extern ffi_type *__get_ffi_type_uint32();
+   extern ffi_type *__get_ffi_type_uint64();
+   extern ffi_type *__get_ffi_type_float();
+   extern ffi_type *__get_ffi_type_double();
+   extern ffi_type *__get_ffi_type_void();
+   extern ffi_type *__get_ffi_type_pointer();
 # endif
 
 #endif
@@ -81,7 +89,6 @@
 %{
 
 #ifdef HAVE_FFI
-#define xxVERBOSE
 
 void
 ExternalFunctionCallback__closure_wrapper_fn(ffi_cif* cif, void* resp, void** args, void* userdata)
@@ -138,10 +145,10 @@
 	    st_arg = __MKINT( *(int *)(args[i]) );
 
 	} else if (argType == __get_ffi_type_uint64()) {
-	    st_arg = __MKUINT( *(long unsigned int *)(args[i]) );
+	    st_arg = __MKUINT( *(unsigned long long *)(args[i]) );
 
 	} else if (argType == __get_ffi_type_sint64()) {
-	    st_arg = __MKINT( *(long int *)(args[i]) );
+	    st_arg = __MKINT( *(long long *)(args[i]) );
 
 	} else if (argType == __get_ffi_type_float()) {
 	    st_arg = __MKSFLOAT( *(float *)(args[i]) );
@@ -245,7 +252,6 @@
 		*(float *)resp = (float)sintResult;
 	    }
 	}
-
     } else if (retType == __get_ffi_type_double()) {
 	if (__isFloat(st_result)) {
 	    *(double *)resp = __floatVal(st_result);
@@ -317,62 +323,62 @@
     and handed out to C. (you can also hand out the callBack directly - as it is a subclass of
     ExternalBytes.
     The actual action of the callback can be changed (at any time later) with:
-        cb action:[:args | Transcript showCR:args. true].
+	cb action:[:args | Transcript showCR:args. true].
 
     Eventually, the callback MUST be released:
-        cb release.
+	cb release.
 
     [supported returnTypes:]
-        int
-        uint
-        uint8
-        uint16
-        uint32
-        uint64
-        sint
-        sint8
-        sint16
-        sint32
-        sint64
-        long    system dependent sint32 or sint64
-        ulong   system dependent uint32 or uint64
+	int
+	uint
+	uint8
+	uint16
+	uint32
+	uint64
+	sint
+	sint8
+	sint16
+	sint32
+	sint64
+	long    system dependent sint32 or sint64
+	ulong   system dependent uint32 or uint64
 
-        bool
-        float
-        double
-        void
+	bool
+	float
+	double
+	void
 
-        pointer, 
-        handle, 
-        charPointer, 
-        bytePointer, 
-        floatPointer
-        doublePointer
-        intPointer
-        shortPointer
-        wcharPointer
+	pointer,
+	handle,
+	charPointer,
+	bytePointer,
+	floatPointer
+	doublePointer
+	intPointer
+	shortPointer
+	wcharPointer
 
-        <name of subclass of ExternalAddress>
+	<name of subclass of ExternalAddress>
 
     [supported argumentTypes:]
-        handle
-        pointer
-        voidPointer
+	handle
+	pointer
+	voidPointer
 
-        long, ulong - system dependent
+	long, ulong - system dependent
 
-        int, 
-        uint8, sint8, uint16, sint16,
-        uint32, sint32,
-        float, double
-        void
+	int,
+	uint8, sint8, uint16, sint16,
+	uint32, sint32,
+	float, double
+	void
 
-        charPointer, floatPointer, doublePointer,
+	charPointer, floatPointer, doublePointer,
 
-        <name of subclass of ExternalAddress>
+	<name of subclass of ExternalAddress>
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 "
 !
 
@@ -663,12 +669,18 @@
 
     void* codePtr;
 
-#ifndef NEW_FFI
+# ifndef NEW_FFI
     closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF));
     codePtr = &(closurePlusCIFp->closure);
-#else
+    DEBUGCODE_IF( @global(Verbose), {
+	printf("old ffi\n");
+    })
+# else
     closurePlusCIFp = (struct closurePlusCIF *) ffi_closure_alloc(sizeof(struct closurePlusCIF), &codePtr);
-#endif
+    DEBUGCODE_IF( @global(Verbose), {
+	printf("new ffi\n");
+    })
+# endif
     pcl = &(closurePlusCIFp->closure);
     cif = &(closurePlusCIFp->cif);
     argTypePtrs = closurePlusCIFp->argTypes;
@@ -778,90 +790,93 @@
 	    __FAIL__(@symbol(UnknownReturnType))
 	}
     }
+    DEBUGCODE_IF( @global(Verbose), {
+	printf("__returnType: %p\n", __returnType);
+    })
 
     /*
      * setup arg-buffers
      */
     for (i=0; i<__numArgsWanted; i++) {
-        ffi_type *thisType;
-        void *argValuePtr;
-        OBJ typeSymbol;
+	ffi_type *thisType;
+	void *argValuePtr;
+	OBJ typeSymbol;
 
-        failureInfo = __mkSmallInteger(i+1);   /* in case there is one */
+	failureInfo = __mkSmallInteger(i+1);   /* in case there is one */
 
-        typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i];
+	typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i];
 
-        if (typeSymbol == @symbol(handle)) {
-            typeSymbol = @symbol(pointer);
-        } else if (typeSymbol == @symbol(voidPointer)) {
-            typeSymbol = @symbol(pointer);
-        }
+	if (typeSymbol == @symbol(handle)) {
+	    typeSymbol = @symbol(pointer);
+	} else if (typeSymbol == @symbol(voidPointer)) {
+	    typeSymbol = @symbol(pointer);
+	}
 
-        if (typeSymbol == @symbol(long)) {
-            if (sizeof(long) == sizeof(int)) {
-                typeSymbol = @symbol(sint);
-            } else {
-                if (sizeof(long) == 4) {
-                    typeSymbol = @symbol(sint32);
-                } else if (sizeof(long) == 8) {
-                    typeSymbol = @symbol(sint64);
-                }
-            }
-        }
-        if (typeSymbol == @symbol(ulong)) {
-            if (sizeof(unsigned long) == sizeof(unsigned int)) {
-                typeSymbol = @symbol(uint);
-            } else {
-                if (sizeof(long) == 4) {
-                    typeSymbol = @symbol(uint32);
-                } else if (sizeof(long) == 8) {
-                    typeSymbol = @symbol(uint64);
-                }
-            }
-        }
+	if (typeSymbol == @symbol(long)) {
+	    if (sizeof(long) == sizeof(int)) {
+		typeSymbol = @symbol(sint);
+	    } else {
+		if (sizeof(long) == 4) {
+		    typeSymbol = @symbol(sint32);
+		} else if (sizeof(long) == 8) {
+		    typeSymbol = @symbol(sint64);
+		}
+	    }
+	}
+	if (typeSymbol == @symbol(ulong)) {
+	    if (sizeof(unsigned long) == sizeof(unsigned int)) {
+		typeSymbol = @symbol(uint);
+	    } else {
+		if (sizeof(long) == 4) {
+		    typeSymbol = @symbol(uint32);
+		} else if (sizeof(long) == 8) {
+		    typeSymbol = @symbol(uint64);
+		}
+	    }
+	}
 
-        if (typeSymbol == @symbol(int)) {
-            thisType = __get_ffi_type_sint();
-        } else if (typeSymbol == @symbol(uint)) {
-            thisType = __get_ffi_type_uint();
-        } else if (typeSymbol == @symbol(uint8)) {
-            thisType = __get_ffi_type_uint8();
-        } else if (typeSymbol == @symbol(sint8)) {
-            thisType = __get_ffi_type_sint8();
-        } else if (typeSymbol == @symbol(uint16)) {
-            thisType = __get_ffi_type_uint16();
-        } else if (typeSymbol == @symbol(sint16)) {
-            thisType = __get_ffi_type_sint16();
-        } else if (typeSymbol == @symbol(uint32)) {
-            thisType = __get_ffi_type_uint32();
-        } else if (typeSymbol == @symbol(sint32)) {
-            thisType = __get_ffi_type_sint32();
-        } else if (typeSymbol == @symbol(uint64)) {
-            thisType = __get_ffi_type_uint64();
-        } else if (typeSymbol == @symbol(sint64)) {
-            thisType = __get_ffi_type_sint64();
+	if (typeSymbol == @symbol(int)) {
+	    thisType = __get_ffi_type_sint();
+	} else if (typeSymbol == @symbol(uint)) {
+	    thisType = __get_ffi_type_uint();
+	} else if (typeSymbol == @symbol(uint8)) {
+	    thisType = __get_ffi_type_uint8();
+	} else if (typeSymbol == @symbol(sint8)) {
+	    thisType = __get_ffi_type_sint8();
+	} else if (typeSymbol == @symbol(uint16)) {
+	    thisType = __get_ffi_type_uint16();
+	} else if (typeSymbol == @symbol(sint16)) {
+	    thisType = __get_ffi_type_sint16();
+	} else if (typeSymbol == @symbol(uint32)) {
+	    thisType = __get_ffi_type_uint32();
+	} else if (typeSymbol == @symbol(sint32)) {
+	    thisType = __get_ffi_type_sint32();
+	} else if (typeSymbol == @symbol(uint64)) {
+	    thisType = __get_ffi_type_uint64();
+	} else if (typeSymbol == @symbol(sint64)) {
+	    thisType = __get_ffi_type_sint64();
 
-        } else if (typeSymbol == @symbol(float)) {
-            thisType = __get_ffi_type_float();
-        } else if (typeSymbol == @symbol(double)) {
-            thisType = __get_ffi_type_double();
-        } else if (typeSymbol == @symbol(void)) {
-            thisType = __get_ffi_type_void();
-        } else if (typeSymbol == @symbol(charPointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(floatPointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(doublePointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(intPointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(bytePointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(shortPointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(wcharPointer)) {
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(pointer)) {
+	} else if (typeSymbol == @symbol(float)) {
+	    thisType = __get_ffi_type_float();
+	} else if (typeSymbol == @symbol(double)) {
+	    thisType = __get_ffi_type_double();
+	} else if (typeSymbol == @symbol(void)) {
+	    thisType = __get_ffi_type_void();
+	} else if (typeSymbol == @symbol(charPointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(floatPointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(doublePointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(intPointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(bytePointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(shortPointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(wcharPointer)) {
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(pointer)) {
 commonPointerTypeArg: ;
 	    thisType = __get_ffi_type_pointer();
 	} else if (typeSymbol == @symbol(bool)) {
@@ -883,6 +898,10 @@
     }
     failureInfo = nil;
 
+    DEBUGCODE_IF( @global(Verbose), {
+	printf("got argTypes\n");
+    })
+
     __callType = FFI_DEFAULT_ABI;
 #ifndef __osx__
     if (callTypeNumber != nil) {
@@ -909,7 +928,7 @@
     }
 #endif
     if (@global(ExternalFunctionCallback:Verbose) == true) {
-        printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (void*)cif);
+	printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (void*)cif);
     }
 
     if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) {
@@ -955,6 +974,7 @@
     ].
 
     "Created: / 11-06-2007 / 21:53:02 / cg"
+    "Modified: / 12-04-2019 / 11:26:53 / Claus Gittinger"
 ! !
 
 !ExternalFunctionCallback methodsFor:'private-releasing'!