Skip to content

Commit

Permalink
implement array indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
pisto committed Mar 4, 2014
1 parent 6447617 commit d381825
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fpsgame/server.cpp
Expand Up @@ -3628,6 +3628,12 @@ using namespace luabridge;
void bindserver(){
//server::
using namespace server;
#define addGlobalRef(name, global) addProperty(name, (decltype(global)*(*)())([]()->decltype(global)*{ return &global; }))
#define addArray(T)\
.beginClass<T>(#T)\
.addFunction("__arrayindex", &T::__arrayindex)\
.addFunction("__arraynewindex", &T::__arraynewindex)\
.endClass()
getGlobalNamespace(L).beginNamespace("server")
.addFunction("sendservmsg", &sendservmsg)
.beginClass<clientinfo>("clientinfo")
Expand Down
31 changes: 31 additions & 0 deletions include/LuaBridge/detail/CFunctions.h
Expand Up @@ -92,6 +92,22 @@ struct CFunc
}
}

lua_Number number;
if ((!result || lua_type (L, -1) == LUA_TNIL) && ({ int isnumber; number = lua_tonumberx (L, 2, &isnumber); isnumber; }))
{
lua_getfield (L, 1, "__arrayindex");
if (lua_type (L, -1) != LUA_TNIL)
{
lua_insert (L, 1);
lua_pushnumber (L, number);
lua_replace (L, 3);
lua_settop (L, 3);
lua_call (L, 2, 1);
return 1;
}
else lua_pop (L, 1);
}

return result;
}

Expand Down Expand Up @@ -138,6 +154,21 @@ struct CFunc
{
assert (lua_isnil (L, -1));
lua_pop (L, 2);
lua_Number number;
if (({ int isnumber; number = lua_tonumberx (L, 2, &isnumber); isnumber; }))
{
lua_getfield (L, 1, "__arraynewindex");
if (lua_type (L, -1) != LUA_TNIL)
{
lua_insert (L, 1);
lua_pushnumber (L, number);
lua_replace (L, 3);
lua_settop (L, 4);
lua_call (L, 3, 0);
return 0;
}
else lua_pop (L, 1);
}
result = luaL_error (L,"no writable variable '%s'", lua_tostring (L, 2));
}
}
Expand Down
31 changes: 31 additions & 0 deletions include/LuaBridge/detail/Namespace.h
Expand Up @@ -201,6 +201,22 @@ class Namespace
}
}

lua_Number number;
if ((!result || lua_type (L, -1) == LUA_TNIL) && ({ int isnumber; number = lua_tonumberx (L, 2, &isnumber); isnumber; }))
{
lua_getfield (L, 1, "__arrayindex");
if (lua_type (L, -1) != LUA_TNIL)
{
lua_insert (L, 1);
lua_pushnumber (L, number);
lua_replace (L, 3);
lua_settop (L, 3);
lua_call (L, 2, 1);
return 1;
}
else lua_pop (L, 1);
}

return result;
}

Expand Down Expand Up @@ -243,6 +259,21 @@ class Namespace
rawgetfield (L, -1, "__parent");
if (lua_isnil (L, -1))
{
lua_Number number;
if (({ int isnumber; number = lua_tonumberx (L, 2, &isnumber); isnumber; }))
{
lua_getfield (L, 1, "__arraynewindex");
if (lua_type (L, -1) != LUA_TNIL)
{
lua_insert (L, 1);
lua_pushnumber (L, number);
lua_replace (L, 3);
lua_settop (L, 4);
lua_call (L, 3, 0);
return 0;
}
else lua_pop (L, 1);
}
// Either the property or __parent must exist.
result = luaL_error (L,
"no member named '%s'", lua_tostring (L, 2));
Expand Down
29 changes: 29 additions & 0 deletions shared/cube.h
Expand Up @@ -24,6 +24,35 @@
#include <climits>
#include <cassert>
#include <ctime>
#include <array>
#include <type_traits>
#include <lua.hpp>

namespace spaghetti
{
extern lua_State *L;
}

template<typename T, size_t len>
struct lua_array : std::array<T, len>
{
using up = std::array<T, len>;
using value_type = typename std::conditional<std::is_scalar<T>::value, T, T&>::type;
lua_array(const lua_array&) = delete;
lua_array() = default;
using up::up;
value_type __arrayindex(int i){
if(i<0 || i>=len) luaL_error(spaghetti::L, "Index %d is out of array bounds (%d)", i, int(len));
return static_cast<up&>(*this)[i];
}
void __arraynewindex(int i, value_type val){
if(i<0 || i>=len) luaL_error(spaghetti::L, "Index %d is out of array bounds (%d)", i, int(len));
static_cast<up&>(*this)[i] = val;
}
operator T*(){
return up::data();
}
};

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
Expand Down

0 comments on commit d381825

Please sign in to comment.