[51] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/lua/lua_raw.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/string.hh"
|
---|
| 10 |
|
---|
| 11 | std::string nlua_typecontent( lua_State* L, int idx )
|
---|
| 12 | {
|
---|
| 13 | switch ( lua_type( L, idx ) )
|
---|
| 14 | {
|
---|
| 15 | case LUA_TNONE : return "NONE";
|
---|
| 16 | case LUA_TNIL : return "NIL";
|
---|
| 17 | case LUA_TBOOLEAN : return lua_toboolean( L, idx ) == 0 ? "false" : "true";
|
---|
| 18 | case LUA_TLIGHTUSERDATA : return nv::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
| 19 | case LUA_TNUMBER : return nv::to_string( lua_tonumber( L, idx ) );
|
---|
| 20 | case LUA_TSTRING : return lua_tostring( L, idx );
|
---|
| 21 | case LUA_TTABLE : return "TABLE";
|
---|
| 22 | case LUA_TFUNCTION : return "FUNCTION";
|
---|
| 23 | case LUA_TUSERDATA : return nv::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
| 24 | case LUA_TTHREAD : return "THREAD";
|
---|
| 25 | default : return "UNKNOWN!";
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | void nlua_shallowcopy( lua_State *L, int index )
|
---|
| 30 | {
|
---|
| 31 | index = lua_absindex(L,index);
|
---|
| 32 | lua_newtable(L);
|
---|
| 33 | lua_pushnil(L);
|
---|
| 34 |
|
---|
| 35 | while ( lua_next( L, index ) != 0 )
|
---|
| 36 | {
|
---|
| 37 | lua_pushvalue(L, -2);
|
---|
| 38 | lua_insert(L, -2);
|
---|
| 39 | lua_settable(L, -4);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void nlua_shallowmerge( lua_State *L, int index )
|
---|
| 44 | {
|
---|
| 45 | index = lua_absindex(L,index);
|
---|
| 46 | lua_pushnil(L);
|
---|
| 47 |
|
---|
| 48 | while( lua_next(L, index) != 0 )
|
---|
| 49 | {
|
---|
| 50 | lua_pushvalue(L, -2);
|
---|
| 51 | lua_insert(L, -2);
|
---|
| 52 | lua_rawset(L, -4);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | void nlua_deepcopy( lua_State *L, int index )
|
---|
| 57 | {
|
---|
| 58 | index = lua_absindex(L,index);
|
---|
| 59 | lua_newtable(L);
|
---|
| 60 | lua_pushnil(L);
|
---|
| 61 |
|
---|
| 62 | while ( lua_next( L, index ) != 0 )
|
---|
| 63 | {
|
---|
| 64 | if ( lua_istable( L, -1 ) )
|
---|
| 65 | {
|
---|
| 66 | nlua_deepcopy( L, -1 );
|
---|
| 67 | lua_insert( L, -2 );
|
---|
| 68 | lua_pop( L, 1 );
|
---|
| 69 | }
|
---|
| 70 | lua_pushvalue(L, -2);
|
---|
| 71 | lua_insert(L, -2);
|
---|
| 72 | lua_settable(L, -4);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|