Skip to content

Commit

Permalink
Use lauxhlib instead of lstate_* helper macros
Browse files Browse the repository at this point in the history
  • Loading branch information
mah0x211 committed Jun 25, 2017
1 parent c4c3551 commit 747ba49
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 256 deletions.
2 changes: 1 addition & 1 deletion codegen.lua
@@ -1,6 +1,6 @@
local TMPL = [=[
#ifdef %s
lstate_num2tbl( L, "%s", %s );
lauxh_pushnum2tbl( L, "%s", %s );
#endif
]=];
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile.in
@@ -1,7 +1,7 @@
CC=@CC@
CFLAGS=@CFLAGS@
WARNINGS=-Wall -Wno-trigraphs -Wmissing-field-initializers -Wmissing-prototypes -Wreturn-type -Wmissing-braces -Wparentheses -Wswitch -Wunused-function -Wunused-label -Wunused-parameter -Wunused-variable -Wunused-value -Wuninitialized -Wunknown-pragmas -Wshadow -Wsign-compare
CPPFLAGS=-I. @CPPFLAGS@
CPPFLAGS=-I. -I../deps/lauxhlib @CPPFLAGS@
LDFLAGS=@LIBFLAG@
LIBS=@LIBS@
TARGET=@PACKAGE@.@LIB_EXTENSION@
Expand Down
34 changes: 17 additions & 17 deletions src/addrinfo.c
Expand Up @@ -32,17 +32,17 @@

static int getnameinfo_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
int flag = lls_optflags( L, 2 );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );
int flag = lauxh_optflags( L, 2 );
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
int rc = getnameinfo( info->ai_addr, info->ai_addrlen, host, NI_MAXHOST,
serv, NI_MAXSERV, flag );

if( rc == 0 ){
lua_createtable( L, 0, 2 );
lstate_str2tbl( L, "host", host );
lstate_str2tbl( L, "service", serv );
lauxh_pushstr2tbl( L, "host", host );
lauxh_pushstr2tbl( L, "service", serv );
return 1;
}

Expand All @@ -56,7 +56,7 @@ static int getnameinfo_lua( lua_State *L )

static int addr_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );
struct sockaddr_un *uaddr = NULL;
struct sockaddr_in *iaddr = NULL;
struct sockaddr_in6 *iaddr6 = NULL;
Expand All @@ -66,25 +66,25 @@ static int addr_lua( lua_State *L )
case AF_INET:
lua_createtable( L, 0, 2 );
iaddr = (struct sockaddr_in*)info->ai_addr;
lstate_num2tbl( L, "port", ntohs( iaddr->sin_port ) );
lstate_str2tbl( L, "ip", inet_ntop( info->ai_family,
lauxh_pushnum2tbl( L, "port", ntohs( iaddr->sin_port ) );
lauxh_pushstr2tbl( L, "ip", inet_ntop( info->ai_family,
(const void*)&iaddr->sin_addr,
buf, INET6_ADDRSTRLEN ) );
return 1;

case AF_INET6:
lua_createtable( L, 0, 2 );
iaddr6 = (struct sockaddr_in6*)info->ai_addr;
lstate_num2tbl( L, "port", ntohs( iaddr->sin_port ) );
lstate_str2tbl( L, "ip", inet_ntop( info->ai_family,
lauxh_pushnum2tbl( L, "port", ntohs( iaddr->sin_port ) );
lauxh_pushstr2tbl( L, "ip", inet_ntop( info->ai_family,
(const void*)&iaddr6->sin6_addr,
buf, INET6_ADDRSTRLEN ) );
return 1;

case AF_UNIX:
lua_createtable( L, 0, 1 );
uaddr = (struct sockaddr_un*)info->ai_addr;
lstate_str2tbl( L, "path", uaddr->sun_path );
lauxh_pushstr2tbl( L, "path", uaddr->sun_path );
return 1;

// unsupported family
Expand All @@ -96,7 +96,7 @@ static int addr_lua( lua_State *L )

static int canonname_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );

if( info->ai_canonname ){
lua_pushstring( L, info->ai_canonname );
Expand All @@ -109,7 +109,7 @@ static int canonname_lua( lua_State *L )

static int protocol_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );

lua_pushinteger( L, info->ai_protocol );

Expand All @@ -119,7 +119,7 @@ static int protocol_lua( lua_State *L )

static int socktype_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );

lua_pushinteger( L, info->ai_socktype );

Expand All @@ -129,7 +129,7 @@ static int socktype_lua( lua_State *L )

static int family_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );

lua_pushinteger( L, info->ai_family );

Expand All @@ -146,7 +146,7 @@ static int tostring_lua( lua_State *L )

static int gc_lua( lua_State *L )
{
struct addrinfo *info = luaL_checkudata( L, 1, ADDRINFO_MT );
struct addrinfo *info = lauxh_checkudata( L, 1, ADDRINFO_MT );

if( info->ai_canonname ){
pdealloc( info->ai_canonname );
Expand Down Expand Up @@ -178,15 +178,15 @@ LUALIB_API int luaopen_llsocket_addrinfo( lua_State *L )
luaL_newmetatable( L, ADDRINFO_MT );
// metamethods
do {
lstate_fn2tbl( L, ptr->name, ptr->func );
lauxh_pushfn2tbl( L, ptr->name, ptr->func );
ptr++;
} while( ptr->name );
// methods
lua_pushstring( L, "__index" );
lua_newtable( L );
ptr = method;
do {
lstate_fn2tbl( L, ptr->name, ptr->func );
lauxh_pushfn2tbl( L, ptr->name, ptr->func );
ptr++;
} while( ptr->name );
lua_rawset( L, -3 );
Expand Down
8 changes: 4 additions & 4 deletions src/device.c
Expand Up @@ -70,7 +70,7 @@ static int macaddrs_lua( lua_State *L )
"%02x:%02x:%02x:%02x:%02x:%02x",
*mac, mac[1], mac[2], mac[3], mac[4], mac[5]
);
lstate_str2tbl( L, it->ifr_name, buf );
lauxh_pushstr2tbl( L, it->ifr_name, buf );
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ static int macaddrs_lua( lua_State *L )
"%02x:%02x:%02x:%02x:%02x:%02x",
*mac, mac[1], mac[2], mac[3], mac[4], mac[5]
);
lstate_str2tbl( L, ptr->ifa_name, buf );
lauxh_pushstr2tbl( L, ptr->ifa_name, buf );
break;
case 8:
mac = (unsigned char*)LLADDR( sd );
Expand All @@ -132,7 +132,7 @@ static int macaddrs_lua( lua_State *L )
*mac, mac[1], mac[2], mac[3], mac[4], mac[5],
mac[6], mac[7]
);
lstate_str2tbl( L, ptr->ifa_name, buf );
lauxh_pushstr2tbl( L, ptr->ifa_name, buf );
break;
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ LUALIB_API int luaopen_llsocket_device( lua_State *L )

