Small changes in casts from void* to functions

Macro moved to llimits.h, and casts from void* to lua_CFunction first
go through 'voidf' (a pointer to a function from void to void), a kind
of void* for functions.
This commit is contained in:
Roberto Ierusalimschy 2024-07-02 11:09:46 -03:00
parent d71fbc3175
commit 781219dbe1
2 changed files with 26 additions and 22 deletions

View File

@ -152,6 +152,26 @@ typedef LUAI_UACINT l_uacInt;
#endif #endif
/*
** Special type equivalent to '(void*)' for functions (to suppress some
** warnings when converting function pointers)
*/
typedef void (*voidf)(void);
/*
** Macro to convert pointer-to-void* to pointer-to-function. This cast
** is undefined according to ISO C, but POSIX assumes that it works.
** (The '__extension__' in gnu compilers is only to avoid warnings.)
*/
#if defined(__GNUC__)
#define cast_func(p) (__extension__ (voidf)(p))
#else
#define cast_func(p) ((voidf)(p))
#endif
/* /*
** non-return type ** non-return type
*/ */

View File

@ -59,11 +59,8 @@ static const char *const CLIBS = "_CLIBS";
#define setprogdir(L) ((void)0) #define setprogdir(L) ((void)0)
/* /* cast void* to a Lua function */
** Special type equivalent to '(void*)' for functions in gcc #define cast_Lfunc(p) cast(lua_CFunction, cast_func(p))
** (to suppress warnings when converting function pointers)
*/
typedef void (*voidf)(void);
/* /*
@ -96,26 +93,13 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
#if defined(LUA_USE_DLOPEN) /* { */ #if defined(LUA_USE_DLOPEN) /* { */
/* /*
** {======================================================================== ** {========================================================================
** This is an implementation of loadlib based on the dlfcn interface. ** This is an implementation of loadlib based on the dlfcn interface,
** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, ** which is available in all POSIX systems.
** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least
** as an emulation layer on top of native functions.
** ========================================================================= ** =========================================================================
*/ */
#include <dlfcn.h> #include <dlfcn.h>
/*
** Macro to convert pointer-to-void* to pointer-to-function. This cast
** is undefined according to ISO C, but POSIX assumes that it works.
** (The '__extension__' in gnu compilers is only to avoid warnings.)
*/
#if defined(__GNUC__)
#define cast_func(p) (__extension__ (lua_CFunction)(p))
#else
#define cast_func(p) ((lua_CFunction)(p))
#endif
static void lsys_unloadlib (void *lib) { static void lsys_unloadlib (void *lib) {
dlclose(lib); dlclose(lib);
@ -131,7 +115,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
lua_CFunction f = cast_func(dlsym(lib, sym)); lua_CFunction f = cast_Lfunc(dlsym(lib, sym));
if (l_unlikely(f == NULL)) if (l_unlikely(f == NULL))
lua_pushstring(L, dlerror()); lua_pushstring(L, dlerror());
return f; return f;
@ -207,7 +191,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) {
static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) {
lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym));
if (f == NULL) pusherror(L); if (f == NULL) pusherror(L);
return f; return f;
} }