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.
175 static void* findclasskey (lua_State *L, const void* key)
177 lua_pushvalue(L, LUA_REGISTRYINDEX);
179 while (lua_next (L, -2)) {
180 lua_pushvalue (L, -2);
181 if (lua_topointer(L, -2) == key) {
182 void* rv = lua_touserdata (L, -1);
193 struct LuaTableEntry {
194 LuaTableEntry (int kt, int vt)
216 static void assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
218 switch (s.valuetype) {
229 (*rv)[key].clone_instance (s.c, s.p);
237 std::vector<LuaTableEntry> _data;
242 static void* findclasskey (lua_State *L, const void* key)
244 lua_pushvalue(L, LUA_REGISTRYINDEX);
246 while (lua_next (L, -2)) {
247 lua_pushvalue (L, -2);
248 if (lua_topointer(L, -2) == key) {
249 void* rv = lua_touserdata (L, -1);
259 static int tableSerialize (lua_State *L)
261 if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
263 luabridge::LuaRef rv (luabridge::newTable (L));
264 std::cout << "CLASS A KEY: " << luabridge::ClassInfo <A>::getClassKey () << "\n";
265 lua_rawgetp (L, LUA_REGISTRYINDEX, luabridge::ClassInfo <A>::getClassKey ());
266 std::cout << " CLASS A TABLE PTR=" << lua_topointer (L, -1) << "\n";
268 // for k,v in pairs (debug.getregistry ()) do print (k,v) end
270 lua_pushvalue (L, -1);
272 while (lua_next (L, -2)) {
273 lua_pushvalue (L, -2);
274 unsigned int const i = luabridge::Stack<unsigned int>::get (L, -1);
275 int t = lua_type(L, -2);
278 std::cout << " " << i << ": '" << lua_tostring(L, -2) << "'\n";
279 rv[i] = lua_tostring(L, -2);
282 std::cout << " " << i << ": " <<
283 (lua_toboolean(L, -2) ? "true" : "false") << "\n";
284 rv[i] = lua_toboolean(L, -2);
287 std::cout << " " << i << ": " << lua_tonumber(L, -2) << "\n";
288 rv[i] = lua_tonumber(L, -2);
292 lua_getmetatable (L, -2);
293 lua_rawgetp (L, -1, luabridge::getIdentityKey ());
294 if (lua_isboolean (L, -1)) {
296 const void* key = lua_topointer (L, -1);
298 void const* classkey = findclasskey (L, key);
301 void* p = luabridge::Userdata::get_ptr (L, -2);
302 rv[i].clone_instance (classkey, p);
312 case LUA_TLIGHTUSERDATA:
314 std::cout << " " << i << ": TYPE=" << t << ": " << lua_topointer(L, -2)<< "\n";
322 luabridge::push (L, rv);
327 LuaTableRef globalref;
329 int runone (LuaState& lua)
332 LuaState lua (*new LuaState);
334 PBD::ReallocPool _mempool ("Devel", 1048576);
335 LuaState lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool));
337 lua.Print.connect (&my_lua_print);
338 lua_State* L = lua.getState();
342 luabridge::getGlobalNamespace (L)
343 .beginNamespace ("Test")
344 .beginStdList <std::string> ("StringList")
348 luabridge::getGlobalNamespace (L)
349 .beginNamespace ("Test")
350 .beginStdVector <std::string> ("StringVector")
354 luabridge::getGlobalNamespace (L)
355 .beginNamespace ("Test")
356 .beginStdMap <std::string,std::string> ("StringStringMap")
360 luabridge::getGlobalNamespace (L)
361 .beginNamespace ("Test")
362 .beginStdSet <std::string> ("StringSet")
367 luabridge::getGlobalNamespace (L)
368 .beginNamespace ("Test")
369 .registerArray <float> ("FloatArray")
370 .registerArray <int> ("IntArray")
371 .beginClass <A> ("A")
372 .addConstructor <void (*) ()> ()
373 .addFunction ("set_int", &A::set_int)
374 .addFunction ("get_int", &A::get_int)
375 .addRefFunction ("get_arg", &A::get_arg)
376 .addRefFunction ("get_arg2", &A::get_arg2)
377 .addRefFunction ("get_args", &A::get_args)
378 .addFunction ("set_ref", &A::set_ref)
379 .addFunction ("get_list", &A::get_list)
380 .addFunction ("set_list", &A::set_list)
381 .addFunction ("ret_enum", &A::ret_enum)
382 .addFunction ("set_enum", &A::set_enum)
383 .addFunction ("get_arr", &A::get_arr)
384 .addFunction ("get_ar2", &A::get_ar2)
385 .addFunction ("get_ar3", &A::get_ar3)
389 luabridge::getGlobalNamespace (L)
390 .beginNamespace ("Test")
391 .beginClass <A> ("A")
392 .addFunction ("pointer", &A::pointer)
393 .addFunction ("minone", &A::minone)
394 .addConst ("cologne", 4711)
396 .addConst ("koln", 4711)
399 luabridge::getGlobalNamespace (L)
400 .beginNamespace ("Dump")
402 .beginClass <LuaTableRef> ("TableRef")
403 .addCFunction ("get", &LuaTableRef::get)
404 .addCFunction ("set", &LuaTableRef::set)
407 //.addCFunction ("dump", tableSerialize)
410 luabridge::push <LuaTableRef *> (L, &globalref);
411 lua_setglobal (L, "ref");
414 #if 0 // session script test
416 "function ArdourSession ()"
417 " local self = { scripts = {}, instances = {} }"
419 " local foreach = function (fn)"
420 " for n, s in pairs (self.scripts) do"
425 " local run = function ()"
426 " for n, s in pairs (self.instances) do"
427 " local status, err = pcall (s)"
428 " if not status then print ('fn \"'.. n .. '\": ', err) end"
433 " local add = function (n, f, a)"
434 " assert(type(n) == 'string', 'function-name must be string')"
435 " assert(type(f) == 'function', 'Given script is a not a function')"
436 " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')"
437 " assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')"
438 " self.scripts[n] = { ['f'] = f, ['a'] = a }"
439 " 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 }"
440 " self.instances[n] = load (string.dump(f), nil, nil, env)(a)"
443 " local remove = function (n)"
444 " self.scripts[n] = nil"
447 " local list = function ()"
449 " foreach (function (n) rv[n] = true end)"
453 " local function basic_serialize (o)"
454 " if type(o) == \"number\" then"
455 " return tostring(o)"
457 " return string.format(\"%q\", o)"
461 " local function serialize (name, value)"
462 " local rv = name .. ' = '"
463 " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then"
464 " return rv .. basic_serialize(value) .. ' '"
465 " elseif type(value) == \"table\" then"
467 " for k,v in pairs(value) do"
468 " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))"
469 " rv = rv .. serialize(fieldname, v) .. ' '"
472 " elseif type(value) == \"function\" then"
473 " return rv .. string.format(\"%q\", string.dump(value))"
475 " error('cannot save a ' .. type(value))"
480 " local save = function ()"
481 " return (serialize('scripts', self.scripts))"
484 " local restore = function (state)"
488 " for n, s in pairs (scripts) do"
489 " add (n, load(s['f']), s['a'])"
493 " return { run = run, add = add, remove = remove,"
494 " list = list, foreach = foreach,"
495 " restore = restore, save = save}"
498 " sess = ArdourSession ()"
499 " ArdourSession = nil"
502 luabridge::LuaRef *lua_run;
503 luabridge::LuaRef *lua_add;
504 luabridge::LuaRef *lua_del;
505 luabridge::LuaRef *lua_save;
506 luabridge::LuaRef *lua_load;
508 luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess");
509 lua.do_command ("sess = nil"); // hide it.
510 lua.do_command ("collectgarbage()");
512 lua_run = new luabridge::LuaRef(lua_sess["run"]);
513 lua_add = new luabridge::LuaRef(lua_sess["add"]);
514 lua_del = new luabridge::LuaRef(lua_sess["remove"]);
515 lua_save = new luabridge::LuaRef(lua_sess["save"]);
516 lua_load = new luabridge::LuaRef(lua_sess["restore"]);
518 lua.do_command ("collectgarbage()");
521 lua.do_command ("function factory (t) return function () local p = t or { } local a = t[1] or 'Nibor' print ('Hello ' .. a) end end");
522 luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
523 luabridge::LuaRef tbl_arg (luabridge::newTable(L));
524 //tbl_arg[1] = "Robin";
525 (*lua_add)("t2", lua_fact, tbl_arg);
527 lua.do_command ("function factory (t) return function () print ('Boo') end end");
528 luabridge::LuaRef lua_fact = luabridge::getGlobal (L, "factory");
529 (*lua_add)("t2", lua_fact());
532 lua.do_command ("function factory (t) return function () print ('Ahoy') end end");
533 luabridge::LuaRef lua_fact2 = luabridge::getGlobal (L, "factory");
534 (*lua_add)("t1", lua_fact2);
536 luabridge::LuaRef savedstate ((*lua_save)());
537 std::string saved = savedstate.cast<std::string>();
543 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
547 for (int i = 0; i < 2; ++i) {
548 lua.do_command ("collectgarbage()");
549 lua.collect_garbage ();
552 } catch (luabridge::LuaException const& e) { printf ("LuaException: %s\n", e.what ()); }
557 add_history("a = Test:A() b = 2 c = 3 d = 'a'");
558 add_history("x = a:get_arg(b) y = a:get_arg2(b, c) z = a:get_args(d) ");
559 add_history("for i,n in ipairs(y) do print (i, n); end");
560 add_history("t = {} t[2] = 7; t[3] = Test:A() t[4] = Test:A() ref:set (t); f = ref:get()");
562 /////////////////////////////////////////////////////////////////////////////
564 while ((line = readline ("> "))) {
565 if (!strcmp (line, "quit")) {
568 if (strlen(line) == 0) {
569 //lua.do_command("collectgarbage();");
572 if (!lua.do_command (line)) {
573 add_history(line); // OK
575 add_history(line); // :)
582 int main (int argc, char **argv)