2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "lua/luastate.h"
23 static int panic (lua_State *L) {
24 lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
26 return 0; /* return to Lua to abort */
30 : L (luaL_newstate ())
36 LuaState::LuaState(lua_State *ls)
43 LuaState::~LuaState() {
49 lua_atpanic (L, &panic);
51 lua_pushlightuserdata (L, this);
52 lua_pushcclosure (L, &LuaState::_print, 1);
53 lua_setglobal (L, "print");
57 LuaState::do_command (std::string cmd) {
58 int result = luaL_dostring (L, cmd.c_str());
60 print ("Error: " + std::string (lua_tostring (L, -1)));
66 LuaState::do_file (std::string fn) {
67 int result = luaL_dofile (L, fn.c_str());
69 print ("Error: " + std::string (lua_tostring (L, -1)));
75 LuaState::collect_garbage () {
76 lua_gc (L, LUA_GCCOLLECT, 0);
80 LuaState::collect_garbage_step () {
81 lua_gc (L, LUA_GCSTEP, 0);
85 LuaState::tweak_rt_gc () {
86 /* GC runs same speed as memory allocation */
87 lua_gc (L, LUA_GCSETPAUSE, 100);
88 lua_gc (L, LUA_GCSETSTEPMUL, 100);
92 LuaState::sandbox (bool rt_safe) {
93 do_command ("loadfile = nil dofile = nil require = nil dofile = nil package = nil debug = nil os.exit = nil os.setlocale = nil rawget = nil rawset = nil coroutine = nil module = nil");
95 do_command ("os = nil io = nil");
101 LuaState::print (std::string text) {
102 Print (text); /* EMIT SIGNAL */
106 LuaState::_print (lua_State *L) {
107 LuaState* const luaState = static_cast <LuaState*> (lua_touserdata (L, lua_upvalueindex (1)));
109 int n = lua_gettop(L); /* number of arguments */
111 lua_getglobal(L, "tostring");
112 for (i=1; i<=n; i++) {
115 lua_pushvalue(L, -1); /* function to be called */
116 lua_pushvalue(L, i); /* value to print */
118 s = lua_tolstring(L, -1, &l); /* get result */
120 return luaL_error(L, "'tostring' must return a string to 'print'");
121 if (i > 1) text += " ";
122 text += std::string (s, l);
123 lua_pop(L, 1); /* pop result */
125 luaState->print (text);