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 | lua::stack_guard::stack_guard( lua::state* L )
|
---|
18 | : L(L), m_level( lua_gettop(L->L) )
|
---|
19 | {
|
---|
20 |
|
---|
21 | }
|
---|
22 |
|
---|
23 | lua::stack_guard::stack_guard( lua::state& L )
|
---|
24 | : L(&L), m_level( lua_gettop((&L)->L) )
|
---|
25 | {
|
---|
26 |
|
---|
27 | }
|
---|
28 |
|
---|
29 | lua::stack_guard::~stack_guard()
|
---|
30 | {
|
---|
31 | lua_settop( L->L, m_level );
|
---|
32 | }
|
---|
33 |
|
---|
34 | lua::state::state( bool load_libs /*= false*/ )
|
---|
35 | {
|
---|
36 | load_lua_library();
|
---|
37 | m_owner = true;
|
---|
38 | L = luaL_newstate( );
|
---|
39 |
|
---|
40 | lua_pushcfunction(L, luaopen_base);
|
---|
41 | lua_pushliteral(L, LUA_TABLIBNAME);
|
---|
42 | lua_call(L, 1, 0);
|
---|
43 |
|
---|
44 | if ( load_libs )
|
---|
45 | {
|
---|
46 | stack_guard guard( this );
|
---|
47 | static const luaL_Reg lualibs[] =
|
---|
48 | {
|
---|
49 | { "string", luaopen_string },
|
---|
50 | { "table", luaopen_table },
|
---|
51 | { "math", luaopen_math },
|
---|
52 | { NULL, NULL}
|
---|
53 | };
|
---|
54 | const luaL_Reg *lib = lualibs;
|
---|
55 | for(; lib->func != NULL; lib++)
|
---|
56 | {
|
---|
57 | lib->func( L );
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | NV_LOG( nv::LOG_TRACE, "Lua state created" );
|
---|
62 | }
|
---|
63 |
|
---|
64 | int lua::state::load_string( const std::string& code, const std::string& name )
|
---|
65 | {
|
---|
66 | NV_LOG( nv::LOG_TRACE, "Loading Lua string '" << name << "'");
|
---|
67 | return luaL_loadbuffer( L, code.c_str(), code.length(), name.c_str() );
|
---|
68 | }
|
---|
69 |
|
---|
70 | int lua::state::load_stream( std::istream& stream, const std::string& name )
|
---|
71 | {
|
---|
72 | NV_LOG( nv::LOG_NOTICE, "Loading Lua stream '" << name << "'");
|
---|
73 | return load_string( std::string(
|
---|
74 | (std::istreambuf_iterator<char>(stream)),
|
---|
75 | std::istreambuf_iterator<char>()), name );
|
---|
76 | }
|
---|
77 |
|
---|
78 | int lua::state::load_file( const std::string& filename )
|
---|
79 | {
|
---|
80 | NV_LOG( nv::LOG_NOTICE, "Loading Lua file '" << filename << "'");
|
---|
81 | return luaL_loadfile( L, filename.c_str() );
|
---|
82 | }
|
---|
83 |
|
---|
84 | bool lua::state::do_string( const std::string& code, const std::string& name, int rvalues )
|
---|
85 | {
|
---|
86 | lua::stack_guard( this );
|
---|
87 | int result = load_string(code,name);
|
---|
88 | if (result)
|
---|
89 | {
|
---|
90 | NV_LOG( nv::LOG_WARNING, "Failed to load string " << name << ": " << lua_tostring(L, -1));
|
---|
91 | return false;
|
---|
92 | }
|
---|
93 | return do_current( name, rvalues ) == 0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | bool lua::state::do_stream( std::istream& stream, const std::string& name )
|
---|
97 | {
|
---|
98 | lua::stack_guard( this );
|
---|
99 | int result = load_stream(stream,name);
|
---|
100 | if (result)
|
---|
101 | {
|
---|
102 | NV_LOG( nv::LOG_WARNING, "Failed to open stream " << name << ": " << lua_tostring(L, -1));
|
---|
103 | return false;
|
---|
104 | }
|
---|
105 | return do_current( name ) == 0;
|
---|
106 | }
|
---|
107 |
|
---|
108 | bool lua::state::do_file( const std::string& filename )
|
---|
109 | {
|
---|
110 | lua::stack_guard( this );
|
---|
111 | int result = load_file(filename);
|
---|
112 | if (result)
|
---|
113 | {
|
---|
114 | NV_LOG( nv::LOG_WARNING, "Failed to open file " << filename << ": " << lua_tostring(L, -1));
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | return do_current( filename ) == 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | int lua::state::do_current( const std::string& name, int rvalues )
|
---|
121 | {
|
---|
122 | int result = lua_pcall(L, 0, rvalues, 0);
|
---|
123 | if (result)
|
---|
124 | {
|
---|
125 | NV_LOG( nv::LOG_WARNING, "Failed to run script " << name << ": " << lua_tostring(L, -1));
|
---|
126 | lua_pop( L, 1 );
|
---|
127 | }
|
---|
128 | return result;
|
---|
129 | }
|
---|
130 |
|
---|
131 | lua::state::~state()
|
---|
132 | {
|
---|
133 | if (m_owner)
|
---|
134 | {
|
---|
135 | lua_close( L );
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | bool lua::state::push( const std::string& path, bool global )
|
---|
140 | {
|
---|
141 | size_t point = path.find('.');
|
---|
142 |
|
---|
143 | if (point == std::string::npos)
|
---|
144 | {
|
---|
145 | if (global)
|
---|
146 | {
|
---|
147 | lua_getglobal( L, path.c_str() );
|
---|
148 | }
|
---|
149 | else
|
---|
150 | {
|
---|
151 | lua_getfield( L, -1, path.c_str() );
|
---|
152 | }
|
---|
153 | return !lua_isnil( L, -1 );
|
---|
154 | }
|
---|
155 |
|
---|
156 | size_t idx = 0;
|
---|
157 | size_t start = 0;
|
---|
158 |
|
---|
159 | while( point != std::string::npos )
|
---|
160 | {
|
---|
161 | if (idx == 0)
|
---|
162 | {
|
---|
163 | if (global)
|
---|
164 | {
|
---|
165 | lua_getglobal( L, path.substr(start,point-start).c_str() );
|
---|
166 | }
|
---|
167 | else
|
---|
168 | {
|
---|
169 | lua_getfield( L, -1, path.substr(start,point-start).c_str() );
|
---|
170 | }
|
---|
171 | }
|
---|
172 | else
|
---|
173 | {
|
---|
174 | if ( lua_istable( L, -1 ) )
|
---|
175 | {
|
---|
176 | lua_pushstring( L, path.substr(start,point-start).c_str() );
|
---|
177 | lua_gettable( L, -2 );
|
---|
178 | lua_insert( L, -2 );
|
---|
179 | lua_pop( L, 1 );
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | lua_pop(L, 1);
|
---|
184 | lua_pushnil(L);
|
---|
185 | return false;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | start = point+1;
|
---|
189 | point = path.find( '.', start );
|
---|
190 | }
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | int lua::state::get_stack_size()
|
---|
196 | {
|
---|
197 | return lua_gettop( L );
|
---|
198 | }
|
---|
199 |
|
---|
200 | lua::table_guard::table_guard( lua::state* lstate, const std::string& table, bool global )
|
---|
201 | : L(lstate), m_guard(lstate)
|
---|
202 | {
|
---|
203 | L->push( table, global );
|
---|
204 | }
|
---|
205 |
|
---|
206 | lua::table_guard::table_guard( lua::state* lstate, const std::string& table, int index, bool global )
|
---|
207 | : L(lstate), m_guard(lstate)
|
---|
208 | {
|
---|
209 | L->push( table, global );
|
---|
210 | lua_rawgeti( L->L, -1, index );
|
---|
211 | }
|
---|
212 |
|
---|
213 | lua::table_guard::table_guard( lua::state* lstate, const std::string& table, const std::string& index, bool global /*= true */ )
|
---|
214 | : L(lstate), m_guard(lstate)
|
---|
215 | {
|
---|
216 | L->push( table, global );
|
---|
217 | lua_pushstring( L->L, index.c_str() );
|
---|
218 | lua_rawget( L->L, -2 );
|
---|
219 | }
|
---|
220 |
|
---|
221 | lua::table_guard::table_guard( const table_guard& parent, const std::string& index )
|
---|
222 | : L( parent.L ), m_guard( parent.L )
|
---|
223 | {
|
---|
224 | lua_getfield( L->L, -1, index.c_str() );
|
---|
225 | }
|
---|
226 |
|
---|
227 | lua::table_guard::table_guard( const table_guard& parent, int index )
|
---|
228 | : L( parent.L ), m_guard( parent.L )
|
---|
229 | {
|
---|
230 | lua_rawgeti( L->L, -1, index );
|
---|
231 | }
|
---|
232 |
|
---|
233 | bool lua::table_guard::has_field( const string& element )
|
---|
234 | {
|
---|
235 | lua_getfield( L->L, -1, element.c_str() );
|
---|
236 | bool result = lua_isnil( L->L, -1 );
|
---|
237 | lua_pop( L->L, 1 );
|
---|
238 | return result;
|
---|
239 | }
|
---|
240 |
|
---|
241 | string lua::table_guard::get_string( const string& element, const string& defval /*= "" */ )
|
---|
242 | {
|
---|
243 | lua_getfield( L->L, -1, element.c_str() );
|
---|
244 | string result( ( lua_type( L->L, -1 ) == LUA_TSTRING ) ? lua_tostring( L->L, -1 ) : defval );
|
---|
245 | lua_pop( L->L, 1 );
|
---|
246 | return result;
|
---|
247 | }
|
---|
248 |
|
---|
249 | char lua::table_guard::get_char( const string& element, char defval /*= "" */ )
|
---|
250 | {
|
---|
251 | lua_getfield( L->L, -1, element.c_str() );
|
---|
252 | char result = ( lua_type( L->L, -1 ) == LUA_TSTRING && lua_rawlen( L->L, -1 ) > 0 ) ? lua_tostring( L->L, -1 )[0] : defval;
|
---|
253 | lua_pop( L->L, 1 );
|
---|
254 | return result;
|
---|
255 | }
|
---|
256 |
|
---|
257 | int lua::table_guard::get_integer( const string& element, int defval /*= "" */ )
|
---|
258 | {
|
---|
259 | lua_getfield( L->L, -1, element.c_str() );
|
---|
260 | int result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tointeger( L->L, -1 ) : defval;
|
---|
261 | lua_pop( L->L, 1 );
|
---|
262 | return result;
|
---|
263 | }
|
---|
264 |
|
---|
265 | double lua::table_guard::get_double( const string& element, double defval /*= "" */ )
|
---|
266 | {
|
---|
267 | lua_getfield( L->L, -1, element.c_str() );
|
---|
268 | double result = lua_type( L->L, -1 ) == LUA_TNUMBER ? lua_tonumber( L->L, -1 ) : defval;
|
---|
269 | lua_pop( L->L, 1 );
|
---|
270 | return result;
|
---|
271 | }
|
---|
272 |
|
---|
273 | bool lua::table_guard::get_boolean( const string& element, bool defval /*= "" */ )
|
---|
274 | {
|
---|
275 | lua_getfield( L->L, -1, element.c_str() );
|
---|
276 | bool result = lua_type( L->L, -1 ) == LUA_TBOOLEAN ? lua_toboolean( L->L, -1 ) != 0 : defval;
|
---|
277 | lua_pop( L->L, 1 );
|
---|
278 | return result;
|
---|
279 | }
|
---|
280 |
|
---|
281 | void lua::state::log_stack()
|
---|
282 | {
|
---|
283 | int top = lua_gettop(L);
|
---|
284 | NV_LOG( LOG_DEBUG, "Stack dump (" << top << ")");
|
---|
285 | for ( int i = 0; i < top; ++i )
|
---|
286 | {
|
---|
287 | NV_LOG( LOG_DEBUG, "#" << i+1 << " - " << lua_typename(L, lua_type(L, i+1) ) << " = " << nlua_typecontent(L, i+1) );
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | lua_State* lua::state::get_raw()
|
---|
292 | {
|
---|
293 | return L;
|
---|
294 | }
|
---|
295 |
|
---|
296 | lua::reference lua::state::register_object( object * o )
|
---|
297 | {
|
---|
298 | if (!o) return ref_none;
|
---|
299 | type_database *db = o->get_root()->get_type_database();
|
---|
300 | if (!db) return ref_none;
|
---|
301 | type_entry* t = db->get_type(typeid(o));
|
---|
302 | if (!t) return ref_none;
|
---|
303 | stack_guard guard( this );
|
---|
304 | lua_getglobal( L, t->name.c_str() );
|
---|
305 | if ( lua_isnil( L, -1 ) )
|
---|
306 | {
|
---|
307 | NV_THROW( runtime_error, std::string( t->name ) + " type not registered!" );
|
---|
308 | }
|
---|
309 | deep_pointer_copy( -1, o );
|
---|
310 | return luaL_ref( L, LUA_REGISTRYINDEX );
|
---|
311 | }
|
---|
312 |
|
---|
313 | void lua::state::unregister_object( object * o )
|
---|
314 | {
|
---|
315 | if (!o) return;
|
---|
316 | stack_guard guard( this );
|
---|
317 | lua_rawgeti( L, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
318 | lua_pushstring( L, "__ptr" );
|
---|
319 | lua_pushboolean( L, false );
|
---|
320 | lua_rawset( L, -3 );
|
---|
321 | lua_pop( L, 1 );
|
---|
322 | luaL_unref( L, LUA_REGISTRYINDEX, o->get_lua_index() );
|
---|
323 | }
|
---|
324 |
|
---|
325 | void lua::state::deep_pointer_copy( int index, void* obj )
|
---|
326 | {
|
---|
327 | index = lua_absindex( L, index );
|
---|
328 | lua_newtable( L );
|
---|
329 | lua_pushnil( L );
|
---|
330 | bool has_functions = false;
|
---|
331 | bool has_metatable = false;
|
---|
332 |
|
---|
333 | while ( lua_next( L, index ) != 0 )
|
---|
334 | {
|
---|
335 | if ( lua_isfunction( L, -1 ) )
|
---|
336 | has_functions = true;
|
---|
337 | else if ( lua_istable( L, -1 ) )
|
---|
338 | {
|
---|
339 | deep_pointer_copy( -1, obj );
|
---|
340 | lua_insert( L, -2 );
|
---|
341 | lua_pop( L, 1 );
|
---|
342 | }
|
---|
343 | lua_pushvalue( L, -2 );
|
---|
344 | lua_insert( L, -2 );
|
---|
345 | lua_settable( L, -4 );
|
---|
346 | }
|
---|
347 |
|
---|
348 | if ( lua_getmetatable( L, -2 ) )
|
---|
349 | {
|
---|
350 | lua_setmetatable( L, -2 );
|
---|
351 | has_metatable = true;
|
---|
352 | }
|
---|
353 |
|
---|
354 | if ( has_functions || has_metatable )
|
---|
355 | {
|
---|
356 | lua_pushstring( L, "__ptr" );
|
---|
357 | lua_pushlightuserdata( L, obj );
|
---|
358 | lua_rawset( L, -3 );
|
---|
359 | }
|
---|
360 | }
|
---|