prepare sharing C++ class instances across lua-interpreters
authorRobin Gareus <robin@gareus.org>
Thu, 7 Jul 2016 02:44:36 +0000 (04:44 +0200)
committerRobin Gareus <robin@gareus.org>
Thu, 7 Jul 2016 13:37:11 +0000 (15:37 +0200)
in particular: lua-lifefime (!) C++ instances.
This allows for dynamic allocation of custom user-data, bound to
the lifetime of the allocating lua-context.

libs/ardour/ardour/lua_api.h
libs/ardour/lua_api.cc
libs/ardour/luabindings.cc
libs/lua/LuaBridge/detail/LuaRef.h
libs/lua/LuaBridge/detail/Userdata.h

index 9ae6bf2ff6b4cbcb35b13b4c22f9a1a39ac1e144..9af96496208383d310c8f29e46b22b9ace75c686 100644 (file)
@@ -181,6 +181,46 @@ namespace ARDOUR { namespace LuaOSC {
                        lo_address _addr;
        };
 
-} } /* namespace */
+}
+
+class LuaTableRef {
+       public:
+               LuaTableRef ();
+               ~LuaTableRef ();
+
+               int get (lua_State* L);
+               int set (lua_State* L);
+
+       private:
+               struct LuaTableEntry {
+                       LuaTableEntry (int kt, int vt)
+                               : keytype (kt)
+                               , valuetype (vt)
+                       { }
+
+                       int keytype;
+                       std::string k_s;
+                       unsigned int k_n;
+
+                       int valuetype;
+                       // LUA_TUSERDATA
+                       const void* c;
+                       void* p;
+                       // LUA_TBOOLEAN
+                       bool b;
+                       // LUA_TSTRING:
+                       std::string s;
+                       // LUA_TNUMBER:
+                       double n;
+               };
+
+               std::vector<LuaTableEntry> _data;
+
+               static void* findclasskey (lua_State *L, const void* key);
+               template<typename T>
+               static void assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s);
+};
+
+} /* namespace */
 
 #endif // _ardour_lua_api_h_
index 94b3f816c3cf72c503de6235837fd05ea1b0f8f2..edee3a34a94b3ac4507948bd1534330f7f09d5fc 100644 (file)
@@ -345,3 +345,153 @@ ARDOUR::LuaAPI::hsla_to_rgba (lua_State *L)
        luabridge::Stack<double>::push (L, a);
        return 4;
 }
