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

Last change on this file since 515 was 515, checked in by epyon, 9 years ago
  • model tag support
  • local transform particle engines
  • fix for 3d textures
  • minor cleanups/fixes
File size: 25.0 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
[509]327nv::string64 nv::lua::table_guard::get_string64( string_view element, string_view defval /*= string_view() */ )
328{
329        lua_getfield( m_state, -1, element.data() );
330        size_t l = 0;
331        const char* str = nullptr;
332        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
333        {
334                str = lua_tolstring( m_state, -1, &l );
335        }
336        else
337        {
338                l = defval.size();
339                str = defval.data();
340        }
341        string64 result( str, l );
342        lua_pop( m_state, 1 );
343        return result;
344}
345
346
[515]347nv::string32 nv::lua::table_guard::get_string32( string_view element, string_view defval /*= string_view() */ )
348{
349        lua_getfield( m_state, -1, element.data() );
350        size_t l = 0;
351        const char* str = nullptr;
352        if ( lua_type( m_state, -1 ) == LUA_TSTRING )
353        {
354                str = lua_tolstring( m_state, -1, &l );
355        }
356        else
357        {
358                l = defval.size();
359                str = defval.data();
360        }
361        string32 result( str, l );
362        lua_pop( m_state, 1 );
363        return result;
364}
365
[399]366char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
[9]367{
[360]368        lua_getfield( m_state, -1, element.data() );
[490]369        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
[206]370        lua_pop( m_state, 1 );
[9]371        return result;
372}
373
[399]374int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
[9]375{
[360]376        lua_getfield( m_state, -1, element.data() );
[206]377        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
378        lua_pop( m_state, 1 );
[204]379        return static_cast< int >( result );
[9]380}
381
[399]382unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
[188]383{
[360]384        lua_getfield( m_state, -1, element.data() );
[490]385        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
[206]386        lua_pop( m_state, 1 );
[188]387        return result;
388}
389
[399]390double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
[9]391{
[360]392        lua_getfield( m_state, -1, element.data() );
[206]393        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
394        lua_pop( m_state, 1 );
[9]395        return result;
396}
397
[505]398char nv::lua::table_guard::get_char( int i, char defval /*= ' ' */ )
399{
400        lua_rawgeti( m_state, -1, i );
401        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
402        lua_pop( m_state, 1 );
403        return result;
404}
405
406int nv::lua::table_guard::get_integer( int i, int defval /*= 0 */ )
407{
408        lua_rawgeti( m_state, -1, i );
409        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
410        lua_pop( m_state, 1 );
411        return static_cast<int>( result );
412}
413
414unsigned nv::lua::table_guard::get_unsigned( int i, unsigned defval /*= 0 */ )
415{
416        lua_rawgeti( m_state, -1, i );
417        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
418        lua_pop( m_state, 1 );
419        return result;
420}
421
422double nv::lua::table_guard::get_double( int i, double defval /*= 0.0 */ )
423{
424        lua_rawgeti( m_state, -1, i );
425        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
426        lua_pop( m_state, 1 );
427        return result;
428}
429
430float nv::lua::table_guard::get_float( int i, float defval /*= 0.0 */ )
431{
432        lua_rawgeti( m_state, -1, i );
433        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
434        lua_pop( m_state, 1 );
435        return result;
436}
437
438bool nv::lua::table_guard::get_boolean( int i, bool defval /*= false */ )
439{
440        lua_rawgeti( m_state, -1, i );
441        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
442        lua_pop( m_state, 1 );
443        return result;
444}
445
[399]446float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
[288]447{
[360]448        lua_getfield( m_state, -1, element.data() );
[406]449        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
[288]450        lua_pop( m_state, 1 );
451        return result;
452}
453
[399]454bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
[9]455{
[360]456        lua_getfield( m_state, -1, element.data() );
[206]457        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
458        lua_pop( m_state, 1 );
[9]459        return result;
460}
461
[399]462bool nv::lua::table_guard::is_table( string_view element )
[296]463{
[360]464        lua_getfield( m_state, -1, element.data() );
[296]465        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
466        lua_pop( m_state, 1 );
467        return result;
468}
469
[505]470bool nv::lua::table_guard::is_table( int i )
471{
472        lua_rawgeti( m_state, -1, i );
473        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
474        lua_pop( m_state, 1 );
475        return result;
476}
477
478bool nv::lua::table_guard::is_number( int i )
479{
480        lua_rawgeti( m_state, -1, i );
481        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
482        lua_pop( m_state, 1 );
483        return result;
484}
485
486bool nv::lua::table_guard::is_boolean( int i )
487{
488        lua_rawgeti( m_state, -1, i );
489        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
490        lua_pop( m_state, 1 );
491        return result;
492}
493
494bool nv::lua::table_guard::is_string( int i )
495{
496        lua_rawgeti( m_state, -1, i );
497        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
498        lua_pop( m_state, 1 );
499        return result;
500}
501
[399]502bool nv::lua::table_guard::is_number( string_view element )
[296]503{
[360]504        lua_getfield( m_state, -1, element.data() );
[296]505        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
506        lua_pop( m_state, 1 );
507        return result;
508}
509
[399]510bool nv::lua::table_guard::is_boolean( string_view element )
[296]511{
[360]512        lua_getfield( m_state, -1, element.data() );
[296]513        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
514        lua_pop( m_state, 1 );
515        return result;
516}
517
[399]518bool nv::lua::table_guard::is_string( string_view element )
[296]519{
[360]520        lua_getfield( m_state, -1, element.data() );
[296]521        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
522        lua_pop( m_state, 1 );
523        return result;
524}
525
[503]526bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
527{
528        NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
529        lua_getfield( m_state, -1, element.data() );
530        if ( lua_type( m_state, -1 ) != LUA_TTABLE )
531        {
532                lua_pop( m_state, 1 );
533                return false;
534        }
535        if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
536        {
537                lua_pop( m_state, 1 );
538                return false;
539        }
540        lua_pop( m_state, 1 );
541        return true;
542}
543
[216]544// state
545
[503]546lua::state::state( lua_State* state, type_database* types )
547        : state_wrapper( state, new type_data( types ), false )
[185]548{
[216]549
[185]550}
551
[503]552lua::state::state( bool load_libs /*= false*/, type_database* types )
553        : state_wrapper( nullptr, new type_data( types ), true )
[185]554{
[216]555        load_lua_library();
556        m_owner = true;
557        m_state = luaL_newstate( );
558
559        lua_pushcfunction(m_state, luaopen_base);
[490]560        lua_pushliteral(m_state, "");
[216]561        lua_call(m_state, 1, 0);
562
[333]563        {
564                // Preregister 8 references for Handle registry
565                lua_newtable(m_state);
566                NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
567                for ( uint32 i = 1; i < 8; ++i )
568                {
569                        lua_newtable(m_state);
570                        luaL_ref( m_state, LUA_REGISTRYINDEX );
571                }
572        }
573
[216]574        if ( load_libs )
575        {
576                stack_guard guard( this );
577                static const luaL_Reg lualibs[] =
578                {
579                        { "string", luaopen_string },
580                        { "table",  luaopen_table },
581                        { "math",   luaopen_math },
582                        { NULL, NULL}
583                };
584                const luaL_Reg *lib = lualibs;
585                for(; lib->func != NULL; lib++)
586                {
[228]587                        lua_pushcfunction( m_state, lib->func );
588                        lua_call(m_state, 0, 1);
589                        lua_setglobal( m_state, lib->name );
[216]590                }
[217]591                register_nova( this );
[216]592        }
593
[365]594        NV_LOG_TRACE( "Lua state created" );
[185]595}
596
[399]597int lua::state::load_string( string_view code, string_view name )
[188]598{
[365]599        NV_LOG_TRACE( "Loading Lua string '", name, "'");
[360]600        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
[188]601}
[185]602
[399]603int lua::state::load_file( string_view filename )
[216]604{
[365]605        NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
[360]606        return luaL_loadfile( m_state, filename.data() );
[216]607}
[212]608
[399]609bool lua::state::do_string( string_view code, string_view name, int rvalues )
[212]610{
[216]611        lua::stack_guard( this );
612        int result = load_string(code,name);
613        if (result)
614        {
[437]615                NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
[216]616                return false;
617        }
618        return do_current( name, rvalues ) == 0;
[212]619}
620
[399]621bool lua::state::do_file( string_view filename )
[216]622{
623        lua::stack_guard( this );
624        int result = load_file(filename);
625        if (result)
626        {
[437]627                NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
[216]628                return false;
629        }
630        return do_current( filename ) == 0;
631}
632
[399]633int lua::state::do_current( string_view name, int rvalues )
[216]634{
635        int result = lua_pcall(m_state, 0, rvalues, 0);
636        if (result)
637        {
[437]638                NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
[216]639                lua_pop( m_state, 1 );
640        }
641        return result;
642}
643
644int lua::state::get_stack_size()
645{
646        return lua_gettop( m_state );
647}
648
[9]649void lua::state::log_stack()
650{
[206]651        int top = lua_gettop(m_state);
[365]652        NV_LOG_DEBUG( "Stack dump (", top, ")");
[9]653        for ( int i = 0; i < top; ++i )
654        {
[437]655                NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
[9]656        }
657}
658
659lua_State* lua::state::get_raw()
660{
[206]661        return m_state;
[9]662}
663
[399]664lua::ref lua::state::register_object( void* o, string_view lua_name )
[77]665{
[265]666        if ( o == nullptr ) return lua::ref( lua::ref::none );
[77]667        stack_guard guard( this );
[360]668        lua_getglobal( m_state, lua_name.data() );
[206]669        if ( lua_isnil( m_state, -1 ) )
[77]670        {
[403]671                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
[77]672        }
673        deep_pointer_copy( -1, o );
[265]674        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[77]675}
[9]676
[399]677lua::ref lua::state::register_proto( string_view id, string_view storage )
[217]678{
679        stack_guard guard( this );
[360]680        lua_getglobal( m_state, storage.data() );
[217]681        if ( lua_isnil( m_state, -1 ) )
682        {
[403]683                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
[217]684        }
[360]685        lua_getfield( m_state, -1, id.data() );
[217]686        if ( lua_isnil( m_state, -1 ) )
687        {
[403]688                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
[217]689        }
[265]690        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
[217]691}
692
[399]693void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
[212]694{
695        stack_guard guard( this );
[360]696        lua_getglobal( m_state, lua_name.data() );
[212]697        if ( lua_isnil( m_state, -1 ) )
698        {
[403]699                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
[212]700        }
701        lua_pushcfunction( m_state, f );
[360]702        lua_setfield( m_state, -2, name.data() );
[212]703}
704
[265]705void lua::state::unregister_object( ref object_index )
[77]706{
[265]707        if ( !object_index.is_valid() ) return;
[77]708        stack_guard guard( this );
[265]709        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[360]710        lua_pushliteral( m_state, "__ptr" );
[206]711        lua_pushboolean( m_state, false );
712        lua_rawset( m_state, -3 );
713        lua_pop( m_state, 1 );
[265]714        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
[77]715}
716
[256]717
[77]718void lua::state::deep_pointer_copy( int index, void* obj )
719{
[490]720        index = nlua_absindex( m_state, index );
[206]721        lua_newtable( m_state );
722        lua_pushnil( m_state );
[77]723        bool has_functions = false;
724        bool has_metatable = false;
725
[206]726        while ( lua_next( m_state, index ) != 0 )
[77]727        {
[206]728                if ( lua_isfunction( m_state, -1 ) )
[77]729                        has_functions = true;
[206]730                else if ( lua_istable( m_state, -1 ) )
[77]731                {
732                        deep_pointer_copy( -1, obj );
[206]733                        lua_insert( m_state, -2 );
734                        lua_pop( m_state, 1 );
[77]735                }
[206]736                lua_pushvalue( m_state, -2 );
737                lua_insert( m_state, -2 );
738                lua_settable( m_state, -4 );
[77]739        }
740
[206]741        if ( lua_getmetatable( m_state, -2 ) )
[77]742        {
[206]743                lua_setmetatable( m_state, -2 );
[77]744                has_metatable = true;
745        }
746
747        if ( has_functions || has_metatable )
748        {
[403]749                lua_pushliteral( m_state, "__ptr" );
[206]750                lua_pushlightuserdata( m_state, obj );
751                lua_rawset( m_state, -3 );
[77]752        }
753}
[163]754
[399]755void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
[163]756{
[265]757        if ( !object_index.is_valid() ) return;
758        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
[437]759        nlua_pushstringview( m_state, metaname );
[206]760        lua_pushlightuserdata( m_state, pointer );
761        lua_rawset( m_state, -3 );
762        lua_pop( m_state, 1 );
763}
[188]764
[399]765void nv::lua::state::register_enum( string_view name, int value )
[215]766{
[262]767        lua_pushinteger( m_state, value );
[360]768        lua_setglobal( m_state, name.data() );
[215]769}
770
[511]771void nv::lua::state::register_enum( const type_entry* type )
772{
773        NV_ASSERT_ALWAYS( type, "type not found!" );
774        NV_ASSERT_ALWAYS( !type->enum_list.empty(), "type not enum!" );
775        type_database* db = type->type_db;
776        for ( auto e : type->enum_list )
777        {
778                lua_pushinteger( m_state, e.value );
779                lua_setglobal( m_state, db->resolve_name( e.name ).data() );
780        }
781}
782
[503]783void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
784{
785        m_lua_types->insert( tid, p, r );
786}
787
[399]788nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
[341]789{
790        int args = empty ? 1 : 2;
791        NV_LUA_STACK_ASSERT( m_state, -args );
792
793        if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
794        {
795                lua_pop( m_state, args );
796                return ref();
797        }
798        if (empty)
799                lua_createtable( m_state, 0, 0 );
800        else
801                nlua_deepcopy( m_state, -2 );
[437]802        nlua_pushstringview( m_state, id );
[341]803        lua_pushvalue( m_state, -2 );
804        lua_rawset( m_state, -4 );
805        lua_replace( m_state, -2 );
806        ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
807        if (!empty) lua_pop( m_state, 1 );
808        return result;
809}
810
[399]811void nv::lua::state::unregister_handle_component_impl( string_view id )
[341]812{
813        NV_LUA_STACK_ASSERT( m_state, -1 );
814
815        if ( lua_isnil( m_state, -1 ) )
816        {
817                lua_pop( m_state, 1 );
818                return;
819        }
[437]820        nlua_pushstringview( m_state, id );
[341]821        lua_pushnil( m_state );
822        lua_rawset( m_state, -3 );
823        lua_pop( m_state, 1 );
824}
825
[399]826void nv::lua::state::register_singleton( string_view name, void* o )
[341]827{
828        if ( o == nullptr ) return;
829        stack_guard guard( this );
[360]830        lua_getglobal( m_state, name.data() );
[341]831        if ( lua_isnil( m_state, -1 ) )
832        {
[403]833                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
[341]834        }
835        deep_pointer_copy( -1, o );
[360]836        lua_setglobal( m_state, name.data() );
[341]837}
838
[503]839nv::lua::state::~state()
840{
841        delete m_lua_types;
842        m_lua_types = nullptr;
843}
844
845template < typename T >
846void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object )
847{
848        T* value = reinterpret_cast<T*>( object );
849        lua_pushinteger( state->get_raw(), lua_Integer( *value ) );
850}
851
852template < typename T >
853void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object )
854{
855        T* value = reinterpret_cast<T*>( object );
856        lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) );
857}
858
859template < typename T >
860void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object )
861{
862        T* value = reinterpret_cast< T* >( object );
863        lua_pushnumber( state->get_raw(), lua_Number( *value ) );
864}
865
866static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object )
867{
868        bool* value = reinterpret_cast<bool*>( object );
869        lua_pushboolean( state->get_raw(), *value );
870}
871
872template < typename T >
873bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index )
874{
875        T* value = reinterpret_cast<T*>( object );
876        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
877        {
878                *value = T( lua_tointeger( state->get_raw(), index ) );
879                return true;
880        }
881        return false;
882}
883
884template < typename T >
885bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index )
886{
887        T* value = reinterpret_cast<T*>( object );
888        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
889        {
890                *value = T( lua_tointeger( state->get_raw(), index ) );
891                return true;
892        }
893        return false;
894}
895
896template < typename T >
897bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index )
898{
899        T* value = reinterpret_cast<T*>( object );
900        if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
901        {
902                *value = T( lua_tonumber( state->get_raw(), index ) );
903                return true;
904        }
905        return false;
906}
907
908static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index )
909{
910        bool* value = reinterpret_cast<bool*>( object );
911        if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN )
912        {
913                *value = bool( lua_toboolean( state->get_raw(), index ) );
914                return true;
915        }
916        return false;
917}
918
919
920void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
921{
922        m_type_read.assign( tid, r );
923        m_type_push.assign( tid, p );
924}
925
926void nv::lua::type_data::register_standard_types()
927{
928        insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read );
929        insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>,    nlua_rtti_signed_read<nv::sint8> );
930        insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>,   nlua_rtti_signed_read<nv::sint16> );
931        insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>,   nlua_rtti_signed_read<nv::sint32> );
932        insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>,  nlua_rtti_unsigned_read<nv::uint8> );
933        insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> );
934        insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> );
935        insert<nv::f32>   ( nlua_rtti_floating_push<nv::f32>,    nlua_rtti_floating_read<nv::f32> );
936        insert<nv::f64>   ( nlua_rtti_floating_push<nv::f64>,    nlua_rtti_floating_read<nv::f64> );
937//      insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
938//      insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
939}
Note: See TracBrowser for help on using the repository browser.