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

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