Safe-guard non-rt-safe sidechain operations
[ardour.git] / libs / lua / luastate.cc
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <assert.h>
20 #include "lua/luastate.h"
21
22 // from lauxlib.c
23 static int panic (lua_State *L) {
24         lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n",
25                         lua_tostring(L, -1));
26         return 0;  /* return to Lua to abort */
27 }
28
29 LuaState::LuaState()
30         : L (luaL_newstate ())
31 {
32         assert (L);
33         init ();
34 }
35
36 LuaState::LuaState(lua_State *ls)
37         : L (ls)
38 {
39         assert (L);
40         init ();
41 }
42
43 LuaState::~LuaState() {
44         lua_close (L);
45 }
46
47 void
48 LuaState::init() {
49         lua_atpanic (L, &panic);
50         luaL_openlibs (L);
51         lua_pushlightuserdata (L, this);
52         lua_pushcclosure (L, &LuaState::_print, 1);
53         lua_setglobal (L, "print");
54 }
55
56 int
57 LuaState::do_command (std::string cmd) {
58         int result = luaL_dostring (L, cmd.c_str());
59         if (result != 0) {
60                 print ("Error: " + std::string (lua_tostring (L, -1)));
61         }
62         return result;
63 }
64
65 int
66 LuaState::do_file (std::string fn) {
67         int result = luaL_dofile (L, fn.c_str());
68         if (result != 0) {
69                 print ("Error: " + std::string (lua_tostring (L, -1)));
70         }
71         return result;
72 }
73
74 void
75 LuaState::collect_garbage () {
76         lua_gc (L, LUA_GCCOLLECT, 0);
77 }
78
79 void
80 LuaState::collect_garbage_step (int debt) {
81         lua_gc (L, LUA_GCSTEP, debt);
82 }
83
84 void
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);
89 }
90
91 void
92 LuaState::sandbox (bool rt_safe) {
93         do_command ("dofile = nil require = nil dofile = nil package = nil debug = nil os.exit = nil os.setlocale = nil rawget = nil rawset = nil coroutine = nil module = nil");
94         if (rt_safe) {
95                 do_command ("os = nil io = nil loadfile = nil");
96         }
97 }
98
99
100 void
101 LuaState::print (std::string text) {
102         Print (text); /* EMIT SIGNAL */
103 }
104
105 int
106 LuaState::_print (lua_State *L) {
107         LuaState* const luaState = static_cast <LuaState*> (lua_touserdata (L, lua_upvalueindex (1)));
108         std::string text;
109         int n = lua_gettop(L);  /* number of arguments */
110         int i;
111         lua_getglobal(L, "tostring");
112         for (i=1; i<=n; i++) {
113                 const char *s;
114                 size_t l;
115                 lua_pushvalue(L, -1);  /* function to be called */
116                 lua_pushvalue(L, i);   /* value to print */
117                 lua_call(L, 1, 1);
118                 s = lua_tolstring(L, -1, &l);  /* get result */
119                 if (s == NULL)
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 */
124         }
125         luaState->print (text);
126         return 0;
127 }