+
+luabridge::LuaRef::Proxy&
+luabridge::LuaRef::Proxy::clone_instance (const void* classkey, void* p) {
+  lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_tableRef);
+  lua_rawgeti (m_L, LUA_REGISTRYINDEX, m_keyRef);
+
+       luabridge::UserdataPtr::push_raw (m_L, p, classkey);
+
+  lua_rawset (m_L, -3);
+  lua_pop (m_L, 1);
+  return *this;
+}
+
+LuaTableRef::LuaTableRef () {}
+LuaTableRef::~LuaTableRef () {}
+
+int
+LuaTableRef::get (lua_State* L)
+{
+       luabridge::LuaRef rv (luabridge::newTable (L));
+       for (std::vector<LuaTableEntry>::const_iterator i = _data.begin (); i != _data.end (); ++i) {
+               switch ((*i).keytype) {
+                       case LUA_TSTRING:
+                               assign(&rv, i->k_s, *i);
+                               break;
+                       case LUA_TNUMBER:
+                               assign(&rv, i->k_n, *i);
+                               break;
+               }
+       }
+       luabridge::push (L, rv);
+       return 1;
+}
+
+int
+LuaTableRef::set (lua_State* L)
+{
+       if (!lua_istable (L, -1)) { return luaL_error (L, "argument is not a table"); }
+       _data.clear ();
+
+       lua_pushvalue (L, -1);
+       lua_pushnil (L);
+       while (lua_next (L, -2)) {
+               lua_pushvalue (L, -2);
+
+               LuaTableEntry s (lua_type(L, -1), lua_type(L, -2));
+               switch (lua_type(L, -1)) {
+                       case LUA_TSTRING:
+                               s.k_s = luabridge::Stack<std::string>::get (L, -1);
+                               break;
+                               ;
+                       case LUA_TNUMBER:
+                               s.k_n = luabridge::Stack<unsigned int>::get (L, -1);
+                               break;
+                       default:
+                               // invalid key
+                               lua_pop (L, 2);
+                               continue;
+               }
+
+               switch(lua_type(L, -2)) {
+                       case LUA_TSTRING:
+                               s.s = luabridge::Stack<std::string>::get (L, -2);
+                               break;
+                       case LUA_TBOOLEAN:
+                               s.b = lua_toboolean (L, -2);
+                               break;
+                       case LUA_TNUMBER:
+                               s.n = lua_tonumber (L, -2);
+                               break;
+                       case LUA_TUSERDATA:
+                               {
+                                       bool ok = false;
+                                       lua_getmetatable (L, -2);
+                                       lua_rawgetp (L, -1, luabridge::getIdentityKey ());
+                                       if (lua_isboolean (L, -1)) {
+                                               lua_pop (L, 1);
+                                               const void* key = lua_topointer (L, -1);
+                                               lua_pop (L, 1);
+                                               void const* classkey = findclasskey (L, key);
+
+                                               if (classkey) {
+                                                       ok = true;
+                                                       s.c = classkey;
+                                                       s.p = luabridge::Userdata::get_ptr (L, -2);
+                                               }
+                                       }  else {
+                                               lua_pop (L, 2);
+                                       }
+
+                                       if (ok) {
+                                               break;
+                                       }
+                                       // invalid userdata -- fall through
+                               }
+                               // no break
+                       case LUA_TFUNCTION: // no support -- we could... string.format("%q", string.dump(value, true))
+                       case LUA_TTABLE: // no nested tables, sorry.
+                       case LUA_TNIL: // fallthrough
+                       default:
+                               // invalid value
+                               lua_pop (L, 2);
+                               continue;
+               }
+
+               _data.push_back(s);
+               lua_pop (L, 2);
+       }
+       return 0;
+}
+
+void*
+LuaTableRef::findclasskey (lua_State *L, const void* key)
+{
+       lua_pushvalue(L, LUA_REGISTRYINDEX);
+       lua_pushnil (L);
+       while (lua_next (L, -2)) {
+               lua_pushvalue (L, -2);
+               if (lua_topointer(L, -2) == key) {
+                       void* rv = lua_touserdata (L, -1);
+                       lua_pop (L, 4);
+                       return rv;
+               }
+               lua_pop (L, 2);
+       }
+       lua_pop (L, 1);
+       return NULL;
+}
+
+template<typename T>
+void LuaTableRef::assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s)
+{
+       switch (s.valuetype) {
+               case LUA_TSTRING:
+                       (*rv)[key] = s.s;
+                       break;
+               case LUA_TBOOLEAN:
+                       (*rv)[key] = s.b;
+                       break;
+               case LUA_TNUMBER:
+                       (*rv)[key] = s.n;
+                       break;
+               case LUA_TUSERDATA:
+                       (*rv)[key].clone_instance (s.c, s.p);
+                       break;
+               default:
+                       assert (0);
+                       break;
+       }
+}
index 393389c5ffba3e9c8a47ba414b6b0091e7e6fd0b..1ff2f3998744b92361200d9b67e607f475c0cdf7 100644 (file)
@@ -169,6 +169,7 @@ CLASSKEYS(ARDOUR::Session);
 CLASSKEYS(ARDOUR::BufferSet);
 CLASSKEYS(ARDOUR::ChanMapping);
 CLASSKEYS(ARDOUR::DSP::DspShm);
+CLASSKEYS(ARDOUR::LuaTableRef);
 CLASSKEYS(PBD::ID);
 CLASSKEYS(ARDOUR::Location);
 CLASSKEYS(ARDOUR::PluginInfo);
@@ -1373,6 +1374,12 @@ LuaBindings::dsp (lua_State* L)
                .endClass ()
 
                .endNamespace () // DSP
+
+               .beginClass <LuaTableRef> ("LuaTableRef")
+               .addCFunction ("get", &LuaTableRef::get)
+               .addCFunction ("set", &LuaTableRef::set)
+               .endClass ()
+
                .endNamespace (); // ARDOUR
 }
 
index 8fae1c038ab48f00ad14edf837472055cdf1cd41..46c756445e78523eacdb546fe8c2337a0f8b5458 100644 (file)
@@ -180,6 +180,11 @@ private:
       return *this;
     }
 
+    // the implementation needs UserdataPtr, which
+    // is not yet defined here.
+    // -> libs/ardour/lua_api.cc
+    Proxy& clone_instance (const void* key, void* p);
+
     //--------------------------------------------------------------------------
     /**
         Assign a new value to this table key.
index e832ef2dc135762a0bf5d00c588c17534e21b2e0..feab28f132b85c32c4434c6852680122797bc149 100644 (file)
@@ -302,6 +302,11 @@ ud __parent (nil)
 public:
   virtual ~Userdata () { }
 
+  static void* get_ptr (lua_State* L, int index) {
+    Userdata* ud = static_cast <Userdata*> (lua_touserdata (L, index));
+    return ud->m_p;
+  }
+
   //--------------------------------------------------------------------------
   /**
     Returns the Userdata* if the class on the Lua stack matches.
@@ -457,6 +462,15 @@ private:
     assert (m_p != 0);
   }
 
+  friend class LuaRef;
+  static inline void push_raw (lua_State* const L, void* p, const void* classkey)
+  {
+    new (lua_newuserdata (L, sizeof (UserdataPtr))) UserdataPtr (p);
+    lua_rawgetp (L, LUA_REGISTRYINDEX, classkey);
+    assert (lua_istable (L, -1));
+    lua_setmetatable (L, -2);
+  }
+
 public:
   /** Push non-const pointer to object.
   */