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

Last change on this file since 496 was 494, checked in by epyon, 9 years ago
  • nlua_tou32array added
File size: 7.5 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#include "nv/lua/lua_raw.hh"
8
9nv::string64 nlua_typecontent( lua_State* L, int idx )
10{
11        int type = lua_type( L, idx );
12        switch ( type )
13        {
14        case LUA_TNONE          : return nv::string64( "NONE" );
15        case LUA_TNIL               : return nv::string64( "NIL" );
16        case LUA_TBOOLEAN               : return nv::string64( lua_toboolean( L, idx ) == 0 ? "false" : "true" );
17        case LUA_TSTRING                : return nv::string64( nlua_tostringview( L, idx ) );
18        case LUA_TTABLE             : return nv::string64( "TABLE" );
19        case LUA_TFUNCTION              : return nv::string64( "FUNCTION" );
20        case LUA_TTHREAD                : return nv::string64( "THREAD" );
21        default : break;
22        }
23        if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA )
24        {
25                nv::string64 buffer;
26                buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
27                return buffer;
28        }
29        else if ( type == LUA_TNUMBER )
30        {
31                nv::string64 buffer;
32                buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
33                return buffer;
34        }
35        return nv::string64( "UNKNOWN!" );
36}
37
38void nlua_pushreversed( lua_State *L, int index )
39{
40        index = nlua_absindex( L, index );
41        int len = static_cast<int>( nlua_rawlen( L, index ) );
42        int i   = len;
43        lua_createtable( L, len, 0 );
44        while ( i != 0 )
45        {
46                lua_rawgeti( L, index, i );
47                lua_rawseti( L, -2, len-i+1 );
48                i--;
49        }
50}
51
52
53void nlua_shallowcopy( lua_State *L, int index )
54{
55        index = nlua_absindex( L, index );
56        lua_createtable( L, 0, 0 );
57        lua_pushnil( L );
58
59        while ( lua_next( L, index ) != 0 )
60        {
61                lua_pushvalue( L, -2 );
62                lua_insert( L, -2 );
63                lua_settable( L, -4 );
64        }
65}
66
67void nlua_shallowicopy( lua_State *L, int index )
68{
69        index = nlua_absindex( L, index );
70        lua_createtable( L, 0, 0 );
71        int i = 0;
72        for(;;)
73        {
74                i++;
75                lua_rawgeti( L, 1, i );
76                if ( lua_isnil( L, -1 ) )
77                {
78                        lua_pop( L, 1 );
79                        break;
80                }
81                lua_rawseti( L, 2, i );
82        };
83}
84
85void nlua_shallowmerge( lua_State *L, int index )
86{
87        index = nlua_absindex( L, index );
88        lua_pushnil( L );
89
90        while( lua_next( L, index ) != 0 )
91        {
92                lua_pushvalue( L, -2 );
93                lua_insert( L, -2 );
94                lua_rawset( L, -4 );
95        }
96}
97
98void nlua_deepcopy( lua_State *L, int index )
99{
100        index = nlua_absindex( L, index );
101        lua_createtable( L, 0, 0 );
102        lua_pushnil( L );
103
104        while ( lua_next( L, index ) != 0 )
105        {
106                if ( lua_istable( L, -1 ) )
107                {
108                        nlua_deepcopy( L, -1 );
109                        lua_insert( L, -2 );
110                        lua_pop( L, 1 );
111                }
112                lua_pushvalue( L, -2 );
113                lua_insert( L, -2 );
114                lua_settable( L, -4 );
115        }
116}
117
118void nlua_toset( lua_State *L, int index )
119{
120        index = nlua_absindex( L, index );
121        lua_createtable( L, 0, 0 );
122        int i = 0;
123        for(;;)
124        {
125                i++;
126                lua_rawgeti( L, index, i );
127                if ( lua_isnil( L, -1 ) )
128                {
129                        lua_pop( L, 1 );
130                        break;
131                }
132                lua_pushboolean( L, true );
133                lua_rawset( L, -3 );
134        };
135}
136
137void nlua_tokeyset( lua_State *L, int index )
138{
139        index = nlua_absindex( L, index );
140        lua_createtable( L, 0, 0 );
141        lua_pushnil( L );
142        while ( lua_next( L, index ) != 0 )
143        {
144                lua_pushvalue( L, -2 );
145                lua_pushboolean( L, true );
146                lua_settable( L, -5 );
147                lua_pop( L, 1 );
148        }
149}
150
151void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index )
152{
153        index = nlua_absindex( L, index );
154        lua_pushstring( L, fname );
155        lua_pushcfunction( L, func );
156        lua_rawset( L, index );
157}
158
159void nlua_register( lua_State *L, const luaL_Reg *l, int index )
160{
161        index = nlua_absindex( L, index );
162        for (; l->name != NULL; l++)
163        {
164                lua_pushstring( L, l->name );
165                lua_pushcfunction( L, l->func );
166                lua_rawset( L, index );
167        }
168}
169
170void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func )
171{
172        lua_getglobal( L, lname );
173        if ( !lua_istable( L, -1 ) )
174        {
175                lua_pop( L, 1 );
176                lua_createtable( L, 0, 0 );
177                nlua_register( L, fname, func, -1 );
178                lua_setglobal( L, lname );
179                return;
180        }
181        nlua_register( L, fname, func, -1 );
182        lua_pop( L, 1 );
183}
184
185void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l )
186{
187        lua_getglobal( L, lname );
188        if ( !lua_istable( L, -1 ) )
189        {
190                lua_pop( L, 1 );
191                lua_createtable( L, 0, 0 );
192                nlua_register( L, l, -1 );
193                lua_setglobal( L, lname );
194                return;
195        }
196        nlua_register( L, l, -1 );
197        lua_pop( L, 1 );
198}
199
200void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
201{
202        index = nlua_absindex( L, index );
203        nv::uint32 flag;
204        if ( lua_istable( L, index ) )
205        {
206                lua_pushnil( L );
207                while ( lua_next( L, index ) != 0 )
208                {
209                        if ( lua_type( L, -1 ) == LUA_TBOOLEAN )
210                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
211                        else
212                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
213                        lua_pop( L, 1 );
214                        if ( flag < count )
215                        {
216                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
217                        }
218                }
219        }
220}
221
222void nlua_toflags_set( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
223{
224        index = nlua_absindex( L, index );
225        nv::uint32 flag;
226        if ( lua_istable( L, index ) )
227        {
228                lua_pushnil( L );
229                while ( lua_next( L, index ) != 0 )
230                {
231                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
232                        lua_pop( L, 1 );
233                        if ( flag < count )
234                        {
235                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
236                        }
237                }
238        }
239}
240
241void nlua_toflags_array( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
242{
243        index = nlua_absindex( L, index );
244        nv::uint32 flag;
245        if ( lua_istable( L, index ) )
246        {
247                lua_pushnil( L );
248                while ( lua_next( L, index ) != 0 )
249                {
250                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
251                        lua_pop( L, 1 );
252                        if ( flag < count )
253                        {
254                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
255                        }
256                }
257        }
258}
259
260nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
261{
262        index = nlua_absindex( L, index );
263        nv::vector<nv::uint8> result;
264        if ( lua_istable( L, index ) )
265        {
266                lua_pushnil( L );
267                while ( lua_next( L, index ) != 0 )
268                {
269                        result.push_back( static_cast<nv::uint8>( lua_tointeger( L, -1 ) ) );
270                        lua_pop( L, 1 );
271                }
272        }
273        return result;
274}
275
276nv::vector<nv::uint32> nlua_tou32array( lua_State *L, int index )
277{
278        index = nlua_absindex( L, index );
279        nv::vector<nv::uint32> result;
280        if ( lua_istable( L, index ) )
281        {
282                lua_pushnil( L );
283                while ( lua_next( L, index ) != 0 )
284                {
285                        result.push_back( static_cast<nv::uint32>( lua_tointeger( L, -1 ) ) );
286                        lua_pop( L, 1 );
287                }
288        }
289        return result;
290}
291
292void * nlua_testudata( lua_State *L, int ud, const char *tname )
293{
294        void *p = lua_touserdata( L, ud );
295        if ( p != NULL )
296        {
297                if ( lua_getmetatable( L, ud ) )
298                {
299                        luaL_getmetatable( L, tname );
300                        if ( !lua_rawequal( L, -1, -2 ) )
301                                p = NULL;
302                        lua_pop( L, 2 );
303                        return p;
304                }
305        }
306        return NULL;
307}
308
309void nlua_setmetatable( lua_State *L, const char *tname )
310{
311        int only_works_for_51;
312        luaL_getmetatable( L, tname );
313        lua_setmetatable( L, -2 );
314}
315
316void nlua_requiref( lua_State *L, const char *modname, lua_CFunction openf, int glb )
317{
318        int only_works_for_51;
319        lua_pushcfunction( L, openf );
320        lua_pushstring( L, modname );
321        lua_call( L, 1, 1 );
322        if ( glb != 0 )
323        {
324                lua_pushvalue( L, LUA_GLOBALSINDEX );
325                lua_pushvalue( L, -2 );
326                lua_setfield( L, -2, modname );
327                lua_pop( L, 1 );
328        }
329}
330
331int nlua_rawlen( lua_State* L, int index )
332{
333        return lua_objlen( L, index );
334}
335
336void nlua_pushglobaltable( lua_State* L )
337{
338        int only_works_for_51;
339        lua_pushvalue( L, LUA_GLOBALSINDEX );
340}
Note: See TracBrowser for help on using the repository browser.