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

Last change on this file since 505 was 505, checked in by epyon, 9 years ago
  • several STL updates
  • several minor fixes
File size: 23.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"
[503]11#include "nv/lua/lua_types.hh"
[319]12#include "nv/core/logging.hh"
[368]13#include "nv/stl/string.hh"
[9]14
15using namespace nv;
16
[216]17// stack_guard
18
[403]19#define NV_LUA_ABORT( func, ... ) \
20        NV_LOG_CRITICAL( "lua::" func " : ", __VA_ARGS__ ) \
21        NV_ABORT( "lua::" func " : critical error!" )
22
23
[121]24lua::stack_guard::stack_guard( lua::state* aL )
[206]25        : L(aL), m_level( lua_gettop(aL->m_state) )
[9]26{
27
28}
29
[121]30lua::stack_guard::stack_guard( lua::state& aL )
[206]31        : L(&aL), m_level( lua_gettop(aL.m_state) )
[86]32{
33
34}
35
[9]36lua::stack_guard::~stack_guard()
37{
[206]38        lua_settop( L->m_state, m_level );
[9]39}
40
[334]41// stack_assert
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.get_raw()), m_expected( lua_gettop(aL.get_raw() ) + expected )
50{
51
52}
53
54nv::lua::stack_assert::stack_assert( lua_State* aL, int expected )
55        : L(aL), m_expected( lua_gettop(aL) + expected )
56{
57
58}
59
60lua::stack_assert::~stack_assert()
61{
62        NV_ASSERT( lua_gettop(L) == m_expected, "Lua stack corruption detected!" );
63}
64
65
[216]66// state_wrapper
67
68void nv::lua::state_wrapper::push_global_table()
[9]69{
[490]70        nlua_pushglobaltable( m_state );
[206]71}
72
[216]73void nv::lua::state_wrapper::pop_global_table()
[206]74{
[216]75        lua_replace( m_state, -2 );
[9]76}
77
[216]78void lua::state_wrapper::call_get()
[9]79{
[216]80        lua_gettable( m_state, -2 );
[9]81}
82
[216]83void lua::state_wrapper::call_get_raw()
[9]84{
[216]85        lua_rawget( m_state, -2 );
[9]86}
87
[399]88void lua::state_wrapper::register_native_function( lfunction f, string_view name )
[9]89{
[216]90        if ( m_global ) push_global_table();
91        lua_pushcfunction( m_state, f );
[360]92        lua_setfield( m_state, -2, name.data() );
[216]93        if ( m_global ) pop_global_table();
[9]94}
95
[216]96lua::state_wrapper::~state_wrapper()
[9]97{
[216]98        if (m_owner)
[9]99        {
[216]100                lua_close( m_state );
[9]101        }
102}
103
[216]104bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
[9]105{
[216]106        if ( !p.resolve( m_state, global ) )
[9]107        {
108                return false;
109        }
[216]110        bool result = !lua_isnil( m_state, -1 );
111        lua_pop( m_state, 1 );
112        return result;
[9]113}
114
[216]115bool nv::lua::state_wrapper::push_function( const path& p, bool global )
[9]116{
[216]117        if ( !p.resolve( m_state, global ) )
[9]118        {
[440]119                NV_LOG_ERROR( "Lua error : not a valid path - ", p.to_string() );
[9]120                return false;
121        }
122
[216]123        if ( !lua_isfunction( m_state, -1 ) )
[9]124        {
[206]125                lua_pop( m_state, 1 );
[440]126                NV_LOG_ERROR( "Lua error : not a valid function - ", p.to_string() );
[216]127                return false;
[9]128        }
[216]129        return true;
[9]130}
131
[216]132int nv::lua::state_wrapper::call_function( int nargs, int nresults )
[9]133{
[216]134        int status = lua_pcall( m_state, nargs, nresults, 0 );
135        if ( status != 0 )
[9]136        {
[437]137                NV_LOG_ERROR( "Lua error : ", nlua_tostringview( m_state, -1 ) );
[216]138                lua_pop( m_state, 1 );
[9]139        }
[216]140        return status;
[9]141}
142
[216]143// table_guard
[9]144
[181]145lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
[503]146        : state_wrapper( lstate->get_raw(), lstate->m_lua_types, false ), m_parent( lstate ), m_level(0)
[9]147{
[206]148        m_global = false;
149        m_level  = lua_gettop( m_state );
[305]150        if ( !p.resolve( m_state, global ) || lua_type(m_state, -1) != LUA_TTABLE )
[181]151        {
[365]152                NV_LOG_ERROR( "Could not resolve table!" );
[181]153                // TODO : error handling
154        }
[9]155}
156
[181]157lua::table_guard::table_guard( const table_guard& parent, const path& p )
[503]158        : state_wrapper( parent.m_state, parent.m_lua_types, false ), m_parent( parent.m_parent ), m_level(0)
[9]159{
[206]160        m_global = false;
161        m_level  = lua_gettop( m_state );
[305]162        if ( !p.resolve( m_state, false ) || lua_type(m_state, -1) != LUA_TTABLE )
[181]163        {
[365]164                NV_LOG_ERROR( "Could not resolve table!" );
[181]165                // TODO : error handling
166        }
[9]167}
168
[216]169lua::table_guard::~table_guard()
170{
171        lua_settop( m_state, m_level );
172}
173
[376]174nv::size_t lua::table_guard::get_size()
[188]175{
[490]176        return nlua_rawlen( m_state, -1 );
[188]177}
178
[399]179bool lua::table_guard::has_field( string_view element )
[9]180{
[360]181        lua_getfield( m_state, -1, element.data() );
[288]182        bool result = !( lua_isnil( m_state, -1 ) );
[206]183        lua_pop( m_state, 1 );
[9]184        return result;
185}
186
[431]187shash64 nv::lua::table_guard::get_string_hash_64( string_view element, uint64 defval /*= 0 */ )
[426]188{
189        lua_getfield( m_state, -1, element.data() );
190        size_t l = 0;
191        const char* str = nullptr;
192        uint64 result = defval;
193        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
194        {
195                str = lua_tolstring( m_state, -1, &l );
196                result = hash_string< uint64 >( str, l );
[486]197                //NV_LOG_DEBUG( str );
[426]198        }
199        lua_pop( m_state, 1 );
[431]200        return shash64( result );
[426]201}
202
[505]203nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ )
[431]204{
205        lua_getfield( m_state, -1, element.data() );
206        size_t l = 0;
207        const char* str = nullptr;
[486]208        shash64 result = shash64( defval );
[431]209        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
210        {
211                str = lua_tolstring( m_state, -1, &l );
[505]212                string_view sv( str, l );
213                result = table ? table->insert( sv ) : shash64( sv );
[431]214        }
215        lua_pop( m_state, 1 );
[486]216        return result;
[431]217}
218
[505]219shash64 nv::lua::table_guard::get_string_hash_64( int i, uint64 defval /*= 0 */ )
[486]220{
[505]221        lua_rawgeti( m_state, -1, i );
[486]222        size_t l = 0;
223        const char* str = nullptr;
[505]224        uint64 result = defval;
225        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
226        {
227                str = lua_tolstring( m_state, -1, &l );
228                result = hash_string< uint64 >( str, l );
229                //NV_LOG_DEBUG( str );
230        }
231        lua_pop( m_state, 1 );
232        return shash64( result );
233}
234
235nv::string128 nv::lua::table_guard::get_string128( int i, string_view defval /*= string_view() */ )
236{
237        lua_rawgeti( m_state, -1, i );
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        string128 result( str, l );
250        lua_pop( m_state, 1 );
251        return result;
252}
253
254const_string nv::lua::table_guard::get_string( int i, string_view defval /*= string_view() */ )
255{
256        lua_rawgeti( m_state, -1, i );
257        size_t l = 0;
258        const char* str = nullptr;
259        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
260        {
261                str = lua_tolstring( m_state, -1, &l );
262        }
263        else
264        {
265                l = defval.size();
266                str = defval.data();
267        }
268        const_string result( str, l );
269        lua_pop( m_state, 1 );
270        return result;
271}
272
273shash64 nv::lua::table_guard::get_string( int i, string_table* table, uint64 defval /*= 0 */ )
274{
275        lua_rawgeti( m_state, -1, i );
276        size_t l = 0;
277        const char* str = nullptr;
[486]278        shash64 result = shash64( defval );
279        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
280        {
281                str = lua_tolstring( m_state, -1, &l );
282                string_view sv( str, l );
283                result = table ? table->insert( sv ) : shash64( sv );
284        }
285        lua_pop( m_state, 1 );
286        return result;
287}
288
[399]289const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
[361]290{
291        lua_getfield( m_state, -1, element.data() );
292        size_t l = 0;
293        const char* str = nullptr;
294        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
295        {
296                str = lua_tolstring( m_state, -1, &l );
297        }
298        else
299        {
300                l = defval.size();
301                str = defval.data();
302        }
303        const_string result( str, l );
304        lua_pop( m_state, 1 );
305        return result;
306}
307
[440]308string128 lua::table_guard::get_string128( string_view element, string_view defval )
309{
310        lua_getfield( m_state, -1, element.data() );
311        size_t l = 0;
312        const char* str = nullptr;
313        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
314        {
315                str = lua_tolstring( m_state, -1, &l );
316        }
317        else
318        {
319                l = defval.size();
320                str = defval.data();
321        }
322        string128 result( str, l );
323        lua_pop( m_state, 1 );
324        return result;
325}
326
[399]327char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
[9]328{
[360]329        lua_getfield( m_state, -1, element.data() );
[490]330        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
[206]331        lua_pop( m_state, 1 );
[9]332        return result;
333}
334
[399]335int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
[9]336{
[360]337        lua_getfield( m_state, -1, element.data() );
[206]338        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
339        lua_pop( m_state, 1 );
[204]340        return static_cast< int >( result );
[9]341}
342
[399]343unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
[188]344{
[360]345        lua_getfield( m_state, -1, element.data() );
[490]346        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
[206]347        lua_pop( m_state, 1 );
[188]348        return result;
349}
350
[399]351double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
[9]352{
[360]353        lua_getfield( m_state, -1, element.data() );
[206]354        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
355        lua_pop( m_state, 1 );
[9]356        return result;
357}
358
[505]359char nv::lua::table_guard::get_char( int i, char defval /*= ' ' */ )
360{
361        lua_rawgeti( m_state, -1, i );
362        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
363        lua_pop( m_state, 1 );
364        return result;
365}
366
367int nv::lua::table_guard::get_integer( int i, int defval /*= 0 */ )
368{
369        lua_rawgeti( m_state, -1, i );
370        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
371        lua_pop( m_state, 1 );
372        return static_cast<int>( result );
373}
374
375unsigned nv::lua::table_guard::get_unsigned( int i, unsigned defval /*= 0 */ )
376{
377        lua_rawgeti( m_state, -1, i );
378        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
379        lua_pop( m_state, 1 );
380        return result;
381}
382
383double nv::lua::table_guard::get_double( int i, double defval /*= 0.0 */ )
384{
385        lua_rawgeti( m_state, -1, i );
386        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
387        lua_pop( m_state, 1 );
388        return result;
389}
390
391float nv::lua::table_guard::get_float( int i, float defval /*= 0.0 */ )
392{
393        lua_rawgeti( m_state, -1, i );
394        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
395        lua_pop( m_state, 1 );
396        return result;
397}
398
399bool nv::lua::table_guard::get_boolean( int i, bool defval /*= false */ )
400{
401        lua_rawgeti( m_state, -1, i );
402        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
403        lua_pop( m_state, 1 );
404        return result;
405}
406
[399]407float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
[288]408{
[360]409        lua_getfield( m_state, -1, element.data() );
[406]410        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
[288]411        lua_pop( m_state, 1 );
412        return result;
413}
414
[399]415bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
[9]416{
[360]417        lua_getfield( m_state, -1, element.data() );
[206]418        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
419        lua_pop( m_state, 1 );
[9]420        return result;
421}
422
[399]423bool nv::lua::table_guard::is_table( string_view element )
[296]424{
[360]425        lua_getfield( m_state, -1, element.data() );
[296]426        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
427        lua_pop( m_state, 1 );
428        return result;
429}
430
[505]431bool nv::lua::table_guard::is_table( int i )
432{
433        lua_rawgeti( m_state, -1, i );
434        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
435        lua_pop( m_state, 1 );
436        return result;
437}
438
439bool nv::lua::table_guard::is_number( int i )
440{
441        lua_rawgeti( m_state, -1, i );
442        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
443        lua_pop( m_state, 1 );
444        return result;
445}
446
447bool nv::lua::table_guard::is_boolean( int i )
448{
449        lua_rawgeti( m_state, -1, i );
450        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
451        lua_pop( m_state, 1 );
452        return result;
453}
454
455bool nv::lua::table_guard::is_string( int i )
456{
457        lua_rawgeti( m_state, -1, i );
458        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
459        lua_pop( m_state, 1 );
460        return result;
461}
462
[399]463bool nv::lua::table_guard::is_number( string_view element )
[296]464{
[360]465        lua_getfield( m_state, -1, element.data() );
[296]466        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
467        lua_pop( m_state, 1 );
468        return result;
469}
470
[399]471bool nv::lua::table_guard::is_boolean( string_view element )
[296]472{
[360]473        lua_getfield( m_state, -1, element.data() );
[296]474        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
475        lua_pop( m_state, 1 );
476        return result;
477}
478
[399]479bool nv::lua::table_guard::is_string( string_view element )
[296]480{
[360]481        lua_getfield( m_state, -1, element.data() );
[296]482        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
483        lua_pop( m_state, 1 );
484        return result;
485}
486
[503]487bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
488{
489        NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
490        lua_getfield( m_state, -1, element.data() );
491        if ( lua_type( m_state, -1 ) != LUA_TTABLE )
492        {
493                lua_pop( m_state, 1 );
494                return false;
495        }
496        if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
497        {
498                lua_pop( m_state, 1 );
499                return false;
500        }
501        lua_pop( m_state, 1 );
502        return true;
503}
504
[216]505// state
506
[503]507lua::state::state( lua_State* state, type_database* types )
508        : state_wrapper( state, new type_data( types ), false )
[185]509{
[216]510
[185]511}
512
[503]513lua::state::state( bool load_libs /*= false*/, type_database* types )
514        : state_wrapper( nullptr, new type_data( types ), true )
[185]515{
[216]516        load_lua_library();
517        m_owner = true;
518        m_state = luaL_newstate( );
519
520        lua_pushcfunction(m_state, luaopen_base);
[490]521        lua_pushliteral(m_state, "");
[216]522        lua_call(m_state, 1, 0);
523
[333]524        {
525                // Preregister 8 references for Handle registry
526                lua_newtable(m_state);
527                NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
528                for ( uint32 i = 1; i < 8; ++i )
529                {
530                        lua_newtable(m_state);
531                        luaL_ref( m_state, LUA_REGISTRYINDEX );
532                }
533        }
534
[216]535        if ( load_libs )
536        {
537                stack_guard guard( this );
538                static const luaL_Reg lualibs[] =
539                {
540                        { "string", luaopen_string },
541                        { "table",  luaopen_table },
542                        { "math",   luaopen_math },
543                        { NULL, NULL}
544                };
545                const luaL_Reg *lib = lualibs;
546                for(; lib->func != NULL; lib++)
547                {
[228]548                        lua_pushcfunction( m_state, lib->func );
549                        lua_call(m_state, 0, 1);
550                        lua_setglobal( m_state, lib->name );
[216]551                }
[217]552                register_nova( this );
[216]553        }
554
[365]555        NV_LOG_TRACE( "Lua state created" );
[185]556}
557
[399]558int lua::state::load_string( string_view code, string_view name )
[188]559{
[365]560        NV_LOG_TRACE( "Loading Lua string '", name, "'");
[360]561        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
[188]562}
[185]563
[399]564int lua::state::load_file( string_view filename )
[216]565{
[365]566        NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
[360]567        return luaL_loadfile( m_state, filename.data() );
[216]568}
[212]569
[399]570bool lua::state::do_string( string_view code, string_view name, int rvalues )
[212]571{
[216]572        lua::stack_guard( this );
573        int result = load_string(code,name);
574        if (result)
575        {
[437]576                NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
[216]577                return false;
578        }
579        return do_current( name, rvalues ) == 0;
[212]580}
581
[399]582bool lua::state::do_file( string_view filename )
[216]583{
584        lua::stack_guard( this );
585        int result = load_file(filename);
586        if (result)
587        {
[437]588                NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
[216]589                return false;
590        }
591        return do_current( filename ) == 0;
592}
593
[399]594int lua::state::do_current( string_view name, int rvalues )
[216]595{
596        int result = lua_pcall(m_state, 0, rvalues, 0);
597        if (result)
598        {
[437]599                NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
[216]600                lua_pop( m_state, 1 );
601        }
602        return result;
603}
604
605int lua::state::get_stack_size()
606{
607        return lua_gettop( m_state );
608}
609
[9]610void lua::state::log_stack()
611{
[206]612        int top = lua_gettop(m_state);
[365]613        NV_LOG_DEBUG( "Stack dump (", top, ")");
[9]614        for ( int i = 0; i < top; ++i )
615        {
[437]616                NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
[9]617        }
618}
619
620lua_State* lua::state::get_raw()
621{
[206]622        return m_state;
[9]623}
624
[399]625lua::ref lua::state::register_object( void* o, string_view lua_name )
[77]626{
[265]627        if ( o == nullptr ) return lua::ref( lua::ref::none );
[77]628        stack_guard guard( this );
[360]629        lua_getglobal( m_state, lua_name.data() );
[206]630        if ( lua_isnil( m_state, -1 ) )
[77]631        {
[403]632                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
[77]633        }
634        deep_pointer_copy( -1, o );
[265]635        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[77]636}
[9]637
[399]638lua::ref lua::state::register_proto( string_view id, string_view storage )
[217]639{
640        stack_guard guard( this );
[360]641        lua_getglobal( m_state, storage.data() );
[217]642        if ( lua_isnil( m_state, -1 ) )
643        {
[403]644                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
[217]645        }
[360]646        lua_getfield( m_state, -1, id.data() );
[217]647        if ( lua_isnil( m_state, -1 ) )
648        {
[403]649                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
[217]650        }
[265]651        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[217]652}
653
[399]654void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
[212]655{
656        stack_guard guard( this );
[360]657        lua_getglobal( m_state, lua_name.data() );
[212]658        if ( lua_isnil( m_state, -1 ) )
659        {
[403]660                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
[212]661        }
662        lua_pushcfunction( m_state, f );
[360]663        lua_setfield( m_state, -2, name.data() );
[212]664}
665
[265]666void lua::state::unregister_object( ref object_index )
[77]667{
[265]668        if ( !object_index.is_valid() ) return;
[77]669        stack_guard guard( this );
[265]670        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]671        lua_pushliteral( m_state, "__ptr" );
[206]672        lua_pushboolean( m_state, false );
673        lua_rawset( m_state, -3 );
674        lua_pop( m_state, 1 );
[265]675        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
[77]676}
677
[256]678
[77]679void lua::state::deep_pointer_copy( int index, void* obj )
680{
[490]681        index = nlua_absindex( m_state, index );
[206]682        lua_newtable( m_state );
683        lua_pushnil( m_state );
[77]684        bool has_functions = false;
685        bool has_metatable = false;
686
[206]687        while ( lua_next( m_state, index ) != 0 )
[77]688        {
[206]689                if ( lua_isfunction( m_state, -1 ) )
[77]690                        has_functions = true;
[206]691                else if ( lua_istable( m_state, -1 ) )
[77]692                {
693                        deep_pointer_copy( -1, obj );
[206]694                        lua_insert( m_state, -2 );
695                        lua_pop( m_state, 1 );
[77]696                }
[206]697                lua_pushvalue( m_state, -2 );
698                lua_insert( m_state, -2 );
699                lua_settable( m_state, -4 );
[77]700        }
701
[206]702        if ( lua_getmetatable( m_state, -2 ) )
[77]703        {
[206]704                lua_setmetatable( m_state, -2 );
[77]705                has_metatable = true;
706        }
707
708        if ( has_functions || has_metatable )
709        {
[403]710                lua_pushliteral( m_state, "__ptr" );
[206]711                lua_pushlightuserdata( m_state, obj );
712                lua_rawset( m_state, -3 );
[77]713        }
714}
[163]715
[399]716void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
[163]717{
[265]718        if ( !object_index.is_valid() ) return;
719        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[437]720        nlua_pushstringview( m_state, metaname );
[206]721        lua_pushlightuserdata( m_state, pointer );
722        lua_rawset( m_state, -3 );
723        lua_pop( m_state, 1 );
724}
[188]725
[399]726void nv::lua::state::register_enum( string_view name, int value )
[215]727{
[262]728        lua_pushinteger( m_state, value );
[360]729        lua_setglobal( m_state, name.data() );
[215]730}
731
[503]732void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
733{
734        m_lua_types->insert( tid, p, r );
735}
736
[399]737nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
[341]738{
739        int args = empty ? 1 : 2;
740        NV_LUA_STACK_ASSERT( m_state, -args );
741
742        if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
743        {
744                lua_pop( m_state, args );
745                return ref();
746        }
747        if (empty)
748                lua_createtable( m_state, 0, 0 );
749        else
750                nlua_deepcopy( m_state, -2 );
[437]751        nlua_pushstringview( m_state, id );
[341]752        lua_pushvalue( m_state, -2 );
753        lua_rawset( m_state, -4 );
754        lua_replace( m_state, -2 );
755        ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
756        if (!empty) lua_pop( m_state, 1 );
757        return result;
758}
759
[399]760void nv::lua::state::unregister_handle_component_impl( string_view id )
[341]761{
762        NV_LUA_STACK_ASSERT( m_state, -1 );
763
764        if ( lua_isnil( m_state, -1 ) )
765        {
766                lua_pop( m_state, 1 );
767                return;
768        }
[437]769        nlua_pushstringview( m_state, id );
[341]770        lua_pushnil( m_state );
771        lua_rawset( m_state, -3 );
772        lua_pop( m_state, 1 );
773}
774
[399]775void nv::lua::state::register_singleton( string_view name, void* o )
[341]776{
777        if ( o == nullptr ) return;
778        stack_guard guard( this );
[360]779        lua_getglobal( m_state, name.data() );
[341]780        if ( lua_isnil( m_state, -1 ) )
781        {
[403]782                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
[341]783        }
784        deep_pointer_copy( -1, o );
[360]785        lua_setglobal( m_state, name.data() );
[341]786}
787
[503]788nv::lua::state::~state()
789{
790        delete m_lua_types;
791        m_lua_types = nullptr;
792}
793
794template < typename T >
795void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object )
796{
797        T* value = reinterpret_cast<T*>( object );
798        lua_pushinteger( state->get_raw(), lua_Integer( *value ) );
799}
800
801template < typename T >
802void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object )
803{
804        T* value = reinterpret_cast<T*>( object );
805        lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) );
806}
807
808template < typename T >
809void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object )
810{
811        T* value = reinterpret_cast< T* >( object );
812        lua_pushnumber( state->get_raw(), lua_Number( *value ) );
813}
814
815static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object )
816{
817        bool* value = reinterpret_cast<bool*>( object );
818        lua_pushboolean( state->get_raw(), *value );
819}
820
821template < typename T >
822bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index )
823{
824        T* value = reinterpret_cast<T*>( object );
825        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
826        {
827                *value = T( lua_tointeger( state->get_raw(), index ) );
828                return true;
829        }
830        return false;
831}
832
833template < typename T >
834bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index )
835{
836        T* value = reinterpret_cast<T*>( object );
837        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
838        {
839                *value = T( lua_tointeger( state->get_raw(), index ) );
840                return true;
841        }
842        return false;
843}
844
845template < typename T >
846bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index )
847{
848        T* value = reinterpret_cast<T*>( object );
849        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
850        {
851                *value = T( lua_tonumber( state->get_raw(), index ) );
852                return true;
853        }
854        return false;
855}
856
857static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index )
858{
859        bool* value = reinterpret_cast<bool*>( object );
860        if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN )
861        {
862                *value = bool( lua_toboolean( state->get_raw(), index ) );
863                return true;
864        }
865        return false;
866}
867
868
869void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
870{
871        m_type_read.assign( tid, r );
872        m_type_push.assign( tid, p );
873}
874
875void nv::lua::type_data::register_standard_types()
876{
877        insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read );
878        insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>,    nlua_rtti_signed_read<nv::sint8> );
879        insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>,   nlua_rtti_signed_read<nv::sint16> );
880        insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>,   nlua_rtti_signed_read<nv::sint32> );
881        insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>,  nlua_rtti_unsigned_read<nv::uint8> );
882        insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> );
883        insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> );
884        insert<nv::f32>   ( nlua_rtti_floating_push<nv::f32>,    nlua_rtti_floating_read<nv::f32> );
885        insert<nv::f64>   ( nlua_rtti_floating_push<nv::f64>,    nlua_rtti_floating_read<nv::f64> );
886//      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
887//      insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
888}
Note: See TracBrowser for help on using the repository browser.