From e42c9a296a8decf8029d9acd25d18c86752f9e40 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 1 Jun 2022 15:59:10 +0200 Subject: [PATCH 1/5] use luaL_checkinteger instead of luaL_checkint luaL_checkint is available only in Lua 5.1 & 5.2 luaL_checkinteger is available in Lua 5.1, 5.2, 5.3 & 5.4 fix #32 --- lua-mosquitto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua-mosquitto.c b/lua-mosquitto.c index 7f298bd..3f29d89 100644 --- a/lua-mosquitto.c +++ b/lua-mosquitto.c @@ -539,7 +539,7 @@ static int ctx_threaded_set(lua_State *L) static int ctx_option(lua_State *L) { ctx_t *ctx = ctx_check(L, 1); - enum mosq_opt_t option = luaL_checkint(L, 2); + enum mosq_opt_t option = luaL_checkinteger(L, 2); int type = lua_type(L, 3); int rc; From 0d230945172b81e1be959d0d66c16dd4105a5012 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 1 Jun 2022 16:00:55 +0200 Subject: [PATCH 2/5] remove useless Lua include --- lua-mosquitto.c | 1 - 1 file changed, 1 deletion(-) diff --git a/lua-mosquitto.c b/lua-mosquitto.c index 3f29d89..f0fb4de 100644 --- a/lua-mosquitto.c +++ b/lua-mosquitto.c @@ -40,7 +40,6 @@ #include #include -#include #include #include From 26ca1763f058413157e5a0ebb310398fe93c1fa3 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 1 Jun 2022 16:04:48 +0200 Subject: [PATCH 3/5] some helper functions could be inlined --- lua-mosquitto.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lua-mosquitto.c b/lua-mosquitto.c index f0fb4de..dab8196 100644 --- a/lua-mosquitto.c +++ b/lua-mosquitto.c @@ -84,7 +84,7 @@ typedef struct { static int mosq_initialized = 0; /* handle mosquitto lib return codes */ -static int mosq__pstatus(lua_State *L, int mosq_errno) { +static inline int mosq__pstatus(lua_State *L, int mosq_errno) { switch (mosq_errno) { case MOSQ_ERR_SUCCESS: lua_pushboolean(L, true); @@ -203,7 +203,7 @@ static int mosq_cleanup(lua_State *L) return mosq__pstatus(L, MOSQ_ERR_SUCCESS); } -static void ctx__on_init(ctx_t *ctx) +static inline void ctx__on_init(ctx_t *ctx) { ctx->on_connect = LUA_REFNIL; ctx->on_disconnect = LUA_REFNIL; @@ -214,7 +214,7 @@ static void ctx__on_init(ctx_t *ctx) ctx->on_log = LUA_REFNIL; } -static void ctx__on_clear(ctx_t *ctx) +static inline void ctx__on_clear(ctx_t *ctx) { luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_connect); luaL_unref(ctx->L, LUA_REGISTRYINDEX, ctx->on_disconnect); @@ -261,7 +261,7 @@ static int mosq_new(lua_State *L) return 1; } -static ctx_t * ctx_check(lua_State *L, int i) +static inline ctx_t * ctx_check(lua_State *L, int i) { return (ctx_t *) luaL_checkudata(L, i, MOSQ_META_CTX); } @@ -749,7 +749,7 @@ static int ctx_unsubscribe(lua_State *L) } } -static int mosq_loop(lua_State *L, bool forever) +static inline int mosq_loop(lua_State *L, bool forever) { ctx_t *ctx = ctx_check(L, 1); int timeout = luaL_optinteger(L, 2, -1); @@ -1252,7 +1252,7 @@ static int callback_type_from_string(const char *typestr) return -1; } -static void mosq_register_defs(lua_State *L, const struct define *D) +static inline void mosq_register_defs(lua_State *L, const struct define *D) { while (D->name != NULL) { lua_pushinteger(L, D->value); From af0526b625ded2f364a24348940edb2d4947882a Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 1 Jun 2022 16:07:06 +0200 Subject: [PATCH 4/5] refactor with lua_pushfstring --- lua-mosquitto.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lua-mosquitto.c b/lua-mosquitto.c index dab8196..f812310 100644 --- a/lua-mosquitto.c +++ b/lua-mosquitto.c @@ -34,7 +34,6 @@ * @module mosquitto */ -#include #include #include #include @@ -132,11 +131,9 @@ static inline int mosq__pstatus(lua_State *L, int mosq_errno) { static int mosq_version(lua_State *L) { int major, minor, rev; - char version[16]; mosquitto_lib_version(&major, &minor, &rev); - sprintf(version, "%i.%i.%i", major, minor, rev); - lua_pushstring(L, version); + lua_pushfstring(L, "%d.%d.%d", major, minor, rev); return 1; } From 831d52780858d675f166d3ecf417a2a3eb45c8ce Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Wed, 1 Jun 2022 16:08:58 +0200 Subject: [PATCH 5/5] move assert.h in compat.h only used here --- compat.h | 1 + lua-mosquitto.c | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/compat.h b/compat.h index c8faee9..0b3a896 100644 --- a/compat.h +++ b/compat.h @@ -1,5 +1,6 @@ #if LUA_VERSION_NUM < 502 +# include # define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) # define luaL_setfuncs(L,l,n) (assert(n==0), luaL_register(L,NULL,l)) #endif diff --git a/lua-mosquitto.c b/lua-mosquitto.c index f812310..b580560 100644 --- a/lua-mosquitto.c +++ b/lua-mosquitto.c @@ -36,7 +36,6 @@ #include #include -#include #include #include