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 |
|
---|
15 | using 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 |
|
---|
24 | lua::stack_guard::stack_guard( lua::state* aL )
|
---|
25 | : L(aL), m_level( lua_gettop(aL->m_state) )
|
---|
26 | {
|
---|
27 |
|
---|
28 | }
|
---|
29 |
|
---|
30 | lua::stack_guard::stack_guard( lua::state& aL )
|
---|
31 | : L(&aL), m_level( lua_gettop(aL.m_state) )
|
---|
32 | {
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 | lua::stack_guard::~stack_guard()
|
---|
37 | {
|
---|
38 | lua_settop( L->m_state, m_level );
|
---|
39 | }
|
---|
40 |
|
---|
41 | // stack_assert
|
---|
42 | nv::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 |
|
---|
48 | nv::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 |
|
---|
54 | nv::lua::stack_assert::stack_assert( lua_State* aL, int expected )
|
---|
55 | : L(aL), m_expected( lua_gettop(aL) + expected )
|
---|
56 | {
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | lua::stack_assert::~stack_assert()
|
---|
61 | {
|
---|
62 | NV_ASSERT( lua_gettop(L) == m_expected, "Lua stack corruption detected!" );
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | // state_wrapper
|
---|
67 |
|
---|
68 | void nv::lua::state_wrapper::push_global_table()
|
---|
69 | {
|
---|
70 | nlua_pushglobaltable( m_state );
|
---|
71 | }
|
---|
72 |
|
---|
73 | void nv::lua::state_wrapper::pop_global_table()
|
---|
74 | {
|
---|
75 | lua_replace( m_state, -2 );
|
---|
76 | }
|
---|
77 |
|
---|
78 | void lua::state_wrapper::call_get()
|
---|
79 | {
|
---|
80 | lua_gettable( m_state, -2 );
|
---|
81 | }
|
---|
82 |
|
---|
83 | void lua::state_wrapper::call_get_raw()
|
---|
84 | {
|
---|
85 | lua_rawget( m_state, -2 );
|
---|
86 | }
|
---|
87 |
|
---|
88 | void 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 |
|
---|
96 | lua::state_wrapper::~state_wrapper()
|
---|
97 | {
|
---|
98 | if (m_owner)
|
---|
99 | {
|
---|
100 | lua_close( m_state );
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool 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 |
|
---|
115 | bool 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 |
|
---|
132 | int 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 |
|
---|
145 | lua::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 |
|
---|
157 | lua::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 |
|
---|
169 | lua::table_guard::~table_guard()
|
---|
170 | {
|
---|
171 | lua_settop( m_state, m_level );
|
---|
172 | }
|
---|
173 |
|
---|
174 | nv::size_t lua::table_guard::get_size()
|
---|
175 | {
|
---|
176 | return nlua_rawlen( m_state, -1 );
|
---|
177 | }
|
---|
178 |
|
---|
179 | bool 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 |
|
---|
187 | shash64 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 |
|
---|
203 | 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 | result = table.insert( string_view( str, l ) );
|
---|
213 | }
|
---|
214 | lua_pop( m_state, 1 );
|
---|
215 | return result;
|
---|
216 | }
|
---|
217 |
|
---|
218 | nv::shash64 nv::lua::table_guard::get_string( string_view element, string_table* table, uint64 defval /*= 0 */ )
|
---|
219 | {
|
---|
220 | lua_getfield( m_state, -1, element.data() );
|
---|
221 | size_t l = 0;
|
---|
222 | const char* str = nullptr;
|
---|
223 | shash64 result = shash64( defval );
|
---|
224 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
225 | {
|
---|
226 | str = lua_tolstring( m_state, -1, &l );
|
---|
227 | string_view sv( str, l );
|
---|
228 | result = table ? table->insert( sv ) : shash64( sv );
|
---|
229 | }
|
---|
230 | lua_pop( m_state, 1 );
|
---|
231 | return result;
|
---|
232 | }
|
---|
233 |
|
---|
234 | const_string lua::table_guard::get_string( string_view element, string_view defval /*= string_view() */ )
|
---|
235 | {
|
---|
236 | lua_getfield( m_state, -1, element.data() );
|
---|
237 | size_t l = 0;
|
---|
238 | const char* str = nullptr;
|
---|
239 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
240 | {
|
---|
241 | str = lua_tolstring( m_state, -1, &l );
|
---|
242 | }
|
---|
243 | else
|
---|
244 | {
|
---|
245 | l = defval.size();
|
---|
246 | str = defval.data();
|
---|
247 | }
|
---|
248 | const_string result( str, l );
|
---|
249 | lua_pop( m_state, 1 );
|
---|
250 | return result;
|
---|
251 | }
|
---|
252 |
|
---|
253 | string128 lua::table_guard::get_string128( string_view element, string_view defval )
|
---|
254 | {
|
---|
255 | lua_getfield( m_state, -1, element.data() );
|
---|
256 | size_t l = 0;
|
---|
257 | const char* str = nullptr;
|
---|
258 | if ( lua_type( m_state, -1 ) == LUA_TSTRING )
|
---|
259 | {
|
---|
260 | str = lua_tolstring( m_state, -1, &l );
|
---|
261 | }
|
---|
262 | else
|
---|
263 | {
|
---|
264 | l = defval.size();
|
---|
265 | str = defval.data();
|
---|
266 | }
|
---|
267 | string128 result( str, l );
|
---|
268 | lua_pop( m_state, 1 );
|
---|
269 | return result;
|
---|
270 | }
|
---|
271 |
|
---|
272 | char lua::table_guard::get_char( string_view element, char defval /*= "" */ )
|
---|
273 | {
|
---|
274 | lua_getfield( m_state, -1, element.data() );
|
---|
275 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && nlua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
276 | lua_pop( m_state, 1 );
|
---|
277 | return result;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int lua::table_guard::get_integer( string_view element, int defval /*= "" */ )
|
---|
281 | {
|
---|
282 | lua_getfield( m_state, -1, element.data() );
|
---|
283 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
284 | lua_pop( m_state, 1 );
|
---|
285 | return static_cast< int >( result );
|
---|
286 | }
|
---|
287 |
|
---|
288 | unsigned lua::table_guard::get_unsigned( string_view element, unsigned defval /*= "" */ )
|
---|
289 | {
|
---|
290 | lua_getfield( m_state, -1, element.data() );
|
---|
291 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? nlua_tounsigned( m_state, -1 ) : defval;
|
---|
292 | lua_pop( m_state, 1 );
|
---|
293 | return result;
|
---|
294 | }
|
---|
295 |
|
---|
296 | double lua::table_guard::get_double( string_view element, double defval /*= "" */ )
|
---|
297 | {
|
---|
298 | lua_getfield( m_state, -1, element.data() );
|
---|
299 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
300 | lua_pop( m_state, 1 );
|
---|
301 | return result;
|
---|
302 | }
|
---|
303 |
|
---|
304 | float nv::lua::table_guard::get_float( string_view element, float defval /*= 0.0 */ )
|
---|
305 | {
|
---|
306 | lua_getfield( m_state, -1, element.data() );
|
---|
307 | float result = lua_type( m_state, -1 ) == LUA_TNUMBER ? static_cast<float>( lua_tonumber( m_state, -1 ) ) : defval;
|
---|
308 | lua_pop( m_state, 1 );
|
---|
309 | return result;
|
---|
310 | }
|
---|
311 |
|
---|
312 | bool lua::table_guard::get_boolean( string_view element, bool defval /*= "" */ )
|
---|
313 | {
|
---|
314 | lua_getfield( m_state, -1, element.data() );
|
---|
315 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
316 | lua_pop( m_state, 1 );
|
---|
317 | return result;
|
---|
318 | }
|
---|
319 |
|
---|
320 | bool nv::lua::table_guard::is_table( string_view element )
|
---|
321 | {
|
---|
322 | lua_getfield( m_state, -1, element.data() );
|
---|
323 | bool result = lua_type( m_state, -1 ) == LUA_TTABLE;
|
---|
324 | lua_pop( m_state, 1 );
|
---|
325 | return result;
|
---|
326 | }
|
---|
327 |
|
---|
328 | bool nv::lua::table_guard::is_number( string_view element )
|
---|
329 | {
|
---|
330 | lua_getfield( m_state, -1, element.data() );
|
---|
331 | bool result = lua_type( m_state, -1 ) == LUA_TNUMBER;
|
---|
332 | lua_pop( m_state, 1 );
|
---|
333 | return result;
|
---|
334 | }
|
---|
335 |
|
---|
336 | bool nv::lua::table_guard::is_boolean( string_view element )
|
---|
337 | {
|
---|
338 | lua_getfield( m_state, -1, element.data() );
|
---|
339 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN;
|
---|
340 | lua_pop( m_state, 1 );
|
---|
341 | return result;
|
---|
342 | }
|
---|
343 |
|
---|
344 | bool nv::lua::table_guard::is_string( string_view element )
|
---|
345 | {
|
---|
346 | lua_getfield( m_state, -1, element.data() );
|
---|
347 | bool result = lua_type( m_state, -1 ) == LUA_TSTRING;
|
---|
348 | lua_pop( m_state, 1 );
|
---|
349 | return result;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | bool nv::lua::table_guard::read( const string_view& element, const type_entry* entry, void* object )
|
---|
354 | {
|
---|
355 | NV_ASSERT_ALWAYS( m_lua_types->get_type_database() == entry->type_db, "Type database mismatch between Lua and entry!" );
|
---|
356 | lua_getfield( m_state, -1, element.data() );
|
---|
357 | if ( lua_type( m_state, -1 ) != LUA_TTABLE )
|
---|
358 | {
|
---|
359 | lua_pop( m_state, 1 );
|
---|
360 | return false;
|
---|
361 | }
|
---|
362 | if ( !nv::lua::read_rtti_type( m_parent, entry, object, -1 ) )
|
---|
363 | {
|
---|
364 | lua_pop( m_state, 1 );
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 | lua_pop( m_state, 1 );
|
---|
368 | return true;
|
---|
369 | }
|
---|
370 |
|
---|
371 | // state
|
---|
372 |
|
---|
373 | lua::state::state( lua_State* state, type_database* types )
|
---|
374 | : state_wrapper( state, new type_data( types ), false )
|
---|
375 | {
|
---|
376 |
|
---|
377 | }
|
---|
378 |
|
---|
379 | lua::state::state( bool load_libs /*= false*/, type_database* types )
|
---|
380 | : state_wrapper( nullptr, new type_data( types ), true )
|
---|
381 | {
|
---|
382 | load_lua_library();
|
---|
383 | m_owner = true;
|
---|
384 | m_state = luaL_newstate( );
|
---|
385 |
|
---|
386 | lua_pushcfunction(m_state, luaopen_base);
|
---|
387 | lua_pushliteral(m_state, "");
|
---|
388 | lua_call(m_state, 1, 0);
|
---|
389 |
|
---|
390 | {
|
---|
391 | // Preregister 8 references for Handle registry
|
---|
392 | lua_newtable(m_state);
|
---|
393 | NV_ASSERT( luaL_ref( m_state, LUA_REGISTRYINDEX ) == 1, "First reference not 1!" );
|
---|
394 | for ( uint32 i = 1; i < 8; ++i )
|
---|
395 | {
|
---|
396 | lua_newtable(m_state);
|
---|
397 | luaL_ref( m_state, LUA_REGISTRYINDEX );
|
---|
398 | }
|
---|
399 | }
|
---|
400 |
|
---|
401 | if ( load_libs )
|
---|
402 | {
|
---|
403 | stack_guard guard( this );
|
---|
404 | static const luaL_Reg lualibs[] =
|
---|
405 | {
|
---|
406 | { "string", luaopen_string },
|
---|
407 | { "table", luaopen_table },
|
---|
408 | { "math", luaopen_math },
|
---|
409 | { NULL, NULL}
|
---|
410 | };
|
---|
411 | const luaL_Reg *lib = lualibs;
|
---|
412 | for(; lib->func != NULL; lib++)
|
---|
413 | {
|
---|
414 | lua_pushcfunction( m_state, lib->func );
|
---|
415 | lua_call(m_state, 0, 1);
|
---|
416 | lua_setglobal( m_state, lib->name );
|
---|
417 | }
|
---|
418 | register_nova( this );
|
---|
419 | }
|
---|
420 |
|
---|
421 | NV_LOG_TRACE( "Lua state created" );
|
---|
422 | }
|
---|
423 |
|
---|
424 | int lua::state::load_string( string_view code, string_view name )
|
---|
425 | {
|
---|
426 | NV_LOG_TRACE( "Loading Lua string '", name, "'");
|
---|
427 | return luaL_loadbuffer( m_state, code.data(), code.length(), name.data() );
|
---|
428 | }
|
---|
429 |
|
---|
430 | int lua::state::load_file( string_view filename )
|
---|
431 | {
|
---|
432 | NV_LOG_NOTICE( "Loading Lua file '", filename, "'");
|
---|
433 | return luaL_loadfile( m_state, filename.data() );
|
---|
434 | }
|
---|
435 |
|
---|
436 | bool lua::state::do_string( string_view code, string_view name, int rvalues )
|
---|
437 | {
|
---|
438 | lua::stack_guard( this );
|
---|
439 | int result = load_string(code,name);
|
---|
440 | if (result)
|
---|
441 | {
|
---|
442 | NV_LOG_WARNING( "Failed to load string ", name, ": ", nlua_tostringview(m_state, -1));
|
---|
443 | return false;
|
---|
444 | }
|
---|
445 | return do_current( name, rvalues ) == 0;
|
---|
446 | }
|
---|
447 |
|
---|
448 | bool lua::state::do_file( string_view filename )
|
---|
449 | {
|
---|
450 | lua::stack_guard( this );
|
---|
451 | int result = load_file(filename);
|
---|
452 | if (result)
|
---|
453 | {
|
---|
454 | NV_LOG_WARNING( "Failed to open file ", filename, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
455 | return false;
|
---|
456 | }
|
---|
457 | return do_current( filename ) == 0;
|
---|
458 | }
|
---|
459 |
|
---|
460 | int lua::state::do_current( string_view name, int rvalues )
|
---|
461 | {
|
---|
462 | int result = lua_pcall(m_state, 0, rvalues, 0);
|
---|
463 | if (result)
|
---|
464 | {
|
---|
465 | NV_LOG_WARNING( "Failed to run script ", name, ": ", nlua_tostringview( m_state, -1 ) );
|
---|
466 | lua_pop( m_state, 1 );
|
---|
467 | }
|
---|
468 | return result;
|
---|
469 | }
|
---|
470 |
|
---|
471 | int lua::state::get_stack_size()
|
---|
472 | {
|
---|
473 | return lua_gettop( m_state );
|
---|
474 | }
|
---|
475 |
|
---|
476 | void lua::state::log_stack()
|
---|
477 | {
|
---|
478 | int top = lua_gettop(m_state);
|
---|
479 | NV_LOG_DEBUG( "Stack dump (", top, ")");
|
---|
480 | for ( int i = 0; i < top; ++i )
|
---|
481 | {
|
---|
482 | NV_LOG_DEBUG( "#", i+1, " - ", lua_typename(m_state, lua_type(m_state, i+1) ), " = ", nlua_typecontent(m_state, i+1) );
|
---|
483 | }
|
---|
484 | }
|
---|
485 |
|
---|
486 | lua_State* lua::state::get_raw()
|
---|
487 | {
|
---|
488 | return m_state;
|
---|
489 | }
|
---|
490 |
|
---|
491 | lua::ref lua::state::register_object( void* o, string_view lua_name )
|
---|
492 | {
|
---|
493 | if ( o == nullptr ) return lua::ref( lua::ref::none );
|
---|
494 | stack_guard guard( this );
|
---|
495 | lua_getglobal( m_state, lua_name.data() );
|
---|
496 | if ( lua_isnil( m_state, -1 ) )
|
---|
497 | {
|
---|
498 | NV_LUA_ABORT( "state::register_object", lua_name, " type not registered!" );
|
---|
499 | }
|
---|
500 | deep_pointer_copy( -1, o );
|
---|
501 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
502 | }
|
---|
503 |
|
---|
504 | lua::ref lua::state::register_proto( string_view id, string_view storage )
|
---|
505 | {
|
---|
506 | stack_guard guard( this );
|
---|
507 | lua_getglobal( m_state, storage.data() );
|
---|
508 | if ( lua_isnil( m_state, -1 ) )
|
---|
509 | {
|
---|
510 | NV_LUA_ABORT( "state::register_proto", "\"", storage, "\" storage not registered!" );
|
---|
511 | }
|
---|
512 | lua_getfield( m_state, -1, id.data() );
|
---|
513 | if ( lua_isnil( m_state, -1 ) )
|
---|
514 | {
|
---|
515 | NV_LUA_ABORT( "state::register_proto", "\"", id, "\" not found in \"", storage, "\"!" );
|
---|
516 | }
|
---|
517 | return lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
518 | }
|
---|
519 |
|
---|
520 | void lua::state::register_native_object_method( string_view lua_name, string_view name, lfunction f )
|
---|
521 | {
|
---|
522 | stack_guard guard( this );
|
---|
523 | lua_getglobal( m_state, lua_name.data() );
|
---|
524 | if ( lua_isnil( m_state, -1 ) )
|
---|
525 | {
|
---|
526 | NV_LUA_ABORT( "state::register_native_object_method", "\"", lua_name, "\" type not registered!" );
|
---|
527 | }
|
---|
528 | lua_pushcfunction( m_state, f );
|
---|
529 | lua_setfield( m_state, -2, name.data() );
|
---|
530 | }
|
---|
531 |
|
---|
532 | void lua::state::unregister_object( ref object_index )
|
---|
533 | {
|
---|
534 | if ( !object_index.is_valid() ) return;
|
---|
535 | stack_guard guard( this );
|
---|
536 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
537 | lua_pushliteral( m_state, "__ptr" );
|
---|
538 | lua_pushboolean( m_state, false );
|
---|
539 | lua_rawset( m_state, -3 );
|
---|
540 | lua_pop( m_state, 1 );
|
---|
541 | luaL_unref( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
546 | {
|
---|
547 | index = nlua_absindex( m_state, index );
|
---|
548 | lua_newtable( m_state );
|
---|
549 | lua_pushnil( m_state );
|
---|
550 | bool has_functions = false;
|
---|
551 | bool has_metatable = false;
|
---|
552 |
|
---|
553 | while ( lua_next( m_state, index ) != 0 )
|
---|
554 | {
|
---|
555 | if ( lua_isfunction( m_state, -1 ) )
|
---|
556 | has_functions = true;
|
---|
557 | else if ( lua_istable( m_state, -1 ) )
|
---|
558 | {
|
---|
559 | deep_pointer_copy( -1, obj );
|
---|
560 | lua_insert( m_state, -2 );
|
---|
561 | lua_pop( m_state, 1 );
|
---|
562 | }
|
---|
563 | lua_pushvalue( m_state, -2 );
|
---|
564 | lua_insert( m_state, -2 );
|
---|
565 | lua_settable( m_state, -4 );
|
---|
566 | }
|
---|
567 |
|
---|
568 | if ( lua_getmetatable( m_state, -2 ) )
|
---|
569 | {
|
---|
570 | lua_setmetatable( m_state, -2 );
|
---|
571 | has_metatable = true;
|
---|
572 | }
|
---|
573 |
|
---|
574 | if ( has_functions || has_metatable )
|
---|
575 | {
|
---|
576 | lua_pushliteral( m_state, "__ptr" );
|
---|
577 | lua_pushlightuserdata( m_state, obj );
|
---|
578 | lua_rawset( m_state, -3 );
|
---|
579 | }
|
---|
580 | }
|
---|
581 |
|
---|
582 | void nv::lua::state::store_metadata( ref object_index, string_view metaname, void* pointer )
|
---|
583 | {
|
---|
584 | if ( !object_index.is_valid() ) return;
|
---|
585 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, object_index.get() );
|
---|
586 | nlua_pushstringview( m_state, metaname );
|
---|
587 | lua_pushlightuserdata( m_state, pointer );
|
---|
588 | lua_rawset( m_state, -3 );
|
---|
589 | lua_pop( m_state, 1 );
|
---|
590 | }
|
---|
591 |
|
---|
592 | void nv::lua::state::register_enum( string_view name, int value )
|
---|
593 | {
|
---|
594 | lua_pushinteger( m_state, value );
|
---|
595 | lua_setglobal( m_state, name.data() );
|
---|
596 | }
|
---|
597 |
|
---|
598 | void nv::lua::state::register_rtti_type( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
599 | {
|
---|
600 | m_lua_types->insert( tid, p, r );
|
---|
601 | }
|
---|
602 |
|
---|
603 | nv::lua::ref nv::lua::state::register_handle_component_impl( string_view id, bool empty )
|
---|
604 | {
|
---|
605 | int args = empty ? 1 : 2;
|
---|
606 | NV_LUA_STACK_ASSERT( m_state, -args );
|
---|
607 |
|
---|
608 | if ( lua_isnil( m_state, -1 ) || (!empty && lua_isnil( m_state, -2 )) )
|
---|
609 | {
|
---|
610 | lua_pop( m_state, args );
|
---|
611 | return ref();
|
---|
612 | }
|
---|
613 | if (empty)
|
---|
614 | lua_createtable( m_state, 0, 0 );
|
---|
615 | else
|
---|
616 | nlua_deepcopy( m_state, -2 );
|
---|
617 | nlua_pushstringview( m_state, id );
|
---|
618 | lua_pushvalue( m_state, -2 );
|
---|
619 | lua_rawset( m_state, -4 );
|
---|
620 | lua_replace( m_state, -2 );
|
---|
621 | ref result = lua::ref( luaL_ref( m_state, LUA_REGISTRYINDEX ) );
|
---|
622 | if (!empty) lua_pop( m_state, 1 );
|
---|
623 | return result;
|
---|
624 | }
|
---|
625 |
|
---|
626 | void nv::lua::state::unregister_handle_component_impl( string_view id )
|
---|
627 | {
|
---|
628 | NV_LUA_STACK_ASSERT( m_state, -1 );
|
---|
629 |
|
---|
630 | if ( lua_isnil( m_state, -1 ) )
|
---|
631 | {
|
---|
632 | lua_pop( m_state, 1 );
|
---|
633 | return;
|
---|
634 | }
|
---|
635 | nlua_pushstringview( m_state, id );
|
---|
636 | lua_pushnil( m_state );
|
---|
637 | lua_rawset( m_state, -3 );
|
---|
638 | lua_pop( m_state, 1 );
|
---|
639 | }
|
---|
640 |
|
---|
641 | void nv::lua::state::register_singleton( string_view name, void* o )
|
---|
642 | {
|
---|
643 | if ( o == nullptr ) return;
|
---|
644 | stack_guard guard( this );
|
---|
645 | lua_getglobal( m_state, name.data() );
|
---|
646 | if ( lua_isnil( m_state, -1 ) )
|
---|
647 | {
|
---|
648 | NV_LUA_ABORT( "state::register_singleton", "\"", name, "\" type not registered!" );
|
---|
649 | }
|
---|
650 | deep_pointer_copy( -1, o );
|
---|
651 | lua_setglobal( m_state, name.data() );
|
---|
652 | }
|
---|
653 |
|
---|
654 | nv::lua::state::~state()
|
---|
655 | {
|
---|
656 | delete m_lua_types;
|
---|
657 | m_lua_types = nullptr;
|
---|
658 | }
|
---|
659 |
|
---|
660 | template < typename T >
|
---|
661 | void nlua_rtti_signed_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
662 | {
|
---|
663 | T* value = reinterpret_cast<T*>( object );
|
---|
664 | lua_pushinteger( state->get_raw(), lua_Integer( *value ) );
|
---|
665 | }
|
---|
666 |
|
---|
667 | template < typename T >
|
---|
668 | void nlua_rtti_unsigned_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
669 | {
|
---|
670 | T* value = reinterpret_cast<T*>( object );
|
---|
671 | lua_pushinteger( state->get_raw(), lua_Unsigned( *value ) );
|
---|
672 | }
|
---|
673 |
|
---|
674 | template < typename T >
|
---|
675 | void nlua_rtti_floating_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
676 | {
|
---|
677 | T* value = reinterpret_cast< T* >( object );
|
---|
678 | lua_pushnumber( state->get_raw(), lua_Number( *value ) );
|
---|
679 | }
|
---|
680 |
|
---|
681 | static void nlua_rtti_boolean_push( nv::lua::state* state, const type_entry*, void* object )
|
---|
682 | {
|
---|
683 | bool* value = reinterpret_cast<bool*>( object );
|
---|
684 | lua_pushboolean( state->get_raw(), *value );
|
---|
685 | }
|
---|
686 |
|
---|
687 | template < typename T >
|
---|
688 | bool nlua_rtti_signed_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
689 | {
|
---|
690 | T* value = reinterpret_cast<T*>( object );
|
---|
691 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
692 | {
|
---|
693 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
694 | return true;
|
---|
695 | }
|
---|
696 | return false;
|
---|
697 | }
|
---|
698 |
|
---|
699 | template < typename T >
|
---|
700 | bool nlua_rtti_unsigned_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
701 | {
|
---|
702 | T* value = reinterpret_cast<T*>( object );
|
---|
703 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
704 | {
|
---|
705 | *value = T( lua_tointeger( state->get_raw(), index ) );
|
---|
706 | return true;
|
---|
707 | }
|
---|
708 | return false;
|
---|
709 | }
|
---|
710 |
|
---|
711 | template < typename T >
|
---|
712 | bool nlua_rtti_floating_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
713 | {
|
---|
714 | T* value = reinterpret_cast<T*>( object );
|
---|
715 | if ( lua_type( state->get_raw(), index ) == LUA_TNUMBER )
|
---|
716 | {
|
---|
717 | *value = T( lua_tonumber( state->get_raw(), index ) );
|
---|
718 | return true;
|
---|
719 | }
|
---|
720 | return false;
|
---|
721 | }
|
---|
722 |
|
---|
723 | static bool nlua_rtti_boolean_read( nv::lua::state* state, const type_entry*, void* object, int index )
|
---|
724 | {
|
---|
725 | bool* value = reinterpret_cast<bool*>( object );
|
---|
726 | if ( lua_type( state->get_raw(), index ) == LUA_TBOOLEAN )
|
---|
727 | {
|
---|
728 | *value = bool( lua_toboolean( state->get_raw(), index ) );
|
---|
729 | return true;
|
---|
730 | }
|
---|
731 | return false;
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | void nv::lua::type_data::insert( thash64 tid, lua_rtti_push_function p, lua_rtti_read_function r )
|
---|
736 | {
|
---|
737 | m_type_read.assign( tid, r );
|
---|
738 | m_type_push.assign( tid, p );
|
---|
739 | }
|
---|
740 |
|
---|
741 | void nv::lua::type_data::register_standard_types()
|
---|
742 | {
|
---|
743 | insert<bool>( nlua_rtti_boolean_push, nlua_rtti_boolean_read );
|
---|
744 | insert<nv::sint8> ( nlua_rtti_signed_push<nv::sint8>, nlua_rtti_signed_read<nv::sint8> );
|
---|
745 | insert<nv::sint16>( nlua_rtti_signed_push<nv::sint16>, nlua_rtti_signed_read<nv::sint16> );
|
---|
746 | insert<nv::sint32>( nlua_rtti_signed_push<nv::sint32>, nlua_rtti_signed_read<nv::sint32> );
|
---|
747 | insert<nv::uint8> ( nlua_rtti_unsigned_push<nv::uint8>, nlua_rtti_unsigned_read<nv::uint8> );
|
---|
748 | insert<nv::uint16>( nlua_rtti_unsigned_push<nv::uint16>, nlua_rtti_unsigned_read<nv::uint16> );
|
---|
749 | insert<nv::uint32>( nlua_rtti_unsigned_push<nv::uint32>, nlua_rtti_unsigned_read<nv::uint32> );
|
---|
750 | insert<nv::f32> ( nlua_rtti_floating_push<nv::f32>, nlua_rtti_floating_read<nv::f32> );
|
---|
751 | insert<nv::f64> ( nlua_rtti_floating_push<nv::f64>, nlua_rtti_floating_read<nv::f64> );
|
---|
752 | // insert<nv::sint64>( nlua_rtti_floating_push<nv::sint64>, nlua_rtti_floating_read<nv::sint64> );
|
---|
753 | // insert<nv::uint64>( nlua_rtti_floating_push<nv::uint64>, nlua_rtti_floating_read<nv::uint64> );
|
---|
754 | }
|
---|