- Timestamp:
- 07/17/13 03:28:22 (12 years ago)
- Location:
- trunk/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/lua.cc
r165 r166 41 41 #undef NV_LUA_FUN_52 42 42 43 #if NV_LUA_VERSION == NV_LUA_5C 44 # define NV_LUA_COMPAT_FUN( rt, fn, fp,u1,u2,u3,u4,u5 ) rt (*fn) fp = nullptr; 45 # include <nv/lib/detail/lua_functions_compat.inc> 46 # undef NV_LUA_COMPAT_FUN 47 int LUA_REGISTRYINDEX = 0; 48 int LUA_VERSION_NUM = 0; 49 size_t (*lua_rawlen) (lua_State *L, int idx) = nullptr; 50 int (*lua_absindex) (lua_State *L, int idx) = nullptr; 51 void (*lua_getglobal) (lua_State *L, const char *var) = nullptr; 52 void (*lua_setglobal) (lua_State *L, const char *var) = nullptr; 53 void (*luaL_requiref) (lua_State *L, const char *modname, lua_CFunction openf, int glb) = nullptr; 54 void (*luaL_setmetatable) (lua_State *L, const char *tname) = nullptr; 55 void* (*luaL_testudata) (lua_State *L, int ud, const char *tname) = nullptr; 56 void (*lua_copy) (lua_State *L, int fromidx, int toidx) = nullptr; 57 int (*lua_compare) (lua_State *L, int idx1, int idx2, int op) = nullptr; 58 void (*lua_rawgetp) (lua_State *L, int idx, const void *p) = nullptr; 59 void (*lua_rawsetp) (lua_State *L, int idx, const void *p) = nullptr; 60 void (*lua_pushglobaltable)(lua_State* L) = nullptr; 61 void (*luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup) = nullptr; 62 int (*luaL_getsubtable) (lua_State *L, int idx, const char *fname) = nullptr; 63 64 const lua_Number* (*lua_version) (lua_State *L) = nullptr; 65 66 // only loaded in 5.1 mode to implement lua_compare 67 int (*lua_equal) (lua_State *L, int idx1, int idx2) = nullptr; 68 int (*lua_lessthan) (lua_State *L, int idx1, int idx2) = nullptr; 69 70 71 # define NV_LUA_COMPAT_FUN( u1,u2,u3,rt2,fn2,fp2,u4,u5 ) static rt2 (*fn2##_compat) fp2 = nullptr; 72 # include <nv/lib/detail/lua_functions_compat.inc> 73 # undef NV_LUA_COMPAT_FUN 74 75 # define NV_LUA_COMPAT_FUN( rt, fn, fp, rt2, fn2, fp2,arg,ret ) \ 76 rt call_##fn2##_compat fp { ret fn2##_compat arg; } 77 # include <nv/lib/detail/lua_functions_compat.inc> 78 # undef NV_LUA_COMPAT_FUN 79 80 # define LUAI_MAXSTACK_52 1000000 81 # define LUAI_FIRSTPSEUDOIDX_52 (-LUAI_MAXSTACK_52 - 1000) 82 # define LUA_REGISTRYINDEX_52 LUAI_FIRSTPSEUDOIDX_52 83 # define LUA_GLOBALSINDEX_51 (-10002) 84 # define LUA_REGISTRYINDEX_51 (-10000) 85 # define lua_upvalueindex_51(i) (LUA_GLOBALSINDEX_51-(i)) 86 # define lua_upvalueindex_52(i) (LUA_REGISTRYINDEX_52-(i)) 87 88 int lua_absindex_51 (lua_State *L, int idx) { return (idx > 0 ? idx : idx + lua_gettop(L) + 1); }; 89 void lua_getglobal_51 (lua_State *L, const char *var) { lua_getfield(L, LUA_GLOBALSINDEX_51, var ); }; 90 void lua_setglobal_51 (lua_State *L, const char *var) { lua_setfield(L, LUA_GLOBALSINDEX_51, var ); }; 91 void luaL_requiref_51 (lua_State *L, const char *modname, lua_CFunction openf, int glb) 92 { 93 lua_pushcfunction(L, openf); 94 lua_pushstring(L, modname); 95 lua_call(L, 1, 1); 96 if (glb != 0) 97 { 98 lua_pushvalue( L, LUA_GLOBALSINDEX_51 ); 99 lua_pushvalue(L, -2); 100 lua_setfield(L, -2, modname); 101 lua_pop(L, 1); 102 } 103 } 104 105 void luaL_setmetatable_51 (lua_State *L, const char *tname) 106 { 107 luaL_getmetatable(L, tname); 108 lua_setmetatable(L, -2); 109 } 110 111 void *luaL_testudata_51 (lua_State *L, int ud, const char *tname) 112 { 113 void *p = lua_touserdata(L, ud); 114 if (p != NULL) 115 { 116 if (lua_getmetatable(L, ud)) 117 { 118 luaL_getmetatable(L, tname); 119 if ( !lua_rawequal(L, -1, -2)) 120 p = NULL; 121 lua_pop(L, 2); 122 return p; 123 } 124 } 125 return NULL; 126 } 127 128 const lua_Number *lua_version_51 (lua_State*) 129 { 130 static const lua_Number version = (lua_Number)LUA_VERSION_NUM; 131 return &version; 132 } 133 134 void lua_copy_51 (lua_State *L, int fromidx, int toidx) 135 { 136 toidx = lua_absindex( L, toidx ); 137 lua_pushvalue( L, fromidx ); 138 lua_replace( L, toidx ); 139 } 140 141 int lua_compare_51(lua_State *L, int idx1, int idx2, int op) 142 { 143 switch (op) 144 { 145 case LUA_OPEQ : return lua_equal( L, idx1, idx2 ); 146 case LUA_OPLT : return lua_lessthan( L, idx1, idx2 ); 147 case LUA_OPLE : return lua_lessthan( L, idx1, idx2 ) || lua_equal( L, idx1, idx2 ); 148 default: 149 return 0; 150 } 151 } 152 153 void lua_rawgetp_51(lua_State *L, int idx, const void *p) 154 { 155 idx = lua_absindex( L, idx ); 156 void* pp = const_cast<void *>(p); // EVIL 157 lua_pushlightuserdata( L, pp ); 158 lua_rawget( L, idx ); 159 } 160 161 void lua_rawsetp_51(lua_State *L, int idx, const void *p) 162 { 163 idx = lua_absindex( L, idx ); 164 void* pp = const_cast<void *>(p); // EVIL 165 lua_pushlightuserdata( L, pp ); 166 lua_insert( L, -1 ); 167 lua_rawset( L, idx ); 168 } 169 170 int luaL_getsubtable_51(lua_State *L, int idx, const char *fname) 171 { 172 lua_getfield(L, idx, fname); 173 if ( lua_istable(L, -1) ) return 1; 174 else { 175 idx = lua_absindex(L, idx); 176 lua_pop(L, 1); 177 lua_newtable(L); 178 lua_pushvalue(L, -1); 179 lua_setfield(L, idx, fname); 180 return 0; 181 } 182 } 183 184 void luaL_setfuncs_51(lua_State *L, const luaL_Reg *l, int nup) 185 { 186 luaL_checkstack(L, nup, "too many upvalues"); 187 for (; l->name != NULL; l++) 188 { 189 for (int i = 0; i < nup; i++) 190 { 191 lua_pushvalue(L, -nup); 192 } 193 lua_pushcclosure(L, l->func, nup); 194 lua_setfield(L, -(nup + 2), l->name); 195 } 196 lua_pop(L, nup); 197 } 198 199 void lua_pushglobaltable_51(lua_State* L) 200 { 201 lua_pushvalue( L, LUA_GLOBALSINDEX_51 ); 202 } 203 204 #endif 205 43 206 bool nv::load_lua_library( const char* path ) 44 207 { 45 208 static nv::library lua_library; 46 209 if ( lua_library.is_open() ) return true; 47 lua_library.open( path ); 210 #if NV_LUA_VERSION == NV_LUA_5C 211 if ( path == nullptr ) 212 { 213 if (!lua_library.try_open( NV_LUA_PATH_JIT ) && 214 !lua_library.try_open( NV_LUA_PATH_52 ) ) 215 { 216 lua_library.open( NV_LUA_PATH_51 ); 217 } 218 } 219 else 220 #else 221 lua_library.open( path ); 222 #endif 48 223 49 224 # define NV_LUA_FUN( rtype, fname, fparams ) *(void **) (&fname) = lua_library.get(#fname); … … 64 239 # undef NV_LUA_FUN_51 65 240 # undef NV_LUA_FUN_52 241 242 #if NV_LUA_VERSION == NV_LUA_5C 243 # define NV_LUA_LOAD( fname ) *(void **) (&fname) = lua_library.get(#fname); 244 # define NV_LUA_LOAD_AS( fname,fname2 ) *(void **) (&fname) = lua_library.get(#fname2); 245 bool version_52 = lua_library.try_get("luaL_checkversion_") != nullptr; 246 if (version_52) 247 { 248 # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, fn2, u5, u6, u7 ) \ 249 *(void **) (&(fn2##_compat)) = lua_library.get(#fn2); \ 250 fn = call_##fn2##_compat; 251 # include <nv/lib/detail/lua_functions_compat.inc> 252 # undef NV_LUA_COMPAT_FUN 253 NV_LUA_LOAD( lua_rawlen ); 254 NV_LUA_LOAD( lua_absindex ); 255 NV_LUA_LOAD( lua_getglobal ); 256 NV_LUA_LOAD( lua_setglobal ); 257 NV_LUA_LOAD( luaL_setmetatable ); 258 NV_LUA_LOAD( luaL_testudata ); 259 NV_LUA_LOAD( lua_version ); 260 NV_LUA_LOAD( lua_copy ); 261 NV_LUA_LOAD( lua_compare ); 262 NV_LUA_LOAD( lua_rawgetp ); 263 NV_LUA_LOAD( lua_rawsetp ); 264 NV_LUA_LOAD( lua_pushglobaltable ); 265 NV_LUA_LOAD( luaL_setfuncs ); 266 NV_LUA_LOAD( luaL_getsubtable ); 267 268 LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_52; 269 LUA_VERSION_NUM = 502; 270 } 271 else 272 { 273 # define NV_LUA_COMPAT_FUN( u1, fn, u2, u3, u4, u5, u6, u7 ) \ 274 *(void **) (&fn) = lua_library.get(#fn); 275 # include <nv/lib/detail/lua_functions_compat.inc> 276 # undef NV_LUA_COMPAT_FUN 277 NV_LUA_LOAD_AS( lua_rawlen, lua_objlen ) 278 lua_absindex = lua_absindex_51; 279 lua_getglobal = lua_getglobal_51; 280 lua_setglobal = lua_setglobal_51; 281 luaL_setmetatable = luaL_setmetatable_51; 282 luaL_testudata = luaL_testudata_51; 283 lua_version = lua_version_51; 284 lua_copy = lua_copy_51; 285 lua_rawgetp = lua_rawgetp_51; 286 lua_rawsetp = lua_rawsetp_51; 287 lua_pushglobaltable = lua_pushglobaltable_51; 288 luaL_setfuncs = luaL_setfuncs_51; 289 luaL_getsubtable = luaL_getsubtable_51; 290 291 NV_LUA_LOAD( lua_lessthan ); 292 NV_LUA_LOAD( lua_equal ); 293 lua_compare = lua_compare_51; 294 LUA_REGISTRYINDEX = LUA_REGISTRYINDEX_51; 295 LUA_VERSION_NUM = 501; 296 } 297 # undef NV_LUA_LOAD 298 #endif 299 66 300 return true; 67 301 } -
trunk/src/library.cc
r121 r166 44 44 { 45 45 m_name = name; 46 open(); 46 if ( !open() ) 47 { 48 m_handle = nullptr; 49 NV_THROW( library_error, "Can't load library!", name ); 50 } 51 } 52 53 bool nv::library::try_open( const std::string& name ) 54 { 55 m_name = name; 56 if ( !open() ) 57 { 58 m_handle = nullptr; 59 return false; 60 } 61 return true; 47 62 } 48 63 … … 52 67 } 53 68 54 void library::open()69 bool library::open( ) 55 70 { 56 71 if ( m_handle != NULL ) 57 72 { 58 return ;73 return true; 59 74 } 60 75 NV_LOG( LOG_NOTICE, "library : loading '" + m_name + "'..." ); … … 73 88 if ( m_handle == NULL ) 74 89 { 75 NV_THROW( library_error, "Can't load library!", name ); 90 NV_LOG( LOG_NOTICE, "library : '" + name + "' failed to open." ); 91 return false; 76 92 } 77 93 NV_LOG( LOG_NOTICE, "library : '" + name + "' loaded." ); 94 return true; 78 95 } 79 96 … … 86 103 } 87 104 return result; 105 } 106 107 void* nv::library::try_get( const std::string& symbol ) 108 { 109 return (void*) NV_LIB_GET( (NV_LIB_HANDLE) m_handle, symbol.c_str() ); 88 110 } 89 111
Note: See TracChangeset
for help on using the changeset viewer.