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 | nv::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 |
|
---|
38 | void 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 |
|
---|
53 | void 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 |
|
---|
67 | void 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 |
|
---|
85 | void 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 |
|
---|
98 | void 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 |
|
---|
118 | void 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 |
|
---|
137 | void 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 |
|
---|
151 | void 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 |
|
---|
159 | void 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 |
|
---|
170 | void 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 |
|
---|
185 | void 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 |
|
---|
200 | void 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 |
|
---|
222 | void 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 |
|
---|
241 | void 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 |
|
---|
260 | nv::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 |
|
---|
276 | void * nlua_testudata( lua_State *L, int ud, const char *tname )
|
---|
277 | {
|
---|
278 | void *p = lua_touserdata( L, ud );
|
---|
279 | if ( p != NULL )
|
---|
280 | {
|
---|
281 | if ( lua_getmetatable( L, ud ) )
|
---|
282 | {
|
---|
283 | luaL_getmetatable( L, tname );
|
---|
284 | if ( !lua_rawequal( L, -1, -2 ) )
|
---|
285 | p = NULL;
|
---|
286 | lua_pop( L, 2 );
|
---|
287 | return p;
|
---|
288 | }
|
---|
289 | }
|
---|
290 | return NULL;
|
---|
291 | }
|
---|
292 |
|
---|
293 | void nlua_setmetatable( lua_State *L, const char *tname )
|
---|
294 | {
|
---|
295 | int only_works_for_51;
|
---|
296 | luaL_getmetatable( L, tname );
|
---|
297 | lua_setmetatable( L, -2 );
|
---|
298 | }
|
---|
299 |
|
---|
300 | void nlua_requiref( lua_State *L, const char *modname, lua_CFunction openf, int glb )
|
---|
301 | {
|
---|
302 | int only_works_for_51;
|
---|
303 | lua_pushcfunction( L, openf );
|
---|
304 | lua_pushstring( L, modname );
|
---|
305 | lua_call( L, 1, 1 );
|
---|
306 | if ( glb != 0 )
|
---|
307 | {
|
---|
308 | lua_pushvalue( L, LUA_GLOBALSINDEX );
|
---|
309 | lua_pushvalue( L, -2 );
|
---|
310 | lua_setfield( L, -2, modname );
|
---|
311 | lua_pop( L, 1 );
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | int nlua_rawlen( lua_State* L, int index )
|
---|
316 | {
|
---|
317 | return lua_objlen( L, index );
|
---|
318 | }
|
---|
319 |
|
---|
320 | void nlua_pushglobaltable( lua_State* L )
|
---|
321 | {
|
---|
322 | int only_works_for_51;
|
---|
323 | lua_pushvalue( L, LUA_GLOBALSINDEX );
|
---|
324 | }
|
---|