[9] | 1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of NV Libraries.
|
---|
| 5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 6 |
|
---|
| 7 | #include "nv/lua/lua_state.hh"
|
---|
| 8 |
|
---|
[51] | 9 | #include "nv/lua/lua_raw.hh"
|
---|
[9] | 10 | #include "nv/logging.hh"
|
---|
| 11 | #include "nv/string.hh"
|
---|
[77] | 12 | #include "nv/root.hh"
|
---|
| 13 | #include "nv/types.hh"
|
---|
[9] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
[121] | 17 | lua::stack_guard::stack_guard( lua::state* aL )
|
---|
[206] | 18 | : L(aL), m_level( lua_gettop(aL->m_state) )
|
---|
[9] | 19 | {
|
---|
| 20 |
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[121] | 23 | lua::stack_guard::stack_guard( lua::state& aL )
|
---|
[206] | 24 | : L(&aL), m_level( lua_gettop(aL.m_state) )
|
---|
[86] | 25 | {
|
---|
| 26 |
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[9] | 29 | lua::stack_guard::~stack_guard()
|
---|
| 30 | {
|
---|
[206] | 31 | lua_settop( L->m_state, m_level );
|
---|
[9] | 32 | }
|
---|
| 33 |
|
---|
[206] | 34 | lua::state::state( lua_State* state ) : state_wrapper( state, false )
|
---|
[9] | 35 | {
|
---|
[206] | 36 |
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
|
---|
| 40 | {
|
---|
[74] | 41 | load_lua_library();
|
---|
[9] | 42 | m_owner = true;
|
---|
[206] | 43 | m_state = luaL_newstate( );
|
---|
[9] | 44 |
|
---|
[206] | 45 | lua_pushcfunction(m_state, luaopen_base);
|
---|
| 46 | lua_pushliteral(m_state, LUA_TABLIBNAME);
|
---|
| 47 | lua_call(m_state, 1, 0);
|
---|
[9] | 48 |
|
---|
[74] | 49 | if ( load_libs )
|
---|
[9] | 50 | {
|
---|
| 51 | stack_guard guard( this );
|
---|
[74] | 52 | static const luaL_Reg lualibs[] =
|
---|
| 53 | {
|
---|
| 54 | { "string", luaopen_string },
|
---|
| 55 | { "table", luaopen_table },
|
---|
| 56 | { "math", luaopen_math },
|
---|
| 57 | { NULL, NULL}
|
---|
| 58 | };
|
---|
| 59 | const luaL_Reg *lib = lualibs;
|
---|
| 60 | for(; lib->func != NULL; lib++)
|
---|
| 61 | {
|
---|
[206] | 62 | lib->func( m_state );
|
---|
[74] | 63 | }
|
---|
[9] | 64 | }
|
---|
| 65 |
|
---|
[74] | 66 | NV_LOG( nv::LOG_TRACE, "Lua state created" );
|
---|
[9] | 67 | }
|
---|
| 68 |
|
---|
| 69 | int lua::state::load_string( const std::string& code, const std::string& name )
|
---|
| 70 | {
|
---|
| 71 | NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'");
|
---|
[206] | 72 | return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() );
|
---|
[9] | 73 | }
|
---|
| 74 |
|
---|
| 75 | int lua::state::load_stream( std::istream& stream, const std::string& name )
|
---|
| 76 | {
|
---|
| 77 | NV_LOG( nv::LOG_NOTICE, "Loading Lua stream '" << name << "'");
|
---|
| 78 | return load_string( std::string(
|
---|
| 79 | (std::istreambuf_iterator<char>(stream)),
|
---|
| 80 | std::istreambuf_iterator<char>()), name );
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | int lua::state::load_file( const std::string& filename )
|
---|
| 84 | {
|
---|
| 85 | NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'");
|
---|
[206] | 86 | return luaL_loadfile( m_state, filename.c_str() );
|
---|
[9] | 87 | }
|
---|
| 88 |
|
---|
[79] | 89 | bool lua::state::do_string( const std::string& code, const std::string& name, int rvalues )
|
---|
[9] | 90 | {
|
---|
| 91 | lua::stack_guard( this );
|
---|
| 92 | int result = load_string(code,name);
|
---|
| 93 | if (result)
|
---|
| 94 | {
|
---|
[206] | 95 | NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(m_state, -1));
|
---|
[9] | 96 | return false;
|
---|
| 97 | }
|
---|
[79] | 98 | return do_current( name, rvalues ) == 0;
|
---|
[9] | 99 | }
|
---|
| 100 |
|
---|
| 101 | bool lua::state::do_stream( std::istream& stream, const std::string& name )
|
---|
| 102 | {
|
---|
| 103 | lua::stack_guard( this );
|
---|
| 104 | int result = load_stream(stream,name);
|
---|
| 105 | if (result)
|
---|
| 106 | {
|
---|
[206] | 107 | NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(m_state, -1));
|
---|
[9] | 108 | return false;
|
---|
| 109 | }
|
---|
| 110 | return do_current( name ) == 0;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | bool lua::state::do_file( const std::string& filename )
|
---|
| 114 | {
|
---|
| 115 | lua::stack_guard( this );
|
---|
| 116 | int result = load_file(filename);
|
---|
| 117 | if (result)
|
---|
| 118 | {
|
---|
[206] | 119 | NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(m_state, -1));
|
---|
[9] | 120 | return false;
|
---|
| 121 | }
|
---|
| 122 | return do_current( filename ) == 0;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[79] | 125 | int lua::state::do_current( const std::string& name, int rvalues )
|
---|
[9] | 126 | {
|
---|
[206] | 127 | int result = lua_pcall(m_state, 0, rvalues, 0);
|
---|
[9] | 128 | if (result)
|
---|
| 129 | {
|
---|
[206] | 130 | NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(m_state, -1));
|
---|
| 131 | lua_pop( m_state, 1 );
|
---|
[9] | 132 | }
|
---|
| 133 | return result;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[206] | 136 | lua::state_wrapper::~state_wrapper()
|
---|
[9] | 137 | {
|
---|
| 138 | if (m_owner)
|
---|
| 139 | {
|
---|
[206] | 140 | lua_close( m_state );
|
---|
[9] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int lua::state::get_stack_size()
|
---|
| 145 | {
|
---|
[206] | 146 | return lua_gettop( m_state );
|
---|
[9] | 147 | }
|
---|
| 148 |
|
---|
[181] | 149 | lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
|
---|
[206] | 150 | : state_wrapper( lstate->get_raw(), false ), m_level(0)
|
---|
[9] | 151 | {
|
---|
[206] | 152 | m_global = false;
|
---|
| 153 | m_level = lua_gettop( m_state );
|
---|
| 154 | if ( !p.resolve( m_state, global ) )
|
---|
[181] | 155 | {
|
---|
| 156 | // TODO : error handling
|
---|
| 157 | }
|
---|
[9] | 158 | }
|
---|
| 159 |
|
---|
[181] | 160 | lua::table_guard::table_guard( const table_guard& parent, const path& p )
|
---|
[206] | 161 | : state_wrapper( parent.m_state, false ), m_level(0)
|
---|
[9] | 162 | {
|
---|
[206] | 163 | m_global = false;
|
---|
| 164 | m_level = lua_gettop( m_state );
|
---|
| 165 | if ( !p.resolve( m_state, false ) )
|
---|
[181] | 166 | {
|
---|
| 167 | // TODO : error handling
|
---|
| 168 | }
|
---|
[9] | 169 | }
|
---|
| 170 |
|
---|
[188] | 171 | size_t lua::table_guard::get_size()
|
---|
| 172 | {
|
---|
[206] | 173 | return lua_rawlen( m_state, -1 );
|
---|
[188] | 174 | }
|
---|
| 175 |
|
---|
| 176 |
|
---|
[9] | 177 | bool lua::table_guard::has_field( const string& element )
|
---|
| 178 | {
|
---|
[206] | 179 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 180 | bool result = lua_isnil( m_state, -1 );
|
---|
| 181 | lua_pop( m_state, 1 );
|
---|
[9] | 182 | return result;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
|
---|
| 186 | {
|
---|
[206] | 187 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 188 | string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
|
---|
| 189 | lua_pop( m_state, 1 );
|
---|
[9] | 190 | return result;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
|
---|
| 194 | {
|
---|
[206] | 195 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 196 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
| 197 | lua_pop( m_state, 1 );
|
---|
[9] | 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
|
---|
| 202 | {
|
---|
[206] | 203 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 204 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
| 205 | lua_pop( m_state, 1 );
|
---|
[204] | 206 | return static_cast< int >( result );
|
---|
[9] | 207 | }
|
---|
| 208 |
|
---|
[188] | 209 | unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
|
---|
| 210 | {
|
---|
[206] | 211 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 212 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
|
---|
| 213 | lua_pop( m_state, 1 );
|
---|
[188] | 214 | return result;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[9] | 217 | double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
|
---|
| 218 | {
|
---|
[206] | 219 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 220 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
| 221 | lua_pop( m_state, 1 );
|
---|
[9] | 222 | return result;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
|
---|
| 226 | {
|
---|
[206] | 227 | lua_getfield( m_state, -1, element.c_str() );
|
---|
| 228 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
| 229 | lua_pop( m_state, 1 );
|
---|
[9] | 230 | return result;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[206] | 233 | void nv::lua::state_wrapper::push_global_table()
|
---|
[185] | 234 | {
|
---|
[206] | 235 | lua_pushglobaltable( m_state );
|
---|
[185] | 236 | }
|
---|
| 237 |
|
---|
[206] | 238 | void nv::lua::state_wrapper::pop_global_table()
|
---|
[185] | 239 | {
|
---|
[206] | 240 | lua_replace( m_state, -2 );
|
---|
[185] | 241 | }
|
---|
| 242 |
|
---|
[206] | 243 | void lua::state_wrapper::call_get()
|
---|
[188] | 244 | {
|
---|
[206] | 245 | lua_gettable( m_state, -2 );
|
---|
[188] | 246 | }
|
---|
[185] | 247 |
|
---|
[206] | 248 | void lua::state_wrapper::call_get_raw()
|
---|
| 249 | {
|
---|
| 250 | lua_rawget( m_state, -2 );
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[212] | 253 |
|
---|
| 254 | void lua::state_wrapper::register_native_function( lfunction f, const char* name )
|
---|
| 255 | {
|
---|
| 256 | if ( m_global ) push_global_table();
|
---|
| 257 | lua_pushcfunction( m_state, f );
|
---|
| 258 | lua_setfield( m_state, -2, name );
|
---|
| 259 | if ( m_global ) pop_global_table();
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[9] | 262 | void lua::state::log_stack()
|
---|
| 263 | {
|
---|
[206] | 264 | int top = lua_gettop(m_state);
|
---|
[9] | 265 | NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")");
|
---|
| 266 | for ( int i = 0; i < top; ++i )
|
---|
| 267 | {
|
---|
[206] | 268 | NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(m_state, lua_type(m_state, i+1) ) << " = " << nlua_typecontent(m_state, i+1) );
|
---|
[9] | 269 | }
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | lua_State* lua::state::get_raw()
|
---|
| 273 | {
|
---|
[206] | 274 | return m_state;
|
---|
[9] | 275 | }
|
---|
| 276 |
|
---|
[187] | 277 | lua::reference lua::state::register_object( object * o, const char* lua_name )
|
---|
[77] | 278 | {
|
---|
[187] | 279 | if ( o == nullptr ) return ref_none;
|
---|
[77] | 280 | stack_guard guard( this );
|
---|
[206] | 281 | lua_getglobal( m_state, lua_name );
|
---|
| 282 | if ( lua_isnil( m_state, -1 ) )
|
---|
[77] | 283 | {
|
---|
[187] | 284 | NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
|
---|
[77] | 285 | }
|
---|
| 286 | deep_pointer_copy( -1, o );
|
---|
[206] | 287 | return luaL_ref( m_state, LUA_REGISTRYINDEX );
|
---|
[77] | 288 | }
|
---|
[9] | 289 |
|
---|
[187] | 290 | lua::reference lua::state::register_object( object * o )
|
---|
| 291 | {
|
---|
| 292 | if ( o == nullptr ) return ref_none;
|
---|
| 293 | type_database *db = o->get_root()->get_type_database();
|
---|
| 294 | if ( db == nullptr ) return ref_none;
|
---|
| 295 | type_entry* t = db->get_type(typeid(*o));
|
---|
| 296 | if ( t == nullptr ) return ref_none;
|
---|
| 297 | return register_object( o, t->name.c_str() );
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[212] | 300 | void lua::state::register_native_object_method( const char* lua_name, const char* name, lfunction f )
|
---|
| 301 | {
|
---|
| 302 | stack_guard guard( this );
|
---|
| 303 | lua_getglobal( m_state, lua_name );
|
---|
| 304 | if ( lua_isnil( m_state, -1 ) )
|
---|
| 305 | {
|
---|
| 306 | NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
|
---|
| 307 | }
|
---|
| 308 | lua_pushcfunction( m_state, f );
|
---|
| 309 | lua_setfield( m_state, -2, name );
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[77] | 312 | void lua::state::unregister_object( object * o )
|
---|
| 313 | {
|
---|
| 314 | if (!o) return;
|
---|
| 315 | stack_guard guard( this );
|
---|
[206] | 316 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
| 317 | lua_pushstring( m_state, "__ptr" );
|
---|
| 318 | lua_pushboolean( m_state, false );
|
---|
| 319 | lua_rawset( m_state, -3 );
|
---|
| 320 | lua_pop( m_state, 1 );
|
---|
| 321 | luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
[77] | 322 | }
|
---|
| 323 |
|
---|
| 324 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
| 325 | {
|
---|
[206] | 326 | index = lua_absindex( m_state, index );
|
---|
| 327 | lua_newtable( m_state );
|
---|
| 328 | lua_pushnil( m_state );
|
---|
[77] | 329 | bool has_functions = false;
|
---|
| 330 | bool has_metatable = false;
|
---|
| 331 |
|
---|
[206] | 332 | while ( lua_next( m_state, index ) != 0 )
|
---|
[77] | 333 | {
|
---|
[206] | 334 | if ( lua_isfunction( m_state, -1 ) )
|
---|
[77] | 335 | has_functions = true;
|
---|
[206] | 336 | else if ( lua_istable( m_state, -1 ) )
|
---|
[77] | 337 | {
|
---|
| 338 | deep_pointer_copy( -1, obj );
|
---|
[206] | 339 | lua_insert( m_state, -2 );
|
---|
| 340 | lua_pop( m_state, 1 );
|
---|
[77] | 341 | }
|
---|
[206] | 342 | lua_pushvalue( m_state, -2 );
|
---|
| 343 | lua_insert( m_state, -2 );
|
---|
| 344 | lua_settable( m_state, -4 );
|
---|
[77] | 345 | }
|
---|
| 346 |
|
---|
[206] | 347 | if ( lua_getmetatable( m_state, -2 ) )
|
---|
[77] | 348 | {
|
---|
[206] | 349 | lua_setmetatable( m_state, -2 );
|
---|
[77] | 350 | has_metatable = true;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
| 353 | if ( has_functions || has_metatable )
|
---|
| 354 | {
|
---|
[206] | 355 | lua_pushstring( m_state, "__ptr" );
|
---|
| 356 | lua_pushlightuserdata( m_state, obj );
|
---|
| 357 | lua_rawset( m_state, -3 );
|
---|
[77] | 358 | }
|
---|
| 359 | }
|
---|
[163] | 360 |
|
---|
| 361 | void lua::state::register_enum( type_database* db, const std::string& name, const std::string& prefix /*= std::string() */ )
|
---|
| 362 | {
|
---|
| 363 | type_entry* et = db->get_type( name );
|
---|
| 364 |
|
---|
| 365 | for ( const auto& entry : et->enum_list )
|
---|
| 366 | {
|
---|
[206] | 367 | lua_pushinteger( m_state, entry.value );
|
---|
[163] | 368 | if ( prefix.empty() )
|
---|
| 369 | {
|
---|
[206] | 370 | lua_setglobal( m_state, entry.name.c_str() );
|
---|
[163] | 371 | }
|
---|
| 372 | else
|
---|
| 373 | {
|
---|
[206] | 374 | lua_setglobal( m_state, ( prefix + entry.name ).c_str() );
|
---|
[163] | 375 | }
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
[185] | 378 |
|
---|
[206] | 379 | void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer )
|
---|
| 380 | {
|
---|
| 381 | if (!o) return;
|
---|
| 382 | stack_guard guard( this );
|
---|
| 383 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
| 384 | lua_pushstring( m_state, metaname.c_str() );
|
---|
| 385 | lua_pushlightuserdata( m_state, pointer );
|
---|
| 386 | lua_rawset( m_state, -3 );
|
---|
| 387 | lua_pop( m_state, 1 );
|
---|
| 388 | }
|
---|
[188] | 389 |
|
---|
[206] | 390 | bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
|
---|
[188] | 391 | {
|
---|
[206] | 392 | if ( !p.resolve( m_state, global ) )
|
---|
[188] | 393 | {
|
---|
| 394 | return false;
|
---|
| 395 | }
|
---|
[206] | 396 | bool result = !lua_isnil( m_state, -1 );
|
---|
| 397 | lua_pop( m_state, 1 );
|
---|
[188] | 398 | return result;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[206] | 401 | bool nv::lua::state_wrapper::push_function( const path& p, bool global )
|
---|
[185] | 402 | {
|
---|
[206] | 403 | if ( !p.resolve( m_state, global ) )
|
---|
[185] | 404 | {
|
---|
| 405 | NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
|
---|
| 406 | return false;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[206] | 409 | if ( !lua_isfunction( m_state, -1 ) )
|
---|
[185] | 410 | {
|
---|
[206] | 411 | lua_pop( m_state, 1 );
|
---|
[185] | 412 | NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
|
---|
| 413 | return false;
|
---|
| 414 | }
|
---|
| 415 | return true;
|
---|
| 416 | }
|
---|
[187] | 417 |
|
---|
[206] | 418 | int nv::lua::state_wrapper::call_function( int nargs, int nresults )
|
---|
| 419 | {
|
---|
| 420 | int status = lua_pcall( m_state, nargs, nresults, 0 );
|
---|
| 421 | if ( status != 0 )
|
---|
| 422 | {
|
---|
| 423 | std::string error = lua_tostring( m_state, -1 );
|
---|
| 424 | lua_pop( m_state, 1 );
|
---|
| 425 | NV_LOG( LOG_ERROR, "Lua error : " << error )
|
---|
| 426 | }
|
---|
| 427 | return status;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | lua::table_guard::~table_guard()
|
---|
| 431 | {
|
---|
| 432 | lua_settop( m_state, m_level );
|
---|
| 433 | }
|
---|
[212] | 434 |
|
---|
| 435 | int nv::lua::upvalue_index( int i )
|
---|
| 436 | {
|
---|
| 437 | return lua_upvalueindex(i);
|
---|
| 438 | }
|
---|