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";
22 luabridge::LuaRef::Proxy&
23 luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
24 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
25 lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
27 luabridge::UserdataPtr::push_raw (m_L, p, classkey);
38 A() { printf ("CTOR %p\n", this); _int = 4; for (int i = 0; i < 256; ++i) {arr[i] = i; ar2[i] = i/256.0; ar3[i] = i;} }
39 ~A() { printf ("DTOR\n"); }
41 void set_int (int a) { _int = a; }
42 int get_int () const { return _int; }
44 int& get_ref () { return _int; }
45 int get_arg (int &a) { printf ("a = %d\n", a); a = _int; printf ("a = %d\n", a); return 1; }
46 void get_arg2 (int &a, int& b) { a = _int; b = 100; }
47 void get_args (std::string &a) { a = "hello"; }
48 void set_ref (int const &a) { _int = a; }
50 float * get_arr () { return arr; }
51 float * get_ar2 () { return ar2; }
52 int * get_ar3 () { return ar3; }
54 void set_list (std::list<std::string> sl) { _sl = sl; }
55 std::list<std::string>& get_list () { return _sl; }
57 uint32_t minone() { return -1; }
58 void pointer (float*f) const { printf ("PTR %p", f); }
64 enum EN ret_enum () { return _en;}
65 void set_enum (enum EN en) { _en = en; }
68 std::list<std::string> _sl;
82 int get (lua_State* L) {
83 luabridge::LuaRef rv (luabridge::newTable (L));
84 for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
85 switch ((*i).keytype) {
87 assign(&rv, i->k_s, *i);
90 assign(&rv, i->k_n, *i);
94 luabridge::push (L, rv);
98 int set (lua_State* L) {
99 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
102 lua_pushvalue (L, -1);
104 while (lua_next (L, -2)) {
105 lua_pushvalue (L, -2);
107 LuaTableEntry s (lua_type(L, -1), lua_type(L, -2));
108 switch (lua_type(L, -1)) {
110 s.k_s = luabridge::Stack<std::string>::get (L, -1);
114 s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
122 switch(lua_type(L, -2)) {
124 s.s = luabridge::Stack<std::string>::get (L, -2);
127 s.b = lua_toboolean (L, -2);
130 s.n = lua_tonumber (L, -2);
135 lua_getmetatable (L, -2);
136 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
137 if (lua_isboolean (L, -1)) {
139 const void* key = lua_topointer (L, -1);
141 void const* classkey = findclasskey (L, key);
146 s.p = luabridge::Userdata::get_ptr (L, -2);
155 // invalid userdata -- fall through
159 case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
160 case LUA_TTABLE: // no nested tables, sorry.
161 case LUA_TNIL: // fallthrough
174 static void* findclasskey (lua_State *L, const void* key)
176 lua_pushvalue(L, LUA_REGISTRYINDEX);
178 while (lua_next (L, -2)) {
179 lua_pushvalue (L, -2);
180 if (lua_topointer(L, -2) == key) {
181 void* rv = lua_touserdata (L, -1);
192 struct LuaTableEntry {
193 LuaTableEntry (int kt, int vt)
215 static void assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
217 switch (s.valuetype) {
228 (*rv)[key].clone_instance (s.c, s.p);
236 std::vector<LuaTableEntry> _data;
241 static void* findclasskey (lua_State *L, const void* key)
243 lua_pushvalue(L, LUA_REGISTRYINDEX);
245 while (lua_next (L, -2)) {
246 lua_pushvalue (L, -2);
247 if (lua_topointer(L, -2) == key) {
248 void* rv = lua_touserdata (L, -1);
258 static int tableSerialize (lua_State *L)
260 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
262 luabridge::LuaRef rv (luabridge::newTable (L));
263 std::cout << "CLASS A KEY: " << luabridge::ClassInfo <A>::getClassKey () << "\n";
264 lua_rawgetp (L, LUA_REGISTRYINDEX, luabridge::ClassInfo <A>::getClassKey ());
265 std::cout << " CLASS A TABLE PTR=" << lua_topointer (L, -1) << "\n";
267 // for k,v in pairs (debug.getregistry ()) do print (k,v) end
269 lua_pushvalue (L, -1);
271 while (lua_next (L, -2)) {
272 lua_pushvalue (L, -2);
273 unsigned int const i = luabridge::Stack<unsigned int>::get (L, -1);
274 int t = lua_type(L, -2);
277 std::cout << " " << i << ": '" << lua_tostring(L, -2) << "'\n";
278 rv[i] = lua_tostring(L, -2);
281 std::cout << " " << i << ": " <<
282 (lua_toboolean(L, -2) ? "true" : "false") << "\n";
283 rv[i] = lua_toboolean(L, -2);
286 std::cout << " " << i << ": " << lua_tonumber(L, -2) << "\n";
287 rv[i] = lua_tonumber(L, -2);
291 lua_getmetatable (L, -2);
292 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
293 if (lua_isboolean (L, -1)) {
295 const void* key = lua_topointer (L, -1);
297 void const* classkey = findclasskey (L, key);
300 void* p = luabridge::Userdata::get_ptr (L, -2);
301 rv[i].clone_instance (classkey, p);
311 case LUA_TLIGHTUSERDATA:
313 std::cout << " " << i << ": TYPE=" << t << ": " << lua_topointer(L, -2)<< "\n";
321 luabridge::push (L, rv);
326 LuaTableRef globalref;
328 int runone (LuaState& lua)
331 LuaState lua (*new LuaState);
333 PBD::ReallocPool _mempool ("Devel", 1048576);
334 LuaState lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool));
336 lua.Print.connect (&my_lua_print);
337 lua_State* L = lua.getState();
341 luabridge::getGlobalNamespace (L)
342 .beginNamespace ("Test")
343 .beginStdList <std::string> ("StringList")
347 luabridge::getGlobalNamespace (L)
348 .beginNamespace ("Test")
349 .beginStdVector <std::string> ("StringVector")
353 luabridge::getGlobalNamespace (L)
354 .beginNamespace ("Test")
355 .beginStdMap <std::string,std::string> ("StringStringMap")
359 luabridge::getGlobalNamespace (L)
360 .beginNamespace ("Test")
361 .beginStdSet <std::string> ("StringSet")
366 luabridge::getGlobalNamespace (L)
367 .beginNamespace ("Test")
368 .registerArray <float> ("FloatArray")
369 .registerArray <int> ("IntArray")
370 .beginClass <A> ("A")
371 .addConstructor <void (*) ()> ()
372 .addFunction ("set_int", &A::set_int)
373 .addFunction ("get_int", &A::get_int)
374 .addRefFunction ("get_arg", &A::get_arg)
375 .addRefFunction ("get_arg2", &A::get_arg2)
376 .addRefFunction ("get_args", &A::get_args)
377 .addFunction ("set_ref", &A::set_ref)
378 .addFunction ("get_list", &A::get_list)
379 .addFunction ("set_list", &A::set_list)
380 .addFunction ("ret_enum", &A::ret_enum)
381 .addFunction ("set_enum", &A::set_enum)
382 .addFunction ("get_arr", &A::get_arr)
383 .addFunction ("get_ar2", &A::get_ar2)
384 .addFunction ("get_ar3", &A::get_ar3)
388 luabridge::getGlobalNamespace (L)
389 .beginNamespace ("Test")
390 .beginClass <A> ("A")
391 .addFunction ("pointer", &A::pointer)
392 .addFunction ("minone", &A::minone)
393 .addConst ("cologne", 4711)
395 .addConst ("koln", 4711)
398 luabridge::getGlobalNamespace (L)
399 .beginNamespace ("Dump")
401 .beginClass <LuaTableRef> ("TableRef")
402 .addCFunction ("get", &LuaTableRef::get)
403 .addCFunction ("set", &LuaTableRef::set)
406 //.addCFunction ("dump", tableSerialize)
409 luabridge::push <LuaTableRef *> (L, &globalref);
410 lua_setglobal (L, "ref");
413 #if 0 // session script test
415 "function ArdourSession ()"
416 " local self = { scripts = {}, instances = {} }"
418 " local foreach = function (fn)"
419 " for n, s in pairs (self.scripts) do"
424 " local run = function ()"
425 " for n, s in pairs (self.instances) do"
426 " local status, err = pcall (s)"
427 " if not status then print ('fn \"'.. n .. '\": ', err) end"
432 " local add = function (n, f, a)"
433 " assert(type(n) == 'string', 'function-name must be string')"
434 " assert(type(f) == 'function', 'Given script is a not a function')"
435 " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
436 " assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')"
437 " self.scripts[n] = { ['f'] = f, ['a'] = a }"
438 " 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 }"
439 " self.instances[n] = load (string.dump(f), nil, nil, env)(a)"
442 " local remove = function (n)"
443 " self.scripts[n] = nil"
446 " local list = function ()"
448 " foreach (function (n) rv[n] = true end)"
452 " local function basic_serialize (o)"
453 " if type(o) == \"number\" then"
454 " return tostring(o)"
456 " return string.format(\"%q\", o)"
460 " local function serialize (name, value)"
461 " local rv = name .. ' = '"
462 " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
463 " return rv .. basic_serialize(value) .. ' '"
464 " elseif type(value) == \"table\" then"
466 " for k,v in pairs(value) do"
467 " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
468 " rv = rv .. serialize(fieldname, v) .. ' '"
471 " elseif type(value) == \"function\" then"
472 " return rv .. string.format(\"%q\", string.dump(value))"
474 " error('cannot save a ' .. type(value))"
479 " local save = function ()"
480 " return (serialize('scripts', self.scripts))"
483 " local restore = function (state)"
487 " for n, s in pairs (scripts) do"
488 " add (n, load(s['f']), s['a'])"
492 " return { run = run, add = add, remove = remove,"
493 " list = list, foreach = foreach,"
494 " restore = restore, save = save}"
497 " sess = ArdourSession ()"
498 " ArdourSession = nil"
501 luabridge::LuaRef *lua_run;
502 luabridge::LuaRef *lua_add;
503 luabridge::LuaRef *lua_del;
504 luabridge::LuaRef *lua_save;
505 luabridge::LuaRef *lua_load;
507 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
508 lua.do_command ("sess = nil"); // hide it.
509 lua.do_command ("collectgarbage()");
511 lua_run = new luabridge::LuaRef(lua_sess["run"]);
512 lua_add = new luabridge::LuaRef(lua_sess["add"]);
513 lua_del = new luabridge::LuaRef(lua_sess["remove"]);
514 lua_save = new luabridge::LuaRef(lua_sess["save"]);
515 lua_load = new luabridge::LuaRef(lua_sess["restore"]);
517 lua.do_command ("collectgarbage()");
520 lua.do_command ("function factory (t) return function () local p = t or { } local a = t[1] or 'Nibor' print ('Hello ' .. a) end end");
521 luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
522 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
523 //tbl_arg[1] = "Robin";
524 (*lua_add)("t2", lua_fact, tbl_arg);
526 lua.do_command ("function factory (t) return function () print ('Boo') end end");
527 luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
528 (*lua_add)("t2", lua_fact());
531 lua.do_command ("function factory (t) return function () print ('Ahoy') end end");
532 luabridge::LuaRef lua_fact2 = luabridge::getGlobal (L, "factory");
533 (*lua_add)("t1", lua_fact2);
535 luabridge::LuaRef savedstate ((*lua_save)());
536 std::string saved = savedstate.cast<std::string>();
542 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
546 for (int i = 0; i < 2; ++i) {
547 lua.do_command ("collectgarbage()");
548 lua.collect_garbage ();
551 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
556 add_history("a = Test:A() b = 2 c = 3 d = 'a'");
557 add_history("x = a:get_arg(b) y = a:get_arg2(b, c) z = a:get_args(d) ");
558 add_history("for i,n in ipairs(y) do print (i, n); end");
559 add_history("t = {} t[2] = 7; t[3] = Test:A() t[4] = Test:A() ref:set (t); f = ref:get()");
561 /////////////////////////////////////////////////////////////////////////////
563 while ((line = readline ("> "))) {
564 if (!strcmp (line, "quit")) {
567 if (strlen(line) == 0) {
568 //lua.do_command("collectgarbage();");
571 if (!lua.do_command (line)) {
572 add_history(line); // OK
574 add_history(line); // :)
581 int main (int argc, char **argv)