#DOCUMENTATION by cg
authorClaus Gittinger <cg@exept.de>
Mon, 19 Feb 2018 17:20:15 +0100
changeset 22551 cb0a07454324
parent 22550 35e87af3c989
child 22552 b14b3b47197e
#DOCUMENTATION by cg class: ExternalFunctionCallback changed: #generateClosure class: ExternalFunctionCallback class comment/format in: #documentation
ExternalFunctionCallback.st
--- a/ExternalFunctionCallback.st	Mon Feb 19 16:37:26 2018 +0100
+++ b/ExternalFunctionCallback.st	Mon Feb 19 17:20:15 2018 +0100
@@ -65,6 +65,15 @@
 
 #endif
 
+#ifdef __osx__
+# define NEW_FFI
+#endif
+
+#ifndef NEW_FFI
+# define ffi_closure_alloc  malloc
+# define ffi_closure_free   free
+#endif
+
 %}
 ! !
 
@@ -280,13 +289,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
+
+        bool
+        float
+        double
+        void
+
+        pointer, 
+        handle, 
+        charPointer, 
+        bytePointer, 
+        floatPointer
+        doublePointer
+        intPointer
+        shortPointer
+        wcharPointer
+
+        <name of subclass of ExternalAddress>
+
+    [supported argumentTypes:]
+        handle
+        pointer
+        voidPointer
+
+        long, ulong - system dependent
+
+        int, 
+        uint8, sint8, uint16, sint16,
+        uint32, sint32,
+        float, double
+        void
+
+        charPointer, floatPointer, doublePointer,
+
+        <name of subclass of ExternalAddress>
 
     [author:]
-	Claus Gittinger
+        Claus Gittinger
 "
 !
 
@@ -565,7 +623,7 @@
     int i;
     ffi_abi __callType = FFI_DEFAULT_ABI;
     int __numArgsWanted;
-#ifdef OLD_FFI
+
     struct closurePlusCIF {
         ffi_closure closure;
         ffi_cif cif;
@@ -574,33 +632,24 @@
     ffi_closure *pcl;
     ffi_cif *cif;
     ffi_type **argTypePtrs;
-#else
-    ffi_closure *closure;
+
     void* codePtr;
-    ffi_cif _cif;
-    ffi_cif *cif = &_cif;
-    ffi_type *argTypes[MAX_ARGS];
-#endif
 
-#ifdef OLD_FFI
+#ifndef NEW_FFI
     closurePlusCIFp = (struct closurePlusCIF *) malloc(sizeof(struct closurePlusCIF));
     codePtr = &(closurePlusCIFp->closure);
-#   define __FAIL__(fcode) \
-        { \
-            failureCode = fcode; free(closurePlusCIFp); goto getOutOfHere; \
-        }
-
 #else
     closurePlusCIFp = (struct closurePlusCIF *) ffi_closure_alloc(sizeof(struct closurePlusCIF), &codePtr);
-#   define __FAIL__(fcode) \
-        { \
-            failureCode = fcode; ffi_closure_free(closure); goto getOutOfHere; \
-        }
 #endif
     pcl = &(closurePlusCIFp->closure);
     cif = &(closurePlusCIFp->cif);
     argTypePtrs = closurePlusCIFp->argTypes;
 
+#   define __FAIL__(fcode) \
+        { \
+            failureCode = fcode; ffi_closure_free(closurePlusCIFp); goto getOutOfHere; \
+        }
+
     if (argTypeSymbols == nil) {
         __numArgsWanted = 0;
     } else if (__isArrayLike(argTypeSymbols)) {
@@ -755,8 +804,15 @@
             thisType = __get_ffi_type_uint16();
         } else if (typeSymbol == @symbol(sint16)) {
             thisType = __get_ffi_type_sint16();
-        } else if ((typeSymbol == @symbol(uint32)) || (typeSymbol == @symbol(sint32))) {
+        } 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)) {
@@ -769,6 +825,14 @@
             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();
@@ -817,7 +881,7 @@
     }
 #endif
     if (@global(ExternalFunctionCallback:Verbose) == true) {
-        printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (INT)cif);
+        printf("prep_cif callType:%d cif-ptr=%p\n", __callType, (void*)cif);
     }
 
     if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) {
@@ -828,12 +892,12 @@
         printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure));
         printf("index is %"_ld_"\n", __intVal(callBackIndex));
     }
-#ifdef OLD_FFI
+#ifndef NEW_FFI
     if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) {
         __FAIL__(@symbol(FFIPrepareClosureFailed))
     }
 #else
-    if (ffi_prep_closure_loc(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex), codePtr)) != FFI_OK) {
+    if (ffi_prep_closure_loc(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)), codePtr) != FFI_OK) {
         __FAIL__(@symbol(FFIPrepareClosureFailed))
     }
 #endif
@@ -877,7 +941,7 @@
 
     __INST(code_) = 0;
     if (pcl) {
-	free(pcl);
+	ffi_closure_free(pcl);
 	RETURN(self);
     }
 %}.