fix uninstaller for variants (Mixbus32C vs Mixbus)
[ardour.git] / tools / luadevel / devel.cc
1 #include <stdint.h>
2 #include <cstdio>
3 #include <iostream>
4 #include <string>
5 #include <list>
6 #include <vector>
7
8 #define LIBPBD_API
9 #include "../../libs/pbd/pbd/reallocpool.h"
10 #include "../../libs/pbd/reallocpool.cc"
11
12 #include <readline/readline.h>
13 #include <readline/history.h>
14
15 #include "lua/luastate.h"
16 #include "LuaBridge/LuaBridge.h"
17
18 static void my_lua_print (std::string s) {
19         std::cout << s << "\n";
20 }
21
22
23 class A {
24         public:
25                 A() { printf ("CTOR\n"); _int = 4; for (int i = 0; i < 256; ++i) {arr[i] = i; ar2[i] = i/256.0; ar3[i] = i;} }
26                 ~A() { printf ("DTOR\n"); }
27
28                 void set_int (int a) { _int = a; }
29                 int  get_int () const { return _int; }
30
31                 int& get_ref () { return _int; }
32                 int get_arg (int &a) { printf ("a = %d\n", a);  a = _int; printf ("a = %d\n", a); return 1; }
33                 void get_arg2 (int &a, int& b) {  a = _int; b = 100; }
34                 void get_args (std::string &a) {  a = "hello"; }
35                 void set_ref (int const &a) { _int = a; }
36
37                 float * get_arr () { return arr; }
38                 float * get_ar2 () { return ar2; }
39                 int * get_ar3 () { return ar3; }
40
41                 void set_list (std::list<std::string> sl) { _sl = sl; }
42                 std::list<std::string>& get_list () { return _sl; }
43
44                 uint32_t minone() { return -1; }
45
46                 enum EN {
47                         RV1 = 1, RV2, RV3
48                 };
49
50                 enum EN ret_enum () { return _en;}
51                 void set_enum (enum EN en) { _en = en; }
52
53         private:
54                 std::list<std::string> _sl;
55                 int _int;
56                 enum EN _en;
57                 float arr[256];
58                 float ar2[256];
59                 int ar3[256];
60 };
61
62 int main (int argc, char **argv)
63 {
64 #if 0
65         LuaState lua;
66 #else
67         PBD::ReallocPool _mempool ("Devel", 1048576);
68         LuaState lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool));
69 #endif
70         lua.Print.connect (&my_lua_print);
71         lua_State* L = lua.getState();
72
73
74 #if 1
75         luabridge::getGlobalNamespace (L)
76                 .beginNamespace ("Test")
77                 .beginStdList <std::string> ("StringList")
78                 .endClass ()
79                 .endNamespace ();
80
81         luabridge::getGlobalNamespace (L)
82                 .beginNamespace ("Test")
83                 .beginStdVector <std::string> ("StringVector")
84                 .endClass ()
85                 .endNamespace ();
86
87         luabridge::getGlobalNamespace (L)
88                 .beginNamespace ("Test")
89                 .beginStdMap <std::string,std::string> ("StringStringMap")
90                 .endClass ()
91                 .endNamespace ();
92
93         luabridge::getGlobalNamespace (L)
94                 .beginNamespace ("Test")
95                 .beginStdSet <std::string> ("StringSet")
96                 .endClass ()
97                 .endNamespace ();
98
99
100         luabridge::getGlobalNamespace (L)
101                 .beginNamespace ("Test")
102                 .registerArray <float> ("FloatArray")
103                 .registerArray <int> ("IntArray")
104                 .beginClass <A> ("A")
105                 .addConstructor <void (*) ()> ()
106                 .addFunction ("set_int", &A::set_int)
107                 .addFunction ("get_int", &A::get_int)
108                 .addRefFunction ("get_arg", &A::get_arg)
109                 .addRefFunction ("get_arg2", &A::get_arg2)
110                 .addRefFunction ("get_args", &A::get_args)
111                 .addFunction ("set_ref", &A::set_ref)
112                 .addFunction ("get_list", &A::get_list)
113                 .addFunction ("set_list", &A::set_list)
114                 .addFunction ("ret_enum", &A::ret_enum)
115                 .addFunction ("set_enum", &A::set_enum)
116                 .addFunction ("get_arr", &A::get_arr)
117                 .addFunction ("get_ar2", &A::get_ar2)
118                 .addFunction ("get_ar3", &A::get_ar3)
119                 .endClass ()
120                 .endNamespace ();
121
122         luabridge::getGlobalNamespace (L)
123                 .beginNamespace ("Test")
124                 .beginClass <A> ("A")
125                 .addFunction ("minone", &A::minone)
126                 .addConst ("cologne", 4711)
127                 .endClass ()
128                 .addConst ("koln", 4711)
129                 .endNamespace ();
130 #endif
131 #if 0 // session  script test
132         lua.do_command (
133                         "function ArdourSession ()"
134                         "  local self = { scripts = {}, instances = {} }"
135                         ""
136                         "  local foreach = function (fn)"
137                         "   for n, s in pairs (self.scripts) do"
138                         "    fn (n, s)"
139                         "   end"
140                         "  end"
141                         ""
142                         "  local run = function ()"
143                         "   for n, s in pairs (self.instances) do"
144                         "     local status, err = pcall (s)"
145                         "     if not status then print ('fn \"'.. n .. '\": ', err) end"
146                         "   end"
147                         "   collectgarbage()"
148                         "  end"
149                         ""
150                         "  local add = function (n, f, a)"
151                         "   assert(type(n) == 'string', 'function-name must be string')"
152                         "   assert(type(f) == 'function', 'Given script is a not a function')"
153                         "   assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
154                         "   assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')"
155                         "   self.scripts[n] = { ['f'] = f, ['a'] = a }"
156                         "   local env = { print = print, Session = Session, tostring = tostring, assert = assert, ipairs = ipairs, error = error, string = string, type = type, tonumber = tonumber, collectgarbage = collectgarbage, pairs = pairs, math = math, table = table, pcall = pcall }"
157                         "   self.instances[n] = load (string.dump(f), nil, nil, env)(a)"
158                         "  end"
159                         ""
160                         "  local remove = function (n)"
161                         "   self.scripts[n] = nil"
162                         "  end"
163                         ""
164                         "  local list = function ()"
165                         "   local rv = {}"
166                         "   foreach (function (n) rv[n] = true end)"
167                         "   return rv"
168                         "  end"
169                         ""
170                         "  local function basic_serialize (o)"
171                         "    if type(o) == \"number\" then"
172                         "     return tostring(o)"
173                         "    else"
174                         "     return string.format(\"%q\", o)"
175                         "    end"
176                         "  end"
177                         ""
178                         "  local function serialize (name, value)"
179                         "   local rv = name .. ' = '"
180                         "   if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
181                         "    return rv .. basic_serialize(value) .. ' '"
182                         "   elseif type(value) == \"table\" then"
183                         "    rv = rv .. '{} '"
184                         "    for k,v in pairs(value) do"
185                         "     local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
186                         "     rv = rv .. serialize(fieldname, v) .. ' '"
187                         "    end"
188                         "    return rv;"
189                         "   elseif type(value) == \"function\" then"
190                         "     return rv .. string.format(\"%q\", string.dump(value))"
191                         "   else"
192                         "    error('cannot save a ' .. type(value))"
193                         "   end"
194                         "  end"
195                         ""
196                         ""
197                         "  local save = function ()"
198                         "   return (serialize('scripts', self.scripts))"
199                         "  end"
200                         ""
201                         "  local restore = function (state)"
202                         "   self.scripts = {}"
203                         "   load (state)()"
204                         "   print (scripts)"
205                         "   for n, s in pairs (scripts) do"
206                         "    add (n, load(s['f']), s['a'])"
207                         "   end"
208                         "  end"
209                         ""
210                         " return { run = run, add = add, remove = remove,"
211                   "          list = list, foreach = foreach,"
212                         "          restore = restore, save = save}"
213                         " end"
214                         " "
215                         " sess = ArdourSession ()"
216                         " ArdourSession = nil"
217                         );
218
219         luabridge::LuaRef *lua_run;
220         luabridge::LuaRef *lua_add;
221         luabridge::LuaRef *lua_del;
222         luabridge::LuaRef *lua_save;
223         luabridge::LuaRef *lua_load;
224         {
225                 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
226                 lua.do_command ("sess = nil"); // hide it.
227                 lua.do_command ("collectgarbage()");
228
229                 lua_run = new luabridge::LuaRef(lua_sess["run"]);
230                 lua_add = new luabridge::LuaRef(lua_sess["add"]);
231                 lua_del = new luabridge::LuaRef(lua_sess["remove"]);
232                 lua_save = new luabridge::LuaRef(lua_sess["save"]);
233                 lua_load = new luabridge::LuaRef(lua_sess["restore"]);
234         }
235         lua.do_command ("collectgarbage()");
236
237 #if 1
238         lua.do_command ("function factory (t) return function () local p = t or { } local a = t[1] or 'Nibor' print ('Hello ' .. a) end end");
239         luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
240         luabridge::LuaRef tbl_arg (luabridge::newTable(L));
241         //tbl_arg[1] = "Robin";
242         (*lua_add)("t2", lua_fact, tbl_arg);
243 #else
244         lua.do_command ("function factory (t) return function () print ('Boo') end end");
245         luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
246         (*lua_add)("t2", lua_fact());
247 #endif
248
249         lua.do_command ("function factory (t) return function () print ('Ahoy') end end");
250         luabridge::LuaRef lua_fact2 = luabridge::getGlobal (L, "factory");
251         (*lua_add)("t1", lua_fact2);
252
253         luabridge::LuaRef savedstate ((*lua_save)());
254         std::string saved = savedstate.cast<std::string>();
255
256         (*lua_del)("t2");
257
258         try {
259                 (*lua_run)();
260         } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
261
262         (*lua_load)(saved);
263
264         for (int i = 0; i < 2; ++i) {
265         lua.do_command ("collectgarbage()");
266         lua.collect_garbage ();
267         try {
268                 (*lua_run)();
269         } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
270         }
271
272 #endif
273
274         add_history("a = Test:A() b = 2 c = 3 d = 'a'");
275         add_history("x = a:get_arg(b)  y = a:get_arg2(b, c)  z = a:get_args(d) ");
276         add_history("for i,n in ipairs(y) do print (i, n); end");
277
278         /////////////////////////////////////////////////////////////////////////////
279         char *line;
280         while ((line = readline ("> "))) {
281                 if (!strcmp (line, "quit")) {
282                         break;
283                 }
284                 if (strlen(line) == 0) {
285                         //lua.do_command("collectgarbage();");
286                         continue;
287                 }
288                 if (!lua.do_command (line)) {
289                         add_history(line); // OK
290                 } else {
291                         add_history(line); // :)
292                 }
293         }
294         printf("\n");
295         return 0;
296 }