Skip to content
Permalink
Browse files
implement array indexing
  • Loading branch information
pisto committed Mar 4, 2014
1 parent 6447617 commit d3818253ff480bfc0d7287742e97937da9bdf4e0
Showing with 97 additions and 0 deletions.
  1. +6 −0 fpsgame/server.cpp
  2. +31 −0 include/LuaBridge/detail/CFunctions.h
  3. +31 −0 include/LuaBridge/detail/Namespace.h
  4. +29 −0 shared/cube.h
@@ -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")
@@ -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;
}

@@ -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));
}
}
@@ -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;
}

@@ -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));
@@ -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

0 comments on commit d381825

Please sign in to comment.