[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
[51] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[51] | 6 |
|
---|
| 7 | #include "nv/lua/lua_raw.hh"
|
---|
| 8 |
|
---|
[441] | 9 | nv::string64 nlua_typecontent( lua_State* L, int idx )
|
---|
[51] | 10 | {
|
---|
[380] | 11 | int type = lua_type( L, idx );
|
---|
| 12 | switch ( type )
|
---|
[51] | 13 | {
|
---|
[441] | 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" );
|
---|
[380] | 21 | default : break;
|
---|
[51] | 22 | }
|
---|
[380] | 23 | if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA )
|
---|
| 24 | {
|
---|
[441] | 25 | nv::string64 buffer;
|
---|
[449] | 26 | buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
[441] | 27 | return buffer;
|
---|
[380] | 28 | }
|
---|
| 29 | else if ( type == LUA_TNUMBER )
|
---|
| 30 | {
|
---|
[441] | 31 | nv::string64 buffer;
|
---|
[449] | 32 | buffer.append( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
[441] | 33 | return buffer;
|
---|
[380] | 34 | }
|
---|
[441] | 35 | return nv::string64( "UNKNOWN!" );
|
---|
[51] | 36 | }
|
---|
| 37 |
|
---|
[85] | 38 | void nlua_pushreversed( lua_State *L, int index )
|
---|
| 39 | {
|
---|
[490] | 40 | index = nlua_absindex( L, index );
|
---|
| 41 | int len = static_cast<int>( nlua_rawlen( L, index ) );
|
---|
[85] | 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 |
|
---|
[51] | 53 | void nlua_shallowcopy( lua_State *L, int index )
|
---|
| 54 | {
|
---|
[490] | 55 | index = nlua_absindex( L, index );
|
---|
[85] | 56 | lua_createtable( L, 0, 0 );
|
---|
| 57 | lua_pushnil( L );
|
---|
[51] | 58 |
|
---|
| 59 | while ( lua_next( L, index ) != 0 )
|
---|
| 60 | {
|
---|
[85] | 61 | lua_pushvalue( L, -2 );
|
---|
| 62 | lua_insert( L, -2 );
|
---|
| 63 | lua_settable( L, -4 );
|
---|
[51] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[85] | 67 | void nlua_shallowicopy( lua_State *L, int index )
|
---|
| 68 | {
|
---|
[490] | 69 | index = nlua_absindex( L, index );
|
---|
[85] | 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 |
|
---|
[51] | 85 | void nlua_shallowmerge( lua_State *L, int index )
|
---|
| 86 | {
|
---|
[490] | 87 | index = nlua_absindex( L, index );
|
---|
[85] | 88 | lua_pushnil( L );
|
---|
[51] | 89 |
|
---|
[85] | 90 | while( lua_next( L, index ) != 0 )
|
---|
[51] | 91 | {
|
---|
[85] | 92 | lua_pushvalue( L, -2 );
|
---|
| 93 | lua_insert( L, -2 );
|
---|
| 94 | lua_rawset( L, -4 );
|
---|
[51] | 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void nlua_deepcopy( lua_State *L, int index )
|
---|
| 99 | {
|
---|
[490] | 100 | index = nlua_absindex( L, index );
|
---|
[85] | 101 | lua_createtable( L, 0, 0 );
|
---|
| 102 | lua_pushnil( L );
|
---|
[51] | 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 | }
|
---|
[85] | 112 | lua_pushvalue( L, -2 );
|
---|
| 113 | lua_insert( L, -2 );
|
---|
| 114 | lua_settable( L, -4 );
|
---|
[51] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[85] | 118 | void nlua_toset( lua_State *L, int index )
|
---|
| 119 | {
|
---|
[490] | 120 | index = nlua_absindex( L, index );
|
---|
[85] | 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 | {
|
---|
[490] | 139 | index = nlua_absindex( L, index );
|
---|
[85] | 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 | {
|
---|
[490] | 153 | index = nlua_absindex( L, index );
|
---|
[85] | 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 | {
|
---|
[490] | 161 | index = nlua_absindex( L, index );
|
---|
[85] | 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 | }
|
---|
[162] | 199 |
|
---|
| 200 | void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
|
---|
| 201 | {
|
---|
[490] | 202 | index = nlua_absindex( L, index );
|
---|
[162] | 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 | {
|
---|
[490] | 224 | index = nlua_absindex( L, index );
|
---|
[162] | 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 | {
|
---|
[490] | 243 | index = nlua_absindex( L, index );
|
---|
[162] | 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 | }
|
---|
[220] | 259 |
|
---|
[383] | 260 | nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
|
---|
[220] | 261 | {
|
---|
[490] | 262 | index = nlua_absindex( L, index );
|
---|
[383] | 263 | nv::vector<nv::uint8> result;
|
---|
[220] | 264 | if ( lua_istable( L, index ) )
|
---|
| 265 | {
|
---|
| 266 | lua_pushnil( L );
|
---|
| 267 | while ( lua_next( L, index ) != 0 )
|
---|
| 268 | {
|
---|
[406] | 269 | result.push_back( static_cast<nv::uint8>( lua_tointeger( L, -1 ) ) );
|
---|
[220] | 270 | lua_pop( L, 1 );
|
---|
| 271 | }
|
---|
| 272 | }
|
---|
| 273 | return result;
|
---|
| 274 | }
|
---|
[490] | 275 |
|
---|
[494] | 276 | nv::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 |
|
---|
[490] | 292 | void * 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 |
|
---|
| 309 | void 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 |
|
---|
| 316 | void 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 |
|
---|
| 331 | int nlua_rawlen( lua_State* L, int index )
|
---|
| 332 | {
|
---|
| 333 | return lua_objlen( L, index );
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | void nlua_pushglobaltable( lua_State* L )
|
---|
| 337 | {
|
---|
| 338 | int only_works_for_51;
|
---|
| 339 | lua_pushvalue( L, LUA_GLOBALSINDEX );
|
---|
| 340 | }
|
---|