From 7368bd6168324eb86c73c8aa8afa368595037828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 25 Feb 2016 16:57:08 +0100 Subject: [PATCH] Use luaL_openlib for natives --- Makefile | 2 +- include/lupi.h | 2 -- src/c/internet.c | 14 +++++++------- src/c/lnative.c | 44 +++++++++++++++++++++++++++++++++++++++++--- src/c/termutils.c | 14 ++++++++------ 5 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 3e82ec7..3e482de 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PREFIX?=x86_64-linux-musl CC = $(PREFIX)-gcc -CFLAGS?=-O2 -std=c99 +CFLAGS?=-O2 -std=c99 -DLUA_COMPAT_MODULE LDFLAGS+= -static -Ldependencies/lib-$(PREFIX) # Project specific stuff diff --git a/include/lupi.h b/include/lupi.h index 2ca0a3d..381c87b 100644 --- a/include/lupi.h +++ b/include/lupi.h @@ -15,9 +15,7 @@ void logm(const char *message); #define logm(m) #endif -/* TODO: move to utils */ #define pushstuple(state, name, value) lua_pushstring((state), (name)); lua_pushstring((state), (value)); lua_settable((state), -3) -#define pushctuple(state, name, value) lua_pushstring((state), (name)); lua_pushcfunction((state), (value)); lua_settable((state), -3) lua_State* getL(); diff --git a/src/c/internet.c b/src/c/internet.c index 9f19864..eb59ab8 100644 --- a/src/c/internet.c +++ b/src/c/internet.c @@ -199,11 +199,11 @@ void internet_start(lua_State *L) { ssl_init(); signal(SIGPIPE, SIG_IGN); - lua_createtable (L, 0, 1); - - pushctuple(L, "open", l_open); - pushctuple(L, "write", l_write); - pushctuple(L, "read", l_read); - - lua_setglobal(L, "net"); + struct luaL_Reg netlib[] = { + {"open", l_open}, + {"write", l_write}, + {"read", l_read}, + {NULL, NULL} + }; + luaL_openlib(L, "net", netlib, 0); } diff --git a/src/c/lnative.c b/src/c/lnative.c index 21fddc9..63da238 100644 --- a/src/c/lnative.c +++ b/src/c/lnative.c @@ -378,8 +378,14 @@ static int l_towlower (lua_State *L) { return 1; } +#ifdef DEBUG +static int l_debug (lua_State *L) { + return 0; +} +#endif + void luanative_start(lua_State *L) { - lua_createtable (L, 0, 1); + /*lua_createtable (L, 0, 1); pushctuple(L, "sleep", l_sleep); pushctuple(L, "log", l_log); @@ -414,7 +420,39 @@ void luanative_start(lua_State *L) { lua_pushstring(L, "debug"); lua_pushboolean(L, 1); lua_settable(L, -3); - #endif + #endif*/ - lua_setglobal(L, "native"); + struct luaL_Reg nativelib[] = { + {"sleep", l_sleep}, + {"log", l_log}, + {"fs_exists", l_fs_exists}, + {"fs_mkdir", l_fs_mkdir}, + {"fs_isdir", l_fs_isdir}, + {"fs_spaceUsed", l_fs_spaceUsed}, + {"fs_open", l_fs_open}, + {"fs_seek", l_fs_seek}, + {"fs_write", l_fs_write}, + {"fs_spaceTotal", l_fs_spaceTotal}, + {"fs_rename", l_fs_rename}, + {"fs_list", l_fs_list}, + {"fs_lastModified", l_fs_lastModified}, + {"fs_remove", l_fs_remove}, + {"fs_close", l_fs_close}, + {"fs_size", l_fs_size}, + {"fs_read", l_fs_read}, + {"wcwidth", l_wcwidth}, + {"towlower", l_towlower}, + {"towupper", l_towupper}, + {"beep", l_beep}, + {"uptime", l_uptime}, + {"totalMemory", l_totalMemory}, + {"freeMemory", l_freeMemory}, + {"pull", l_pull}, + #ifdef DEBUG + {"debug", l_debug}, + #endif + {NULL, NULL} + }; + + luaL_openlib(L, "native", nativelib, 0); } diff --git a/src/c/termutils.c b/src/c/termutils.c index 11e2a80..b645695 100644 --- a/src/c/termutils.c +++ b/src/c/termutils.c @@ -50,10 +50,12 @@ void termutils_start(lua_State *L) { new.c_cflag &= ~(CSIZE | PARENB); new.c_cflag |= CS8; - lua_createtable (L, 0, 1); - pushctuple(L, "getSize", l_get_term_sz); - pushctuple(L, "init", l_term_init); - pushctuple(L, "restore", l_term_restore); - - lua_setglobal(L, "termutils"); + struct luaL_Reg termlib[] = { + {"getSize", l_get_term_sz}, + {"init", l_term_init}, + {"restore", l_term_restore}, + {NULL, NULL} + }; + luaL_openlib(L, "termutils", termlib, 0); + } \ No newline at end of file