aboutsummaryrefslogtreecommitdiff
path: root/src/lua/api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua/api.c')
-rw-r--r--src/lua/api.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/lua/api.c b/src/lua/api.c
index e2fdd30..47194ad 100644
--- a/src/lua/api.c
+++ b/src/lua/api.c
@@ -4,12 +4,26 @@
bool lua_api_init(lua_api_t *lua)
{
lua->state = luaL_newstate();
+ if (!lua->state)
+ return false;
+
luaL_openlibs(lua->state);
- luaL_requiref(lua->state, "log", lua_log_library, 1);
- lua_pop(lua->state, 1);
+
+ lua_api_require(lua, "log", lua_log_library);
return true;
}
+void lua_api_require(lua_api_t *lua, const char *name, lua_library_t lib_fn)
+{
+ lua_pushcfunction(lua->state, lib_fn);
+ lua_pushstring(lua->state, name);
+ lua_call(lua->state, 1, 1);
+
+ lua_pushvalue(lua->state, -1);
+ lua_setglobal(lua->state, name);
+ lua_pop(lua->state, 1);
+}
+
void lua_api_close(lua_api_t *lua)
{
lua_close(lua->state);