[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 |
|
---|
[368] | 9 | #include "nv/stl/string.hh"
|
---|
[51] | 10 |
|
---|
| 11 | std::string nlua_typecontent( lua_State* L, int idx )
|
---|
| 12 | {
|
---|
[380] | 13 | int type = lua_type( L, idx );
|
---|
| 14 | switch ( type )
|
---|
[51] | 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";
|
---|
[380] | 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 ) );
|
---|
[51] | 21 | case LUA_TSTRING : return lua_tostring( L, idx );
|
---|
| 22 | case LUA_TTABLE : return "TABLE";
|
---|
| 23 | case LUA_TFUNCTION : return "FUNCTION";
|
---|
[380] | 24 | // case LUA_TUSERDATA : return std::to_string( nv::uint64( lua_touserdata( L, idx ) ) );
|
---|
[51] | 25 | case LUA_TTHREAD : return "THREAD";
|
---|
[380] | 26 | default : break;
|
---|
[51] | 27 | }
|
---|
[380] | 28 | char buffer[64];
|
---|
| 29 | if ( type == LUA_TLIGHTUSERDATA || type == LUA_TUSERDATA )
|
---|
| 30 | {
|
---|
| 31 | size_t l = nv::uint64_to_buffer( nv::uint64( lua_touserdata( L, idx ) ), buffer );
|
---|
| 32 | return std::string( buffer, l );
|
---|
| 33 | }
|
---|
| 34 | else if ( type == LUA_TNUMBER )
|
---|
| 35 | {
|
---|
| 36 | size_t l = nv::f64_to_buffer( lua_tonumber( L, idx ), buffer );
|
---|
| 37 | return std::string( buffer, l );
|
---|
| 38 | }
|
---|
| 39 | return "UNKNOWN!";
|
---|
[51] | 40 | }
|
---|
| 41 |
|
---|
[85] | 42 | void nlua_pushreversed( lua_State *L, int index )
|
---|
| 43 | {
|
---|
| 44 | index = lua_absindex( L, index );
|
---|
[121] | 45 | int len = static_cast<int>( lua_rawlen( L, index ) );
|
---|
[85] | 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 |
|
---|
[51] | 57 | void nlua_shallowcopy( lua_State *L, int index )
|
---|
| 58 | {
|
---|
[85] | 59 | index = lua_absindex( L, index );
|
---|
| 60 | lua_createtable( L, 0, 0 );
|
---|
| 61 | lua_pushnil( L );
|
---|
[51] | 62 |
|
---|
| 63 | while ( lua_next( L, index ) != 0 )
|
---|
| 64 | {
|
---|
[85] | 65 | lua_pushvalue( L, -2 );
|
---|
| 66 | lua_insert( L, -2 );
|
---|
| 67 | lua_settable( L, -4 );
|
---|
[51] | 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[85] | 71 | void 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 |
|
---|
[51] | 89 | void nlua_shallowmerge( lua_State *L, int index )
|
---|
| 90 | {
|
---|
[85] | 91 | index = lua_absindex( L, index );
|
---|
| 92 | lua_pushnil( L );
|
---|
[51] | 93 |
|
---|
[85] | 94 | while( lua_next( L, index ) != 0 )
|
---|
[51] | 95 | {
|
---|
[85] | 96 | lua_pushvalue( L, -2 );
|
---|
| 97 | lua_insert( L, -2 );
|
---|
| 98 | lua_rawset( L, -4 );
|
---|
[51] | 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void nlua_deepcopy( lua_State *L, int index )
|
---|
| 103 | {
|
---|
[85] | 104 | index = lua_absindex( L, index );
|
---|
| 105 | lua_createtable( L, 0, 0 );
|
---|
| 106 | lua_pushnil( L );
|
---|
[51] | 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 | }
|
---|
[85] | 116 | lua_pushvalue( L, -2 );
|
---|
| 117 | lua_insert( L, -2 );
|
---|
| 118 | lua_settable( L, -4 );
|
---|
[51] | 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[85] | 122 | void 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 |
|
---|
| 141 | void 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 |
|
---|
| 155 | void 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 |
|
---|
| 163 | void 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 |
|
---|
| 174 | void 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 |
|
---|
| 189 | void 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 | }
|
---|
[162] | 203 |
|
---|
| 204 | void 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 |
|
---|
| 226 | void 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 |
|
---|
| 245 | void 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 | }
|
---|
[220] | 263 |
|
---|
[383] | 264 | nv::vector<nv::uint8> nlua_tobytearray( lua_State *L, int index )
|
---|
[220] | 265 | {
|
---|
| 266 | index = lua_absindex( L, index );
|
---|
[383] | 267 | nv::vector<nv::uint8> result;
|
---|
[220] | 268 | if ( lua_istable( L, index ) )
|
---|
| 269 | {
|
---|
| 270 | lua_pushnil( L );
|
---|
| 271 | while ( lua_next( L, index ) != 0 )
|
---|
| 272 | {
|
---|
[406] | 273 | result.push_back( static_cast<nv::uint8>( lua_tointeger( L, -1 ) ) );
|
---|
[220] | 274 | lua_pop( L, 1 );
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 | return result;
|
---|
| 278 | }
|
---|