Use luaL_openlib for natives

This commit is contained in:
Łukasz Magiera 2016-02-25 16:57:08 +01:00
parent ba2214876b
commit 7368bd6168
5 changed files with 57 additions and 19 deletions

View File

@ -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

View File

@ -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();

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
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);
lua_setglobal(L, "termutils");
}