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

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