Changeset 77 for trunk/src/lua/lua_state.cc
- Timestamp:
- 06/01/13 22:47:44 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lua/lua_state.cc
r74 r77 10 10 #include "nv/logging.hh" 11 11 #include "nv/string.hh" 12 #include "nv/root.hh" 13 #include "nv/types.hh" 12 14 13 15 using namespace nv; … … 286 288 } 287 289 288 290 lua::reference lua::state::register_object( object * o ) 291 { 292 if (!o) return ref_none; 293 type_database *db = o->get_root()->get_type_database(); 294 if (!db) return ref_none; 295 type_entry* t = db->get_type(typeid(o)); 296 if (!t) return ref_none; 297 stack_guard guard( this ); 298 lua_getglobal( L, t->name.text ); 299 if ( lua_isnil( L, -1 ) ) 300 { 301 NV_THROW( runtime_error, std::string( t->name.text ) + " type not registered!" ); 302 } 303 deep_pointer_copy( -1, o ); 304 return luaL_ref( L, LUA_REGISTRYINDEX ); 305 } 306 307 void lua::state::unregister_object( object * o ) 308 { 309 if (!o) return; 310 stack_guard guard( this ); 311 lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() ); 312 lua_pushstring( L, "__ptr" ); 313 lua_pushboolean( L, false ); 314 lua_rawset( L, -3 ); 315 lua_pop( L, 1 ); 316 luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() ); 317 } 318 319 void lua::state::deep_pointer_copy( int index, void* obj ) 320 { 321 index = lua_absindex( L, index ); 322 lua_newtable( L ); 323 lua_pushnil( L ); 324 bool has_functions = false; 325 bool has_metatable = false; 326 327 while ( lua_next( L, index ) != 0 ) 328 { 329 if ( lua_isfunction( L, -1 ) ) 330 has_functions = true; 331 else if ( lua_istable( L, -1 ) ) 332 { 333 deep_pointer_copy( -1, obj ); 334 lua_insert( L, -2 ); 335 lua_pop( L, 1 ); 336 } 337 lua_pushvalue( L, -2 ); 338 lua_insert( L, -2 ); 339 lua_settable( L, -4 ); 340 } 341 342 if ( lua_getmetatable( L, -2 ) ) 343 { 344 lua_setmetatable( L, -2 ); 345 has_metatable = true; 346 } 347 348 if ( has_functions || has_metatable ) 349 { 350 lua_pushstring( L, "__ptr" ); 351 lua_pushlightuserdata( L, obj ); 352 lua_rawset( L, -3 ); 353 } 354 }
Note: See TracChangeset
for help on using the changeset viewer.