debugging message assumes that method calls ("x:y()") have an initial
self argument; we remove it, causing off-by-one error messages
This commit is contained in:
@@ -81,14 +81,20 @@ public:
|
||||
|
||||
lua_pop(L, 2); // drop metatable and method table
|
||||
}
|
||||
|
||||
|
||||
// get userdata from Lua stack and return pointer to T object
|
||||
static T *check(lua_State *L, int narg)
|
||||
static T *check( lua_State *L, int narg, bool bIsSelf = false )
|
||||
{
|
||||
userdataType *ud =
|
||||
static_cast<userdataType*>(luaL_checkudata(L, narg, s_className));
|
||||
if(!ud) luaL_typerror(L, narg, s_className);
|
||||
return ud->pT; // pointer to T object
|
||||
userdataType *pUserdata = static_cast<userdataType*>( luaL_checkudata(L, narg, s_className) );
|
||||
if( pUserdata == NULL )
|
||||
{
|
||||
if( bIsSelf )
|
||||
luaL_typerror( L, narg, s_className );
|
||||
else
|
||||
LuaHelpers::TypeError( narg, s_className );
|
||||
}
|
||||
|
||||
return pUserdata->pT; // pointer to T object
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -97,7 +103,7 @@ private:
|
||||
static int thunk(lua_State *L)
|
||||
{
|
||||
// stack has userdata, followed by method args
|
||||
T *obj = check(L, 1); // get 'self', or if you prefer, 'this'
|
||||
T *obj = check( L, 1, true ); // get self
|
||||
lua_remove(L, 1); // remove self so member function args start at index 1
|
||||
// get member function from upvalue
|
||||
MyRegType *l = static_cast<MyRegType*>(lua_touserdata(L, lua_upvalueindex(1)));
|
||||
|
||||
@@ -379,6 +379,17 @@ void LuaManager::Fail( const CString &err )
|
||||
lua_error( L );
|
||||
}
|
||||
|
||||
/* Like luaL_typerror, but without the special case for argument 1 being "self"
|
||||
* in method calls, so we give a correct error message after we remove self. */
|
||||
int LuaHelpers::TypeError( int iArgNo, const char *szName )
|
||||
{
|
||||
lua_Debug debug;
|
||||
lua_getstack( LUA->L, 0, &debug );
|
||||
lua_getinfo( LUA->L, "n", &debug );
|
||||
return luaL_error( LUA->L, "bad argument #%d to \"%s\" (%s expected, got %s)",
|
||||
iArgNo, debug.name? debug.name:"(unknown)", szName, lua_typename(LUA->L, lua_type(LUA->L, iArgNo)) );
|
||||
}
|
||||
|
||||
|
||||
LuaFunctionList::LuaFunctionList( CString name_, lua_CFunction func_ )
|
||||
{
|
||||
|
||||
@@ -144,6 +144,8 @@ namespace LuaHelpers
|
||||
lua_rawseti( L, -2, i+1 );
|
||||
}
|
||||
}
|
||||
|
||||
int TypeError( int narg, const char *tname );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user