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

Last change on this file since 435 was 435, checked in by epyon, 10 years ago
  • short_string implementation - first usages
File size: 6.4 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
9#include "nv/stl/string.hh"
10
11std::string nlua_typecontent( lua_State* L, int idx )
12{
13        int type = lua_type( L, idx );
14        switch ( type )
15        {
16        case LUA_TNONE          : return "NONE";
17        case LUA_TNIL               : return "NIL";
18        case LUA_TBOOLEAN               : return lua_toboolean( L, idx ) == 0 ? "false" : "true";
19        //case LUA_TLIGHTUSERDATA       : return std::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
20        //case LUA_TNUMBER              : return std::to_string( lua_tonumber( L, idx ) );
21        case LUA_TSTRING                : return lua_tostring( L, idx );
22        case LUA_TTABLE             : return "TABLE";
23        case LUA_TFUNCTION              : return "FUNCTION";
24//      case LUA_TUSERDATA              : return std::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
25        case LUA_TTHREAD                : return "THREAD";
26        default : break;
27        }
28        char buffer_data[64];
29        nv::array_ref< char > buffer( buffer_data, 64 );
30        if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA )
31        {
32                size_t l = nv::uint64_to_buffer( buffer, nv::uint64( lua_touserdata( L, idx ) ) );
33                return std::string( buffer_data, l );
34        }
35        else if ( type == LUA_TNUMBER )
36        {
37                size_t l = nv::f64_to_buffer( buffer, lua_tonumber( L, idx ) );
38                return std::string( buffer_data, l );
39        }
40        return "UNKNOWN!";
41}
42
43void nlua_pushreversed( lua_State *L, int index )
44{
45        index = lua_absindex( L, index );
46        int len = static_cast<int>( lua_rawlen( L, index ) );
47        int i   = len;
48        lua_createtable( L, len, 0 );
49        while ( i != 0 )
50        {
51                lua_rawgeti( L, index, i );
52                lua_rawseti( L, -2, len-i+1 );
53                i--;
54        }
55}
56
57
58void nlua_shallowcopy( lua_State *L, int index )
59{
60        index = lua_absindex( L, index );
61        lua_createtable( L, 0, 0 );
62        lua_pushnil( L );
63
64        while ( lua_next( L, index ) != 0 )
65        {
66                lua_pushvalue( L, -2 );
67                lua_insert( L, -2 );
68                lua_settable( L, -4 );
69        }
70}
71
72void nlua_shallowicopy( lua_State *L, int index )
73{
74        index = lua_absindex( L, index );
75        lua_createtable( L, 0, 0 );
76        int i = 0;
77        for(;;)
78        {
79                i++;
80                lua_rawgeti( L, 1, i );
81                if ( lua_isnil( L, -1 ) )
82                {
83                        lua_pop( L, 1 );
84                        break;
85                }
86                lua_rawseti( L, 2, i );
87        };
88}
89
90void nlua_shallowmerge( lua_State *L, int index )
91{
92        index = lua_absindex( L, index );
93        lua_pushnil( L );
94
95        while( lua_next( L, index ) != 0 )
96        {
97                lua_pushvalue( L, -2 );
98                lua_insert( L, -2 );
99                lua_rawset( L, -4 );
100        }
101}
102
103void nlua_deepcopy( lua_State *L, int index )
104{
105        index = lua_absindex( L, index );
106        lua_createtable( L, 0, 0 );
107        lua_pushnil( L );
108
109        while ( lua_next( L, index ) != 0 )
110        {
111                if ( lua_istable( L, -1 ) )
112                {
113                        nlua_deepcopy( L, -1 );
114                        lua_insert( L, -2 );
115                        lua_pop( L, 1 );
116                }
117                lua_pushvalue( L, -2 );
118                lua_insert( L, -2 );
119                lua_settable( L, -4 );
120        }
121}
122
123void nlua_toset( lua_State *L, int index )
124{
125        index = lua_absindex( L, index );
126        lua_createtable( L, 0, 0 );
127        int i = 0;
128        for(;;)
129        {
130                i++;
131                lua_rawgeti( L, index, i );
132                if ( lua_isnil( L, -1 ) )
133                {
134                        lua_pop( L, 1 );
135                        break;
136                }
137                lua_pushboolean( L, true );
138                lua_rawset( L, -3 );
139        };
140}
141
142void nlua_tokeyset( lua_State *L, int index )
143{
144        index = lua_absindex( L, index );
145        lua_createtable( L, 0, 0 );
146        lua_pushnil( L );
147        while ( lua_next( L, index ) != 0 )
148        {
149                lua_pushvalue( L, -2 );
150                lua_pushboolean( L, true );
151                lua_settable( L, -5 );
152                lua_pop( L, 1 );
153        }
154}
155
156void nlua_register( lua_State *L, const char* fname, lua_CFunction func, int index )
157{
158        index = lua_absindex( L, index );
159        lua_pushstring( L, fname );
160        lua_pushcfunction( L, func );
161        lua_rawset( L, index );
162}
163
164void nlua_register( lua_State *L, const luaL_Reg *l, int index )
165{
166        index = lua_absindex( L, index );
167        for (; l->name != NULL; l++)
168        {
169                lua_pushstring( L, l->name );
170                lua_pushcfunction( L, l->func );
171                lua_rawset( L, index );
172        }
173}
174
175void nlua_register( lua_State *L, const char* lname, const char* fname, lua_CFunction func )
176{
177        lua_getglobal( L, lname );
178        if ( !lua_istable( L, -1 ) )
179        {
180                lua_pop( L, 1 );
181                lua_createtable( L, 0, 0 );
182                nlua_register( L, fname, func, -1 );
183                lua_setglobal( L, lname );
184                return;
185        }
186        nlua_register( L, fname, func, -1 );
187        lua_pop( L, 1 );
188}
189
190void nlua_register( lua_State *L, const char* lname, const luaL_Reg *l )
191{
192        lua_getglobal( L, lname );
193        if ( !lua_istable( L, -1 ) )
194        {
195                lua_pop( L, 1 );
196                lua_createtable( L, 0, 0 );
197                nlua_register( L, l, -1 );
198                lua_setglobal( L, lname );
199                return;
200        }
201        nlua_register( L, l, -1 );
202        lua_pop( L, 1 );
203}
204
205void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
206{
207        index = lua_absindex( L, index );
208        nv::uint32 flag;
209        if ( lua_istable( L, index ) )
210        {
211                lua_pushnil( L );
212                while ( lua_next( L, index ) != 0 )
213                {
214                        if ( lua_type( L, -1 ) == LUA_TBOOLEAN )
215                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
216                        else
217                                flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
218                        lua_pop( L, 1 );
219                        if ( flag < count )
220                        {
221                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
222                        }
223                }
224        }
225}
226
227void nlua_toflags_set( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
228{
229        index = lua_absindex( L, index );
230        nv::uint32 flag;
231        if ( lua_istable( L, index ) )
232        {
233                lua_pushnil( L );
234                while ( lua_next( L, index ) != 0 )
235                {
236                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-2 ) );
237                        lua_pop( L, 1 );
238                        if ( flag < count )
239                        {
240                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
241                        }
242                }
243        }
244}
245
246void nlua_toflags_array( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
247{
248        index = lua_absindex( L, index );
249        nv::uint32 flag;
250        if ( lua_istable( L, index ) )
251        {
252                lua_pushnil( L );
253                while ( lua_next( L, index ) != 0 )
254                {
255                        flag = static_cast< nv::uint32 >( lua_tointeger( L ,-1 ) );
256                        lua_pop( L, 1 );
257                        if ( flag < count )
258                        {
259                                data[ flag / 8 ] |= ( 1 << static_cast< nv::uint8 >( flag % 8 ) );
260                        }
261                }
262        }
263}
264
265nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
266{
267        index = lua_absindex( L, index );
268        nv::vector<nv::uint8> result;
269        if ( lua_istable( L, index ) )
270        {
271                lua_pushnil( L );
272                while ( lua_next( L, index ) != 0 )
273                {
274                        result.push_back( static_cast<nv::uint8>( lua_tointeger( L, -1 ) ) );
275                        lua_pop( L, 1 );
276                }
277        }
278        return result;
279}
Note: See TracBrowser for help on using the repository browser.