source: trunk/src/lua/lua_state.cc @ 406

Last change on this file since 406 was 406, checked in by epyon, 10 years ago
  • code compiles cleanly on maximum warning level
File size: 14.6 KB
RevLine 
[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"
[319]11#include "nv/core/logging.hh"
[368]12#include "nv/stl/string.hh"
[9]13
14using namespace nv;
15
[216]16// stack_guard
17
[403]18#define NV_LUA_ABORT( func, ... ) \
19        NV_LOG_CRITICAL( "lua::" func " : ", __VA_ARGS__ ) \
20        NV_ABORT( "lua::" func " : critical error!" )
21
22
[121]23lua::stack_guard::stack_guard( lua::state* aL )
[206]24        : L(aL), m_level( lua_gettop(aL->m_state) )
[9]25{
26
27}
28
[121]29lua::stack_guard::stack_guard( lua::state& aL )
[206]30        : L(&aL), m_level( lua_gettop(aL.m_state) )
[86]31{
32
33}
34
[9]35lua::stack_guard::~stack_guard()
36{
[206]37        lua_settop( L->m_state, m_level );
[9]38}
39
[334]40// stack_assert
41nv::lua::stack_assert::stack_assert( lua::state* aL, int expected )
42        : L(aL->get_raw()), m_expected( lua_gettop(aL->get_raw() ) + expected )
43{
44
45}
46
47nv::lua::stack_assert::stack_assert( lua::state& aL, int expected )
48        : L(aL.get_raw()), m_expected( lua_gettop(aL.get_raw() ) + expected )
49{
50
51}
52
53nv::lua::stack_assert::stack_assert( lua_State* aL, int expected )
54        : L(aL), m_expected( lua_gettop(aL) + expected )
55{
56
57}
58
59lua::stack_assert::~stack_assert()
60{
61        NV_ASSERT( lua_gettop(L) == m_expected, "Lua stack corruption detected!" );
62}
63
64
[216]65// state_wrapper
66
67void nv::lua::state_wrapper::push_global_table()
[9]68{
[216]69        lua_pushglobaltable( m_state );
[206]70}
71
[216]72void nv::lua::state_wrapper::pop_global_table()
[206]73{
[216]74        lua_replace( m_state, -2 );
[9]75}
76
[216]77void lua::state_wrapper::call_get()
[9]78{
[216]79        lua_gettable( m_state, -2 );
[9]80}
81
[216]82void lua::state_wrapper::call_get_raw()
[9]83{
[216]84        lua_rawget( m_state, -2 );
[9]85}
86
[399]87void lua::state_wrapper::register_native_function( lfunction f, string_view name )
[9]88{
[216]89        if ( m_global ) push_global_table();
90        lua_pushcfunction( m_state, f );
[360]91        lua_setfield( m_state, -2, name.data() );
[216]92        if ( m_global ) pop_global_table();
[9]93}
94
[216]95lua::state_wrapper::~state_wrapper()
[9]96{
[216]97        if (m_owner)
[9]98        {
[216]99                lua_close( m_state );
[9]100        }
101}
102
[216]103bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
[9]104{
[216]105        if ( !p.resolve( m_state, global ) )
[9]106        {
107                return false;
108        }
[216]109        bool result = !lua_isnil( m_state, -1 );
110        lua_pop( m_state, 1 );
111        return result;
[9]112}
113
[216]114bool nv::lua::state_wrapper::push_function( const path& p, bool global )
[9]115{
[216]116        if ( !p.resolve( m_state, global ) )
[9]117        {
[365]118                NV_LOG_ERROR( "Lua error : not a valid path - ", p.to_string() );
[9]119                return false;
120        }
121
[216]122        if ( !lua_isfunction( m_state, -1 ) )
[9]123        {
[206]124                lua_pop( m_state, 1 );
[365]125                NV_LOG_ERROR( "Lua error : not a valid function - ", p.to_string() );
[216]126                return false;
[9]127        }
[216]128        return true;
[9]129}
130
[216]131int nv::lua::state_wrapper::call_function( int nargs, int nresults )
[9]132{
[216]133        int status = lua_pcall( m_state, nargs, nresults, 0 );
134        if ( status != 0 )
[9]135        {
[365]136                NV_LOG_ERROR( "Lua error : ", lua_tostring( m_state, -1 ) );
[216]137                lua_pop( m_state, 1 );
[9]138        }
[216]139        return status;
[9]140}
141
[216]142// table_guard
[9]143
[181]144lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
[206]145        : state_wrapper( lstate->get_raw(), false ), m_level(0)
[9]146{
[206]147        m_global = false;
148        m_level  = lua_gettop( m_state );
[305]149        if ( !p.resolve( m_state, global ) || lua_type(m_state, -1) != LUA_TTABLE )
[181]150        {
[365]151                NV_LOG_ERROR( "Could not resolve table!" );
[181]152                // TODO : error handling
153        }
[9]154}
155
[181]156lua::table_guard::table_guard( const table_guard& parent, const path& p )
[206]157        : state_wrapper( parent.m_state, false ), m_level(0)
[9]158{
[206]159        m_global = false;
160        m_level  = lua_gettop( m_state );
[305]161        if ( !p.resolve( m_state, false ) || lua_type(m_state, -1) != LUA_TTABLE )
[181]162        {
[365]163                NV_LOG_ERROR( "Could not resolve table!" );
[181]164                // TODO : error handling
165        }
[9]166}
167
[216]168lua::table_guard::~table_guard()
169{
170        lua_settop( m_state, m_level );
171}
172
[376]173nv::size_t lua::table_guard::get_size()
[188]174{
[206]175        return lua_rawlen( m_state, -1 );
[188]176}
177
[399]178bool lua::table_guard::has_field( string_view element )
[9]179{
[360]180        lua_getfield( m_state, -1, element.data() );
[288]181        bool result = !( lua_isnil( m_state, -1 ) );
[206]182        lua_pop( m_state, 1 );
[9]183        return result;
184}
185
[399]186std::string lua::table_guard::get_std_string( string_view element, string_view defval /*= string_view() */ )
[9]187{
[360]188        lua_getfield( m_state, -1, element.data() );
[361]189        size_t l = 0;
190        const char* str = nullptr;
191        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
192        {
193                str = lua_tolstring( m_state, -1, &l );
194        }
195        else
196        {
197                l = defval.size();
198                str = defval.data();
199        }
200        std::string result( str, l );
[206]201        lua_pop( m_state, 1 );
[9]202        return result;
203}
204
[399]205const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
[361]206{
207        lua_getfield( m_state, -1, element.data() );
208        size_t l = 0;
209        const char* str = nullptr;
210        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
211        {
212                str = lua_tolstring( m_state, -1, &l );
213        }
214        else
215        {
216                l = defval.size();
217                str = defval.data();
218        }
219        const_string result( str, l );
220        lua_pop( m_state, 1 );
221        return result;
222}
223
[399]224char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
[9]225{
[360]226        lua_getfield( m_state, -1, element.data() );
[206]227        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
228        lua_pop( m_state, 1 );
[9]229        return result;
230}
231
[399]232int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
[9]233{
[360]234        lua_getfield( m_state, -1, element.data() );
[206]235        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
236        lua_pop( m_state, 1 );
[204]237        return static_cast< int >( result );
[9]238}
239
[399]240unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
[188]241{
[360]242        lua_getfield( m_state, -1, element.data() );
[206]243        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
244        lua_pop( m_state, 1 );
[188]245        return result;
246}
247
[399]248double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
[9]249{
[360]250        lua_getfield( m_state, -1, element.data() );
[206]251        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
252        lua_pop( m_state, 1 );
[9]253        return result;
254}
255
[288]256
[399]257float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
[288]258{
[360]259        lua_getfield( m_state, -1, element.data() );
[406]260        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
[288]261        lua_pop( m_state, 1 );
262        return result;
263}
264
[399]265bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
[9]266{
[360]267        lua_getfield( m_state, -1, element.data() );
[206]268        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
269        lua_pop( m_state, 1 );
[9]270        return result;
271}
272
[399]273bool nv::lua::table_guard::is_table( string_view element )
[296]274{
[360]275        lua_getfield( m_state, -1, element.data() );
[296]276        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
277        lua_pop( m_state, 1 );
278        return result;
279}
280
[399]281bool nv::lua::table_guard::is_number( string_view element )
[296]282{
[360]283        lua_getfield( m_state, -1, element.data() );
[296]284        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
285        lua_pop( m_state, 1 );
286        return result;
287}
288
[399]289bool nv::lua::table_guard::is_boolean( string_view element )
[296]290{
[360]291        lua_getfield( m_state, -1, element.data() );
[296]292        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
293        lua_pop( m_state, 1 );
294        return result;
295}
296
[399]297bool nv::lua::table_guard::is_string( string_view element )
[296]298{
[360]299        lua_getfield( m_state, -1, element.data() );
[296]300        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
301        lua_pop( m_state, 1 );
302        return result;
303}
304
305
[216]306// state
307
[262]308lua::state::state( lua_State* state ) : state_wrapper( state, false )
[185]309{
[216]310
[185]311}
312
[262]313lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
[185]314{
[216]315        load_lua_library();
316        m_owner = true;
317        m_state = luaL_newstate( );
318
319        lua_pushcfunction(m_state, luaopen_base);
320        lua_pushliteral(m_state, LUA_TABLIBNAME);
321        lua_call(m_state, 1, 0);
322
[333]323        {
324                // Preregister 8 references for Handle registry
325                lua_newtable(m_state);
326                NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
327                for ( uint32 i = 1; i < 8; ++i )
328                {
329                        lua_newtable(m_state);
330                        luaL_ref( m_state, LUA_REGISTRYINDEX );
331                }
332        }
333
[216]334        if ( load_libs )
335        {
336                stack_guard guard( this );
337                static const luaL_Reg lualibs[] =
338                {
339                        { "string", luaopen_string },
340                        { "table",  luaopen_table },
341                        { "math",   luaopen_math },
342                        { NULL, NULL}
343                };
344                const luaL_Reg *lib = lualibs;
345                for(; lib->func != NULL; lib++)
346                {
[228]347                        lua_pushcfunction( m_state, lib->func );
348                        lua_call(m_state, 0, 1);
349                        lua_setglobal( m_state, lib->name );
[216]350                }
[217]351                register_nova( this );
[216]352        }
353
[365]354        NV_LOG_TRACE( "Lua state created" );
[185]355}
356
[399]357int lua::state::load_string( string_view code, string_view name )
[188]358{
[365]359        NV_LOG_TRACE( "Loading Lua string '", name, "'");
[360]360        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
[188]361}
[185]362
[399]363int lua::state::load_file( string_view filename )
[216]364{
[365]365        NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
[360]366        return luaL_loadfile( m_state, filename.data() );
[216]367}
[212]368
[399]369bool lua::state::do_string( string_view code, string_view name, int rvalues )
[212]370{
[216]371        lua::stack_guard( this );
372        int result = load_string(code,name);
373        if (result)
374        {
[365]375                NV_LOG_WARNING( "Failed to load string ", name, ": ", lua_tostring(m_state, -1));
[216]376                return false;
377        }
378        return do_current( name, rvalues ) == 0;
[212]379}
380
[399]381bool lua::state::do_file( string_view filename )
[216]382{
383        lua::stack_guard( this );
384        int result = load_file(filename);
385        if (result)
386        {
[365]387                NV_LOG_WARNING( "Failed to open file ", filename, ": ", lua_tostring( m_state, -1 ) );
[216]388                return false;
389        }
390        return do_current( filename ) == 0;
391}
392
[399]393int lua::state::do_current( string_view name, int rvalues )
[216]394{
395        int result = lua_pcall(m_state, 0, rvalues, 0);
396        if (result)
397        {
[365]398                NV_LOG_WARNING( "Failed to run script ", name, ": ", lua_tostring( m_state, -1 ) );
[216]399                lua_pop( m_state, 1 );
400        }
401        return result;
402}
403
404int lua::state::get_stack_size()
405{
406        return lua_gettop( m_state );
407}
408
[9]409void lua::state::log_stack()
410{
[206]411        int top = lua_gettop(m_state);
[365]412        NV_LOG_DEBUG( "Stack dump (", top, ")");
[9]413        for ( int i = 0; i < top; ++i )
414        {
[365]415                NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
[9]416        }
417}
418
419lua_State* lua::state::get_raw()
420{
[206]421        return m_state;
[9]422}
423
[399]424lua::ref lua::state::register_object( void* o, string_view lua_name )
[77]425{
[265]426        if ( o == nullptr ) return lua::ref( lua::ref::none );
[77]427        stack_guard guard( this );
[360]428        lua_getglobal( m_state, lua_name.data() );
[206]429        if ( lua_isnil( m_state, -1 ) )
[77]430        {
[403]431                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
[77]432        }
433        deep_pointer_copy( -1, o );
[265]434        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[77]435}
[9]436
[399]437lua::ref lua::state::register_proto( string_view id, string_view storage )
[217]438{
439        stack_guard guard( this );
[360]440        lua_getglobal( m_state, storage.data() );
[217]441        if ( lua_isnil( m_state, -1 ) )
442        {
[403]443                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
[217]444        }
[360]445        lua_getfield( m_state, -1, id.data() );
[217]446        if ( lua_isnil( m_state, -1 ) )
447        {
[403]448                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
[217]449        }
[265]450        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[217]451}
452
[399]453void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
[212]454{
455        stack_guard guard( this );
[360]456        lua_getglobal( m_state, lua_name.data() );
[212]457        if ( lua_isnil( m_state, -1 ) )
458        {
[403]459                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
[212]460        }
461        lua_pushcfunction( m_state, f );
[360]462        lua_setfield( m_state, -2, name.data() );
[212]463}
464
[265]465void lua::state::unregister_object( ref object_index )
[77]466{
[265]467        if ( !object_index.is_valid() ) return;
[77]468        stack_guard guard( this );
[265]469        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]470        lua_pushliteral( m_state, "__ptr" );
[206]471        lua_pushboolean( m_state, false );
472        lua_rawset( m_state, -3 );
473        lua_pop( m_state, 1 );
[265]474        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
[77]475}
476
[256]477
[77]478void lua::state::deep_pointer_copy( int index, void* obj )
479{
[206]480        index = lua_absindex( m_state, index );
481        lua_newtable( m_state );
482        lua_pushnil( m_state );
[77]483        bool has_functions = false;
484        bool has_metatable = false;
485
[206]486        while ( lua_next( m_state, index ) != 0 )
[77]487        {
[206]488                if ( lua_isfunction( m_state, -1 ) )
[77]489                        has_functions = true;
[206]490                else if ( lua_istable( m_state, -1 ) )
[77]491                {
492                        deep_pointer_copy( -1, obj );
[206]493                        lua_insert( m_state, -2 );
494                        lua_pop( m_state, 1 );
[77]495                }
[206]496                lua_pushvalue( m_state, -2 );
497                lua_insert( m_state, -2 );
498                lua_settable( m_state, -4 );
[77]499        }
500
[206]501        if ( lua_getmetatable( m_state, -2 ) )
[77]502        {
[206]503                lua_setmetatable( m_state, -2 );
[77]504                has_metatable = true;
505        }
506
507        if ( has_functions || has_metatable )
508        {
[403]509                lua_pushliteral( m_state, "__ptr" );
[206]510                lua_pushlightuserdata( m_state, obj );
511                lua_rawset( m_state, -3 );
[77]512        }
513}
[163]514
[399]515void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
[163]516{
[265]517        if ( !object_index.is_valid() ) return;
518        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]519        lua_pushlstring( m_state, metaname.data(), metaname.size() );
[206]520        lua_pushlightuserdata( m_state, pointer );
521        lua_rawset( m_state, -3 );
522        lua_pop( m_state, 1 );
523}
[188]524
[399]525void nv::lua::state::register_enum( string_view name, int value )
[215]526{
[262]527        lua_pushinteger( m_state, value );
[360]528        lua_setglobal( m_state, name.data() );
[215]529}
530
[399]531nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
[341]532{
533        int args = empty ? 1 : 2;
534        NV_LUA_STACK_ASSERT( m_state, -args );
535
536        if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
537        {
538                lua_pop( m_state, args );
539                return ref();
540        }
541        if (empty)
542                lua_createtable( m_state, 0, 0 );
543        else
544                nlua_deepcopy( m_state, -2 );
[360]545        lua_pushlstring( m_state, id.data(), id.size() );
[341]546        lua_pushvalue( m_state, -2 );
547        lua_rawset( m_state, -4 );
548        lua_replace( m_state, -2 );
549        ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
550        if (!empty) lua_pop( m_state, 1 );
551        return result;
552}
553
[399]554void nv::lua::state::unregister_handle_component_impl( string_view id )
[341]555{
556        NV_LUA_STACK_ASSERT( m_state, -1 );
557
558        if ( lua_isnil( m_state, -1 ) )
559        {
560                lua_pop( m_state, 1 );
561                return;
562        }
[360]563        lua_pushlstring( m_state, id.data(), id.size() );
[341]564        lua_pushnil( m_state );
565        lua_rawset( m_state, -3 );
566        lua_pop( m_state, 1 );
567}
568
[399]569void nv::lua::state::register_singleton( string_view name, void* o )
[341]570{
571        if ( o == nullptr ) return;
572        stack_guard guard( this );
[360]573        lua_getglobal( m_state, name.data() );
[341]574        if ( lua_isnil( m_state, -1 ) )
575        {
[403]576                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
[341]577        }
578        deep_pointer_copy( -1, o );
[360]579        lua_setglobal( m_state, name.data() );
[341]580}
581
Note: See TracBrowser for help on using the repository browser.