[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
[9] | 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.
|
---|
[9] | 6 |
|
---|
| 7 | #include "nv/lua/lua_state.hh"
|
---|
| 8 |
|
---|
[51] | 9 | #include "nv/lua/lua_raw.hh"
|
---|
[217] | 10 | #include "nv/lua/lua_nova.hh"
|
---|
[503] | 11 | #include "nv/lua/lua_types.hh"
|
---|
[319] | 12 | #include "nv/core/logging.hh"
|
---|
[368] | 13 | #include "nv/stl/string.hh"
|
---|
[9] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
[216] | 17 | // stack_guard
|
---|
| 18 |
|
---|
[403] | 19 | #define NV_LUA_ABORT( func, ... ) \
|
---|
| 20 | NV_LOG_CRITICAL( "lua::" func " : ", __VA_ARGS__ ) \
|
---|
| 21 | NV_ABORT( "lua::" func " : critical error!" )
|
---|
| 22 |
|
---|
| 23 |
|
---|
[121] | 24 | lua::stack_guard::stack_guard( lua::state* aL )
|
---|
[206] | 25 | : L(aL), m_level( lua_gettop(aL->m_state) )
|
---|
[9] | 26 | {
|
---|
| 27 |
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[121] | 30 | lua::stack_guard::stack_guard( lua::state& aL )
|
---|
[206] | 31 | : L(&aL), m_level( lua_gettop(aL.m_state) )
|
---|
[86] | 32 | {
|
---|
| 33 |
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[9] | 36 | lua::stack_guard::~stack_guard()
|
---|
| 37 | {
|
---|
[206] | 38 | lua_settop( L->m_state, m_level );
|
---|
[9] | 39 | }
|
---|
| 40 |
|
---|
[334] | 41 | // stack_assert
|
---|
| 42 | nv::lua::stack_assert::stack_assert( lua::state* aL, int expected )
|
---|
| 43 | : L(aL->get_raw()), m_expected( lua_gettop(aL->get_raw() ) + expected )
|
---|
| 44 | {
|
---|
| 45 |
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | nv::lua::stack_assert::stack_assert( lua::state& aL, int expected )
|
---|
| 49 | : L(aL.get_raw()), m_expected( lua_gettop(aL.get_raw() ) + expected )
|
---|
| 50 | {
|
---|
| 51 |
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | nv::lua::stack_assert::stack_assert( lua_State* aL, int expected )
|
---|
| 55 | : L(aL), m_expected( lua_gettop(aL) + expected )
|
---|
| 56 | {
|
---|
| 57 |
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | lua::stack_assert::~stack_assert()
|
---|
| 61 | {
|
---|
| 62 | NV_ASSERT( lua_gettop(L) == m_expected, "Lua stack corruption detected!" );
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 |
|
---|
[216] | 66 | // state_wrapper
|
---|
| 67 |
|
---|
| 68 | void nv::lua::state_wrapper::push_global_table()
|
---|
[9] | 69 | {
|
---|
[490] | 70 | nlua_pushglobaltable( m_state );
|
---|
[206] | 71 | }
|
---|
| 72 |
|
---|
[216] | 73 | void nv::lua::state_wrapper::pop_global_table()
|
---|
[206] | 74 | {
|
---|
[216] | 75 | lua_replace( m_state, -2 );
|
---|
[9] | 76 | }
|
---|
| 77 |
|
---|
[216] | 78 | void lua::state_wrapper::call_get()
|
---|
[9] | 79 | {
|
---|
[216] | 80 | lua_gettable( m_state, -2 );
|
---|
[9] | 81 | }
|
---|
| 82 |
|
---|
[216] | 83 | void lua::state_wrapper::call_get_raw()
|
---|
[9] | 84 | {
|
---|
[216] | 85 | lua_rawget( m_state, -2 );
|
---|
[9] | 86 | }
|
---|
| 87 |
|
---|
[399] | 88 | void lua::state_wrapper::register_native_function( lfunction f, string_view name )
|
---|
[9] | 89 | {
|
---|
[216] | 90 | if ( m_global ) push_global_table();
|
---|
| 91 | lua_pushcfunction( m_state, f );
|
---|
[360] | 92 | lua_setfield( m_state, -2, name.data() );
|
---|
[216] | 93 | if ( m_global ) pop_global_table();
|
---|
[9] | 94 | }
|
---|
| 95 |
|
---|
[216] | 96 | lua::state_wrapper::~state_wrapper()
|
---|
[9] | 97 | {
|
---|
[216] | 98 | if (m_owner)
|
---|
[9] | 99 | {
|
---|
[216] | 100 | lua_close( m_state );
|
---|
[9] | 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[216] | 104 | bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
|
---|
[9] | 105 | {
|
---|
[216] | 106 | if ( !p.resolve( m_state, global ) )
|
---|
[9] | 107 | {
|
---|
| 108 | return false;
|
---|
| 109 | }
|
---|
[216] | 110 | bool result = !lua_isnil( m_state, -1 );
|
---|
| 111 | lua_pop( m_state, 1 );
|
---|
| 112 | return result;
|
---|
[9] | 113 | }
|
---|
| 114 |
|
---|
[216] | 115 | bool nv::lua::state_wrapper::push_function( const path& p, bool global )
|
---|
[9] | 116 | {
|
---|
[216] | 117 | if ( !p.resolve( m_state, global ) )
|
---|
[9] | 118 | {
|
---|
[440] | 119 | NV_LOG_ERROR( "Lua error : not a valid path - ", p.to_string() );
|
---|
[9] | 120 | return false;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[216] | 123 | if ( !lua_isfunction( m_state, -1 ) )
|
---|
[9] | 124 | {
|
---|
[206] | 125 | lua_pop( m_state, 1 );
|
---|
[440] | 126 | NV_LOG_ERROR( "Lua error : not a valid function - ", p.to_string() );
|
---|
[216] | 127 | return false;
|
---|
[9] | 128 | }
|
---|
[216] | 129 | return true;
|
---|
[9] | 130 | }
|
---|
| 131 |
|
---|
[216] | 132 | int nv::lua::state_wrapper::call_function( int nargs, int nresults )
|
---|
[9] | 133 | {
|
---|
[216] | 134 | int status = lua_pcall( m_state, nargs, nresults, 0 );
|
---|
| 135 | if ( status != 0 )
|
---|
[9] | 136 | {
|
---|
[437] | 137 | NV_LOG_ERROR( "Lua error : ", nlua_tostringview( m_state, -1 ) );
|
---|
[216] | 138 | lua_pop( m_state, 1 );
|
---|
[9] | 139 | }
|
---|
[216] | 140 | return status;
|
---|
[9] | 141 | }
|
---|
| 142 |
|
---|
[216] | 143 | // table_guard
|
---|
[9] | 144 |
|
---|
[181] | 145 | lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
|
---|
[503] | 146 | : state_wrapper( lstate->get_raw(), lstate->m_lua_types, false ), m_parent( lstate ), m_level(0)
|
---|
[9] | 147 | {
|
---|
[206] | 148 | m_global = false;
|
---|
| 149 | m_level = lua_gettop( m_state );
|
---|
[305] | 150 | if ( !p.resolve( m_state, global ) || lua_type(m_state, -1) != LUA_TTABLE )
|
---|
[181] | 151 | {
|
---|
[365] | 152 | NV_LOG_ERROR( "Could not resolve table!" );
|
---|
[181] | 153 | // TODO : error handling
|
---|
| 154 | }
|
---|
[9] | 155 | }
|
---|
| 156 |
|
---|
[181] | 157 | lua::table_guard::table_guard( const table_guard& parent, const path& p )
|
---|
[503] | 158 | : state_wrapper( parent.m_state, parent.m_lua_types, false ), m_parent( parent.m_parent ), m_level(0)
|
---|
[9] | 159 | {
|
---|
[206] | 160 | m_global = false;
|
---|
| 161 | m_level = lua_gettop( m_state );
|
---|
[305] | 162 | if ( !p.resolve( m_state, false ) || lua_type(m_state, -1) != LUA_TTABLE )
|
---|
[181] | 163 | {
|
---|
[365] | 164 | NV_LOG_ERROR( "Could not resolve table!" );
|
---|
[181] | 165 | // TODO : error handling
|
---|
| 166 | }
|
---|
[9] | 167 | }
|
---|
| 168 |
|
---|
[216] | 169 | lua::table_guard::~table_guard()
|
---|
| 170 | {
|
---|
| 171 | lua_settop( m_state, m_level );
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[376] | 174 | nv::size_t lua::table_guard::get_size()
|
---|
[188] | 175 | {
|
---|
[490] | 176 | return nlua_rawlen( m_state, -1 );
|
---|
[188] | 177 | }
|
---|
| 178 |
|
---|
[399] | 179 | bool lua::table_guard::has_field( string_view element )
|
---|
[9] | 180 | {
|
---|
[360] | 181 | lua_getfield( m_state, -1, element.data() );
|
---|
[288] | 182 | bool result = !( lua_isnil( m_state, -1 ) );
|
---|
[206] | 183 | lua_pop( m_state, 1 );
|
---|
[9] | 184 | return result;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[431] | 187 | shash64 nv::lua::table_guard::get_string_hash_64( string_view element, uint64 defval /*= 0 */ )
|
---|
[426] | 188 | {
|
---|
| 189 | lua_getfield( m_state, -1, element.data() );
|
---|
| 190 | size_t l = 0;
|
---|
| 191 | const char* str = nullptr;
|
---|
| 192 | uint64 result = defval;
|
---|
| 193 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 194 | {
|
---|
| 195 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 196 | result = hash_string< uint64 >( str, l );
|
---|
[486] | 197 | //NV_LOG_DEBUG( str );
|
---|
[426] | 198 | }
|
---|
| 199 | lua_pop( m_state, 1 );
|
---|
[431] | 200 | return shash64( result );
|
---|
[426] | 201 | }
|
---|
| 202 |
|
---|
[505] | 203 | nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ )
|
---|
[431] | 204 | {
|
---|
| 205 | lua_getfield( m_state, -1, element.data() );
|
---|
| 206 | size_t l = 0;
|
---|
| 207 | const char* str = nullptr;
|
---|
[486] | 208 | shash64 result = shash64( defval );
|
---|
[431] | 209 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 210 | {
|
---|
| 211 | str = lua_tolstring( m_state, -1, &l );
|
---|
[505] | 212 | string_view sv( str, l );
|
---|
| 213 | result = table ? table->insert( sv ) : shash64( sv );
|
---|
[431] | 214 | }
|
---|
| 215 | lua_pop( m_state, 1 );
|
---|
[486] | 216 | return result;
|
---|
[431] | 217 | }
|
---|
| 218 |
|
---|
[505] | 219 | shash64 nv::lua::table_guard::get_string_hash_64( int i, uint64 defval /*= 0 */ )
|
---|
[486] | 220 | {
|
---|
[505] | 221 | lua_rawgeti( m_state, -1, i );
|
---|
[486] | 222 | size_t l = 0;
|
---|
| 223 | const char* str = nullptr;
|
---|
[505] | 224 | uint64 result = defval;
|
---|
| 225 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 226 | {
|
---|
| 227 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 228 | result = hash_string< uint64 >( str, l );
|
---|
| 229 | //NV_LOG_DEBUG( str );
|
---|
| 230 | }
|
---|
| 231 | lua_pop( m_state, 1 );
|
---|
| 232 | return shash64( result );
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | nv::string128 nv::lua::table_guard::get_string128( int i, string_view defval /*= string_view() */ )
|
---|
| 236 | {
|
---|
| 237 | lua_rawgeti( m_state, -1, i );
|
---|
| 238 | size_t l = 0;
|
---|
| 239 | const char* str = nullptr;
|
---|
| 240 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 241 | {
|
---|
| 242 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 243 | }
|
---|
| 244 | else
|
---|
| 245 | {
|
---|
| 246 | l = defval.size();
|
---|
| 247 | str = defval.data();
|
---|
| 248 | }
|
---|
| 249 | string128 result( str, l );
|
---|
| 250 | lua_pop( m_state, 1 );
|
---|
| 251 | return result;
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | const_string nv::lua::table_guard::get_string( int i, string_view defval /*= string_view() */ )
|
---|
| 255 | {
|
---|
| 256 | lua_rawgeti( m_state, -1, i );
|
---|
| 257 | size_t l = 0;
|
---|
| 258 | const char* str = nullptr;
|
---|
| 259 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 260 | {
|
---|
| 261 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 262 | }
|
---|
| 263 | else
|
---|
| 264 | {
|
---|
| 265 | l = defval.size();
|
---|
| 266 | str = defval.data();
|
---|
| 267 | }
|
---|
| 268 | const_string result( str, l );
|
---|
| 269 | lua_pop( m_state, 1 );
|
---|
| 270 | return result;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | shash64 nv::lua::table_guard::get_string( int i, string_table* table, uint64 defval /*= 0 */ )
|
---|
| 274 | {
|
---|
| 275 | lua_rawgeti( m_state, -1, i );
|
---|
| 276 | size_t l = 0;
|
---|
| 277 | const char* str = nullptr;
|
---|
[486] | 278 | shash64 result = shash64( defval );
|
---|
| 279 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 280 | {
|
---|
| 281 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 282 | string_view sv( str, l );
|
---|
| 283 | result = table ? table->insert( sv ) : shash64( sv );
|
---|
| 284 | }
|
---|
| 285 | lua_pop( m_state, 1 );
|
---|
| 286 | return result;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[399] | 289 | const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
|
---|
[361] | 290 | {
|
---|
| 291 | lua_getfield( m_state, -1, element.data() );
|
---|
| 292 | size_t l = 0;
|
---|
| 293 | const char* str = nullptr;
|
---|
| 294 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 295 | {
|
---|
| 296 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 297 | }
|
---|
| 298 | else
|
---|
| 299 | {
|
---|
| 300 | l = defval.size();
|
---|
| 301 | str = defval.data();
|
---|
| 302 | }
|
---|
| 303 | const_string result( str, l );
|
---|
| 304 | lua_pop( m_state, 1 );
|
---|
| 305 | return result;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
[440] | 308 | string128 lua::table_guard::get_string128( string_view element, string_view defval )
|
---|
| 309 | {
|
---|
| 310 | lua_getfield( m_state, -1, element.data() );
|
---|
| 311 | size_t l = 0;
|
---|
| 312 | const char* str = nullptr;
|
---|
| 313 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 314 | {
|
---|
| 315 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 316 | }
|
---|
| 317 | else
|
---|
| 318 | {
|
---|
| 319 | l = defval.size();
|
---|
| 320 | str = defval.data();
|
---|
| 321 | }
|
---|
| 322 | string128 result( str, l );
|
---|
| 323 | lua_pop( m_state, 1 );
|
---|
| 324 | return result;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[509] | 327 | nv::string64 nv::lua::table_guard::get_string64( string_view element, string_view defval /*= string_view() */ )
|
---|
| 328 | {
|
---|
| 329 | lua_getfield( m_state, -1, element.data() );
|
---|
| 330 | size_t l = 0;
|
---|
| 331 | const char* str = nullptr;
|
---|
| 332 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 333 | {
|
---|
| 334 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 335 | }
|
---|
| 336 | else
|
---|
| 337 | {
|
---|
| 338 | l = defval.size();
|
---|
| 339 | str = defval.data();
|
---|
| 340 | }
|
---|
| 341 | string64 result( str, l );
|
---|
| 342 | lua_pop( m_state, 1 );
|
---|
| 343 | return result;
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 |
|
---|
[399] | 347 | char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
|
---|
[9] | 348 | {
|
---|
[360] | 349 | lua_getfield( m_state, -1, element.data() );
|
---|
[490] | 350 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
[206] | 351 | lua_pop( m_state, 1 );
|
---|
[9] | 352 | return result;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[399] | 355 | int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
|
---|
[9] | 356 | {
|
---|
[360] | 357 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 358 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
| 359 | lua_pop( m_state, 1 );
|
---|
[204] | 360 | return static_cast< int >( result );
|
---|
[9] | 361 | }
|
---|
| 362 |
|
---|
[399] | 363 | unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
|
---|
[188] | 364 | {
|
---|
[360] | 365 | lua_getfield( m_state, -1, element.data() );
|
---|
[490] | 366 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
|
---|
[206] | 367 | lua_pop( m_state, 1 );
|
---|
[188] | 368 | return result;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[399] | 371 | double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
|
---|
[9] | 372 | {
|
---|
[360] | 373 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 374 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
| 375 | lua_pop( m_state, 1 );
|
---|
[9] | 376 | return result;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[505] | 379 | char nv::lua::table_guard::get_char( int i, char defval /*= ' ' */ )
|
---|
| 380 | {
|
---|
| 381 | lua_rawgeti( m_state, -1, i );
|
---|
| 382 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
| 383 | lua_pop( m_state, 1 );
|
---|
| 384 | return result;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | int nv::lua::table_guard::get_integer( int i, int defval /*= 0 */ )
|
---|
| 388 | {
|
---|
| 389 | lua_rawgeti( m_state, -1, i );
|
---|
| 390 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
| 391 | lua_pop( m_state, 1 );
|
---|
| 392 | return static_cast<int>( result );
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | unsigned nv::lua::table_guard::get_unsigned( int i, unsigned defval /*= 0 */ )
|
---|
| 396 | {
|
---|
| 397 | lua_rawgeti( m_state, -1, i );
|
---|
| 398 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
|
---|
| 399 | lua_pop( m_state, 1 );
|
---|
| 400 | return result;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | double nv::lua::table_guard::get_double( int i, double defval /*= 0.0 */ )
|
---|
| 404 | {
|
---|
| 405 | lua_rawgeti( m_state, -1, i );
|
---|
| 406 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
| 407 | lua_pop( m_state, 1 );
|
---|
| 408 | return result;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | float nv::lua::table_guard::get_float( int i, float defval /*= 0.0 */ )
|
---|
| 412 | {
|
---|
| 413 | lua_rawgeti( m_state, -1, i );
|
---|
| 414 | float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
|
---|
| 415 | lua_pop( m_state, 1 );
|
---|
| 416 | return result;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | bool nv::lua::table_guard::get_boolean( int i, bool defval /*= false */ )
|
---|
| 420 | {
|
---|
| 421 | lua_rawgeti( m_state, -1, i );
|
---|
| 422 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
| 423 | lua_pop( m_state, 1 );
|
---|
| 424 | return result;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[399] | 427 | float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
|
---|
[288] | 428 | {
|
---|
[360] | 429 | lua_getfield( m_state, -1, element.data() );
|
---|
[406] | 430 | float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
|
---|
[288] | 431 | lua_pop( m_state, 1 );
|
---|
| 432 | return result;
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[399] | 435 | bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
|
---|
[9] | 436 | {
|
---|
[360] | 437 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 438 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
| 439 | lua_pop( m_state, 1 );
|
---|
[9] | 440 | return result;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[399] | 443 | bool nv::lua::table_guard::is_table( string_view element )
|
---|
[296] | 444 | {
|
---|
[360] | 445 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 446 | bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
|
---|
| 447 | lua_pop( m_state, 1 );
|
---|
| 448 | return result;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
[505] | 451 | bool nv::lua::table_guard::is_table( int i )
|
---|
| 452 | {
|
---|
| 453 | lua_rawgeti( m_state, -1, i );
|
---|
| 454 | bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
|
---|
| 455 | lua_pop( m_state, 1 );
|
---|
| 456 | return result;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
| 459 | bool nv::lua::table_guard::is_number( int i )
|
---|
| 460 | {
|
---|
| 461 | lua_rawgeti( m_state, -1, i );
|
---|
| 462 | bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
|
---|
| 463 | lua_pop( m_state, 1 );
|
---|
| 464 | return result;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | bool nv::lua::table_guard::is_boolean( int i )
|
---|
| 468 | {
|
---|
| 469 | lua_rawgeti( m_state, -1, i );
|
---|
| 470 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
|
---|
| 471 | lua_pop( m_state, 1 );
|
---|
| 472 | return result;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | bool nv::lua::table_guard::is_string( int i )
|
---|
| 476 | {
|
---|
| 477 | lua_rawgeti( m_state, -1, i );
|
---|
| 478 | bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
|
---|
| 479 | lua_pop( m_state, 1 );
|
---|
| 480 | return result;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
[399] | 483 | bool nv::lua::table_guard::is_number( string_view element )
|
---|
[296] | 484 | {
|
---|
[360] | 485 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 486 | bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
|
---|
| 487 | lua_pop( m_state, 1 );
|
---|
| 488 | return result;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[399] | 491 | bool nv::lua::table_guard::is_boolean( string_view element )
|
---|
[296] | 492 | {
|
---|
[360] | 493 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 494 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
|
---|
| 495 | lua_pop( m_state, 1 );
|
---|
| 496 | return result;
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[399] | 499 | bool nv::lua::table_guard::is_string( string_view element )
|
---|
[296] | 500 | {
|
---|
[360] | 501 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 502 | bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
|
---|
| 503 | lua_pop( m_state, 1 );
|
---|
| 504 | return result;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[503] | 507 | bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
|
---|
| 508 | {
|
---|
| 509 | NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
|
---|
| 510 | lua_getfield( m_state, -1, element.data() );
|
---|
| 511 | if ( lua_type( m_state, -1 ) != LUA_TTABLE )
|
---|
| 512 | {
|
---|
| 513 | lua_pop( m_state, 1 );
|
---|
| 514 | return false;
|
---|
| 515 | }
|
---|
| 516 | if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
|
---|
| 517 | {
|
---|
| 518 | lua_pop( m_state, 1 );
|
---|
| 519 | return false;
|
---|
| 520 | }
|
---|
| 521 | lua_pop( m_state, 1 );
|
---|
| 522 | return true;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[216] | 525 | // state
|
---|
| 526 |
|
---|
[503] | 527 | lua::state::state( lua_State* state, type_database* types )
|
---|
| 528 | : state_wrapper( state, new type_data( types ), false )
|
---|
[185] | 529 | {
|
---|
[216] | 530 |
|
---|
[185] | 531 | }
|
---|
| 532 |
|
---|
[503] | 533 | lua::state::state( bool load_libs /*= false*/, type_database* types )
|
---|
| 534 | : state_wrapper( nullptr, new type_data( types ), true )
|
---|
[185] | 535 | {
|
---|
[216] | 536 | load_lua_library();
|
---|
| 537 | m_owner = true;
|
---|
| 538 | m_state = luaL_newstate( );
|
---|
| 539 |
|
---|
| 540 | lua_pushcfunction(m_state, luaopen_base);
|
---|
[490] | 541 | lua_pushliteral(m_state, "");
|
---|
[216] | 542 | lua_call(m_state, 1, 0);
|
---|
| 543 |
|
---|
[333] | 544 | {
|
---|
| 545 | // Preregister 8 references for Handle registry
|
---|
| 546 | lua_newtable(m_state);
|
---|
| 547 | NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
|
---|
| 548 | for ( uint32 i = 1; i < 8; ++i )
|
---|
| 549 | {
|
---|
| 550 | lua_newtable(m_state);
|
---|
| 551 | luaL_ref( m_state, LUA_REGISTRYINDEX );
|
---|
| 552 | }
|
---|
| 553 | }
|
---|
| 554 |
|
---|
[216] | 555 | if ( load_libs )
|
---|
| 556 | {
|
---|
| 557 | stack_guard guard( this );
|
---|
| 558 | static const luaL_Reg lualibs[] =
|
---|
| 559 | {
|
---|
| 560 | { "string", luaopen_string },
|
---|
| 561 | { "table", luaopen_table },
|
---|
| 562 | { "math", luaopen_math },
|
---|
| 563 | { NULL, NULL}
|
---|
| 564 | };
|
---|
| 565 | const luaL_Reg *lib = lualibs;
|
---|
| 566 | for(; lib->func != NULL; lib++)
|
---|
| 567 | {
|
---|
[228] | 568 | lua_pushcfunction( m_state, lib->func );
|
---|
| 569 | lua_call(m_state, 0, 1);
|
---|
| 570 | lua_setglobal( m_state, lib->name );
|
---|
[216] | 571 | }
|
---|
[217] | 572 | register_nova( this );
|
---|
[216] | 573 | }
|
---|
| 574 |
|
---|
[365] | 575 | NV_LOG_TRACE( "Lua state created" );
|
---|
[185] | 576 | }
|
---|
| 577 |
|
---|
[399] | 578 | int lua::state::load_string( string_view code, string_view name )
|
---|
[188] | 579 | {
|
---|
[365] | 580 | NV_LOG_TRACE( "Loading Lua string '", name, "'");
|
---|
[360] | 581 | return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
|
---|
[188] | 582 | }
|
---|
[185] | 583 |
|
---|
[399] | 584 | int lua::state::load_file( string_view filename )
|
---|
[216] | 585 | {
|
---|
[365] | 586 | NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
|
---|
[360] | 587 | return luaL_loadfile( m_state, filename.data() );
|
---|
[216] | 588 | }
|
---|
[212] | 589 |
|
---|
[399] | 590 | bool lua::state::do_string( string_view code, string_view name, int rvalues )
|
---|
[212] | 591 | {
|
---|
[216] | 592 | lua::stack_guard( this );
|
---|
| 593 | int result = load_string(code,name);
|
---|
| 594 | if (result)
|
---|
| 595 | {
|
---|
[437] | 596 | NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
|
---|
[216] | 597 | return false;
|
---|
| 598 | }
|
---|
| 599 | return do_current( name, rvalues ) == 0;
|
---|
[212] | 600 | }
|
---|
| 601 |
|
---|
[399] | 602 | bool lua::state::do_file( string_view filename )
|
---|
[216] | 603 | {
|
---|
| 604 | lua::stack_guard( this );
|
---|
| 605 | int result = load_file(filename);
|
---|
| 606 | if (result)
|
---|
| 607 | {
|
---|
[437] | 608 | NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
[216] | 609 | return false;
|
---|
| 610 | }
|
---|
| 611 | return do_current( filename ) == 0;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[399] | 614 | int lua::state::do_current( string_view name, int rvalues )
|
---|
[216] | 615 | {
|
---|
| 616 | int result = lua_pcall(m_state, 0, rvalues, 0);
|
---|
| 617 | if (result)
|
---|
| 618 | {
|
---|
[437] | 619 | NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
[216] | 620 | lua_pop( m_state, 1 );
|
---|
| 621 | }
|
---|
| 622 | return result;
|
---|
| 623 | }
|
---|
| 624 |
|
---|
| 625 | int lua::state::get_stack_size()
|
---|
| 626 | {
|
---|
| 627 | return lua_gettop( m_state );
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[9] | 630 | void lua::state::log_stack()
|
---|
| 631 | {
|
---|
[206] | 632 | int top = lua_gettop(m_state);
|
---|
[365] | 633 | NV_LOG_DEBUG( "Stack dump (", top, ")");
|
---|
[9] | 634 | for ( int i = 0; i < top; ++i )
|
---|
| 635 | {
|
---|
[437] | 636 | NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
|
---|
[9] | 637 | }
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | lua_State* lua::state::get_raw()
|
---|
| 641 | {
|
---|
[206] | 642 | return m_state;
|
---|
[9] | 643 | }
|
---|
| 644 |
|
---|
[399] | 645 | lua::ref lua::state::register_object( void* o, string_view lua_name )
|
---|
[77] | 646 | {
|
---|
[265] | 647 | if ( o == nullptr ) return lua::ref( lua::ref::none );
|
---|
[77] | 648 | stack_guard guard( this );
|
---|
[360] | 649 | lua_getglobal( m_state, lua_name.data() );
|
---|
[206] | 650 | if ( lua_isnil( m_state, -1 ) )
|
---|
[77] | 651 | {
|
---|
[403] | 652 | NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
|
---|
[77] | 653 | }
|
---|
| 654 | deep_pointer_copy( -1, o );
|
---|
[265] | 655 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
[77] | 656 | }
|
---|
[9] | 657 |
|
---|
[399] | 658 | lua::ref lua::state::register_proto( string_view id, string_view storage )
|
---|
[217] | 659 | {
|
---|
| 660 | stack_guard guard( this );
|
---|
[360] | 661 | lua_getglobal( m_state, storage.data() );
|
---|
[217] | 662 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 663 | {
|
---|
[403] | 664 | NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
|
---|
[217] | 665 | }
|
---|
[360] | 666 | lua_getfield( m_state, -1, id.data() );
|
---|
[217] | 667 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 668 | {
|
---|
[403] | 669 | NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
|
---|
[217] | 670 | }
|
---|
[265] | 671 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
[217] | 672 | }
|
---|
| 673 |
|
---|
[399] | 674 | void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
|
---|
[212] | 675 | {
|
---|
| 676 | stack_guard guard( this );
|
---|
[360] | 677 | lua_getglobal( m_state, lua_name.data() );
|
---|
[212] | 678 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 679 | {
|
---|
[403] | 680 | NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
|
---|
[212] | 681 | }
|
---|
| 682 | lua_pushcfunction( m_state, f );
|
---|
[360] | 683 | lua_setfield( m_state, -2, name.data() );
|
---|
[212] | 684 | }
|
---|
| 685 |
|
---|
[265] | 686 | void lua::state::unregister_object( ref object_index )
|
---|
[77] | 687 | {
|
---|
[265] | 688 | if ( !object_index.is_valid() ) return;
|
---|
[77] | 689 | stack_guard guard( this );
|
---|
[265] | 690 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[360] | 691 | lua_pushliteral( m_state, "__ptr" );
|
---|
[206] | 692 | lua_pushboolean( m_state, false );
|
---|
| 693 | lua_rawset( m_state, -3 );
|
---|
| 694 | lua_pop( m_state, 1 );
|
---|
[265] | 695 | luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[77] | 696 | }
|
---|
| 697 |
|
---|
[256] | 698 |
|
---|
[77] | 699 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
| 700 | {
|
---|
[490] | 701 | index = nlua_absindex( m_state, index );
|
---|
[206] | 702 | lua_newtable( m_state );
|
---|
| 703 | lua_pushnil( m_state );
|
---|
[77] | 704 | bool has_functions = false;
|
---|
| 705 | bool has_metatable = false;
|
---|
| 706 |
|
---|
[206] | 707 | while ( lua_next( m_state, index ) != 0 )
|
---|
[77] | 708 | {
|
---|
[206] | 709 | if ( lua_isfunction( m_state, -1 ) )
|
---|
[77] | 710 | has_functions = true;
|
---|
[206] | 711 | else if ( lua_istable( m_state, -1 ) )
|
---|
[77] | 712 | {
|
---|
| 713 | deep_pointer_copy( -1, obj );
|
---|
[206] | 714 | lua_insert( m_state, -2 );
|
---|
| 715 | lua_pop( m_state, 1 );
|
---|
[77] | 716 | }
|
---|
[206] | 717 | lua_pushvalue( m_state, -2 );
|
---|
| 718 | lua_insert( m_state, -2 );
|
---|
| 719 | lua_settable( m_state, -4 );
|
---|
[77] | 720 | }
|
---|
| 721 |
|
---|
[206] | 722 | if ( lua_getmetatable( m_state, -2 ) )
|
---|
[77] | 723 | {
|
---|
[206] | 724 | lua_setmetatable( m_state, -2 );
|
---|
[77] | 725 | has_metatable = true;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | if ( has_functions || has_metatable )
|
---|
| 729 | {
|
---|
[403] | 730 | lua_pushliteral( m_state, "__ptr" );
|
---|
[206] | 731 | lua_pushlightuserdata( m_state, obj );
|
---|
| 732 | lua_rawset( m_state, -3 );
|
---|
[77] | 733 | }
|
---|
| 734 | }
|
---|
[163] | 735 |
|
---|
[399] | 736 | void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
|
---|
[163] | 737 | {
|
---|
[265] | 738 | if ( !object_index.is_valid() ) return;
|
---|
| 739 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[437] | 740 | nlua_pushstringview( m_state, metaname );
|
---|
[206] | 741 | lua_pushlightuserdata( m_state, pointer );
|
---|
| 742 | lua_rawset( m_state, -3 );
|
---|
| 743 | lua_pop( m_state, 1 );
|
---|
| 744 | }
|
---|
[188] | 745 |
|
---|
[399] | 746 | void nv::lua::state::register_enum( string_view name, int value )
|
---|
[215] | 747 | {
|
---|
[262] | 748 | lua_pushinteger( m_state, value );
|
---|
[360] | 749 | lua_setglobal( m_state, name.data() );
|
---|
[215] | 750 | }
|
---|
| 751 |
|
---|
[503] | 752 | void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
| 753 | {
|
---|
| 754 | m_lua_types->insert( tid, p, r );
|
---|
| 755 | }
|
---|
| 756 |
|
---|
[399] | 757 | nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
|
---|
[341] | 758 | {
|
---|
| 759 | int args = empty ? 1 : 2;
|
---|
| 760 | NV_LUA_STACK_ASSERT( m_state, -args );
|
---|
| 761 |
|
---|
| 762 | if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
|
---|
| 763 | {
|
---|
| 764 | lua_pop( m_state, args );
|
---|
| 765 | return ref();
|
---|
| 766 | }
|
---|
| 767 | if (empty)
|
---|
| 768 | lua_createtable( m_state, 0, 0 );
|
---|
| 769 | else
|
---|
| 770 | nlua_deepcopy( m_state, -2 );
|
---|
[437] | 771 | nlua_pushstringview( m_state, id );
|
---|
[341] | 772 | lua_pushvalue( m_state, -2 );
|
---|
| 773 | lua_rawset( m_state, -4 );
|
---|
| 774 | lua_replace( m_state, -2 );
|
---|
| 775 | ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
| 776 | if (!empty) lua_pop( m_state, 1 );
|
---|
| 777 | return result;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[399] | 780 | void nv::lua::state::unregister_handle_component_impl( string_view id )
|
---|
[341] | 781 | {
|
---|
| 782 | NV_LUA_STACK_ASSERT( m_state, -1 );
|
---|
| 783 |
|
---|
| 784 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 785 | {
|
---|
| 786 | lua_pop( m_state, 1 );
|
---|
| 787 | return;
|
---|
| 788 | }
|
---|
[437] | 789 | nlua_pushstringview( m_state, id );
|
---|
[341] | 790 | lua_pushnil( m_state );
|
---|
| 791 | lua_rawset( m_state, -3 );
|
---|
| 792 | lua_pop( m_state, 1 );
|
---|
| 793 | }
|
---|
| 794 |
|
---|
[399] | 795 | void nv::lua::state::register_singleton( string_view name, void* o )
|
---|
[341] | 796 | {
|
---|
| 797 | if ( o == nullptr ) return;
|
---|
| 798 | stack_guard guard( this );
|
---|
[360] | 799 | lua_getglobal( m_state, name.data() );
|
---|
[341] | 800 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 801 | {
|
---|
[403] | 802 | NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
|
---|
[341] | 803 | }
|
---|
| 804 | deep_pointer_copy( -1, o );
|
---|
[360] | 805 | lua_setglobal( m_state, name.data() );
|
---|
[341] | 806 | }
|
---|
| 807 |
|
---|
[503] | 808 | nv::lua::state::~state()
|
---|
| 809 | {
|
---|
| 810 | delete m_lua_types;
|
---|
| 811 | m_lua_types = nullptr;
|
---|
| 812 | }
|
---|
| 813 |
|
---|
| 814 | template < typename T >
|
---|
| 815 | void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 816 | {
|
---|
| 817 | T* value = reinterpret_cast<T*>( object );
|
---|
| 818 | lua_pushinteger( state->get_raw(), lua_Integer( *value ) );
|
---|
| 819 | }
|
---|
| 820 |
|
---|
| 821 | template < typename T >
|
---|
| 822 | void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 823 | {
|
---|
| 824 | T* value = reinterpret_cast<T*>( object );
|
---|
| 825 | lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) );
|
---|
| 826 | }
|
---|
| 827 |
|
---|
| 828 | template < typename T >
|
---|
| 829 | void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 830 | {
|
---|
| 831 | T* value = reinterpret_cast< T* >( object );
|
---|
| 832 | lua_pushnumber( state->get_raw(), lua_Number( *value ) );
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 836 | {
|
---|
| 837 | bool* value = reinterpret_cast<bool*>( object );
|
---|
| 838 | lua_pushboolean( state->get_raw(), *value );
|
---|
| 839 | }
|
---|
| 840 |
|
---|
| 841 | template < typename T >
|
---|
| 842 | bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 843 | {
|
---|
| 844 | T* value = reinterpret_cast<T*>( object );
|
---|
| 845 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 846 | {
|
---|
| 847 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
| 848 | return true;
|
---|
| 849 | }
|
---|
| 850 | return false;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 | template < typename T >
|
---|
| 854 | bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 855 | {
|
---|
| 856 | T* value = reinterpret_cast<T*>( object );
|
---|
| 857 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 858 | {
|
---|
| 859 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
| 860 | return true;
|
---|
| 861 | }
|
---|
| 862 | return false;
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | template < typename T >
|
---|
| 866 | bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 867 | {
|
---|
| 868 | T* value = reinterpret_cast<T*>( object );
|
---|
| 869 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 870 | {
|
---|
| 871 | *value = T( lua_tonumber( state->get_raw(), index ) );
|
---|
| 872 | return true;
|
---|
| 873 | }
|
---|
| 874 | return false;
|
---|
| 875 | }
|
---|
| 876 |
|
---|
| 877 | static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 878 | {
|
---|
| 879 | bool* value = reinterpret_cast<bool*>( object );
|
---|
| 880 | if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN )
|
---|
| 881 | {
|
---|
| 882 | *value = bool( lua_toboolean( state->get_raw(), index ) );
|
---|
| 883 | return true;
|
---|
| 884 | }
|
---|
| 885 | return false;
|
---|
| 886 | }
|
---|
| 887 |
|
---|
| 888 |
|
---|
| 889 | void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
| 890 | {
|
---|
| 891 | m_type_read.assign( tid, r );
|
---|
| 892 | m_type_push.assign( tid, p );
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | void nv::lua::type_data::register_standard_types()
|
---|
| 896 | {
|
---|
| 897 | insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read );
|
---|
| 898 | insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>, nlua_rtti_signed_read<nv::sint8> );
|
---|
| 899 | insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>, nlua_rtti_signed_read<nv::sint16> );
|
---|
| 900 | insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>, nlua_rtti_signed_read<nv::sint32> );
|
---|
| 901 | insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>, nlua_rtti_unsigned_read<nv::uint8> );
|
---|
| 902 | insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> );
|
---|
| 903 | insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> );
|
---|
| 904 | insert<nv::f32> ( nlua_rtti_floating_push<nv::f32>, nlua_rtti_floating_read<nv::f32> );
|
---|
| 905 | insert<nv::f64> ( nlua_rtti_floating_push<nv::f64>, nlua_rtti_floating_read<nv::f64> );
|
---|
| 906 | // insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
|
---|
| 907 | // insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
|
---|
| 908 | }
|
---|