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

Last change on this file since 431 was 431, checked in by epyon, 10 years ago
  • hash storage type
  • string hash storage type
  • several minor fixes
  • more cleanups needed
File size: 15.4 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
[431]186shash64 nv::lua::table_guard::get_string_hash_64( string_view element, uint64 defval /*= 0 */ )
[426]187{
188        lua_getfield( m_state, -1, element.data() );
189        size_t l = 0;
190        const char* str = nullptr;
191        uint64 result = defval;
192        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
193        {
194                str = lua_tolstring( m_state, -1, &l );
195                result = hash_string< uint64 >( str, l );
196        }
197        lua_pop( m_state, 1 );
[431]198        return shash64( result );
[426]199}
200
[431]201shash64 nv::lua::table_guard::get_string( string_view element, string_table& table, uint64 defval /*= 0 */ )
202{
203        lua_getfield( m_state, -1, element.data() );
204        size_t l = 0;
205        const char* str = nullptr;
206        uint64 result = defval;
207        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
208        {
209                str = lua_tolstring( m_state, -1, &l );
210                result = table.insert( string_view( str, l ) ).value();
211        }
212        lua_pop( m_state, 1 );
213        return shash64( result );
214}
215
[399]216std::string lua::table_guard::get_std_string( string_view element, string_view defval /*= string_view() */ )
[9]217{
[360]218        lua_getfield( m_state, -1, element.data() );
[361]219        size_t l = 0;
220        const char* str = nullptr;
221        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
222        {
223                str = lua_tolstring( m_state, -1, &l );
224        }
225        else
226        {
227                l = defval.size();
228                str = defval.data();
229        }
230        std::string result( str, l );
[206]231        lua_pop( m_state, 1 );
[9]232        return result;
233}
234
[399]235const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
[361]236{
237        lua_getfield( m_state, -1, element.data() );
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        const_string result( str, l );
250        lua_pop( m_state, 1 );
251        return result;
252}
253
[399]254char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
[9]255{
[360]256        lua_getfield( m_state, -1, element.data() );
[206]257        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
258        lua_pop( m_state, 1 );
[9]259        return result;
260}
261
[399]262int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
[9]263{
[360]264        lua_getfield( m_state, -1, element.data() );
[206]265        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
266        lua_pop( m_state, 1 );
[204]267        return static_cast< int >( result );
[9]268}
269
[399]270unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
[188]271{
[360]272        lua_getfield( m_state, -1, element.data() );
[206]273        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
274        lua_pop( m_state, 1 );
[188]275        return result;
276}
277
[399]278double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
[9]279{
[360]280        lua_getfield( m_state, -1, element.data() );
[206]281        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
282        lua_pop( m_state, 1 );
[9]283        return result;
284}
285
[399]286float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
[288]287{
[360]288        lua_getfield( m_state, -1, element.data() );
[406]289        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
[288]290        lua_pop( m_state, 1 );
291        return result;
292}
293
[399]294bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
[9]295{
[360]296        lua_getfield( m_state, -1, element.data() );
[206]297        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
298        lua_pop( m_state, 1 );
[9]299        return result;
300}
301
[399]302bool nv::lua::table_guard::is_table( string_view element )
[296]303{
[360]304        lua_getfield( m_state, -1, element.data() );
[296]305        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
306        lua_pop( m_state, 1 );
307        return result;
308}
309
[399]310bool nv::lua::table_guard::is_number( string_view element )
[296]311{
[360]312        lua_getfield( m_state, -1, element.data() );
[296]313        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
314        lua_pop( m_state, 1 );
315        return result;
316}
317
[399]318bool nv::lua::table_guard::is_boolean( string_view element )
[296]319{
[360]320        lua_getfield( m_state, -1, element.data() );
[296]321        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
322        lua_pop( m_state, 1 );
323        return result;
324}
325
[399]326bool nv::lua::table_guard::is_string( string_view element )
[296]327{
[360]328        lua_getfield( m_state, -1, element.data() );
[296]329        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
330        lua_pop( m_state, 1 );
331        return result;
332}
333
334
[216]335// state
336
[262]337lua::state::state( lua_State* state ) : state_wrapper( state, false )
[185]338{
[216]339
[185]340}
341
[262]342lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true )
[185]343{
[216]344        load_lua_library();
345        m_owner = true;
346        m_state = luaL_newstate( );
347
348        lua_pushcfunction(m_state, luaopen_base);
349        lua_pushliteral(m_state, LUA_TABLIBNAME);
350        lua_call(m_state, 1, 0);
351
[333]352        {
353                // Preregister 8 references for Handle registry
354                lua_newtable(m_state);
355                NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
356                for ( uint32 i = 1; i < 8; ++i )
357                {
358                        lua_newtable(m_state);
359                        luaL_ref( m_state, LUA_REGISTRYINDEX );
360                }
361        }
362
[216]363        if ( load_libs )
364        {
365                stack_guard guard( this );
366                static const luaL_Reg lualibs[] =
367                {
368                        { "string", luaopen_string },
369                        { "table",  luaopen_table },
370                        { "math",   luaopen_math },
371                        { NULL, NULL}
372                };
373                const luaL_Reg *lib = lualibs;
374                for(; lib->func != NULL; lib++)
375                {
[228]376                        lua_pushcfunction( m_state, lib->func );
377                        lua_call(m_state, 0, 1);
378                        lua_setglobal( m_state, lib->name );
[216]379                }
[217]380                register_nova( this );
[216]381        }
382
[365]383        NV_LOG_TRACE( "Lua state created" );
[185]384}
385
[399]386int lua::state::load_string( string_view code, string_view name )
[188]387{
[365]388        NV_LOG_TRACE( "Loading Lua string '", name, "'");
[360]389        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
[188]390}
[185]391
[399]392int lua::state::load_file( string_view filename )
[216]393{
[365]394        NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
[360]395        return luaL_loadfile( m_state, filename.data() );
[216]396}
[212]397
[399]398bool lua::state::do_string( string_view code, string_view name, int rvalues )
[212]399{
[216]400        lua::stack_guard( this );
401        int result = load_string(code,name);
402        if (result)
403        {
[365]404                NV_LOG_WARNING( "Failed to load string ", name, ": ", lua_tostring(m_state, -1));
[216]405                return false;
406        }
407        return do_current( name, rvalues ) == 0;
[212]408}
409
[399]410bool lua::state::do_file( string_view filename )
[216]411{
412        lua::stack_guard( this );
413        int result = load_file(filename);
414        if (result)
415        {
[365]416                NV_LOG_WARNING( "Failed to open file ", filename, ": ", lua_tostring( m_state, -1 ) );
[216]417                return false;
418        }
419        return do_current( filename ) == 0;
420}
421
[399]422int lua::state::do_current( string_view name, int rvalues )
[216]423{
424        int result = lua_pcall(m_state, 0, rvalues, 0);
425        if (result)
426        {
[365]427                NV_LOG_WARNING( "Failed to run script ", name, ": ", lua_tostring( m_state, -1 ) );
[216]428                lua_pop( m_state, 1 );
429        }
430        return result;
431}
432
433int lua::state::get_stack_size()
434{
435        return lua_gettop( m_state );
436}
437
[9]438void lua::state::log_stack()
439{
[206]440        int top = lua_gettop(m_state);
[365]441        NV_LOG_DEBUG( "Stack dump (", top, ")");
[9]442        for ( int i = 0; i < top; ++i )
443        {
[365]444                NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
[9]445        }
446}
447
448lua_State* lua::state::get_raw()
449{
[206]450        return m_state;
[9]451}
452
[399]453lua::ref lua::state::register_object( void* o, string_view lua_name )
[77]454{
[265]455        if ( o == nullptr ) return lua::ref( lua::ref::none );
[77]456        stack_guard guard( this );
[360]457        lua_getglobal( m_state, lua_name.data() );
[206]458        if ( lua_isnil( m_state, -1 ) )
[77]459        {
[403]460                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
[77]461        }
462        deep_pointer_copy( -1, o );
[265]463        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[77]464}
[9]465
[399]466lua::ref lua::state::register_proto( string_view id, string_view storage )
[217]467{
468        stack_guard guard( this );
[360]469        lua_getglobal( m_state, storage.data() );
[217]470        if ( lua_isnil( m_state, -1 ) )
471        {
[403]472                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
[217]473        }
[360]474        lua_getfield( m_state, -1, id.data() );
[217]475        if ( lua_isnil( m_state, -1 ) )
476        {
[403]477                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
[217]478        }
[265]479        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[217]480}
481
[399]482void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
[212]483{
484        stack_guard guard( this );
[360]485        lua_getglobal( m_state, lua_name.data() );
[212]486        if ( lua_isnil( m_state, -1 ) )
487        {
[403]488                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
[212]489        }
490        lua_pushcfunction( m_state, f );
[360]491        lua_setfield( m_state, -2, name.data() );
[212]492}
493
[265]494void lua::state::unregister_object( ref object_index )
[77]495{
[265]496        if ( !object_index.is_valid() ) return;
[77]497        stack_guard guard( this );
[265]498        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]499        lua_pushliteral( m_state, "__ptr" );
[206]500        lua_pushboolean( m_state, false );
501        lua_rawset( m_state, -3 );
502        lua_pop( m_state, 1 );
[265]503        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
[77]504}
505
[256]506
[77]507void lua::state::deep_pointer_copy( int index, void* obj )
508{
[206]509        index = lua_absindex( m_state, index );
510        lua_newtable( m_state );
511        lua_pushnil( m_state );
[77]512        bool has_functions = false;
513        bool has_metatable = false;
514
[206]515        while ( lua_next( m_state, index ) != 0 )
[77]516        {
[206]517                if ( lua_isfunction( m_state, -1 ) )
[77]518                        has_functions = true;
[206]519                else if ( lua_istable( m_state, -1 ) )
[77]520                {
521                        deep_pointer_copy( -1, obj );
[206]522                        lua_insert( m_state, -2 );
523                        lua_pop( m_state, 1 );
[77]524                }
[206]525                lua_pushvalue( m_state, -2 );
526                lua_insert( m_state, -2 );
527                lua_settable( m_state, -4 );
[77]528        }
529
[206]530        if ( lua_getmetatable( m_state, -2 ) )
[77]531        {
[206]532                lua_setmetatable( m_state, -2 );
[77]533                has_metatable = true;
534        }
535
536        if ( has_functions || has_metatable )
537        {
[403]538                lua_pushliteral( m_state, "__ptr" );
[206]539                lua_pushlightuserdata( m_state, obj );
540                lua_rawset( m_state, -3 );
[77]541        }
542}
[163]543
[399]544void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
[163]545{
[265]546        if ( !object_index.is_valid() ) return;
547        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]548        lua_pushlstring( m_state, metaname.data(), metaname.size() );
[206]549        lua_pushlightuserdata( m_state, pointer );
550        lua_rawset( m_state, -3 );
551        lua_pop( m_state, 1 );
552}
[188]553
[399]554void nv::lua::state::register_enum( string_view name, int value )
[215]555{
[262]556        lua_pushinteger( m_state, value );
[360]557        lua_setglobal( m_state, name.data() );
[215]558}
559
[399]560nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
[341]561{
562        int args = empty ? 1 : 2;
563        NV_LUA_STACK_ASSERT( m_state, -args );
564
565        if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
566        {
567                lua_pop( m_state, args );
568                return ref();
569        }
570        if (empty)
571                lua_createtable( m_state, 0, 0 );
572        else
573                nlua_deepcopy( m_state, -2 );
[360]574        lua_pushlstring( m_state, id.data(), id.size() );
[341]575        lua_pushvalue( m_state, -2 );
576        lua_rawset( m_state, -4 );
577        lua_replace( m_state, -2 );
578        ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
579        if (!empty) lua_pop( m_state, 1 );
580        return result;
581}
582
[399]583void nv::lua::state::unregister_handle_component_impl( string_view id )
[341]584{
585        NV_LUA_STACK_ASSERT( m_state, -1 );
586
587        if ( lua_isnil( m_state, -1 ) )
588        {
589                lua_pop( m_state, 1 );
590                return;
591        }
[360]592        lua_pushlstring( m_state, id.data(), id.size() );
[341]593        lua_pushnil( m_state );
594        lua_rawset( m_state, -3 );
595        lua_pop( m_state, 1 );
596}
597
[399]598void nv::lua::state::register_singleton( string_view name, void* o )
[341]599{
600        if ( o == nullptr ) return;
601        stack_guard guard( this );
[360]602        lua_getglobal( m_state, name.data() );
[341]603        if ( lua_isnil( m_state, -1 ) )
604        {
[403]605                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
[341]606        }
607        deep_pointer_copy( -1, o );
[360]608        lua_setglobal( m_state, name.data() );
[341]609}
610
Note: See TracBrowser for help on using the repository browser.