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

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