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

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