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

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