1 | // Copyright (C) 2012-2013 ChaosForge / Kornel Kisielewicz
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of NV Libraries.
|
---|
5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
6 |
|
---|
7 | #include "nv/lua/lua_state.hh"
|
---|
8 |
|
---|
9 | #include "nv/lua/lua_raw.hh"
|
---|
10 | #include "nv/logging.hh"
|
---|
11 | #include "nv/string.hh"
|
---|
12 | #include "nv/root.hh"
|
---|
13 | #include "nv/types.hh"
|
---|
14 |
|
---|
15 | using namespace nv;
|
---|
16 |
|
---|
17 | // stack_guard
|
---|
18 |
|
---|
19 | lua::stack_guard::stack_guard( lua::state* aL )
|
---|
20 | : L(aL), m_level( lua_gettop(aL->m_state) )
|
---|
21 | {
|
---|
22 |
|
---|
23 | }
|
---|
24 |
|
---|
25 | lua::stack_guard::stack_guard( lua::state& aL )
|
---|
26 | : L(&aL), m_level( lua_gettop(aL.m_state) )
|
---|
27 | {
|
---|
28 |
|
---|
29 | }
|
---|
30 |
|
---|
31 | lua::stack_guard::~stack_guard()
|
---|
32 | {
|
---|
33 | lua_settop( L->m_state, m_level );
|
---|
34 | }
|
---|
35 |
|
---|
36 | // state_wrapper
|
---|
37 |
|
---|
38 | void nv::lua::state_wrapper::push_global_table()
|
---|
39 | {
|
---|
40 | lua_pushglobaltable( m_state );
|
---|
41 | }
|
---|
42 |
|
---|
43 | void nv::lua::state_wrapper::pop_global_table()
|
---|
44 | {
|
---|
45 | lua_replace( m_state, -2 );
|
---|
46 | }
|
---|
47 |
|
---|
48 | void lua::state_wrapper::call_get()
|
---|
49 | {
|
---|
50 | lua_gettable( m_state, -2 );
|
---|
51 | }
|
---|
52 |
|
---|
53 | void lua::state_wrapper::call_get_raw()
|
---|
54 | {
|
---|
55 | lua_rawget( m_state, -2 );
|
---|
56 | }
|
---|
57 |
|
---|
58 | void lua::state_wrapper::register_native_function( lfunction f, const char* name )
|
---|
59 | {
|
---|
60 | if ( m_global ) push_global_table();
|
---|
61 | lua_pushcfunction( m_state, f );
|
---|
62 | lua_setfield( m_state, -2, name );
|
---|
63 | if ( m_global ) pop_global_table();
|
---|
64 | }
|
---|
65 |
|
---|
66 | lua::state_wrapper::~state_wrapper()
|
---|
67 | {
|
---|
68 | if (m_owner)
|
---|
69 | {
|
---|
70 | lua_close( m_state );
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | bool nv::lua::state_wrapper::is_defined( const path& p, bool global )
|
---|
75 | {
|
---|
76 | if ( !p.resolve( m_state, global ) )
|
---|
77 | {
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 | bool result = !lua_isnil( m_state, -1 );
|
---|
81 | lua_pop( m_state, 1 );
|
---|
82 | return result;
|
---|
83 | }
|
---|
84 |
|
---|
85 | bool nv::lua::state_wrapper::push_function( const path& p, bool global )
|
---|
86 | {
|
---|
87 | if ( !p.resolve( m_state, global ) )
|
---|
88 | {
|
---|
89 | NV_LOG( LOG_ERROR, "Lua error : not a valid path - " + p.to_string() );
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if ( !lua_isfunction( m_state, -1 ) )
|
---|
94 | {
|
---|
95 | lua_pop( m_state, 1 );
|
---|
96 | NV_LOG( LOG_ERROR, "Lua error : not a valid function - " + p.to_string() );
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | int nv::lua::state_wrapper::call_function( int nargs, int nresults )
|
---|
103 | {
|
---|
104 | int status = lua_pcall( m_state, nargs, nresults, 0 );
|
---|
105 | if ( status != 0 )
|
---|
106 | {
|
---|
107 | std::string error = lua_tostring( m_state, -1 );
|
---|
108 | lua_pop( m_state, 1 );
|
---|
109 | NV_LOG( LOG_ERROR, "Lua error : " << error )
|
---|
110 | }
|
---|
111 | return status;
|
---|
112 | }
|
---|
113 |
|
---|
114 | // table_guard
|
---|
115 |
|
---|
116 | lua::table_guard::table_guard( lua::state* lstate, const path& p, bool global )
|
---|
117 | : state_wrapper( lstate->get_raw(), false ), m_level(0)
|
---|
118 | {
|
---|
119 | m_global = false;
|
---|
120 | m_level = lua_gettop( m_state );
|
---|
121 | if ( !p.resolve( m_state, global ) )
|
---|
122 | {
|
---|
123 | // TODO : error handling
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | lua::table_guard::table_guard( const table_guard& parent, const path& p )
|
---|
128 | : state_wrapper( parent.m_state, false ), m_level(0)
|
---|
129 | {
|
---|
130 | m_global = false;
|
---|
131 | m_level = lua_gettop( m_state );
|
---|
132 | if ( !p.resolve( m_state, false ) )
|
---|
133 | {
|
---|
134 | // TODO : error handling
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | lua::table_guard::~table_guard()
|
---|
139 | {
|
---|
140 | lua_settop( m_state, m_level );
|
---|
141 | }
|
---|
142 |
|
---|
143 | size_t lua::table_guard::get_size()
|
---|
144 | {
|
---|
145 | return lua_rawlen( m_state, -1 );
|
---|
146 | }
|
---|
147 |
|
---|
148 | bool lua::table_guard::has_field( const string& element )
|
---|
149 | {
|
---|
150 | lua_getfield( m_state, -1, element.c_str() );
|
---|
151 | bool result = lua_isnil( m_state, -1 );
|
---|
152 | lua_pop( m_state, 1 );
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 |
|
---|
156 | string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
|
---|
157 | {
|
---|
158 | lua_getfield( m_state, -1, element.c_str() );
|
---|
159 | string result( ( lua_type( m_state, -1 ) == LUA_TSTRING ) ? lua_tostring( m_state, -1 ) : defval );
|
---|
160 | lua_pop( m_state, 1 );
|
---|
161 | return result;
|
---|
162 | }
|
---|
163 |
|
---|
164 | char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
|
---|
165 | {
|
---|
166 | lua_getfield( m_state, -1, element.c_str() );
|
---|
167 | char result = ( lua_type( m_state, -1 ) == LUA_TSTRING && lua_rawlen( m_state, -1 ) > 0 ) ? lua_tostring( m_state, -1 )[0] : defval;
|
---|
168 | lua_pop( m_state, 1 );
|
---|
169 | return result;
|
---|
170 | }
|
---|
171 |
|
---|
172 | int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
|
---|
173 | {
|
---|
174 | lua_getfield( m_state, -1, element.c_str() );
|
---|
175 | lua_Integer result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tointeger( m_state, -1 ) : defval;
|
---|
176 | lua_pop( m_state, 1 );
|
---|
177 | return static_cast< int >( result );
|
---|
178 | }
|
---|
179 |
|
---|
180 | unsigned lua::table_guard::get_unsigned( const string& element, unsigned defval /*= "" */ )
|
---|
181 | {
|
---|
182 | lua_getfield( m_state, -1, element.c_str() );
|
---|
183 | unsigned result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tounsigned( m_state, -1 ) : defval;
|
---|
184 | lua_pop( m_state, 1 );
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 |
|
---|
188 | double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
|
---|
189 | {
|
---|
190 | lua_getfield( m_state, -1, element.c_str() );
|
---|
191 | double result = lua_type( m_state, -1 ) == LUA_TNUMBER ? lua_tonumber( m_state, -1 ) : defval;
|
---|
192 | lua_pop( m_state, 1 );
|
---|
193 | return result;
|
---|
194 | }
|
---|
195 |
|
---|
196 | bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
|
---|
197 | {
|
---|
198 | lua_getfield( m_state, -1, element.c_str() );
|
---|
199 | bool result = lua_type( m_state, -1 ) == LUA_TBOOLEAN ? lua_toboolean( m_state, -1 ) != 0 : defval;
|
---|
200 | lua_pop( m_state, 1 );
|
---|
201 | return result;
|
---|
202 | }
|
---|
203 |
|
---|
204 | // state
|
---|
205 |
|
---|
206 | lua::state::state( lua_State* state ) : state_wrapper( state, false ), m_type_database( nullptr )
|
---|
207 | {
|
---|
208 |
|
---|
209 | }
|
---|
210 |
|
---|
211 | lua::state::state( bool load_libs /*= false*/ ) : state_wrapper( nullptr, true ), m_type_database( nullptr )
|
---|
212 | {
|
---|
213 | load_lua_library();
|
---|
214 | m_owner = true;
|
---|
215 | m_state = luaL_newstate( );
|
---|
216 |
|
---|
217 | lua_pushcfunction(m_state, luaopen_base);
|
---|
218 | lua_pushliteral(m_state, LUA_TABLIBNAME);
|
---|
219 | lua_call(m_state, 1, 0);
|
---|
220 |
|
---|
221 | if ( load_libs )
|
---|
222 | {
|
---|
223 | m_type_database = new type_database();
|
---|
224 | stack_guard guard( this );
|
---|
225 | static const luaL_Reg lualibs[] =
|
---|
226 | {
|
---|
227 | { "string", luaopen_string },
|
---|
228 | { "table", luaopen_table },
|
---|
229 | { "math", luaopen_math },
|
---|
230 | { NULL, NULL}
|
---|
231 | };
|
---|
232 | const luaL_Reg *lib = lualibs;
|
---|
233 | for(; lib->func != NULL; lib++)
|
---|
234 | {
|
---|
235 | lib->func( m_state );
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | NV_LOG( nv::LOG_TRACE, "Lua state created" );
|
---|
240 | }
|
---|
241 |
|
---|
242 | int lua::state::load_string( const std::string& code, const std::string& name )
|
---|
243 | {
|
---|
244 | NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'");
|
---|
245 | return luaL_loadbuffer( m_state, code.c_str(), code.length(), name.c_str() );
|
---|
246 | }
|
---|
247 |
|
---|
248 | int lua::state::load_stream( std::istream& stream, const std::string& name )
|
---|
249 | {
|
---|
250 | NV_LOG( nv::LOG_NOTICE, "Loading Lua stream '" << name << "'");
|
---|
251 | return load_string( std::string(
|
---|
252 | (std::istreambuf_iterator<char>(stream)),
|
---|
253 | std::istreambuf_iterator<char>()), name );
|
---|
254 | }
|
---|
255 |
|
---|
256 | int lua::state::load_file( const std::string& filename )
|
---|
257 | {
|
---|
258 | NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'");
|
---|
259 | return luaL_loadfile( m_state, filename.c_str() );
|
---|
260 | }
|
---|
261 |
|
---|
262 | bool lua::state::do_string( const std::string& code, const std::string& name, int rvalues )
|
---|
263 | {
|
---|
264 | lua::stack_guard( this );
|
---|
265 | int result = load_string(code,name);
|
---|
266 | if (result)
|
---|
267 | {
|
---|
268 | NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(m_state, -1));
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 | return do_current( name, rvalues ) == 0;
|
---|
272 | }
|
---|
273 |
|
---|
274 | bool lua::state::do_stream( std::istream& stream, const std::string& name )
|
---|
275 | {
|
---|
276 | lua::stack_guard( this );
|
---|
277 | int result = load_stream(stream,name);
|
---|
278 | if (result)
|
---|
279 | {
|
---|
280 | NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(m_state, -1));
|
---|
281 | return false;
|
---|
282 | }
|
---|
283 | return do_current( name ) == 0;
|
---|
284 | }
|
---|
285 |
|
---|
286 | bool lua::state::do_file( const std::string& filename )
|
---|
287 | {
|
---|
288 | lua::stack_guard( this );
|
---|
289 | int result = load_file(filename);
|
---|
290 | if (result)
|
---|
291 | {
|
---|
292 | NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(m_state, -1));
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 | return do_current( filename ) == 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | int lua::state::do_current( const std::string& name, int rvalues )
|
---|
299 | {
|
---|
300 | int result = lua_pcall(m_state, 0, rvalues, 0);
|
---|
301 | if (result)
|
---|
302 | {
|
---|
303 | NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(m_state, -1));
|
---|
304 | lua_pop( m_state, 1 );
|
---|
305 | }
|
---|
306 | return result;
|
---|
307 | }
|
---|
308 |
|
---|
309 | int lua::state::get_stack_size()
|
---|
310 | {
|
---|
311 | return lua_gettop( m_state );
|
---|
312 | }
|
---|
313 |
|
---|
314 | void lua::state::log_stack()
|
---|
315 | {
|
---|
316 | int top = lua_gettop(m_state);
|
---|
317 | NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")");
|
---|
318 | for ( int i = 0; i < top; ++i )
|
---|
319 | {
|
---|
320 | NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(m_state, lua_type(m_state, i+1) ) << " = " << nlua_typecontent(m_state, i+1) );
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | lua_State* lua::state::get_raw()
|
---|
325 | {
|
---|
326 | return m_state;
|
---|
327 | }
|
---|
328 |
|
---|
329 | lua::reference lua::state::register_object( object * o, const char* lua_name )
|
---|
330 | {
|
---|
331 | if ( o == nullptr ) return ref_none;
|
---|
332 | stack_guard guard( this );
|
---|
333 | lua_getglobal( m_state, lua_name );
|
---|
334 | if ( lua_isnil( m_state, -1 ) )
|
---|
335 | {
|
---|
336 | NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
|
---|
337 | }
|
---|
338 | deep_pointer_copy( -1, o );
|
---|
339 | return luaL_ref( m_state, LUA_REGISTRYINDEX );
|
---|
340 | }
|
---|
341 |
|
---|
342 | lua::reference lua::state::register_object( object * o )
|
---|
343 | {
|
---|
344 | if ( o == nullptr || m_type_database == nullptr ) return ref_none;
|
---|
345 | type_entry* t = m_type_database->get_type(typeid(*o));
|
---|
346 | if ( t == nullptr ) return ref_none;
|
---|
347 | return register_object( o, t->name.c_str() );
|
---|
348 | }
|
---|
349 |
|
---|
350 | void lua::state::register_native_object_method( const char* lua_name, const char* name, lfunction f )
|
---|
351 | {
|
---|
352 | stack_guard guard( this );
|
---|
353 | lua_getglobal( m_state, lua_name );
|
---|
354 | if ( lua_isnil( m_state, -1 ) )
|
---|
355 | {
|
---|
356 | NV_THROW( runtime_error, std::string( lua_name ) + " type not registered!" );
|
---|
357 | }
|
---|
358 | lua_pushcfunction( m_state, f );
|
---|
359 | lua_setfield( m_state, -2, name );
|
---|
360 | }
|
---|
361 |
|
---|
362 | void lua::state::unregister_object( object * o )
|
---|
363 | {
|
---|
364 | if (!o) return;
|
---|
365 | stack_guard guard( this );
|
---|
366 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
367 | lua_pushstring( m_state, "__ptr" );
|
---|
368 | lua_pushboolean( m_state, false );
|
---|
369 | lua_rawset( m_state, -3 );
|
---|
370 | lua_pop( m_state, 1 );
|
---|
371 | luaL_unref( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
372 | }
|
---|
373 |
|
---|
374 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
375 | {
|
---|
376 | index = lua_absindex( m_state, index );
|
---|
377 | lua_newtable( m_state );
|
---|
378 | lua_pushnil( m_state );
|
---|
379 | bool has_functions = false;
|
---|
380 | bool has_metatable = false;
|
---|
381 |
|
---|
382 | while ( lua_next( m_state, index ) != 0 )
|
---|
383 | {
|
---|
384 | if ( lua_isfunction( m_state, -1 ) )
|
---|
385 | has_functions = true;
|
---|
386 | else if ( lua_istable( m_state, -1 ) )
|
---|
387 | {
|
---|
388 | deep_pointer_copy( -1, obj );
|
---|
389 | lua_insert( m_state, -2 );
|
---|
390 | lua_pop( m_state, 1 );
|
---|
391 | }
|
---|
392 | lua_pushvalue( m_state, -2 );
|
---|
393 | lua_insert( m_state, -2 );
|
---|
394 | lua_settable( m_state, -4 );
|
---|
395 | }
|
---|
396 |
|
---|
397 | if ( lua_getmetatable( m_state, -2 ) )
|
---|
398 | {
|
---|
399 | lua_setmetatable( m_state, -2 );
|
---|
400 | has_metatable = true;
|
---|
401 | }
|
---|
402 |
|
---|
403 | if ( has_functions || has_metatable )
|
---|
404 | {
|
---|
405 | lua_pushstring( m_state, "__ptr" );
|
---|
406 | lua_pushlightuserdata( m_state, obj );
|
---|
407 | lua_rawset( m_state, -3 );
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | void lua::state::register_enum_values( const std::string& name, const std::string& prefix /*= std::string() */ )
|
---|
412 | {
|
---|
413 | type_entry* et = m_type_database->get_type( name );
|
---|
414 |
|
---|
415 | for ( const auto& entry : et->enum_list )
|
---|
416 | {
|
---|
417 | lua_pushinteger( m_state, entry.value );
|
---|
418 | if ( prefix.empty() )
|
---|
419 | {
|
---|
420 | lua_setglobal( m_state, entry.name.c_str() );
|
---|
421 | }
|
---|
422 | else
|
---|
423 | {
|
---|
424 | lua_setglobal( m_state, ( prefix + entry.name ).c_str() );
|
---|
425 | }
|
---|
426 | }
|
---|
427 | }
|
---|
428 |
|
---|
429 | void nv::lua::state::store_metadata( object* o, const std::string& metaname, void* pointer )
|
---|
430 | {
|
---|
431 | if (!o) return;
|
---|
432 | stack_guard guard( this );
|
---|
433 | lua_rawgeti( m_state, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
434 | lua_pushstring( m_state, metaname.c_str() );
|
---|
435 | lua_pushlightuserdata( m_state, pointer );
|
---|
436 | lua_rawset( m_state, -3 );
|
---|
437 | lua_pop( m_state, 1 );
|
---|
438 | }
|
---|
439 |
|
---|
440 | nv::lua::state::~state()
|
---|
441 | {
|
---|
442 | if (m_type_database != nullptr )
|
---|
443 | {
|
---|
444 | delete m_type_database;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|