e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#ifndef LUA_SCRIPT_PRIVATE_H
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define LUA_SCRIPT_PRIVATE_H 1
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#include "dlua-script.h"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#include "lualib.h"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#include "lauxlib.h"
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#if !defined(LUA_VERSION_NUM)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_setfield(L, i, k) (lua_pushstring(L, k), lua_settable(L, i))
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_getref(L, ref) lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define luaL_unref(L, ref) luaL_unref(L, LUA_REGISTRYINDEX, ref);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#endif
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 502
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define luaL_newmetatable(L, tn) \
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi (luaL_newmetatable(L, tn) ? (lua_pushstring(L, tn), lua_setfield(L, -2, "__name"), 1) : 0)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define luaL_newlibtable(L, l) (lua_createtable(L, 0, sizeof(l)/sizeof(*(l))-1))
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define luaL_newlib(L, l) (luaL_newlibtable(L, l), luaL_register(L, NULL, l))
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_load(L, r, s, fn, m) lua_load(L, r, s, fn)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid luaL_setmetatable (lua_State *L, const char *tname);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#endif
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi/* consistency helpers */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_isstring(L, n) (lua_isstring(L, n) == 1)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_isnumber(L, n) (lua_isnumber(L, n) == 1)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define lua_toboolean(L, n) (lua_toboolean(L, n) == 1)
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_STRING(n, s) { .name = n, .type = DLUA_TABLE_VALUE_STRING, .v.s = s }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_INTEGER(n, i) { .name = n, .type = DLUA_TABLE_VALUE_INTEGER, .v.i = i }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_ENUM(n) { .name = #n, .type = DLUA_TABLE_VALUE_INTEGER, .v.i = n }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_DOUBLE(n, d) { .name = n, .type = DLUA_TABLE_VALUE_DOUBLE, .v.d = d }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_BOOLEAN(n, b) { .name = n, .type = DLUA_TABLE_VALUE_BOOLEAN, .v.b = b }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_NULL(n, s) { .name = n, .type = DLUA_TABLE_VALUE_NULL }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#define DLUA_TABLE_END { .name = NULL }
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistruct dlua_script {
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct dlua_script *prev,*next;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi pool_t pool;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi lua_State *L;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *filename;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi struct istream *in;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi ssize_t last_read;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
9698cd24356147d8a5471260062ea7e10e692fdfAki Tuomi int ref;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi bool init:1;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi};
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomienum dlua_table_value_type {
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi DLUA_TABLE_VALUE_STRING = 0,
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi DLUA_TABLE_VALUE_INTEGER,
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi DLUA_TABLE_VALUE_DOUBLE,
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi DLUA_TABLE_VALUE_BOOLEAN,
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi DLUA_TABLE_VALUE_NULL
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi};
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistruct dlua_table_values {
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *name;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi enum dlua_table_value_type type;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi union {
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const char *s;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi ptrdiff_t i;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi double d;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi bool b;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi } v;
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi};
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi/* Get dlua_script from lua_State */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomistruct dlua_script *dlua_script_from_state(lua_State *L);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi/* register 'dovecot' global */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid dlua_dovecot_register(struct dlua_script *script);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi/* push 'dovecot' global on top of stack */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid dlua_getdovecot(struct dlua_script *script);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi/* assign values to table on idx */
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomivoid dlua_setmembers(struct dlua_script *script,
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi const struct dlua_table_values *values, int idx);
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi
e88e76e782853e826d00524ee1133e64fb69b3d9Aki Tuomi#endif