ExternalFunctionCallback.st
changeset 22552 b14b3b47197e
parent 22551 cb0a07454324
child 24068 3add4c42c177
--- a/ExternalFunctionCallback.st	Mon Feb 19 17:20:15 2018 +0100
+++ b/ExternalFunctionCallback.st	Mon Feb 19 17:29:54 2018 +0100
@@ -115,26 +115,43 @@
 	argType = cif->arg_types[i];
 	if (argType == __get_ffi_type_sint()) {
 	    st_arg = __MKINT( *(int *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_uint()) {
 	    st_arg = __MKUINT( *(unsigned int *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_uint8()) {
 	    st_arg = __MKSMALLINT( *(unsigned char *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_sint8()) {
 	    st_arg = __MKSMALLINT( *(char *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_uint16()) {
 	    st_arg = __MKSMALLINT( *(unsigned short *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_sint16()) {
 	    st_arg = __MKSMALLINT( *(short *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_uint32()) {
 	    st_arg = __MKUINT( *(unsigned int *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_sint32()) {
 	    st_arg = __MKINT( *(int *)(args[i]) );
+
+	} else if (argType == __get_ffi_type_uint64()) {
+	    st_arg = __MKUINT( *(long unsigned int *)(args[i]) );
+
+	} else if (argType == __get_ffi_type_sint64()) {
+	    st_arg = __MKINT( *(long int *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_float()) {
 	    st_arg = __MKSFLOAT( *(float *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_double()) {
 	    st_arg = __MKFLOAT( *(double *)(args[i]) );
+
 	} else if (argType == __get_ffi_type_pointer()) {
 	    st_arg = __MKEXTERNALADDRESS( *(void **)(args[i]) );
+
 	} else {
 	    if (@global(ExternalFunctionCallback:Verbose) == true) {
 		fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid argument type %"_lx_" - arg %d\n", (INT)argType, i);
@@ -196,20 +213,28 @@
 
     if (retType == __get_ffi_type_sint()) {
 	*(int *)resp = sintResult;
+
     } else if (retType == __get_ffi_type_uint()) {
 	*(int *)resp = uintResult;
+
     } else if (retType == __get_ffi_type_uint8()) {
 	*(unsigned char *)resp = uintResult;
+
     } else if (retType == __get_ffi_type_sint8()) {
 	*(char *)resp = sintResult;
+
     } else if (retType == __get_ffi_type_uint16()) {
 	*(unsigned short *)resp = uintResult;
+
     } else if (retType == __get_ffi_type_sint16()) {
 	*(short *)resp = sintResult;
+
     } else if (retType == __get_ffi_type_uint32()) {
 	*(int *)resp = uintResult;
+
     } else if (retType == __get_ffi_type_sint32()) {
 	*(int *)resp = sintResult;
+
     } else if (retType == __get_ffi_type_float()) {
 	if (__isFloat(st_result)) {
 	    *(float *)resp = (float)__floatVal(st_result);
@@ -220,6 +245,7 @@
 		*(float *)resp = (float)sintResult;
 	    }
 	}
+
     } else if (retType == __get_ffi_type_double()) {
 	if (__isFloat(st_result)) {
 	    *(double *)resp = __floatVal(st_result);
@@ -230,8 +256,10 @@
 		*(double *)resp = (double)sintResult;
 	    }
 	}
+
     } else if (retType == __get_ffi_type_pointer()) {
 	*(void **)resp = (void *)__externalAddressVal( st_result );
+
     } else {
 	if (@global(ExternalFunctionCallback:Verbose) == true) {
 	    fprintf(stderr, "ExternalFunctionCallback(wrapper): invalid result type %"_ld_"\n", (INT)retType);
@@ -625,9 +653,9 @@
     int __numArgsWanted;
 
     struct closurePlusCIF {
-        ffi_closure closure;
-        ffi_cif cif;
-        ffi_type *argTypes[MAX_ARGS];
+	ffi_closure closure;
+	ffi_cif cif;
+	ffi_type *argTypes[MAX_ARGS];
     } *closurePlusCIFp;
     ffi_closure *pcl;
     ffi_cif *cif;
@@ -646,109 +674,109 @@
     argTypePtrs = closurePlusCIFp->argTypes;
 
 #   define __FAIL__(fcode) \
-        { \
-            failureCode = fcode; ffi_closure_free(closurePlusCIFp); goto getOutOfHere; \
-        }
+	{ \
+	    failureCode = fcode; ffi_closure_free(closurePlusCIFp); goto getOutOfHere; \
+	}
 
     if (argTypeSymbols == nil) {
-        __numArgsWanted = 0;
+	__numArgsWanted = 0;
     } else if (__isArrayLike(argTypeSymbols)) {
-        __numArgsWanted = __arraySize(argTypeSymbols);
+	__numArgsWanted = __arraySize(argTypeSymbols);
     } else {
-        __FAIL__(@symbol(BadArgumentTypeVector))
+	__FAIL__(@symbol(BadArgumentTypeVector))
     }
 
     if (__numArgsWanted > MAX_ARGS) {
-        __FAIL__(@symbol(TooManyArguments))
+	__FAIL__(@symbol(TooManyArguments))
     }
 
     /*
      * validate the return type
      */
     if (returnTypeSymbol == @symbol(voidPointer)) {
-        returnTypeSymbol = @symbol(handle);
+	returnTypeSymbol = @symbol(handle);
     }
 
     if (returnTypeSymbol == @symbol(int)) {
-        __returnType = __get_ffi_type_sint();
+	__returnType = __get_ffi_type_sint();
     } else if (returnTypeSymbol == @symbol(uint)) {
-        __returnType = __get_ffi_type_uint();
+	__returnType = __get_ffi_type_uint();
     } else if (returnTypeSymbol == @symbol(uint8)) {
-        __returnType = __get_ffi_type_uint8();
+	__returnType = __get_ffi_type_uint8();
     } else if (returnTypeSymbol == @symbol(uint16)) {
-        __returnType = __get_ffi_type_uint16();
+	__returnType = __get_ffi_type_uint16();
     } else if (returnTypeSymbol == @symbol(uint32)) {
-        __returnType = __get_ffi_type_uint32();
+	__returnType = __get_ffi_type_uint32();
     } else if (returnTypeSymbol == @symbol(uint64)) {
-        __returnType = __get_ffi_type_uint64();
+	__returnType = __get_ffi_type_uint64();
 
     } else if (returnTypeSymbol == @symbol(sint)) {
-        __returnType = __get_ffi_type_sint();
+	__returnType = __get_ffi_type_sint();
     } else if (returnTypeSymbol == @symbol(sint8)) {
-        __returnType = __get_ffi_type_sint8();
+	__returnType = __get_ffi_type_sint8();
     } else if (returnTypeSymbol == @symbol(sint16)) {
-        __returnType = __get_ffi_type_sint16();
+	__returnType = __get_ffi_type_sint16();
     } else if (returnTypeSymbol == @symbol(sint32)) {
-        __returnType = __get_ffi_type_sint32();
+	__returnType = __get_ffi_type_sint32();
     } else if (returnTypeSymbol == @symbol(sint64)) {
-        __returnType = __get_ffi_type_sint64();
+	__returnType = __get_ffi_type_sint64();
 
     } else if (returnTypeSymbol == @symbol(long)) {
-        if (sizeof(long) == 4) {
-           returnTypeSymbol = @symbol(sint32);
-           __returnType = __get_ffi_type_sint32();
-        } else if (sizeof(long) == 8) {
-           returnTypeSymbol = @symbol(sint64);
-           __returnType = __get_ffi_type_sint64();
-        } else {
-            __FAIL__(@symbol(UnknownReturnType))
-        }
+	if (sizeof(long) == 4) {
+	   returnTypeSymbol = @symbol(sint32);
+	   __returnType = __get_ffi_type_sint32();
+	} else if (sizeof(long) == 8) {
+	   returnTypeSymbol = @symbol(sint64);
+	   __returnType = __get_ffi_type_sint64();
+	} else {
+	    __FAIL__(@symbol(UnknownReturnType))
+	}
 
     } else if (returnTypeSymbol == @symbol(ulong)) {
-        if (sizeof(long) == 4) {
-           returnTypeSymbol = @symbol(uint32);
-           __returnType = __get_ffi_type_uint32();
-        }else if (sizeof(long) == 8) {
-           returnTypeSymbol = @symbol(uint64);
-           __returnType = __get_ffi_type_uint64();
-        } else {
-            __FAIL__(@symbol(UnknownReturnType))
-        }
+	if (sizeof(long) == 4) {
+	   returnTypeSymbol = @symbol(uint32);
+	   __returnType = __get_ffi_type_uint32();
+	}else if (sizeof(long) == 8) {
+	   returnTypeSymbol = @symbol(uint64);
+	   __returnType = __get_ffi_type_uint64();
+	} else {
+	    __FAIL__(@symbol(UnknownReturnType))
+	}
 
     } else if (returnTypeSymbol == @symbol(bool)) {
-        __returnType = __get_ffi_type_uint();
+	__returnType = __get_ffi_type_uint();
 
     } else if (returnTypeSymbol == @symbol(float)) {
-        __returnType = __get_ffi_type_float();
+	__returnType = __get_ffi_type_float();
     } else if (returnTypeSymbol == @symbol(double)) {
-        __returnType = __get_ffi_type_double();
+	__returnType = __get_ffi_type_double();
 
     } else if (returnTypeSymbol == @symbol(void)) {
-        __returnType = __get_ffi_type_void();
+	__returnType = __get_ffi_type_void();
     } else if ((returnTypeSymbol == @symbol(pointer))
-               || (returnTypeSymbol == @symbol(handle))
-               || (returnTypeSymbol == @symbol(charPointer))
-               || (returnTypeSymbol == @symbol(bytePointer))
-               || (returnTypeSymbol == @symbol(floatPointer))
-               || (returnTypeSymbol == @symbol(doublePointer))
-               || (returnTypeSymbol == @symbol(intPointer))
-               || (returnTypeSymbol == @symbol(shortPointer))
-               || (returnTypeSymbol == @symbol(wcharPointer))) {
-        __returnType = __get_ffi_type_pointer();
+	       || (returnTypeSymbol == @symbol(handle))
+	       || (returnTypeSymbol == @symbol(charPointer))
+	       || (returnTypeSymbol == @symbol(bytePointer))
+	       || (returnTypeSymbol == @symbol(floatPointer))
+	       || (returnTypeSymbol == @symbol(doublePointer))
+	       || (returnTypeSymbol == @symbol(intPointer))
+	       || (returnTypeSymbol == @symbol(shortPointer))
+	       || (returnTypeSymbol == @symbol(wcharPointer))) {
+	__returnType = __get_ffi_type_pointer();
     } else {
-        if (__isSymbol(returnTypeSymbol)
-         && ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) {
-            if (! __isBehaviorLike(returnValueClass)) {
-                __FAIL__(@symbol(NonBehaviorReturnType))
-            }
-            if (! __qIsSubclassOfExternalAddress(returnValueClass)) {
-                __FAIL__(@symbol(NonExternalAddressReturnType))
-            }
-            __returnType = __get_ffi_type_pointer();
-            returnTypeSymbol = @symbol(pointer);
-        } else {
-            __FAIL__(@symbol(UnknownReturnType))
-        }
+	if (__isSymbol(returnTypeSymbol)
+	 && ((returnValueClass = __GLOBAL_GET(returnTypeSymbol)) != nil)) {
+	    if (! __isBehaviorLike(returnValueClass)) {
+		__FAIL__(@symbol(NonBehaviorReturnType))
+	    }
+	    if (! __qIsSubclassOfExternalAddress(returnValueClass)) {
+		__FAIL__(@symbol(NonExternalAddressReturnType))
+	    }
+	    __returnType = __get_ffi_type_pointer();
+	    returnTypeSymbol = @symbol(pointer);
+	} else {
+	    __FAIL__(@symbol(UnknownReturnType))
+	}
     }
 
     /*
@@ -804,13 +832,13 @@
             thisType = __get_ffi_type_uint16();
         } else if (typeSymbol == @symbol(sint16)) {
             thisType = __get_ffi_type_sint16();
-        } else if ((typeSymbol == @symbol(uint32)) {
+        } else if (typeSymbol == @symbol(uint32)) {
             thisType = __get_ffi_type_uint32();
-        } else if ((typeSymbol == @symbol(sint32))) {
+        } else if (typeSymbol == @symbol(sint32)) {
             thisType = __get_ffi_type_sint32();
-        } else if ((typeSymbol == @symbol(uint64)) {
+        } else if (typeSymbol == @symbol(uint64)) {
             thisType = __get_ffi_type_uint64();
-        } else if ((typeSymbol == @symbol(sint64))) {
+        } else if (typeSymbol == @symbol(sint64)) {
             thisType = __get_ffi_type_sint64();
 
         } else if (typeSymbol == @symbol(float)) {
@@ -835,23 +863,23 @@
             thisType = __get_ffi_type_pointer();
         } else if (typeSymbol == @symbol(pointer)) {
 commonPointerTypeArg: ;
-            thisType = __get_ffi_type_pointer();
-        } else if (typeSymbol == @symbol(bool)) {
-            thisType = __get_ffi_type_uint();
-        } else if (__isSymbol(typeSymbol)
-             && ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) {
-            if (! __isBehaviorLike(argValueClass)) {
-                __FAIL__(@symbol(NonBehaviorArgumentType))
-            }
-            if (! __qIsSubclassOfExternalAddress(argValueClass)) {
-                __FAIL__(@symbol(NonExternalAddressArgumentType))
-            }
-            goto commonPointerTypeArg; /* sorry */
-        } else {
-            __FAIL__(@symbol(UnknownArgumentType))
-        }
+	    thisType = __get_ffi_type_pointer();
+	} else if (typeSymbol == @symbol(bool)) {
+	    thisType = __get_ffi_type_uint();
+	} else if (__isSymbol(typeSymbol)
+	     && ((argValueClass = __GLOBAL_GET(typeSymbol)) != nil)) {
+	    if (! __isBehaviorLike(argValueClass)) {
+		__FAIL__(@symbol(NonBehaviorArgumentType))
+	    }
+	    if (! __qIsSubclassOfExternalAddress(argValueClass)) {
+		__FAIL__(@symbol(NonExternalAddressArgumentType))
+	    }
+	    goto commonPointerTypeArg; /* sorry */
+	} else {
+	    __FAIL__(@symbol(UnknownArgumentType))
+	}
 
-        argTypePtrs[i] = thisType;
+	argTypePtrs[i] = thisType;
     }
     failureInfo = nil;
 
@@ -859,24 +887,24 @@
 #ifndef __osx__
     if (callTypeNumber != nil) {
 # ifdef CALLTYPE_FFI_STDCALL
-        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) {
-            __callType = CALLTYPE_FFI_STDCALL;
-        }
+	if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_API)) {
+	    __callType = CALLTYPE_FFI_STDCALL;
+	}
 # endif
 # ifdef CALLTYPE_FFI_V8
-        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) {
-            __callType = CALLTYPE_FFI_V8;
-        }
+	if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V8)) {
+	    __callType = CALLTYPE_FFI_V8;
+	}
 # endif
 # ifdef CALLTYPE_FFI_V9
-        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) {
-            __callType = CALLTYPE_FFI_V9;
-        }
+	if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_V9)) {
+	    __callType = CALLTYPE_FFI_V9;
+	}
 # endif
 # ifdef CALLTYPE_FFI_UNIX64
-        if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) {
-            __callType = CALLTYPE_FFI_UNIX64;
-        }
+	if (callTypeNumber == @global(ExternalLibraryFunction:CALLTYPE_UNIX64)) {
+	    __callType = CALLTYPE_FFI_UNIX64;
+	}
 # endif
     }
 #endif
@@ -885,30 +913,30 @@
     }
 
     if (ffi_prep_cif(cif, __callType, __numArgsWanted, __returnType, argTypePtrs) != FFI_OK) {
-        __FAIL__(@symbol(FFIPrepareFailed))
+	__FAIL__(@symbol(FFIPrepareFailed))
     }
 
     if (@global(ExternalFunctionCallback:Verbose) == true) {
-        printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure));
-        printf("index is %"_ld_"\n", __intVal(callBackIndex));
+	printf("closure is 0x%"_lx_" (%d bytes)\n", (INT)pcl, (int)sizeof(ffi_closure));
+	printf("index is %"_ld_"\n", __intVal(callBackIndex));
     }
 #ifndef NEW_FFI
     if (ffi_prep_closure(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)) /* userdata */) != FFI_OK) {
-        __FAIL__(@symbol(FFIPrepareClosureFailed))
+	__FAIL__(@symbol(FFIPrepareClosureFailed))
     }
 #else
     if (ffi_prep_closure_loc(pcl, cif, ExternalFunctionCallback__closure_wrapper_fn, (void *)(__intVal(callBackIndex)), codePtr) != FFI_OK) {
-        __FAIL__(@symbol(FFIPrepareClosureFailed))
+	__FAIL__(@symbol(FFIPrepareClosureFailed))
     }
 #endif
     if (@global(ExternalFunctionCallback:Verbose) == true) {
-        printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif));
-        printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun));
-        printf("pcl code at %"_lx_" is:\n", (INT)pcl);
-        printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]);
-        printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]);
-        printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]);
-        printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]);
+	printf("pcl->cif is 0x%"_lx_"\n", (INT)(pcl->cif));
+	printf("pcl->fun is 0x%"_lx_"\n", (INT)(pcl->fun));
+	printf("pcl code at %"_lx_" is:\n", (INT)pcl);
+	printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[0],((unsigned char *)pcl)[1],((unsigned char *)pcl)[2],((unsigned char *)pcl)[3]);
+	printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[4],((unsigned char *)pcl)[5],((unsigned char *)pcl)[6],((unsigned char *)pcl)[7]);
+	printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[8],((unsigned char *)pcl)[9],((unsigned char *)pcl)[10],((unsigned char *)pcl)[11]);
+	printf("  %02x %02x %02x %02x\n", ((unsigned char *)pcl)[12],((unsigned char *)pcl)[13],((unsigned char *)pcl)[14],((unsigned char *)pcl)[15]);
     }
     __INST(code_) = (OBJ)pcl;
 
@@ -922,8 +950,8 @@
 getOutOfHere: ;
 %}.
     failureCode notNil ifTrue:[
-        self primitiveFailed:(failureCode->failureInfo).   "see failureCode and failureInfo for details"
-        ^ nil
+	self primitiveFailed:(failureCode->failureInfo).   "see failureCode and failureInfo for details"
+	^ nil
     ].
 
     "Created: / 11-06-2007 / 21:53:02 / cg"
@@ -957,4 +985,3 @@
 version_CVS
     ^ '$Header$'
 ! !
-