Lua: make external (static) functions available for const objects.

This fixes iterators on const std::lists<>& and const std::map<>&
This commit is contained in:
Robin Gareus
2017-03-30 03:39:28 +02:00
parent eb1bd9d8e2
commit 412b6a4cb9
2 changed files with 10 additions and 4 deletions

View File

@@ -1167,7 +1167,7 @@ struct CFunc
// generate an iterator
template <class T, class C>
static int listIterHelper (lua_State *L, C * const t)
static int listIterHelper (lua_State *L, C const * const t)
{
if (!t) { return luaL_error (L, "invalid pointer to std::list<>/std::vector"); }
typedef typename C::const_iterator IterType;
@@ -1180,7 +1180,7 @@ struct CFunc
template <class T, class C>
static int listIter (lua_State *L)
{
C * const t = Userdata::get <C> (L, 1, false);
C const * const t = Userdata::get <C> (L, 1, true);
return listIterHelper<T, C> (L, t);
}
@@ -1284,7 +1284,7 @@ struct CFunc
static int mapIter (lua_State *L)
{
typedef std::map<K, V> C;
C * const t = Userdata::get <C> (L, 1, false);
C const * const t = Userdata::get <C> (L, 1, true);
if (!t) { return luaL_error (L, "invalid pointer to std::map"); }
typedef typename C::const_iterator IterType;
new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin());
@@ -1378,7 +1378,7 @@ struct CFunc
template <class T, class C>
static int setIter (lua_State *L)
{
C * const t = Userdata::get <C> (L, 1, false);
C const * const t = Userdata::get <C> (L, 1, true);
if (!t) { return luaL_error (L, "invalid pointer to std::set"); }
typedef typename C::const_iterator IterType;
new (lua_newuserdata (L, sizeof (IterType*))) IterType (t->begin());

View File

@@ -984,6 +984,8 @@ private:
DATADOC ("Ext C Function", name, fp)
assert (lua_istable (L, -1));
lua_pushcclosure (L, fp, 0);
lua_pushvalue (L, -1);
rawsetfield (L, -5, name); // const table
rawsetfield (L, -3, name); // class table
return *this;
}
@@ -1320,11 +1322,15 @@ private:
set_shared_class ();
assert (lua_istable (L, -1));
lua_pushcclosure (L, fp, 0);
lua_pushvalue (L, -1);
rawsetfield (L, -5, name); // const table
rawsetfield (L, -3, name); // class table
set_weak_class ();
assert (lua_istable (L, -1));
lua_pushcclosure (L, fp, 0);
lua_pushvalue (L, -1);
rawsetfield (L, -5, name); // const table
rawsetfield (L, -3, name); // class table
return *this;