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
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
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
366char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
367{
368        lua_getfield( m_state, -1, element.data() );
369        char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
370        lua_pop( m_state, 1 );
371        return result;
372}
373
374int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
375{
376        lua_getfield( m_state, -1, element.data() );
377        lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
378        lua_pop( m_state, 1 );
379        return static_cast< int >( result );
380}
381
382unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
383{
384        lua_getfield( m_state, -1, element.data() );
385        unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
386        lua_pop( m_state, 1 );
387        return result;
388}
389
390double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
391{
392        lua_getfield( m_state, -1, element.data() );
393        double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
394        lua_pop( m_state, 1 );
395        return result;
396}
397
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
446float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
447{
448        lua_getfield( m_state, -1, element.data() );
449        float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
450        lua_pop( m_state, 1 );
451        return result;
452}
453
454bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
455{
456        lua_getfield( m_state, -1, element.data() );
457        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
458        lua_pop( m_state, 1 );
459        return result;
460}
461
462bool nv::lua::table_guard::is_table( string_view element )
463{
464        lua_getfield( m_state, -1, element.data() );
465        bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
466        lua_pop( m_state, 1 );
467        return result;
468}
469
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
502bool nv::lua::table_guard::is_number( string_view element )
503{
504        lua_getfield( m_state, -1, element.data() );
505        bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
506        lua_pop( m_state, 1 );
507        return result;
508}
509
510bool nv::lua::table_guard::is_boolean( string_view element )
511{
512        lua_getfield( m_state, -1, element.data() );
513        bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
514        lua_pop( m_state, 1 );
515        return result;
516}
517
518bool nv::lua::table_guard::is_string( string_view element )
519{
520        lua_getfield( m_state, -1, element.data() );
521        bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
522        lua_pop( m_state, 1 );
523        return result;
524}
525
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
544// state
545
546lua::state::state( lua_State* state, type_database* types )
547        : state_wrapper( state, new type_data( types ), false )
548{
549
550}
551
552lua::state::state( bool load_libs /*= false*/, type_database* types )
553        : state_wrapper( nullptr, new type_data( types ), true )
554{
555        load_lua_library();
556        m_owner = true;
557        m_state = luaL_newstate( );
558
559        lua_pushcfunction(m_state, luaopen_base);
560        lua_pushliteral(m_state, "");
561        lua_call(m_state, 1, 0);
562
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
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                {
587                        lua_pushcfunction( m_state, lib->func );
588                        lua_call(m_state, 0, 1);
589                        lua_setglobal( m_state, lib->name );
590                }
591                register_nova( this );
592        }
593
594        NV_LOG_TRACE( "Lua state created" );
595}
596
597int lua::state::load_string( string_view code, string_view name )
598{
599        NV_LOG_TRACE( "Loading Lua string '", name, "'");
600        return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
601}
602
603int lua::state::load_file( string_view filename )
604{
605        NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
606        return luaL_loadfile( m_state, filename.data() );
607}
608
609bool lua::state::do_string( string_view code, string_view name, int rvalues )
610{
611        lua::stack_guard( this );
612        int result = load_string(code,name);
613        if (result)
614        {
615                NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
616                return false;
617        }
618        return do_current( name, rvalues ) == 0;
619}
620
621bool lua::state::do_file( string_view filename )
622{
623        lua::stack_guard( this );
624        int result = load_file(filename);
625        if (result)
626        {
627                NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
628                return false;
629        }
630        return do_current( filename ) == 0;
631}
632
633int lua::state::do_current( string_view name, int rvalues )
634{
635        int result = lua_pcall(m_state, 0, rvalues, 0);
636        if (result)
637        {
638                NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
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
649void lua::state::log_stack()
650{
651        int top = lua_gettop(m_state);
652        NV_LOG_DEBUG( "Stack dump (", top, ")");
653        for ( int i = 0; i < top; ++i )
654        {
655                NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
656        }
657}
658
659lua_State* lua::state::get_raw()
660{
661        return m_state;
662}
663
664lua::ref lua::state::register_object( void* o, string_view lua_name )
665{
666        if ( o == nullptr ) return lua::ref( lua::ref::none );
667        stack_guard guard( this );
668        lua_getglobal( m_state, lua_name.data() );
669        if ( lua_isnil( m_state, -1 ) )
670        {
671                NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
672        }
673        deep_pointer_copy( -1, o );
674        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
675}
676
677lua::ref lua::state::register_proto( string_view id, string_view storage )
678{
679        stack_guard guard( this );
680        lua_getglobal( m_state, storage.data() );
681        if ( lua_isnil( m_state, -1 ) )
682        {
683                NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
684        }
685        lua_getfield( m_state, -1, id.data() );
686        if ( lua_isnil( m_state, -1 ) )
687        {
688                NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
689        }
690        return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
691}
692
693void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
694{
695        stack_guard guard( this );
696        lua_getglobal( m_state, lua_name.data() );
697        if ( lua_isnil( m_state, -1 ) )
698        {
699                NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
700        }
701        lua_pushcfunction( m_state, f );
702        lua_setfield( m_state, -2, name.data() );
703}
704
705void lua::state::unregister_object( ref object_index )
706{
707        if ( !object_index.is_valid() ) return;
708        stack_guard guard( this );
709        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
710        lua_pushliteral( m_state, "__ptr" );
711        lua_pushboolean( m_state, false );
712        lua_rawset( m_state, -3 );
713        lua_pop( m_state, 1 );
714        luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
715}
716
717
718void lua::state::deep_pointer_copy( int index, void* obj )
719{
720        index = nlua_absindex( m_state, index );
721        lua_newtable( m_state );
722        lua_pushnil( m_state );
723        bool has_functions = false;
724        bool has_metatable = false;
725
726        while ( lua_next( m_state, index ) != 0 )
727        {
728                if ( lua_isfunction( m_state, -1 ) )
729                        has_functions = true;
730                else if ( lua_istable( m_state, -1 ) )
731                {
732                        deep_pointer_copy( -1, obj );
733                        lua_insert( m_state, -2 );
734                        lua_pop( m_state, 1 );
735                }
736                lua_pushvalue( m_state, -2 );
737                lua_insert( m_state, -2 );
738                lua_settable( m_state, -4 );
739        }
740
741        if ( lua_getmetatable( m_state, -2 ) )
742        {
743                lua_setmetatable( m_state, -2 );
744                has_metatable = true;
745        }
746
747        if ( has_functions || has_metatable )
748        {
749                lua_pushliteral( m_state, "__ptr" );
750                lua_pushlightuserdata( m_state, obj );
751                lua_rawset( m_state, -3 );
752        }
753}
754
755void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
756{
757        if ( !object_index.is_valid() ) return;
758        lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
759        nlua_pushstringview( m_state, metaname );
760        lua_pushlightuserdata( m_state, pointer );
761        lua_rawset( m_state, -3 );
762        lua_pop( m_state, 1 );
763}
764
765void nv::lua::state::register_enum( string_view name, int value )
766{
767        lua_pushinteger( m_state, value );
768        lua_setglobal( m_state, name.data() );
769}
770
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
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
788nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
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 );
802        nlua_pushstringview( m_state, id );
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
811void nv::lua::state::unregister_handle_component_impl( string_view id )
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        }
820        nlua_pushstringview( m_state, id );
821        lua_pushnil( m_state );
822        lua_rawset( m_state, -3 );
823        lua_pop( m_state, 1 );
824}
825
826void nv::lua::state::register_singleton( string_view name, void* o )
827{
828        if ( o == nullptr ) return;
829        stack_guard guard( this );
830        lua_getglobal( m_state, name.data() );
831        if ( lua_isnil( m_state, -1 ) )
832        {
833                NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
834        }
835        deep_pointer_copy( -1, o );
836        lua_setglobal( m_state, name.data() );
837}
838
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.