Changeset 164 for trunk/src/lib/lua.cc
- Timestamp:
- 07/16/13 23:30:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/lua.cc
r109 r164 11 11 #include "nv/library.hh" 12 12 13 /* State manipulation */ 14 lua_State * (*lua_newstate) (lua_Alloc f, void *ud) = nullptr; 15 void (*lua_close) (lua_State *L) = nullptr; 16 lua_State * (*lua_newthread) (lua_State *L) = nullptr; 17 lua_CFunction (*lua_atpanic) (lua_State *L, lua_CFunction panicf) = nullptr; 18 const lua_Number * (*lua_version) (lua_State *L) = nullptr; 13 #if NV_LUA_VERSION == NV_LUA_52 14 # undef luaL_loadfile 15 # undef luaL_loadbuffer 16 # undef luaL_prepbuffer 17 # undef lua_tonumber 18 # undef lua_tointeger 19 # undef lua_tounsigned 20 # undef lua_call 21 # undef lua_pcall 22 # undef lua_yield 23 #endif 19 24 20 /* Basic stack manipulation */ 21 int (*lua_absindex) (lua_State *L, int idx) = nullptr; 22 int (*lua_gettop) (lua_State *L) = nullptr; 23 void (*lua_settop) (lua_State *L, int idx) = nullptr; 24 void (*lua_pushvalue) (lua_State *L, int idx) = nullptr; 25 void (*lua_remove) (lua_State *L, int idx) = nullptr; 26 void (*lua_insert) (lua_State *L, int idx) = nullptr; 27 void (*lua_replace) (lua_State *L, int idx) = nullptr; 28 void (*lua_copy) (lua_State *L, int fromidx, int toidx) = nullptr; 29 int (*lua_checkstack) (lua_State *L, int sz) = nullptr; 30 void (*lua_xmove) (lua_State *from, lua_State *to, int n) = nullptr; 25 #define NV_LUA_FUN( rtype, fname, fparams ) rtype (*fname) fparams = nullptr; 26 #if NV_LUA_VERSION == NV_LUA_52 27 # define NV_LUA_FUN_51( rtype, fname, fparams ) 28 # define NV_LUA_FUN_52 NV_LUA_FUN 29 #else 30 # define NV_LUA_FUN_51 NV_LUA_FUN 31 # define NV_LUA_FUN_52( rtype, fname, fparams ) 32 #endif 31 33 32 /* Access functions (stack -> C) */ 33 int (*lua_isnumber) (lua_State *L, int idx) = nullptr; 34 int (*lua_isstring) (lua_State *L, int idx) = nullptr; 35 int (*lua_iscfunction) (lua_State *L, int idx) = nullptr; 36 int (*lua_isuserdata) (lua_State *L, int idx) = nullptr; 37 int (*lua_type) (lua_State *L, int idx) = nullptr; 38 const char* (*lua_typename) (lua_State *L, int tp) = nullptr; 34 #include <nv/lib/detail/lua_functions.inc> 39 35 40 lua_Number (*lua_tonumberx) (lua_State *L, int idx, int *isnum) = nullptr; 41 lua_Integer (*lua_tointegerx) (lua_State *L, int idx, int *isnum) = nullptr; 42 lua_Unsigned (*lua_tounsignedx) (lua_State *L, int idx, int *isnum) = nullptr; 43 int (*lua_toboolean) (lua_State *L, int idx) = nullptr; 44 const char* (*lua_tolstring) (lua_State *L, int idx, size_t *len) = nullptr; 45 size_t (*lua_rawlen) (lua_State *L, int idx) = nullptr; 46 lua_CFunction (*lua_tocfunction) (lua_State *L, int idx) = nullptr; 47 void* (*lua_touserdata) (lua_State *L, int idx) = nullptr; 48 lua_State* (*lua_tothread) (lua_State *L, int idx) = nullptr; 49 const void* (*lua_topointer) (lua_State *L, int idx) = nullptr; 50 51 /* Comparison and arithmetic functions */ 52 void (*lua_arith) (lua_State *L, int op) = nullptr; 53 int (*lua_rawequal) (lua_State *L, int idx1, int idx2) = nullptr; 54 int (*lua_compare) (lua_State *L, int idx1, int idx2, int op) = nullptr; 55 56 /* Push functions (C -> stack) */ 57 void (*lua_pushnil) (lua_State *L) = nullptr; 58 void (*lua_pushnumber) (lua_State *L, lua_Number n) = nullptr; 59 void (*lua_pushinteger) (lua_State *L, lua_Integer n) = nullptr; 60 void (*lua_pushunsigned) (lua_State *L, lua_Unsigned n) = nullptr; 61 const char* (*lua_pushlstring) (lua_State *L, const char *s, size_t l) = nullptr; 62 const char* (*lua_pushstring) (lua_State *L, const char *s) = nullptr; 63 const char* (*lua_pushvfstring) (lua_State *L, const char *fmt, 64 va_list argp) = nullptr; 65 const char * (*lua_pushfstring) (lua_State *L, const char *fmt, ...) = nullptr; 66 void (*lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n) = nullptr; 67 void (*lua_pushboolean) (lua_State *L, int b) = nullptr; 68 void (*lua_pushlightuserdata) (lua_State *L, void *p) = nullptr; 69 int (*lua_pushthread) (lua_State *L) = nullptr; 70 71 /* Get functions (Lua -> stack) */ 72 void (*lua_getglobal) (lua_State *L, const char *var) = nullptr; 73 void (*lua_gettable) (lua_State *L, int idx) = nullptr; 74 void (*lua_getfield) (lua_State *L, int idx, const char *k) = nullptr; 75 void (*lua_rawget) (lua_State *L, int idx) = nullptr; 76 void (*lua_rawgeti) (lua_State *L, int idx, int n) = nullptr; 77 void (*lua_rawgetp) (lua_State *L, int idx, const void *p) = nullptr; 78 void (*lua_createtable) (lua_State *L, int narr, int nrec) = nullptr; 79 void* (*lua_newuserdata) (lua_State *L, size_t sz) = nullptr; 80 int (*lua_getmetatable) (lua_State *L, int objindex) = nullptr; 81 void (*lua_getuservalue) (lua_State *L, int idx) = nullptr; 82 83 /* Set functions (stack -> Lua) */ 84 void (*lua_setglobal) (lua_State *L, const char *var) = nullptr; 85 void (*lua_settable) (lua_State *L, int idx) = nullptr; 86 void (*lua_setfield) (lua_State *L, int idx, const char *k) = nullptr; 87 void (*lua_rawset) (lua_State *L, int idx) = nullptr; 88 void (*lua_rawseti) (lua_State *L, int idx, int n) = nullptr; 89 void (*lua_rawsetp) (lua_State *L, int idx, const void *p) = nullptr; 90 int (*lua_setmetatable) (lua_State *L, int objindex) = nullptr; 91 void (*lua_setuservalue) (lua_State *L, int idx) = nullptr; 92 93 /* 'load' and 'call' functions (load and run Lua code) */ 94 void (*lua_callk) (lua_State *L, int nargs, int nresults, int ctx, lua_CFunction k) = nullptr; 95 int (*lua_getctx) (lua_State *L, int *ctx) = nullptr; 96 int (*lua_pcallk) (lua_State *L, int nargs, int nresults, int errfunc, int ctx, lua_CFunction k) = nullptr; 97 int (*lua_load) (lua_State *L, lua_Reader reader, void *dt, const char *chunkname, const char *mode) = nullptr; 98 int (*lua_dump) (lua_State *L, lua_Writer writer, void *data) = nullptr; 99 100 /* Coroutine functions */ 101 int (*lua_yieldk) (lua_State *L, int nresults, int ctx, lua_CFunction k) = nullptr; 102 int (*lua_resume) (lua_State *L, lua_State *from, int narg) = nullptr; 103 int (*lua_status) (lua_State *L) = nullptr; 104 105 /* Garbage-collection function and options */ 106 int (*lua_gc) (lua_State *L, int what, int data) = nullptr; 107 108 /* Miscellaneous functions */ 109 int (*lua_error) (lua_State *L) = nullptr; 110 int (*lua_next) (lua_State *L, int idx) = nullptr; 111 void (*lua_concat) (lua_State *L, int n) = nullptr; 112 void (*lua_len) (lua_State *L, int idx) = nullptr; 113 lua_Alloc (*lua_getallocf) (lua_State *L, void **ud) = nullptr; 114 void (*lua_setallocf) (lua_State *L, lua_Alloc f, void *ud) = nullptr; 115 116 /* Debug API */ 117 int (*lua_getstack) (lua_State *L, int level, lua_Debug *ar) = nullptr; 118 int (*lua_getinfo) (lua_State *L, const char *what, lua_Debug *ar) = nullptr; 119 const char * (*lua_getlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr; 120 const char * (*lua_setlocal) (lua_State *L, const lua_Debug *ar, int n) = nullptr; 121 const char * (*lua_getupvalue) (lua_State *L, int funcindex, int n) = nullptr; 122 const char * (*lua_setupvalue) (lua_State *L, int funcindex, int n) = nullptr; 123 void * (*lua_upvalueid) (lua_State *L, int fidx, int n) = nullptr; 124 void (*lua_upvaluejoin) (lua_State *L, int fidx1, int n1, int fidx2, int n2) = nullptr; 125 int (*lua_sethook) (lua_State *L, lua_Hook func, int mask, int count) = nullptr; 126 lua_Hook (*lua_gethook) (lua_State *L) = nullptr; 127 int (*lua_gethookmask) (lua_State *L) = nullptr; 128 int (*lua_gethookcount) (lua_State *L) = nullptr; 129 130 /* lualib API */ 131 int (*luaopen_base) (lua_State *L) = nullptr; 132 int (*luaopen_coroutine) (lua_State *L) = nullptr; 133 int (*luaopen_table) (lua_State *L) = nullptr; 134 int (*luaopen_io) (lua_State *L) = nullptr; 135 int (*luaopen_os) (lua_State *L) = nullptr; 136 int (*luaopen_string) (lua_State *L) = nullptr; 137 int (*luaopen_bit32) (lua_State *L) = nullptr; 138 int (*luaopen_math) (lua_State *L) = nullptr; 139 int (*luaopen_debug) (lua_State *L) = nullptr; 140 int (*luaopen_package) (lua_State *L) = nullptr; 141 int (*luaL_openlibs) (lua_State *L) = nullptr; 142 143 /* lauxlib API */ 144 void (*luaL_checkversion_) (lua_State *L, lua_Number ver) = nullptr; 145 int (*luaL_getmetafield) (lua_State *L, int obj, const char *e) = nullptr; 146 int (*luaL_callmeta) (lua_State *L, int obj, const char *e) = nullptr; 147 const char * (*luaL_tolstring) (lua_State *L, int idx, size_t *len) = nullptr; 148 int (*luaL_argerror) (lua_State *L, int numarg, const char *extramsg) = nullptr; 149 const char * (*luaL_checklstring) (lua_State *L, int numArg, size_t *l) = nullptr; 150 const char * (*luaL_optlstring) (lua_State *L, int numArg, const char *def, size_t *l) = nullptr; 151 lua_Number (*luaL_checknumber) (lua_State *L, int numArg) = nullptr; 152 lua_Number (*luaL_optnumber) (lua_State *L, int nArg, lua_Number def) = nullptr; 153 lua_Integer (*luaL_checkinteger) (lua_State *L, int numArg) = nullptr; 154 lua_Integer (*luaL_optinteger) (lua_State *L, int nArg, lua_Integer def) = nullptr; 155 lua_Unsigned (*luaL_checkunsigned) (lua_State *L, int numArg) = nullptr; 156 lua_Unsigned (*luaL_optunsigned) (lua_State *L, int numArg, lua_Unsigned def) = nullptr; 157 void (*luaL_checkstack) (lua_State *L, int sz, const char *msg) = nullptr; 158 void (*luaL_checktype) (lua_State *L, int narg, int t) = nullptr; 159 void (*luaL_checkany) (lua_State *L, int narg) = nullptr; 160 int (*luaL_newmetatable) (lua_State *L, const char *tname) = nullptr; 161 void (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr; 162 void * (*luaL_testudata) (lua_State *L, int ud, const char *tname) = nullptr; 163 void * (*luaL_checkudata) (lua_State *L, int ud, const char *tname) = nullptr; 164 void (*luaL_where) (lua_State *L, int lvl) = nullptr; 165 int (*luaL_error) (lua_State *L, const char *fmt, ...) = nullptr; 166 int (*luaL_checkoption) (lua_State *L, int narg, const char *def, const char *const lst[]) = nullptr; 167 int (*luaL_fileresult) (lua_State *L, int stat, const char *fname) = nullptr; 168 int (*luaL_execresult) (lua_State *L, int stat) = nullptr; 169 170 int (*luaL_ref) (lua_State *L, int t) = nullptr; 171 void (*luaL_unref) (lua_State *L, int t, int ref) = nullptr; 172 int (*luaL_loadfilex) (lua_State *L, const char *filename, const char *mode) = nullptr; 173 int (*luaL_loadbufferx) (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) = nullptr; 174 int (*luaL_loadstring) (lua_State *L, const char *s) = nullptr; 175 lua_State * (*luaL_newstate) (void) = nullptr; 176 int (*luaL_len) (lua_State *L, int idx) = nullptr; 177 const char * (*luaL_gsub) (lua_State *L, const char *s, const char *p, const char *r) = nullptr; 178 void (*luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup) = nullptr; 179 int (*luaL_getsubtable) (lua_State *L, int idx, const char *fname) = nullptr; 180 void (*luaL_traceback) (lua_State *L, lua_State *L1, const char *msg, int level) = nullptr; 181 void (*luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr; 36 #undef NV_LUA_FUN 37 #undef NV_LUA_FUN_51 38 #undef NV_LUA_FUN_52 182 39 183 40 bool nv::load_lua_library( const char* path ) 184 41 { 185 # define NV_LUA_LOAD( symbol ) *(void **) (&symbol) = lua_library.get(#symbol);186 187 42 static nv::library lua_library; 188 43 if ( lua_library.is_open() ) return true; 189 44 lua_library.open( path ); 190 45 46 # define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname); 47 # if NV_LUA_VERSION == NV_LUA_52 48 # define NV_LUA_FUN_51( rtype, fname, fparams ) 49 # define NV_LUA_FUN_52 NV_LUA_FUN 50 # else 51 # define NV_LUA_FUN_51 NV_LUA_FUN 52 # define NV_LUA_FUN_52( rtype, fname, fparams ) 53 # endif 191 54 192 /* State manipulation */ 193 NV_LUA_LOAD(lua_newstate); 194 NV_LUA_LOAD(lua_close); 195 NV_LUA_LOAD(lua_newthread); 196 NV_LUA_LOAD(lua_atpanic); 197 NV_LUA_LOAD(lua_version); 55 #include <nv/lib/detail/lua_functions.inc> 198 56 199 /* Basic stack manipulation */ 200 NV_LUA_LOAD(lua_absindex); 201 NV_LUA_LOAD(lua_gettop); 202 NV_LUA_LOAD(lua_settop); 203 NV_LUA_LOAD(lua_pushvalue); 204 NV_LUA_LOAD(lua_remove); 205 NV_LUA_LOAD(lua_insert); 206 NV_LUA_LOAD(lua_replace); 207 NV_LUA_LOAD(lua_copy); 208 NV_LUA_LOAD(lua_checkstack); 209 NV_LUA_LOAD(lua_xmove); 210 211 /* Access functions (stack -> C) */ 212 NV_LUA_LOAD(lua_isnumber); 213 NV_LUA_LOAD(lua_isstring); 214 NV_LUA_LOAD(lua_iscfunction); 215 NV_LUA_LOAD(lua_isuserdata); 216 NV_LUA_LOAD(lua_type); 217 NV_LUA_LOAD(lua_typename); 218 219 NV_LUA_LOAD(lua_tonumberx); 220 NV_LUA_LOAD(lua_tointegerx); 221 NV_LUA_LOAD(lua_tounsignedx); 222 NV_LUA_LOAD(lua_toboolean); 223 NV_LUA_LOAD(lua_tolstring); 224 NV_LUA_LOAD(lua_rawlen); 225 NV_LUA_LOAD(lua_tocfunction); 226 NV_LUA_LOAD(lua_touserdata); 227 NV_LUA_LOAD(lua_tothread); 228 NV_LUA_LOAD(lua_topointer); 229 230 /* Comparison and arithmetic functions */ 231 NV_LUA_LOAD(lua_arith); 232 NV_LUA_LOAD(lua_rawequal); 233 NV_LUA_LOAD(lua_compare); 234 235 /* Push functions (C -> stack) */ 236 NV_LUA_LOAD(lua_pushnil); 237 NV_LUA_LOAD(lua_pushnumber); 238 NV_LUA_LOAD(lua_pushinteger); 239 NV_LUA_LOAD(lua_pushunsigned); 240 NV_LUA_LOAD(lua_pushlstring); 241 NV_LUA_LOAD(lua_pushstring); 242 NV_LUA_LOAD(lua_pushvfstring); 243 NV_LUA_LOAD(lua_pushfstring); 244 NV_LUA_LOAD(lua_pushcclosure); 245 NV_LUA_LOAD(lua_pushboolean); 246 NV_LUA_LOAD(lua_pushlightuserdata); 247 NV_LUA_LOAD(lua_pushthread); 248 249 /* Get functions (Lua -> stack) */ 250 NV_LUA_LOAD(lua_getglobal); 251 NV_LUA_LOAD(lua_gettable); 252 NV_LUA_LOAD(lua_getfield); 253 NV_LUA_LOAD(lua_rawget); 254 NV_LUA_LOAD(lua_rawgeti); 255 NV_LUA_LOAD(lua_rawgetp); 256 NV_LUA_LOAD(lua_createtable); 257 NV_LUA_LOAD(lua_newuserdata); 258 NV_LUA_LOAD(lua_getmetatable); 259 NV_LUA_LOAD(lua_getuservalue); 260 261 /* Set functions (stack -> Lua) */ 262 NV_LUA_LOAD(lua_setglobal); 263 NV_LUA_LOAD(lua_settable); 264 NV_LUA_LOAD(lua_setfield); 265 NV_LUA_LOAD(lua_rawset); 266 NV_LUA_LOAD(lua_rawseti); 267 NV_LUA_LOAD(lua_rawsetp); 268 NV_LUA_LOAD(lua_setmetatable); 269 NV_LUA_LOAD(lua_setuservalue); 270 271 /* 'load' and 'call' functions (load and run Lua code) */ 272 NV_LUA_LOAD(lua_callk); 273 NV_LUA_LOAD(lua_getctx); 274 NV_LUA_LOAD(lua_pcallk); 275 NV_LUA_LOAD(lua_load); 276 NV_LUA_LOAD(lua_dump); 277 278 /* Coroutine functions */ 279 NV_LUA_LOAD(lua_yieldk); 280 NV_LUA_LOAD(lua_resume); 281 NV_LUA_LOAD(lua_status); 282 283 /* Garbage-collection function and options */ 284 NV_LUA_LOAD(lua_gc); 285 286 /* Miscellaneous functions */ 287 NV_LUA_LOAD(lua_error); 288 NV_LUA_LOAD(lua_next); 289 NV_LUA_LOAD(lua_concat); 290 NV_LUA_LOAD(lua_len); 291 NV_LUA_LOAD(lua_getallocf); 292 NV_LUA_LOAD(lua_setallocf); 293 294 /* Debug API */ 295 NV_LUA_LOAD(lua_getstack); 296 NV_LUA_LOAD(lua_getinfo); 297 NV_LUA_LOAD(lua_getlocal); 298 NV_LUA_LOAD(lua_setlocal); 299 NV_LUA_LOAD(lua_getupvalue); 300 NV_LUA_LOAD(lua_setupvalue); 301 NV_LUA_LOAD(lua_upvalueid); 302 NV_LUA_LOAD(lua_upvaluejoin); 303 NV_LUA_LOAD(lua_sethook); 304 NV_LUA_LOAD(lua_gethook); 305 NV_LUA_LOAD(lua_gethookmask); 306 NV_LUA_LOAD(lua_gethookcount); 307 308 /* lualib API */ 309 NV_LUA_LOAD(luaopen_base); 310 NV_LUA_LOAD(luaopen_coroutine); 311 NV_LUA_LOAD(luaopen_table); 312 NV_LUA_LOAD(luaopen_io); 313 NV_LUA_LOAD(luaopen_os); 314 NV_LUA_LOAD(luaopen_string); 315 NV_LUA_LOAD(luaopen_bit32); 316 NV_LUA_LOAD(luaopen_math); 317 NV_LUA_LOAD(luaopen_debug); 318 NV_LUA_LOAD(luaopen_package); 319 NV_LUA_LOAD(luaL_openlibs); 320 321 /* lauxlib API */ 322 NV_LUA_LOAD(luaL_checkversion_); 323 NV_LUA_LOAD(luaL_getmetafield); 324 NV_LUA_LOAD(luaL_callmeta); 325 NV_LUA_LOAD(luaL_tolstring); 326 NV_LUA_LOAD(luaL_argerror); 327 NV_LUA_LOAD(luaL_checklstring); 328 NV_LUA_LOAD(luaL_optlstring); 329 NV_LUA_LOAD(luaL_checknumber); 330 NV_LUA_LOAD(luaL_optnumber); 331 NV_LUA_LOAD(luaL_checkinteger); 332 NV_LUA_LOAD(luaL_optinteger); 333 NV_LUA_LOAD(luaL_checkunsigned); 334 NV_LUA_LOAD(luaL_optunsigned); 335 NV_LUA_LOAD(luaL_checkstack); 336 NV_LUA_LOAD(luaL_checktype); 337 NV_LUA_LOAD(luaL_checkany); 338 NV_LUA_LOAD(luaL_newmetatable); 339 NV_LUA_LOAD(luaL_setmetatable); 340 NV_LUA_LOAD(luaL_testudata); 341 NV_LUA_LOAD(luaL_checkudata); 342 NV_LUA_LOAD(luaL_where); 343 NV_LUA_LOAD(luaL_error); 344 NV_LUA_LOAD(luaL_checkoption); 345 NV_LUA_LOAD(luaL_fileresult); 346 NV_LUA_LOAD(luaL_execresult); 347 348 NV_LUA_LOAD(luaL_ref); 349 NV_LUA_LOAD(luaL_unref); 350 NV_LUA_LOAD(luaL_loadfilex); 351 NV_LUA_LOAD(luaL_loadbufferx); 352 NV_LUA_LOAD(luaL_loadstring); 353 NV_LUA_LOAD(luaL_newstate); 354 NV_LUA_LOAD(luaL_len); 355 NV_LUA_LOAD(luaL_gsub); 356 NV_LUA_LOAD(luaL_setfuncs); 357 NV_LUA_LOAD(luaL_getsubtable); 358 NV_LUA_LOAD(luaL_traceback); 359 NV_LUA_LOAD(luaL_requiref); 360 361 # undef NV_LUA_LOAD 57 #undef NV_LUA_FUN 58 #undef NV_LUA_FUN_51 59 #undef NV_LUA_FUN_52 362 60 return true; 363 61 }
Note: See TracChangeset
for help on using the changeset viewer.