60a683535355e5cfbe3bbf4105bce89ee162e858
[apps/madmutt.git] / lib-lua / runtime.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; either version 2 of the License, or (at
5  *  your option) any later version.
6  *
7  *  This program is distributed in the hope that it will be useful, but
8  *  WITHOUT ANY WARRANTY; without even the implied warranty of
9  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  *  General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License
13  *  along with this program; if not, write to the Free Software
14  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
15  *  MA 02110-1301, USA.
16  *
17  *  Copyright © 2007 Pierre Habouzit
18  */
19
20 #include <lib-lib/lib-lib.h>
21 #include "lib-lua_priv.h"
22
23 static lua_State *L;
24
25 static const luaL_Reg lualibs[] = {
26     {"",              luaopen_base},
27     {LUA_LOADLIBNAME, luaopen_package},
28     {LUA_TABLIBNAME,  luaopen_table},
29     {LUA_IOLIBNAME,   luaopen_io},
30     {LUA_STRLIBNAME,  luaopen_string},
31     {LUA_MATHLIBNAME, luaopen_math},
32     {LUA_DBLIBNAME,   luaopen_debug},
33     {LUA_MADMUTT,     luaopen_madmutt},
34 };
35
36 void mlua_initialize(void)
37 {
38     int i;
39
40     L = lua_open();
41     for (i = 0; i < countof(lualibs); i++) {
42         lua_pushcfunction(L, lualibs[i].func);
43         lua_pushstring(L, lualibs[i].name);
44         lua_call(L, 1, 0);
45     }
46 }
47
48 void mlua_shutdown(void)
49 {
50     lua_close(L);
51 }
52
53
54 int mlua_dofile(const char *filename)
55 {
56     return luaL_dofile(L, filename);
57 }
58
59 int mlua_wrap(void (*errfun)(const char *fmt, ...), int status)
60 {
61     if (status) {
62         (*errfun)("-[lua]-: %s\n", lua_tostring(L, -1));
63         lua_pop(L, 1);
64     }
65     return status;
66 }
67