lua_newtable( L );
do {
lstate_fn2tbl( L, ptr->name, ptr->func );
lauxh_pushfn2tbl( L, ptr->name, ptr->func );
ptr++;
} while( ptr->name );

Expand Down
12 changes: 6 additions & 6 deletions src/inet.c
Expand Up @@ -33,14 +33,14 @@

static int getaddrinfo_lua( lua_State *L )
{
const char *node = lls_optstring( L, 1, NULL );
const char *service = lls_optstring( L, 2, NULL );
const char *node = lauxh_optstring( L, 1, NULL );
const char *service = lauxh_optstring( L, 2, NULL );
// SOCK_STREAM:tcp | SOCK_DGRAM:udp | SOCK_SEQPACKET
int socktype = (int)lls_optinteger( L, 3, 0 );
int socktype = (int)lauxh_optinteger( L, 3, 0 );
// IPPROTO_TCP:tcp | IPPROTO_UDP:udp | 0:automatic
int protocol = (int)lls_optinteger( L, 4, 0 );
int protocol = (int)lauxh_optinteger( L, 4, 0 );
// AI_PASSIVE:bind socket if node is null
int flags = lls_optflags( L, 5 );
int flags = lauxh_optflags( L, 5 );
struct addrinfo *list = NULL;
struct addrinfo *ptr = NULL;
int idx = 1;
Expand Down Expand Up @@ -84,7 +84,7 @@ static int getaddrinfo_lua( lua_State *L )
LUALIB_API int luaopen_llsocket_inet( lua_State *L )
{
lua_newtable( L );
lstate_fn2tbl( L, "getaddrinfo", getaddrinfo_lua );
lauxh_pushfn2tbl( L, "getaddrinfo", getaddrinfo_lua );

return 1;
}
Expand Down
144 changes: 5 additions & 139 deletions src/llsocket.h
Expand Up @@ -56,6 +56,7 @@
#include <lauxlib.h>
#include <lualib.h>

#include "lauxhlib.h"
#include "config.h"


Expand All @@ -67,53 +68,6 @@
#define pdealloc(p) free((void*)p)


// helper macros
#define lstate_isref(ref) \
((ref) >= 0)

#define lstate_ref(L) \
luaL_ref(L,LUA_REGISTRYINDEX)

#define lstate_refat(L,idx) \
(lua_pushvalue(L,idx),luaL_ref(L,LUA_REGISTRYINDEX))

#define lstate_pushref(L,ref) \
lua_rawgeti( L, LUA_REGISTRYINDEX, ref )

#define lstate_unref(L,ref) \
(luaL_unref( L, LUA_REGISTRYINDEX, ref ),LUA_NOREF)

#define lstate_setmetatable(L,label) do { \
luaL_getmetatable( L, label ); \
lua_setmetatable( L, -2 ); \
}while(0)

