[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 | {
|
---|
| 40 | index = lua_absindex( L, index );
|
---|
[121] | 41 | int len = static_cast<int>( lua_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 | {
|
---|
[85] | 55 | index = lua_absindex( L, index );
|
---|
| 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 | {
|
---|
| 69 | index = lua_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 |
|
---|
[51] | 85 | void nlua_shallowmerge( lua_State *L, int index )
|
---|
| 86 | {
|
---|
[85] | 87 | index = lua_absindex( L, index );
|
---|
| 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 | {
|
---|
[85] | 100 | index = lua_absindex( L, index );
|
---|
| 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 | {
|
---|
| 120 | index = lua_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 = lua_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 = lua_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 = lua_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 | }
|
---|
[162] | 199 |
|
---|
| 200 | void nlua_toflags( lua_State *L, int index, nv::uint8* data, nv::uint32 count )
|
---|
| 201 | {
|
---|
| 202 | index = lua_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 = lua_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 = lua_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 | }
|
---|
[220] | 259 |
|
---|
[383] | 260 | nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
|
---|
[220] | 261 | {
|
---|
| 262 | index = lua_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 | }
|
---|