source: trunk/src/lua/lua_raw.cc @ 217

Last change on this file since 217 was 162, checked in by epyon, 12 years ago
  • flags - flags support, essentially a std::bitset implementation with .data() method and enum class support
File size: 5.6 KB
Line 
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
11std::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
29void nlua_pushreversed( lua_State *L, int index )
30{
31        index = lua_absindex( L, index );
32        int len = static_cast<int>( lua_rawlen( L, index ) );
33        int i   = len;
34        lua_createtable( L, len, 0 );
35        while ( i != 0 )
36        {
37                lua_rawgeti( L, index, i );
38                lua_rawseti( L, -2, len-i+1 );
39                i--;
40        }
41}
42
43
44void nlua_shallowcopy( lua_State *L, int index )
45{
46        index = lua_absindex( L, index );
47        lua_createtable( L, 0, 0 );
48        lua_pushnil( L );
49
50        while ( lua_next( L, index ) != 0 )
51        {
52                lua_pushvalue( L, -2 );
53                lua_insert( L, -2 );
54                lua_settable( L, -4 );
55        }
56}
57
58void nlua_shallowicopy( lua_State *L, int index )
59{
60        index = lua_absindex( L, index );
61        lua_createtable( L, 0, 0 );
62        int i = 0;
63        for(;;)
64        {
65                i++;
66                lua_rawgeti( L, 1, i );
67                if ( lua_isnil( L, -1 ) )
68                {
69                        lua_pop( L, 1 );
70                        break;
71                }
72                lua_rawseti( L, 2, i );
73        };
74}
75
76void nlua_shallowmerge( lua_State *L, int index )
77{
78        index = lua_absindex( L, index );
79        lua_pushnil( L );
80
81        while( lua_next( L, index ) != 0 )
82        {
83                lua_pushvalue( L, -2 );
84                lua_insert( L, -2 );
85                lua_rawset( L, -4 );
86        }
87}
88
89void nlua_deepcopy( lua_State *L, int index )
90{
91        index = lua_absindex( L, index );
92        lua_createtable( L, 0, 0 );
93        lua_pushnil( L );
94
95        while ( lua_next( L, index ) != 0 )
96        {
97                if ( lua_istable( L, -1 ) )
98                {
99                        nlua_deepcopy( L, -1 );
100                        lua_insert( L, -2 );
101                        lua_pop( L, 1 );
102                }
103                lua_pushvalue( L, -2 );
104                lua_insert( L, -2 );
105                lua_settable( L, -4 );
106        }
107}
108
109void nlua_toset( lua_State *L, int index )
110{
111        index = lua_absindex( L, index );
112        lua_createtable( L, 0, 0 );
113        int i = 0;
114        for(;;)
115        {
116                i++;
117                lua_rawgeti( L, index, i );
118                if ( lua_isnil( L, -1 ) )
119                {
120                        lua_pop( L, 1 );
121                        break;
122                }
123                lua_pushboolean( L, true );
124                lua_rawset( L, -3 );
125        };
126}
127
128void nlua_tokeyset( lua_State *L, int index )
129{
130        index = lua_absindex( L, index );
131        lua_createtable( L, 0, 0 );
132        lua_pushnil( L );
133        while ( lua_next( L, index ) != 0 )
134        {
135                lua_pushvalue( L, -2 );
136                lua_pushboolean( L, true );
137                lua_settable( L, -5 );
138                lua_pop( L, 1 );
139        }
140}
141
142void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index )
143{
144        index = lua_absindex( L, index );
145        lua_pushstring( L, fname );
146        lua_pushcfunction( L, func );
147        lua_rawset( L, index );
148}
149
150void nlua_register( lua_State *L, const luaL_Reg *l, int index )
151{
152        index = lua_absindex( L, index );
153        for (; l->name != NULL; l++)
154        {
155                lua_pushstring( L, l->name );
156                lua_pushcfunction( L, l->func );
157                lua_rawset( L, index );
158        }
159}
160
161void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func )
162{
163        lua_getglobal( L, lname );
164        if ( !lua_istable( L, -1 ) )
165        {
166                lua_pop( L, 1 );
167                lua_createtable( L, 0, 0 );
168                nlua_register( L, fname, func, -1 );
169                lua_setglobal( L, lname );
170                return;
171        }
172        nlua_register( L, fname, func, -1 );
173        lua_pop( L, 1 );
174}
175
176void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l )
177{
178        lua_getglobal( L, lname );
179        if ( !lua_istable( L, -1 ) )
180        {
181                lua_pop( L, 1 );
182                lua_createtable( L, 0, 0 );
183                nlua_register( L, l, -1 );
184                lua_setglobal( L, lname );
185                return;
186        }
187        nlua_register( L, l, -1 );
188        lua_pop( L, 1 );
189}
190
191void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
192{
193        index = lua_absindex( L, index );
194        nv::uint32 flag;
195        if ( lua_istable( L, index ) )
196        {
197                lua_pushnil( L );
198                while ( lua_next( L, index ) != 0 )
199                {
200                        if ( lua_type( L, -1 ) == LUA_TBOOLEAN )
201                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
202                        else
203                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
204                        lua_pop( L, 1 );
205                        if ( flag < count )
206                        {
207                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
208                        }
209                }
210        }
211}
212
213void nlua_toflags_set( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
214{
215        index = lua_absindex( L, index );
216        nv::uint32 flag;
217        if ( lua_istable( L, index ) )
218        {
219                lua_pushnil( L );
220                while ( lua_next( L, index ) != 0 )
221                {
222                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
223                        lua_pop( L, 1 );
224                        if ( flag < count )
225                        {
226                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
227                        }
228                }
229        }
230}
231
232void nlua_toflags_array( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
233{
234        index = lua_absindex( L, index );
235        nv::uint32 flag;
236        if ( lua_istable( L, index ) )
237        {
238                lua_pushnil( L );
239                while ( lua_next( L, index ) != 0 )
240                {
241                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
242                        lua_pop( L, 1 );
243                        if ( flag < count )
244                        {
245                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
246                        }
247                }
248        }
249}
Note: See TracBrowser for help on using the repository browser.