aboutsummaryrefslogtreecommitdiff
path: root/include/lua_state.h
blob: 0caf62f0da4c5422afde1242bf7e7625d3388ab9 (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
31
32
33
34
35
36
37
38
39
40
#ifndef PKGIT_LUA_STATE_H
#define PKGIT_LUA_STATE_H

#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include <stdbool.h>

extern lua_State *L;
extern bool L_is_loaded;

bool init_lua_state(void);
bool init_lua_config(void);
void free_lua_state(void);
void free_lua_config(void);

bool config_hook_on_update(void);

#define lua_get_field_type(name, type, file, pop_num)                          \
	do {                                                                       \
		lua_getfield(L, -1, name);                                             \
		if (!lua_is##type(L, -1)) {                                            \
			log_error("lua: expected type '" #type "' for '%s' in '%.*s'",     \
					  name, str_fmt(file));                                    \
			lua_pop(L, pop_num);                                               \
			return false;                                                      \
		}                                                                      \
	} while (0)

#define lua_run_function(name, file, params, returns, pop_num)                 \
	do {                                                                       \
		if (lua_pcall(L, params, returns, 0)) {                                \
			log_error("lua: '%s' function borked in %.*s: %s",                 \
					  name, str_fmt(file), lua_tostring(L, -1));               \
			lua_pop(L, pop_num);                                               \
			return false;                                                      \
		}                                                                      \
	} while (0)

#endif