[8032] | 1 | #ifndef _LUNAR_H |
---|
| 2 | #define _LUNAR_H |
---|
| 3 | |
---|
[7962] | 4 | #include <string> |
---|
[8271] | 5 | #include <cassert> |
---|
| 6 | #include "script.h" |
---|
[7821] | 7 | |
---|
[7962] | 8 | #include "luaincl.h" |
---|
[8408] | 9 | #include "script_method.h" |
---|
[7962] | 10 | |
---|
| 11 | |
---|
[8032] | 12 | |
---|
[8408] | 13 | template <typename T> class Lunar |
---|
| 14 | { |
---|
| 15 | typedef struct { T *pT; } |
---|
| 16 | userdataType; |
---|
| 17 | public: |
---|
[9869] | 18 | typedef Executor<lua_State*>* mfp; |
---|
[8408] | 19 | typedef struct { const char *name; mfp mfunc; } |
---|
| 20 | RegType; |
---|
[7821] | 21 | |
---|
[8032] | 22 | |
---|
[8408] | 23 | static void Register(Script* script, const std::string& className, const ScriptMethod* scriptMethods) |
---|
| 24 | { |
---|
| 25 | if(script != NULL) |
---|
| 26 | Register(script->getLuaState(), className, scriptMethods); |
---|
| 27 | } |
---|
[8032] | 28 | |
---|
[8408] | 29 | static void Register(lua_State *L, const std::string& className, const ScriptMethod* scriptMethods) |
---|
| 30 | { |
---|
| 31 | Lunar<T>::className = className; |
---|
| 32 | Lunar<T>::scriptMethods = scriptMethods; |
---|
| 33 | lua_newtable(L); |
---|
| 34 | int methods = lua_gettop(L); |
---|
[7821] | 35 | |
---|
[8408] | 36 | luaL_newmetatable(L, Lunar<T>::className.c_str()); |
---|
| 37 | int metatable = lua_gettop(L); |
---|
[7821] | 38 | |
---|
[8408] | 39 | // store method table in globals so that |
---|
| 40 | // scripts can add functions written in Lua. |
---|
| 41 | lua_pushvalue(L, methods); |
---|
| 42 | set(L, LUA_GLOBALSINDEX, Lunar<T>::className.c_str()); |
---|
[7821] | 43 | |
---|
[8408] | 44 | // hide metatable from Lua getmetatable() |
---|
| 45 | lua_pushvalue(L, methods); |
---|
| 46 | set(L, metatable, "__metatable"); |
---|
[7821] | 47 | |
---|
[8408] | 48 | lua_pushvalue(L, methods); |
---|
| 49 | set(L, metatable, "__index"); |
---|
[7821] | 50 | |
---|
[8408] | 51 | lua_pushcfunction(L, tostring_T); |
---|
| 52 | set(L, metatable, "__tostring"); |
---|
[7821] | 53 | |
---|
[8408] | 54 | lua_pushcfunction(L, gc_T); |
---|
| 55 | set(L, metatable, "__gc"); |
---|
[7821] | 56 | |
---|
[8408] | 57 | lua_newtable(L); // mt for method table |
---|
| 58 | lua_pushcfunction(L, new_T); |
---|
| 59 | lua_pushvalue(L, -1); // dup new_T function |
---|
| 60 | set(L, methods, "new"); // add new_T to method table |
---|
| 61 | set(L, -3, "__call"); // mt.__call = new_T |
---|
| 62 | lua_setmetatable(L, methods); |
---|
[7821] | 63 | |
---|
[8408] | 64 | // fill method table with methods from class T |
---|
| 65 | /* as it was originally |
---|
| 66 | for (RegType *l = T::methods; l->name; l++) |
---|
| 67 | { |
---|
| 68 | lua_pushstring(L, l->name); |
---|
| 69 | lua_pushlightuserdata(L, (void*)l); |
---|
| 70 | lua_pushcclosure(L, thunk, 1); |
---|
| 71 | lua_settable(L, methods); |
---|
| 72 | }*/ |
---|
| 73 | for (unsigned int i = 0; i < Lunar<T>::scriptMethods->size(); i++) |
---|
| 74 | { |
---|
| 75 | lua_pushstring(L, Lunar<T>::scriptMethods->name(i).c_str()); |
---|
| 76 | lua_pushlightuserdata(L, (void*)Lunar<T>::scriptMethods->executor(i)); |
---|
| 77 | lua_pushcclosure(L, thunk, 1); |
---|
| 78 | lua_settable(L, methods); |
---|
| 79 | } |
---|
| 80 | lua_pop(L, 2); // drop metatable and method table |
---|
| 81 | } |
---|
[7821] | 82 | |
---|
[8408] | 83 | // call named lua method from userdata method table |
---|
| 84 | static int call(lua_State *L, const char *method, |
---|
| 85 | int nargs=0, int nresults=LUA_MULTRET, int errfunc=0) |
---|
| 86 | { |
---|
| 87 | int base = lua_gettop(L) - nargs; // userdata index |
---|
| 88 | if (!luaL_checkudata(L, base, Lunar<T>::className.c_str())) |
---|
| 89 | { |
---|
| 90 | lua_settop(L, base-1); // drop userdata and args |
---|
| 91 | lua_pushfstring(L, "not a valid %s userdata", Lunar<T>::className.c_str()); |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
[7821] | 94 | |
---|
[8408] | 95 | lua_pushstring(L, method); // method name |
---|
| 96 | lua_gettable(L, base); // get method from userdata |
---|
| 97 | if (lua_isnil(L, -1)) |
---|
| 98 | { // no method? |
---|
| 99 | lua_settop(L, base-1); // drop userdata and args |
---|
| 100 | lua_pushfstring(L, "%s missing method '%s'", Lunar<T>::className.c_str(), method); |
---|
| 101 | return -1; |
---|
| 102 | } |
---|
| 103 | lua_insert(L, base); // put method under userdata, args |
---|
[7821] | 104 | |
---|
[8408] | 105 | int status = lua_pcall(L, 1+nargs, nresults, errfunc); // call method |
---|
| 106 | if (status) |
---|
| 107 | { |
---|
| 108 | const char *msg = lua_tostring(L, -1); |
---|
| 109 | if (msg == NULL) msg = "(error with no message)"; |
---|
| 110 | lua_pushfstring(L, "%s:%s status = %d\n%s", |
---|
| 111 | Lunar<T>::className.c_str(), method, status, msg); |
---|
| 112 | lua_remove(L, base); // remove old message |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | return lua_gettop(L) - base + 1; // number of results |
---|
| 116 | } |
---|
[7821] | 117 | |
---|
[8408] | 118 | // push onto the Lua stack a userdata containing a pointer to T object |
---|
| 119 | static int push(lua_State *L, T *obj, bool gc=false) |
---|
| 120 | { |
---|
| 121 | if (!obj) { lua_pushnil(L); return 0; } |
---|
| 122 | luaL_getmetatable(L, Lunar<T>::className.c_str()); // lookup metatable in Lua registry |
---|
| 123 | if (lua_isnil(L, -1)) luaL_error(L, "%s missing metatable", Lunar<T>::className.c_str()); |
---|
| 124 | int mt = lua_gettop(L); |
---|
| 125 | subtable(L, mt, "userdata", "v"); |
---|
| 126 | userdataType *ud = |
---|
| 127 | static_cast<userdataType*>(pushuserdata(L, obj, sizeof(userdataType))); |
---|
| 128 | if (ud) |
---|
| 129 | { |
---|
| 130 | ud->pT = obj; // store pointer to object in userdata |
---|
| 131 | lua_pushvalue(L, mt); |
---|
| 132 | lua_setmetatable(L, -2); |
---|
| 133 | if (gc == false) |
---|
| 134 | { |
---|
| 135 | lua_checkstack(L, 3); |
---|
| 136 | subtable(L, mt, "do not trash", "k"); |
---|
| 137 | lua_pushvalue(L, -2); |
---|
| 138 | lua_pushboolean(L, 1); |
---|
| 139 | lua_settable(L, -3); |
---|
| 140 | lua_pop(L, 1); |
---|
[7821] | 141 | } |
---|
[8408] | 142 | } |
---|
| 143 | lua_replace(L, mt); |
---|
| 144 | lua_settop(L, mt); |
---|
| 145 | return mt; // index of userdata containing pointer to T object |
---|
| 146 | } |
---|
[7821] | 147 | |
---|
| 148 | |
---|
[8408] | 149 | static int insertObject(lua_State* L, T* obj, const std::string& name, bool gc=false) |
---|
| 150 | { |
---|
| 151 | int objectRef = push(L, obj, gc); |
---|
[7962] | 152 | |
---|
[8408] | 153 | lua_pushlstring(L, name.c_str(), name.size()); |
---|
| 154 | lua_pushvalue(L, objectRef ); |
---|
| 155 | lua_settable(L, LUA_GLOBALSINDEX ); |
---|
[7962] | 156 | |
---|
[8408] | 157 | return objectRef; |
---|
| 158 | } |
---|
[7962] | 159 | |
---|
| 160 | |
---|
[8408] | 161 | static int insertObject(Script* script, T* obj, const std::string& name, bool gc=false) |
---|
| 162 | { |
---|
| 163 | assert (script != NULL); |
---|
| 164 | return insertObject(script->getLuaState(), obj, name, gc); |
---|
| 165 | } |
---|
[8044] | 166 | |
---|
[8408] | 167 | // get userdata from Lua stack and return pointer to T object |
---|
| 168 | static T *check(lua_State *L, int narg) |
---|
| 169 | { |
---|
| 170 | userdataType *ud = |
---|
| 171 | static_cast<userdataType*>(luaL_checkudata(L, narg, Lunar<T>::className.c_str())); |
---|
| 172 | if(!ud) luaL_typerror(L, narg, Lunar<T>::className.c_str()); |
---|
| 173 | return ud->pT; // pointer to T object |
---|
| 174 | } |
---|
[8044] | 175 | |
---|
[8408] | 176 | private: |
---|
| 177 | Lunar(); // hide default constructor |
---|
[7821] | 178 | |
---|
[8408] | 179 | // member function dispatcher |
---|
| 180 | static int thunk(lua_State *L) |
---|
| 181 | { |
---|
| 182 | // stack has userdata, followed by method args |
---|
| 183 | T *obj = check(L, 1); // get 'self', or if you prefer, 'this' |
---|
| 184 | lua_remove(L, 1); // remove self so member function args start at index 1 |
---|
| 185 | // get member function from upvalue |
---|
[9869] | 186 | mfp l = static_cast<mfp>(lua_touserdata(L, lua_upvalueindex(1))); |
---|
| 187 | (*l)(obj, L); // call member function |
---|
| 188 | return l->hasRetVal()? 1 : 0; |
---|
[8408] | 189 | } |
---|
[7821] | 190 | |
---|
[8408] | 191 | // create a new T object and |
---|
| 192 | // push onto the Lua stack a userdata containing a pointer to T object |
---|
| 193 | static int new_T(lua_State *L) |
---|
| 194 | { |
---|
| 195 | lua_remove(L, 1); // use classname:new(), instead of classname.new() |
---|
| 196 | T *obj = new T(); // call constructor for T objects |
---|
| 197 | push(L, obj, true); // gc_T will delete this object |
---|
| 198 | return 1; // userdata containing pointer to T object |
---|
| 199 | } |
---|
[7821] | 200 | |
---|
[8408] | 201 | // garbage collection metamethod |
---|
| 202 | static int gc_T(lua_State *L) |
---|
| 203 | { |
---|
| 204 | if (luaL_getmetafield(L, 1, "do not trash")) |
---|
| 205 | { |
---|
| 206 | lua_pushvalue(L, 1); // dup userdata |
---|
| 207 | lua_gettable(L, -2); |
---|
| 208 | if (!lua_isnil(L, -1)) return 0; // do not delete object |
---|
| 209 | } |
---|
| 210 | userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); |
---|
| 211 | T *obj = ud->pT; |
---|
| 212 | if (obj) delete obj; // call destructor for T objects |
---|
| 213 | return 0; |
---|
| 214 | } |
---|
[7821] | 215 | |
---|
[8408] | 216 | static int tostring_T (lua_State *L) |
---|
| 217 | { |
---|
| 218 | char buff[32]; |
---|
| 219 | userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); |
---|
| 220 | T *obj = ud->pT; |
---|
| 221 | sprintf(buff, "%p", obj); |
---|
| 222 | lua_pushfstring(L, "%s (%s)", Lunar<T>::className.c_str(), buff); |
---|
| 223 | return 1; |
---|
| 224 | } |
---|
[7821] | 225 | |
---|
[8408] | 226 | static void set(lua_State *L, int table_index, const char *key) |
---|
| 227 | { |
---|
| 228 | lua_pushstring(L, key); |
---|
| 229 | lua_insert(L, -2); // swap value and key |
---|
| 230 | lua_settable(L, table_index); |
---|
| 231 | } |
---|
[7821] | 232 | |
---|
[8408] | 233 | static void weaktable(lua_State *L, const char *mode) |
---|
| 234 | { |
---|
| 235 | lua_newtable(L); |
---|
| 236 | lua_pushvalue(L, -1); // table is its own metatable |
---|
| 237 | lua_setmetatable(L, -2); |
---|
| 238 | lua_pushliteral(L, "__mode"); |
---|
| 239 | lua_pushstring(L, mode); |
---|
| 240 | lua_settable(L, -3); // metatable.__mode = mode |
---|
| 241 | } |
---|
[7821] | 242 | |
---|
[8408] | 243 | static void subtable(lua_State *L, int tindex, const char *name, const char *mode) |
---|
| 244 | { |
---|
| 245 | lua_pushstring(L, name); |
---|
| 246 | lua_gettable(L, tindex); |
---|
| 247 | if (lua_isnil(L, -1)) |
---|
| 248 | { |
---|
| 249 | lua_pop(L, 1); |
---|
| 250 | lua_checkstack(L, 3); |
---|
| 251 | weaktable(L, mode); |
---|
| 252 | lua_pushstring(L, name); |
---|
| 253 | lua_pushvalue(L, -2); |
---|
| 254 | lua_settable(L, tindex); |
---|
| 255 | } |
---|
| 256 | } |
---|
[7821] | 257 | |
---|
[8408] | 258 | static void *pushuserdata(lua_State *L, void *key, size_t sz) |
---|
| 259 | { |
---|
| 260 | void *ud = 0; |
---|
| 261 | lua_pushlightuserdata(L, key); |
---|
| 262 | lua_gettable(L, -2); // lookup[key] |
---|
| 263 | if (lua_isnil(L, -1)) |
---|
| 264 | { |
---|
| 265 | lua_pop(L, 1); // drop nil |
---|
| 266 | lua_checkstack(L, 3); |
---|
| 267 | ud = lua_newuserdata(L, sz); // create new userdata |
---|
| 268 | lua_pushlightuserdata(L, key); |
---|
| 269 | lua_pushvalue(L, -2); // dup userdata |
---|
| 270 | lua_settable(L, -4); // lookup[key] = userdata |
---|
| 271 | } |
---|
| 272 | return ud; |
---|
| 273 | } |
---|
[7821] | 274 | |
---|
[8408] | 275 | private: |
---|
| 276 | static std::string className; |
---|
| 277 | static const ScriptMethod* scriptMethods; |
---|
| 278 | }; |
---|
[7821] | 279 | |
---|
[8408] | 280 | template <typename T> |
---|
| 281 | std::string Lunar<T>::className = ""; |
---|
| 282 | template <typename T> |
---|
| 283 | const ScriptMethod* Lunar<T>::scriptMethods = NULL; |
---|
| 284 | |
---|
[8032] | 285 | #endif /* _LUNAR_H */ |
---|
[7821] | 286 | |
---|