9 #include "../../libs/pbd/pbd/reallocpool.h"
10 #include "../../libs/pbd/reallocpool.cc"
12 #include <readline/readline.h>
13 #include <readline/history.h>
15 #include "lua/luastate.h"
16 #include "LuaBridge/LuaBridge.h"
18 static void my_lua_print (std::string s) {
19 std::cout << s << "\n";
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"); }
28 void set_int (int a) { _int = a; }
29 int get_int () const { return _int; }
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; }
37 float * get_arr () { return arr; }
38 float * get_ar2 () { return ar2; }
39 int * get_ar3 () { return ar3; }
41 void set_list (std::list<std::string> sl) { _sl = sl; }
42 std::list<std::string>& get_list () { return _sl; }
44 uint32_t minone() { return -1; }
50 enum EN ret_enum () { return _en;}
51 void set_enum (enum EN en) { _en = en; }
54 std::list<std::string> _sl;
62 int main (int argc, char **argv)
67 PBD::ReallocPool _mempool ("Devel", 1048576);
68 LuaState lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool));
70 lua.Print.connect (&my_lua_print);
71 lua_State* L = lua.getState();
75 luabridge::getGlobalNamespace (L)
76 .beginNamespace ("Test")
77 .beginStdList <std::string> ("StringList")
81 luabridge::getGlobalNamespace (L)
82 .beginNamespace ("Test")
83 .beginStdVector <std::string> ("StringVector")
87 luabridge::getGlobalNamespace (L)
88 .beginNamespace ("Test")
89 .beginStdMap <std::string,std::string> ("StringStringMap")
93 luabridge::getGlobalNamespace (L)
94 .beginNamespace ("Test")
95 .beginStdSet <std::string> ("StringSet")
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)
122 luabridge::getGlobalNamespace (L)
123 .beginNamespace ("Test")
124 .beginClass <A> ("A")
125 .addFunction ("minone", &A::minone)
126 .addConst ("cologne", 4711)
128 .addConst ("koln", 4711)
131 #if 0 // session script test
133 "function ArdourSession ()"
134 " local self = { scripts = {}, instances = {} }"
136 " local foreach = function (fn)"
137 " for n, s in pairs (self.scripts) do"
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"
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)"
160 " local remove = function (n)"
161 " self.scripts[n] = nil"
164 " local list = function ()"
166 " foreach (function (n) rv[n] = true end)"
170 " local function basic_serialize (o)"
171 " if type(o) == \"number\" then"
172 " return tostring(o)"
174 " return string.format(\"%q\", o)"
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"
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) .. ' '"
189 " elseif type(value) == \"function\" then"
190 " return rv .. string.format(\"%q\", string.dump(value))"
192 " error('cannot save a ' .. type(value))"
197 " local save = function ()"
198 " return (serialize('scripts', self.scripts))"
201 " local restore = function (state)"
205 " for n, s in pairs (scripts) do"
206 " add (n, load(s['f']), s['a'])"
210 " return { run = run, add = add, remove = remove,"
211 " list = list, foreach = foreach,"
212 " restore = restore, save = save}"
215 " sess = ArdourSession ()"
216 " ArdourSession = nil"
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;
225 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
226 lua.do_command ("sess = nil"); // hide it.
227 lua.do_command ("collectgarbage()");
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"]);
235 lua.do_command ("collectgarbage()");
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);
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());
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);
253 luabridge::LuaRef savedstate ((*lua_save)());
254 std::string saved = savedstate.cast<std::string>();
260 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
264 for (int i = 0; i < 2; ++i) {
265 lua.do_command ("collectgarbage()");
266 lua.collect_garbage ();
269 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
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");
278 /////////////////////////////////////////////////////////////////////////////
280 while ((line = readline ("> "))) {
281 if (!strcmp (line, "quit")) {
284 if (strlen(line) == 0) {
285 //lua.do_command("collectgarbage();");
288 if (!lua.do_command (line)) {
289 add_history(line); // OK
291 add_history(line); // :)