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

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