#define lstate_fn2tbl(L,k,v) do{ \
lua_pushstring(L,k); \
lua_pushcfunction(L,v); \
lua_rawset(L,-3); \
}while(0)

#define lstate_str2tbl(L,k,v) do{ \
lua_pushstring(L,k); \
lua_pushstring(L,v); \
lua_rawset(L,-3); \
}while(0)

#define lstate_num2tbl(L,k,v) do{ \
lua_pushstring(L,k); \
lua_pushnumber(L,v); \
lua_rawset(L,-3); \
}while(0)

#define lstate_bool2tbl(L,k,v) do{ \
lua_pushstring(L,k); \
lua_pushboolean(L,v); \
lua_rawset(L,-3); \
}while(0)



#define SOCKET_MT "llsocket.socket"
#define ADDRINFO_MT "llsocket.addrinfo"

Expand Down Expand Up @@ -171,7 +125,7 @@ static inline struct addrinfo *lls_addrinfo_alloc( lua_State *L,
memcpy( (void*)info->ai_addr, (void*)src->ai_addr,
src->ai_addrlen );
// set metatable
lstate_setmetatable( L, ADDRINFO_MT );
lauxh_setmetatable( L, ADDRINFO_MT );
return info;
}
else if( info->ai_canonname ){
Expand Down Expand Up @@ -215,99 +169,11 @@ static inline void *lls_checkudata( lua_State *L, int idx, const char *tname )
}


static inline const char *lls_checklstring( lua_State *L, int idx, size_t *len )
{
luaL_checktype( L, idx, LUA_TSTRING );

return lua_tolstring( L, idx, len );
}


static inline const char *lls_optlstring( lua_State *L, int idx,
const char *def, size_t *len )
{
if( lua_isnoneornil( L, idx ) ){
return def;
}

luaL_checktype( L, idx, LUA_TSTRING );

return lua_tolstring( L, idx, len );
}


static inline const char *lls_checkstring( lua_State *L, int idx )
{
luaL_checktype( L, idx, LUA_TSTRING );

return lua_tostring( L, idx );
}


static inline const char *lls_optstring( lua_State *L, int idx,
const char *def )
{
if( lua_isnoneornil( L, idx ) ){
return def;
}

luaL_checktype( L, idx, LUA_TSTRING );

return lua_tostring( L, idx );
}


static inline lua_Integer lls_checkinteger( lua_State *L, int idx )
{
luaL_checktype( L, idx, LUA_TNUMBER );

return lua_tointeger( L, idx );
}


static inline lua_Integer lls_optinteger( lua_State *L, int idx,
lua_Integer def )
{
if( lua_isnoneornil( L, idx ) ){
return def;
}

luaL_checktype( L, idx, LUA_TNUMBER );

return lua_tointeger( L, idx );
}


static inline int lls_optboolean( lua_State *L, int idx, int def )
{
if( lua_isnoneornil( L, idx ) ){
return def > 0;
}

luaL_checktype( L, idx, LUA_TBOOLEAN );

return lua_toboolean( L, idx );
}


static inline int lls_optflags( lua_State *L, int idx )
{
const int argc = lua_gettop( L );
int flg = 0;

for(; idx <= argc; idx++ ){
flg |= (int)lls_optinteger( L, idx, 0 );
}

return flg;
}


static inline int lls_checksockaddr( lua_State *L, int idx, int family,
int socktype,
struct sockaddr_storage *sockaddr )
{
const char *str = lls_checkstring( L, idx );
const char *str = lauxh_checkstring( L, idx );
struct addrinfo *list = NULL;
int rc = lls_getaddrinfo( &list, str, NULL, family, socktype, 0,
AI_NUMERICHOST );
Expand All @@ -324,7 +190,7 @@ static inline int lls_checksockaddr( lua_State *L, int idx, int family,
static inline int lls_check4inaddr( lua_State *L, int idx, int socktype,
struct in_addr *addr )
{
const char *str = lls_checkstring( L, idx );
const char *str = lauxh_checkstring( L, idx );
struct addrinfo *list = NULL;
int rc = lls_getaddrinfo( &list, str, NULL, AF_INET, socktype, 0,
AI_NUMERICHOST );
Expand Down Expand Up @@ -353,7 +219,7 @@ static inline int lls_opt4inaddr( lua_State *L, int idx, int socktype,
static inline int lls_check6inaddr( lua_State *L, int idx, int socktype,
struct in6_addr *addr )
{
const char *str = lls_checkstring( L, idx );
const char *str = lauxh_checkstring( L, idx );
struct addrinfo *list = NULL;
int rc = lls_getaddrinfo( &list, str, NULL, AF_INET6, socktype, 0,
AI_NUMERICHOST );
Expand Down

0 comments on commit 747ba49

Please sign in to comment.