diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-02 11:09:46 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-07-02 11:09:46 -0300 |
| commit | 781219dbe16fc327f5b828e1ff6fa45ec3265cba (patch) | |
| tree | 2dfec8c7e051d41f436278df976ca2e5e0303704 | |
| parent | d71fbc3175d3f1f9dff89edc3f04cd20447fe091 (diff) | |
| download | lua-781219dbe16fc327f5b828e1ff6fa45ec3265cba.tar.gz lua-781219dbe16fc327f5b828e1ff6fa45ec3265cba.tar.bz2 lua-781219dbe16fc327f5b828e1ff6fa45ec3265cba.zip | |
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.
| -rw-r--r-- | llimits.h | 20 | ||||
| -rw-r--r-- | loadlib.c | 28 |
2 files changed, 26 insertions, 22 deletions
| @@ -153,6 +153,26 @@ typedef LUAI_UACINT l_uacInt; | |||
| 153 | 153 | ||
| 154 | 154 | ||
| 155 | /* | 155 | /* |
| 156 | ** Special type equivalent to '(void*)' for functions (to suppress some | ||
| 157 | ** warnings when converting function pointers) | ||
| 158 | */ | ||
| 159 | typedef void (*voidf)(void); | ||
| 160 | |||
| 161 | |||
| 162 | /* | ||
| 163 | ** Macro to convert pointer-to-void* to pointer-to-function. This cast | ||
| 164 | ** is undefined according to ISO C, but POSIX assumes that it works. | ||
| 165 | ** (The '__extension__' in gnu compilers is only to avoid warnings.) | ||
| 166 | */ | ||
| 167 | #if defined(__GNUC__) | ||
| 168 | #define cast_func(p) (__extension__ (voidf)(p)) | ||
| 169 | #else | ||
| 170 | #define cast_func(p) ((voidf)(p)) | ||
| 171 | #endif | ||
| 172 | |||
| 173 | |||
| 174 | |||
| 175 | /* | ||
| 156 | ** non-return type | 176 | ** non-return type |
| 157 | */ | 177 | */ |
| 158 | #if !defined(l_noret) | 178 | #if !defined(l_noret) |
| @@ -59,11 +59,8 @@ static const char *const CLIBS = "_CLIBS"; | |||
| 59 | #define setprogdir(L) ((void)0) | 59 | #define setprogdir(L) ((void)0) |
| 60 | 60 | ||
| 61 | 61 | ||
| 62 | /* | 62 | /* cast void* to a Lua function */ |
| 63 | ** Special type equivalent to '(void*)' for functions in gcc | 63 | #define cast_Lfunc(p) cast(lua_CFunction, cast_func(p)) |
| 64 | ** (to suppress warnings when converting function pointers) | ||
| 65 | */ | ||
| 66 | typedef void (*voidf)(void); | ||
| 67 | 64 | ||
| 68 | 65 | ||
| 69 | /* | 66 | /* |
| @@ -96,26 +93,13 @@ static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym); | |||
| 96 | #if defined(LUA_USE_DLOPEN) /* { */ | 93 | #if defined(LUA_USE_DLOPEN) /* { */ |
| 97 | /* | 94 | /* |
| 98 | ** {======================================================================== | 95 | ** {======================================================================== |
| 99 | ** This is an implementation of loadlib based on the dlfcn interface. | 96 | ** This is an implementation of loadlib based on the dlfcn interface, |
| 100 | ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD, | 97 | ** which is available in all POSIX systems. |
| 101 | ** NetBSD, AIX 4.2, HPUX 11, and probably most other Unix flavors, at least | ||
| 102 | ** as an emulation layer on top of native functions. | ||
| 103 | ** ========================================================================= | 98 | ** ========================================================================= |
| 104 | */ | 99 | */ |
| 105 | 100 | ||
| 106 | #include <dlfcn.h> | 101 | #include <dlfcn.h> |
| 107 | 102 | ||
| 108 | /* | ||
| 109 | ** Macro to convert pointer-to-void* to pointer-to-function. This cast | ||
| 110 | ** is undefined according to ISO C, but POSIX assumes that it works. | ||
| 111 | ** (The '__extension__' in gnu compilers is only to avoid warnings.) | ||
| 112 | */ | ||
| 113 | #if defined(__GNUC__) | ||
| 114 | #define cast_func(p) (__extension__ (lua_CFunction)(p)) | ||
| 115 | #else | ||
| 116 | #define cast_func(p) ((lua_CFunction)(p)) | ||
| 117 | #endif | ||
| 118 | |||
| 119 | 103 | ||
| 120 | static void lsys_unloadlib (void *lib) { | 104 | static void lsys_unloadlib (void *lib) { |
| 121 | dlclose(lib); | 105 | dlclose(lib); |
| @@ -131,7 +115,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { | |||
| 131 | 115 | ||
| 132 | 116 | ||
| 133 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { | 117 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { |
| 134 | lua_CFunction f = cast_func(dlsym(lib, sym)); | 118 | lua_CFunction f = cast_Lfunc(dlsym(lib, sym)); |
| 135 | if (l_unlikely(f == NULL)) | 119 | if (l_unlikely(f == NULL)) |
| 136 | lua_pushstring(L, dlerror()); | 120 | lua_pushstring(L, dlerror()); |
| 137 | return f; | 121 | return f; |
| @@ -207,7 +191,7 @@ static void *lsys_load (lua_State *L, const char *path, int seeglb) { | |||
| 207 | 191 | ||
| 208 | 192 | ||
| 209 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { | 193 | static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { |
| 210 | lua_CFunction f = (lua_CFunction)(voidf)GetProcAddress((HMODULE)lib, sym); | 194 | lua_CFunction f = cast_Lfunc(GetProcAddress((HMODULE)lib, sym)); |
| 211 | if (f == NULL) pusherror(L); | 195 | if (f == NULL) pusherror(L); |
| 212 | return f; | 196 | return f; |
| 213 | } | 197 | } |
