[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 |
|
---|
[431] | 203 | shash64 nv::lua::table_guard::get_string( string_view element, string_table& table, uint64 defval /*= 0 */ )
|
---|
| 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 );
|
---|
[486] | 212 | result = table.insert( string_view( str, l ) );
|
---|
[431] | 213 | }
|
---|
| 214 | lua_pop( m_state, 1 );
|
---|
[486] | 215 | return result;
|
---|
[431] | 216 | }
|
---|
| 217 |
|
---|
[486] | 218 | nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ )
|
---|
| 219 | {
|
---|
| 220 | lua_getfield( m_state, -1, element.data() );
|
---|
| 221 | size_t l = 0;
|
---|
| 222 | const char* str = nullptr;
|
---|
| 223 | shash64 result = shash64( defval );
|
---|
| 224 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 225 | {
|
---|
| 226 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 227 | string_view sv( str, l );
|
---|
| 228 | result = table ? table->insert( sv ) : shash64( sv );
|
---|
| 229 | }
|
---|
| 230 | lua_pop( m_state, 1 );
|
---|
| 231 | return result;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[399] | 234 | const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
|
---|
[361] | 235 | {
|
---|
| 236 | lua_getfield( m_state, -1, element.data() );
|
---|
| 237 | size_t l = 0;
|
---|
| 238 | const char* str = nullptr;
|
---|
| 239 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 240 | {
|
---|
| 241 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 242 | }
|
---|
| 243 | else
|
---|
| 244 | {
|
---|
| 245 | l = defval.size();
|
---|
| 246 | str = defval.data();
|
---|
| 247 | }
|
---|
| 248 | const_string result( str, l );
|
---|
| 249 | lua_pop( m_state, 1 );
|
---|
| 250 | return result;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[440] | 253 | string128 lua::table_guard::get_string128( string_view element, string_view defval )
|
---|
| 254 | {
|
---|
| 255 | lua_getfield( m_state, -1, element.data() );
|
---|
| 256 | size_t l = 0;
|
---|
| 257 | const char* str = nullptr;
|
---|
| 258 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
| 259 | {
|
---|
| 260 | str = lua_tolstring( m_state, -1, &l );
|
---|
| 261 | }
|
---|
| 262 | else
|
---|
| 263 | {
|
---|
| 264 | l = defval.size();
|
---|
| 265 | str = defval.data();
|
---|
| 266 | }
|
---|
| 267 | string128 result( str, l );
|
---|
| 268 | lua_pop( m_state, 1 );
|
---|
| 269 | return result;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[399] | 272 | char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
|
---|
[9] | 273 | {
|
---|
[360] | 274 | lua_getfield( m_state, -1, element.data() );
|
---|
[490] | 275 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
[206] | 276 | lua_pop( m_state, 1 );
|
---|
[9] | 277 | return result;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[399] | 280 | int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
|
---|
[9] | 281 | {
|
---|
[360] | 282 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 283 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
| 284 | lua_pop( m_state, 1 );
|
---|
[204] | 285 | return static_cast< int >( result );
|
---|
[9] | 286 | }
|
---|
| 287 |
|
---|
[399] | 288 | unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
|
---|
[188] | 289 | {
|
---|
[360] | 290 | lua_getfield( m_state, -1, element.data() );
|
---|
[490] | 291 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
|
---|
[206] | 292 | lua_pop( m_state, 1 );
|
---|
[188] | 293 | return result;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[399] | 296 | double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
|
---|
[9] | 297 | {
|
---|
[360] | 298 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 299 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
| 300 | lua_pop( m_state, 1 );
|
---|
[9] | 301 | return result;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[399] | 304 | float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
|
---|
[288] | 305 | {
|
---|
[360] | 306 | lua_getfield( m_state, -1, element.data() );
|
---|
[406] | 307 | float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
|
---|
[288] | 308 | lua_pop( m_state, 1 );
|
---|
| 309 | return result;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[399] | 312 | bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
|
---|
[9] | 313 | {
|
---|
[360] | 314 | lua_getfield( m_state, -1, element.data() );
|
---|
[206] | 315 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
| 316 | lua_pop( m_state, 1 );
|
---|
[9] | 317 | return result;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[399] | 320 | bool nv::lua::table_guard::is_table( string_view element )
|
---|
[296] | 321 | {
|
---|
[360] | 322 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 323 | bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
|
---|
| 324 | lua_pop( m_state, 1 );
|
---|
| 325 | return result;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[399] | 328 | bool nv::lua::table_guard::is_number( string_view element )
|
---|
[296] | 329 | {
|
---|
[360] | 330 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 331 | bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
|
---|
| 332 | lua_pop( m_state, 1 );
|
---|
| 333 | return result;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[399] | 336 | bool nv::lua::table_guard::is_boolean( string_view element )
|
---|
[296] | 337 | {
|
---|
[360] | 338 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 339 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
|
---|
| 340 | lua_pop( m_state, 1 );
|
---|
| 341 | return result;
|
---|
| 342 | }
|
---|
| 343 |
|
---|
[399] | 344 | bool nv::lua::table_guard::is_string( string_view element )
|
---|
[296] | 345 | {
|
---|
[360] | 346 | lua_getfield( m_state, -1, element.data() );
|
---|
[296] | 347 | bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
|
---|
| 348 | lua_pop( m_state, 1 );
|
---|
| 349 | return result;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 |
|
---|
[503] | 353 | bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
|
---|
| 354 | {
|
---|
| 355 | NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
|
---|
| 356 | lua_getfield( m_state, -1, element.data() );
|
---|
| 357 | if ( lua_type( m_state, -1 ) != LUA_TTABLE )
|
---|
| 358 | {
|
---|
| 359 | lua_pop( m_state, 1 );
|
---|
| 360 | return false;
|
---|
| 361 | }
|
---|
| 362 | if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
|
---|
| 363 | {
|
---|
| 364 | lua_pop( m_state, 1 );
|
---|
| 365 | return false;
|
---|
| 366 | }
|
---|
| 367 | lua_pop( m_state, 1 );
|
---|
| 368 | return true;
|
---|
| 369 | }
|
---|
| 370 |
|
---|
[216] | 371 | // state
|
---|
| 372 |
|
---|
[503] | 373 | lua::state::state( lua_State* state, type_database* types )
|
---|
| 374 | : state_wrapper( state, new type_data( types ), false )
|
---|
[185] | 375 | {
|
---|
[216] | 376 |
|
---|
[185] | 377 | }
|
---|
| 378 |
|
---|
[503] | 379 | lua::state::state( bool load_libs /*= false*/, type_database* types )
|
---|
| 380 | : state_wrapper( nullptr, new type_data( types ), true )
|
---|
[185] | 381 | {
|
---|
[216] | 382 | load_lua_library();
|
---|
| 383 | m_owner = true;
|
---|
| 384 | m_state = luaL_newstate( );
|
---|
| 385 |
|
---|
| 386 | lua_pushcfunction(m_state, luaopen_base);
|
---|
[490] | 387 | lua_pushliteral(m_state, "");
|
---|
[216] | 388 | lua_call(m_state, 1, 0);
|
---|
| 389 |
|
---|
[333] | 390 | {
|
---|
| 391 | // Preregister 8 references for Handle registry
|
---|
| 392 | lua_newtable(m_state);
|
---|
| 393 | NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
|
---|
| 394 | for ( uint32 i = 1; i < 8; ++i )
|
---|
| 395 | {
|
---|
| 396 | lua_newtable(m_state);
|
---|
| 397 | luaL_ref( m_state, LUA_REGISTRYINDEX );
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[216] | 401 | if ( load_libs )
|
---|
| 402 | {
|
---|
| 403 | stack_guard guard( this );
|
---|
| 404 | static const luaL_Reg lualibs[] =
|
---|
| 405 | {
|
---|
| 406 | { "string", luaopen_string },
|
---|
| 407 | { "table", luaopen_table },
|
---|
| 408 | { "math", luaopen_math },
|
---|
| 409 | { NULL, NULL}
|
---|
| 410 | };
|
---|
| 411 | const luaL_Reg *lib = lualibs;
|
---|
| 412 | for(; lib->func != NULL; lib++)
|
---|
| 413 | {
|
---|
[228] | 414 | lua_pushcfunction( m_state, lib->func );
|
---|
| 415 | lua_call(m_state, 0, 1);
|
---|
| 416 | lua_setglobal( m_state, lib->name );
|
---|
[216] | 417 | }
|
---|
[217] | 418 | register_nova( this );
|
---|
[216] | 419 | }
|
---|
| 420 |
|
---|
[365] | 421 | NV_LOG_TRACE( "Lua state created" );
|
---|
[185] | 422 | }
|
---|
| 423 |
|
---|
[399] | 424 | int lua::state::load_string( string_view code, string_view name )
|
---|
[188] | 425 | {
|
---|
[365] | 426 | NV_LOG_TRACE( "Loading Lua string '", name, "'");
|
---|
[360] | 427 | return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
|
---|
[188] | 428 | }
|
---|
[185] | 429 |
|
---|
[399] | 430 | int lua::state::load_file( string_view filename )
|
---|
[216] | 431 | {
|
---|
[365] | 432 | NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
|
---|
[360] | 433 | return luaL_loadfile( m_state, filename.data() );
|
---|
[216] | 434 | }
|
---|
[212] | 435 |
|
---|
[399] | 436 | bool lua::state::do_string( string_view code, string_view name, int rvalues )
|
---|
[212] | 437 | {
|
---|
[216] | 438 | lua::stack_guard( this );
|
---|
| 439 | int result = load_string(code,name);
|
---|
| 440 | if (result)
|
---|
| 441 | {
|
---|
[437] | 442 | NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
|
---|
[216] | 443 | return false;
|
---|
| 444 | }
|
---|
| 445 | return do_current( name, rvalues ) == 0;
|
---|
[212] | 446 | }
|
---|
| 447 |
|
---|
[399] | 448 | bool lua::state::do_file( string_view filename )
|
---|
[216] | 449 | {
|
---|
| 450 | lua::stack_guard( this );
|
---|
| 451 | int result = load_file(filename);
|
---|
| 452 | if (result)
|
---|
| 453 | {
|
---|
[437] | 454 | NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
[216] | 455 | return false;
|
---|
| 456 | }
|
---|
| 457 | return do_current( filename ) == 0;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[399] | 460 | int lua::state::do_current( string_view name, int rvalues )
|
---|
[216] | 461 | {
|
---|
| 462 | int result = lua_pcall(m_state, 0, rvalues, 0);
|
---|
| 463 | if (result)
|
---|
| 464 | {
|
---|
[437] | 465 | NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
[216] | 466 | lua_pop( m_state, 1 );
|
---|
| 467 | }
|
---|
| 468 | return result;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | int lua::state::get_stack_size()
|
---|
| 472 | {
|
---|
| 473 | return lua_gettop( m_state );
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[9] | 476 | void lua::state::log_stack()
|
---|
| 477 | {
|
---|
[206] | 478 | int top = lua_gettop(m_state);
|
---|
[365] | 479 | NV_LOG_DEBUG( "Stack dump (", top, ")");
|
---|
[9] | 480 | for ( int i = 0; i < top; ++i )
|
---|
| 481 | {
|
---|
[437] | 482 | NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
|
---|
[9] | 483 | }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | lua_State* lua::state::get_raw()
|
---|
| 487 | {
|
---|
[206] | 488 | return m_state;
|
---|
[9] | 489 | }
|
---|
| 490 |
|
---|
[399] | 491 | lua::ref lua::state::register_object( void* o, string_view lua_name )
|
---|
[77] | 492 | {
|
---|
[265] | 493 | if ( o == nullptr ) return lua::ref( lua::ref::none );
|
---|
[77] | 494 | stack_guard guard( this );
|
---|
[360] | 495 | lua_getglobal( m_state, lua_name.data() );
|
---|
[206] | 496 | if ( lua_isnil( m_state, -1 ) )
|
---|
[77] | 497 | {
|
---|
[403] | 498 | NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
|
---|
[77] | 499 | }
|
---|
| 500 | deep_pointer_copy( -1, o );
|
---|
[265] | 501 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
[77] | 502 | }
|
---|
[9] | 503 |
|
---|
[399] | 504 | lua::ref lua::state::register_proto( string_view id, string_view storage )
|
---|
[217] | 505 | {
|
---|
| 506 | stack_guard guard( this );
|
---|
[360] | 507 | lua_getglobal( m_state, storage.data() );
|
---|
[217] | 508 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 509 | {
|
---|
[403] | 510 | NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
|
---|
[217] | 511 | }
|
---|
[360] | 512 | lua_getfield( m_state, -1, id.data() );
|
---|
[217] | 513 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 514 | {
|
---|
[403] | 515 | NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
|
---|
[217] | 516 | }
|
---|
[265] | 517 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
[217] | 518 | }
|
---|
| 519 |
|
---|
[399] | 520 | void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
|
---|
[212] | 521 | {
|
---|
| 522 | stack_guard guard( this );
|
---|
[360] | 523 | lua_getglobal( m_state, lua_name.data() );
|
---|
[212] | 524 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 525 | {
|
---|
[403] | 526 | NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
|
---|
[212] | 527 | }
|
---|
| 528 | lua_pushcfunction( m_state, f );
|
---|
[360] | 529 | lua_setfield( m_state, -2, name.data() );
|
---|
[212] | 530 | }
|
---|
| 531 |
|
---|
[265] | 532 | void lua::state::unregister_object( ref object_index )
|
---|
[77] | 533 | {
|
---|
[265] | 534 | if ( !object_index.is_valid() ) return;
|
---|
[77] | 535 | stack_guard guard( this );
|
---|
[265] | 536 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[360] | 537 | lua_pushliteral( m_state, "__ptr" );
|
---|
[206] | 538 | lua_pushboolean( m_state, false );
|
---|
| 539 | lua_rawset( m_state, -3 );
|
---|
| 540 | lua_pop( m_state, 1 );
|
---|
[265] | 541 | luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[77] | 542 | }
|
---|
| 543 |
|
---|
[256] | 544 |
|
---|
[77] | 545 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
| 546 | {
|
---|
[490] | 547 | index = nlua_absindex( m_state, index );
|
---|
[206] | 548 | lua_newtable( m_state );
|
---|
| 549 | lua_pushnil( m_state );
|
---|
[77] | 550 | bool has_functions = false;
|
---|
| 551 | bool has_metatable = false;
|
---|
| 552 |
|
---|
[206] | 553 | while ( lua_next( m_state, index ) != 0 )
|
---|
[77] | 554 | {
|
---|
[206] | 555 | if ( lua_isfunction( m_state, -1 ) )
|
---|
[77] | 556 | has_functions = true;
|
---|
[206] | 557 | else if ( lua_istable( m_state, -1 ) )
|
---|
[77] | 558 | {
|
---|
| 559 | deep_pointer_copy( -1, obj );
|
---|
[206] | 560 | lua_insert( m_state, -2 );
|
---|
| 561 | lua_pop( m_state, 1 );
|
---|
[77] | 562 | }
|
---|
[206] | 563 | lua_pushvalue( m_state, -2 );
|
---|
| 564 | lua_insert( m_state, -2 );
|
---|
| 565 | lua_settable( m_state, -4 );
|
---|
[77] | 566 | }
|
---|
| 567 |
|
---|
[206] | 568 | if ( lua_getmetatable( m_state, -2 ) )
|
---|
[77] | 569 | {
|
---|
[206] | 570 | lua_setmetatable( m_state, -2 );
|
---|
[77] | 571 | has_metatable = true;
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | if ( has_functions || has_metatable )
|
---|
| 575 | {
|
---|
[403] | 576 | lua_pushliteral( m_state, "__ptr" );
|
---|
[206] | 577 | lua_pushlightuserdata( m_state, obj );
|
---|
| 578 | lua_rawset( m_state, -3 );
|
---|
[77] | 579 | }
|
---|
| 580 | }
|
---|
[163] | 581 |
|
---|
[399] | 582 | void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
|
---|
[163] | 583 | {
|
---|
[265] | 584 | if ( !object_index.is_valid() ) return;
|
---|
| 585 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
[437] | 586 | nlua_pushstringview( m_state, metaname );
|
---|
[206] | 587 | lua_pushlightuserdata( m_state, pointer );
|
---|
| 588 | lua_rawset( m_state, -3 );
|
---|
| 589 | lua_pop( m_state, 1 );
|
---|
| 590 | }
|
---|
[188] | 591 |
|
---|
[399] | 592 | void nv::lua::state::register_enum( string_view name, int value )
|
---|
[215] | 593 | {
|
---|
[262] | 594 | lua_pushinteger( m_state, value );
|
---|
[360] | 595 | lua_setglobal( m_state, name.data() );
|
---|
[215] | 596 | }
|
---|
| 597 |
|
---|
[503] | 598 | void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
| 599 | {
|
---|
| 600 | m_lua_types->insert( tid, p, r );
|
---|
| 601 | }
|
---|
| 602 |
|
---|
[399] | 603 | nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
|
---|
[341] | 604 | {
|
---|
| 605 | int args = empty ? 1 : 2;
|
---|
| 606 | NV_LUA_STACK_ASSERT( m_state, -args );
|
---|
| 607 |
|
---|
| 608 | if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
|
---|
| 609 | {
|
---|
| 610 | lua_pop( m_state, args );
|
---|
| 611 | return ref();
|
---|
| 612 | }
|
---|
| 613 | if (empty)
|
---|
| 614 | lua_createtable( m_state, 0, 0 );
|
---|
| 615 | else
|
---|
| 616 | nlua_deepcopy( m_state, -2 );
|
---|
[437] | 617 | nlua_pushstringview( m_state, id );
|
---|
[341] | 618 | lua_pushvalue( m_state, -2 );
|
---|
| 619 | lua_rawset( m_state, -4 );
|
---|
| 620 | lua_replace( m_state, -2 );
|
---|
| 621 | ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
| 622 | if (!empty) lua_pop( m_state, 1 );
|
---|
| 623 | return result;
|
---|
| 624 | }
|
---|
| 625 |
|
---|
[399] | 626 | void nv::lua::state::unregister_handle_component_impl( string_view id )
|
---|
[341] | 627 | {
|
---|
| 628 | NV_LUA_STACK_ASSERT( m_state, -1 );
|
---|
| 629 |
|
---|
| 630 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 631 | {
|
---|
| 632 | lua_pop( m_state, 1 );
|
---|
| 633 | return;
|
---|
| 634 | }
|
---|
[437] | 635 | nlua_pushstringview( m_state, id );
|
---|
[341] | 636 | lua_pushnil( m_state );
|
---|
| 637 | lua_rawset( m_state, -3 );
|
---|
| 638 | lua_pop( m_state, 1 );
|
---|
| 639 | }
|
---|
| 640 |
|
---|
[399] | 641 | void nv::lua::state::register_singleton( string_view name, void* o )
|
---|
[341] | 642 | {
|
---|
| 643 | if ( o == nullptr ) return;
|
---|
| 644 | stack_guard guard( this );
|
---|
[360] | 645 | lua_getglobal( m_state, name.data() );
|
---|
[341] | 646 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 647 | {
|
---|
[403] | 648 | NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
|
---|
[341] | 649 | }
|
---|
| 650 | deep_pointer_copy( -1, o );
|
---|
[360] | 651 | lua_setglobal( m_state, name.data() );
|
---|
[341] | 652 | }
|
---|
| 653 |
|
---|
[503] | 654 | nv::lua::state::~state()
|
---|
| 655 | {
|
---|
| 656 | delete m_lua_types;
|
---|
| 657 | m_lua_types = nullptr;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | template < typename T >
|
---|
| 661 | void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 662 | {
|
---|
| 663 | T* value = reinterpret_cast<T*>( object );
|
---|
| 664 | lua_pushinteger( state->get_raw(), lua_Integer( *value ) );
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | template < typename T >
|
---|
| 668 | void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 669 | {
|
---|
| 670 | T* value = reinterpret_cast<T*>( object );
|
---|
| 671 | lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) );
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | template < typename T >
|
---|
| 675 | void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 676 | {
|
---|
| 677 | T* value = reinterpret_cast< T* >( object );
|
---|
| 678 | lua_pushnumber( state->get_raw(), lua_Number( *value ) );
|
---|
| 679 | }
|
---|
| 680 |
|
---|
| 681 | static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
| 682 | {
|
---|
| 683 | bool* value = reinterpret_cast<bool*>( object );
|
---|
| 684 | lua_pushboolean( state->get_raw(), *value );
|
---|
| 685 | }
|
---|
| 686 |
|
---|
| 687 | template < typename T >
|
---|
| 688 | bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 689 | {
|
---|
| 690 | T* value = reinterpret_cast<T*>( object );
|
---|
| 691 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 692 | {
|
---|
| 693 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
| 694 | return true;
|
---|
| 695 | }
|
---|
| 696 | return false;
|
---|
| 697 | }
|
---|
| 698 |
|
---|
| 699 | template < typename T >
|
---|
| 700 | bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 701 | {
|
---|
| 702 | T* value = reinterpret_cast<T*>( object );
|
---|
| 703 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 704 | {
|
---|
| 705 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
| 706 | return true;
|
---|
| 707 | }
|
---|
| 708 | return false;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | template < typename T >
|
---|
| 712 | bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 713 | {
|
---|
| 714 | T* value = reinterpret_cast<T*>( object );
|
---|
| 715 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
| 716 | {
|
---|
| 717 | *value = T( lua_tonumber( state->get_raw(), index ) );
|
---|
| 718 | return true;
|
---|
| 719 | }
|
---|
| 720 | return false;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 | static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
| 724 | {
|
---|
| 725 | bool* value = reinterpret_cast<bool*>( object );
|
---|
| 726 | if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN )
|
---|
| 727 | {
|
---|
| 728 | *value = bool( lua_toboolean( state->get_raw(), index ) );
|
---|
| 729 | return true;
|
---|
| 730 | }
|
---|
| 731 | return false;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 |
|
---|
| 735 | void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
| 736 | {
|
---|
| 737 | m_type_read.assign( tid, r );
|
---|
| 738 | m_type_push.assign( tid, p );
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | void nv::lua::type_data::register_standard_types()
|
---|
| 742 | {
|
---|
| 743 | insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read );
|
---|
| 744 | insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>, nlua_rtti_signed_read<nv::sint8> );
|
---|
| 745 | insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>, nlua_rtti_signed_read<nv::sint16> );
|
---|
| 746 | insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>, nlua_rtti_signed_read<nv::sint32> );
|
---|
| 747 | insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>, nlua_rtti_unsigned_read<nv::uint8> );
|
---|
| 748 | insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> );
|
---|
| 749 | insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> );
|
---|
| 750 | insert<nv::f32> ( nlua_rtti_floating_push<nv::f32>, nlua_rtti_floating_read<nv::f32> );
|
---|
| 751 | insert<nv::f64> ( nlua_rtti_floating_push<nv::f64>, nlua_rtti_floating_read<nv::f64> );
|
---|
| 752 | // insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
|
---|
| 753 | // insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
|
---|
| 754 | }
|
---|