3
// Copyright 2008 by Nigel Atkinson suprapilot+LuaCode@gmail.com
5
// This library is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU Lesser General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
10
// This library is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU Lesser General Public License for more details.
15
// A copy of theGNU General Public License is availible at:
16
// <http://www.gnu.org/licenses/>.
18
// Makes interfacing with Lua easier.
19
// Tested with Lua 5.1.3 and 5.1.2.
20
// MSVC 7.12 (2003) and GCC (g++) 4.1.2
23
// Added LuaException class and changed all throws to use it. User code can use it too.
24
// It grabs any string, if any, from the top of the Lua stack.
39
// Used when the object to set the LuaRef to, on creation, is on the top of the lua stack.
44
fromStack( lua_State *L ) : mL(L)
48
// Pops a number of objects off the Lua stack once out of scope.
49
// Used to ensure the stack is left as it was, even if exceptions occur.
57
luaPop( lua_State *L, int num=1 ): mL(L), mNum(num)
68
// Basic "ANY" class for representing Lua objects.
69
// Used in calling Lua functions as parameters.
79
LuaVal() : mType( LUA_TNIL ), obj(NULL)
82
LuaVal( double n ) : mType( LUA_TNUMBER ), d( n ), obj(NULL)
85
LuaVal( const std::string& n ) : mType( LUA_TSTRING ), str( n ), obj(NULL)
88
LuaVal( const char *n ) : mType( LUA_TSTRING ), str( n ), obj(NULL)
91
LuaVal( lua_CFunction n ) : mType( LUA_TFUNCTION ), func( n ), obj(NULL)
94
LuaVal( LuaRef *o ) : mType( LUA_TLIGHTUSERDATA ), obj(o)
97
void push( lua_State *L );
105
std::string luaError;
110
// Do we we need a copy constuctor? I think the default supplied one works.
113
LuaException( lua_State *L, const char *str, const char *filename, long fileline ) : error(str), file(filename), line(fileline)
115
if( lua_gettop( L ) != 0 )
116
if( lua_isstring( L, -1 ) )
117
luaError = lua_tostring( L, -1 );
121
const std::string& getError() { return error; }
122
const std::string& getLuaError() { return luaError; }
123
lua_State *getLuaState() { return mL; }
124
std::string getErrorForDisplay()
126
std::stringstream ss;
128
ss << "*** " << error << " ***" << std::endl;
129
ss << "*** " << luaError << " ***" << std::endl;
130
ss << "*** In file: " << file << " Line: " << line << " ***" << std::endl;
135
~LuaException() throw () {}
138
// A Lua Object. Represents a Lua object within a Lua state.
139
// Each is referenced, so the Lua GC will not garbage collect this object while this instance is in scope.
140
// Once out of scope, the Lua object could be garbage collected, depending on any other references.
143
int mRef; // Index to reference of object.
144
lua_State *mL; // and the vm
146
// Private internal class for returning as a proxy object. This is used when asked for a table element.
147
// This class can not be instantated outside of LuaRef. It lets you use syntax like the following:
149
// tbl["index"] = something;
151
// given that "tbl" is a LuaRef.
155
// Allow outside class access to members, and private constructor.
158
std::string mKey; // Element index
159
int mRef; // Reference to table
162
// Private constructor.
163
tableElement( lua_State *L, int ref, const char *key ) : mKey(key), mRef( ref ), mL(L)
168
// Put table element on top of Lua stack.
171
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
172
lua_getfield( mL, -1, mKey.c_str() );
175
// Return the 'type' of the table element.
181
ret = lua_type( mL, -1 );
182
lua_pop( mL, 2 ); // Remove index and table from stack.
187
double operator = ( double f )
190
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
191
lua_pushnumber( mL, f );
192
lua_setfield( mL, -2, mKey.c_str() );
197
std::string & operator = ( std::string & str )
199
operator = ( str.c_str() ); // Just re-use the const char * code.
204
const char * operator = ( const char *str )
207
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
208
lua_pushstring( mL, str );
209
lua_setfield( mL, -2, mKey.c_str() );
214
lua_CFunction operator = ( lua_CFunction func )
217
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
218
lua_pushcfunction( mL, func );
219
lua_setfield( mL, -2, mKey.c_str() );
224
LuaRef & operator = ( LuaRef &obj )
228
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
230
lua_setfield( mL, -2, mKey.c_str() );
237
luaPop p1(mL,2); // Removes index and table from Lua stack once out of scope.
241
return lua_tonumber( mL, -1 );
244
operator std::string()
247
std::string str( lua_tostring( mL, -1 ) );
248
lua_pop( mL, 2 ); // Removes index and table from Lua stack
266
LuaRef( lua_State *L ) : mRef( LUA_NOREF ), mL( L ) // For "new" objects, that will be assigned a value later.
269
LuaRef( lua_State *L, const char *name ) : mL( L ) // Reference an existing object. Note that numbers and strings
270
{ // will be copied. Any changes will not change the original.
271
lua_getglobal( mL, name );
272
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
275
LuaRef( fromStack fs ) : mL( fs.mL ) // Reference an existing object that is on top of the Lua stack.
276
{ // Note same cavet as above.
277
mRef = luaL_ref( mL, LUA_REGISTRYINDEX ); // Removes from stack.
280
LuaRef( tableElement & te ) : mL( te.mL ) // Reference a table element.
282
lua_rawgeti( te.mL, LUA_REGISTRYINDEX, te.mRef );
283
lua_getfield( te.mL, -1, te.mKey.c_str() );
284
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
287
LuaRef(const LuaRef &obj ) : mL( obj.mL )
289
LuaRef& objRef = const_cast<LuaRef&>(obj);
291
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
294
~LuaRef() // We're gone. Release reference to object.
296
luaL_unref( mL, LUA_REGISTRYINDEX, mRef );
299
// Place object on top of Lua stack.
302
lua_rawgeti( mL, LUA_REGISTRYINDEX, mRef );
305
// Return the 'type' of the object
311
ret = lua_type( mL, -1 );
317
// Create a new table.
321
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
324
// Create a new table and associate a global variable with it.
325
void createTable( const char *name )
331
// Return a proxy to a table element givin an index.
332
tableElement operator [] ( const char *key )
336
if( ! lua_istable( mL, -1 ) )
337
throw LuaException( mL, "LuaRef operator [] used on a non Lua table", __FILE__, __LINE__ );
341
return tableElement( mL, mRef, key );
344
// Return a proxy to a table element givin an index.
345
tableElement operator [] ( int key )
349
if( ! lua_istable( mL, -1 ) )
350
throw LuaException( mL, "LuaRef operator [] used on a non Lua table", __FILE__, __LINE__ );
354
std::stringstream ss;
358
return tableElement( mL, mRef, ss.str().c_str() );
361
// Reference an object referenced by "obj"
362
LuaRef & operator = ( LuaRef &obj )
364
luaL_unref( mL, LUA_REGISTRYINDEX, mRef );
367
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
372
// Reference a number as a new object.
373
double operator = ( double f )
375
luaL_unref( mL, LUA_REGISTRYINDEX, mRef );
376
lua_pushnumber( mL, f );
377
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
381
// Reference a string as a new object.
382
std::string & operator = ( std::string & str )
384
operator = ( str.c_str() );
388
// Reference a string.
389
const char * operator = ( const char *str )
391
luaL_unref( mL, LUA_REGISTRYINDEX, mRef );
393
lua_pushstring( mL, str );
394
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
398
// Reference a function.
399
lua_CFunction operator = ( lua_CFunction func )
401
luaL_unref( mL, LUA_REGISTRYINDEX, mRef );
402
lua_pushcfunction( mL, func );
403
mRef = luaL_ref( mL, LUA_REGISTRYINDEX );
407
// Associate referenced object with a name, or global variable.
408
void store( const char *name )
411
lua_setglobal( mL, name);
414
// Associate referenced object as a member of a table.
415
void store( const char *name, LuaRef & table )
417
if( table.type() != LUA_TTABLE )
418
throw LuaException( mL, "given object is not a table.", __FILE__, __LINE__ );
422
lua_setfield( mL, -2, name ); // Pops value
423
lua_pop( mL, 1 ); // Pops table
426
// Return as a number.
433
if( lua_isnumber( mL, -1 ) )
434
ret = lua_tonumber( mL, -1 );
436
throw LuaException( mL, "LuaRef referenced object is not a number.", __FILE__, __LINE__ );
441
// Return as a string
442
operator std::string()
449
if( lua_isstring( mL, -1 ) )
450
ret = lua_tostring( mL, -1 );
452
throw LuaException( mL, "LuaRef referenced object is not a string.", __FILE__, __LINE__ );
457
// Return as an object
458
template<typename Treturn>
459
Treturn asObject(const char* className)
465
Treturn* ptrObj = * static_cast<Treturn**>(luaL_checkudata(mL, -1, className));
469
// Return as an object
470
template<typename Treturn>
471
Treturn asObject(const std::string& className)
473
return asObject<Treturn>(className.c_str());
476
// The next few overloads of operator () allow calling a referenced Lua object (provided its a function),
477
// with the same syntax as calling a C++ function. Only the types LuaVal has conversions for can be used.
478
// Upto 4 parameters, but more can be added. Returns true on succesfull call. No results are returned.
480
LuaRef operator () ()
484
if( lua_isfunction( mL, -1 ) )
486
if( lua_pcall( mL, 0, 1, 0 ) != 0 )
487
throw LuaException( mL, "Error running function in LuaRef operator ()", __FILE__, __LINE__ );
492
throw LuaException( mL, "LuaRef operator () called but does not reference a function", __FILE__, __LINE__ );
500
LuaRef operator () ( LuaVal p1 )
504
if( lua_isfunction( mL, -1 ) )
508
if( lua_pcall( mL, 1, 1, 0 ) != 0 )
509
throw LuaException( mL, "Error running function in LuaRef operator ()", __FILE__, __LINE__ );
514
throw LuaException( mL, "LuaRef operator () called but does not reference a function", __FILE__, __LINE__ );
522
LuaRef operator () ( LuaVal p1, LuaVal p2 )
526
if( lua_isfunction( mL, -1 ) )
531
if( lua_pcall( mL, 2, 1, 0 ) != 0 )
532
throw LuaException( mL, "Error running function in LuaRef operator ()", __FILE__, __LINE__ );
537
throw LuaException( mL, "LuaRef operator () called but does not reference a function", __FILE__, __LINE__ );
545
LuaRef operator () ( LuaVal p1, LuaVal p2, LuaVal p3 )
549
if( lua_isfunction( mL, -1 ) )
555
if( lua_pcall( mL, 3, 1, 0 ) != 0 )
556
throw LuaException( mL,"Error running function in LuaRef operator ()" , __FILE__, __LINE__ );
561
throw LuaException( mL, "LuaRef operator () called but does not reference a function", __FILE__, __LINE__ );
569
LuaRef operator () ( LuaVal p1, LuaVal p2, LuaVal p3, LuaVal p4 )
573
if( lua_isfunction( mL, -1 ) )
580
if( lua_pcall( mL, 4, 1, 0 ) != 0 )
581
throw LuaException( mL, "Error running function in LuaRef operator ()", __FILE__, __LINE__ );
586
throw LuaException( mL,"LuaRef operator () called but does not reference a function" , __FILE__, __LINE__ );
597
#endif // __LUAOBJECT_H