aboutsummaryrefslogtreecommitdiff
path: root/src/lua/api.c
blob: 47194ad546fd74338882d6074f42501d5729ba4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "api.h"
#include "log.h"

bool lua_api_init(lua_api_t *lua)
{
    lua->state = luaL_newstate();
    if (!lua->state)
        return false;

    luaL_openlibs(lua->state);

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