lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hello again everyone :)

I am having some trouble with usertags/data, and I was wondering if someone
might clear some stuff up form. I have the following peice of code that
creates a new global and pushes a user tag on the stack as its values:

void VLLuaHandler::SetErrorHandler()
  lua_setglobal(m_cState, "_handler");
  lua_pushusertag(m_cState, this, LUA_ANYTAG);

  lua_register(m_cState, LUA_ERRORMESSAGE, HandleLuaError);

As you can see, I create the global "_handler" and push a piece of user data
(in this case "this", an instance of the VLLuaHandler class) on to the
stack, using LUA_ANYTAG, because I want it to match by value rather then tag
(and to be quite honest, when I tried to use my own tag I kept getting a
GPF, strange...)

I then register my HandleLuaError function for the _ERRORMESSAGE lua
function (using the LUA_ERRORMESSAGE define from lua.h).

My HandleLuaError function looks as follows:

int HandleLuaError(lua_State *cState)
 char *sBuffer = NULL;
 VLLuaHandler *cHandler = NULL;

 // I assumed that this would push my user data on to the stack, by getting
the global "_handler"  that I previously set
 lua_getglobal(cState, "_handler");
 int nArgs = lua_gettop(cState);

 // Test code to figure out what was going on. This loop is supposed to
MessageBox me the type of the data on the stack, and it works fine for stack
index 1,returns 'string'. It crashes the program when it it tries to get the
type of stack index 2....hmmmm

 for(int i = 1; i <= nArgs; i++)
  VLErrorNotifier::Ref().Notify(lua_typename(cState, lua_type(cState, i)),

 // Not even sure of any of this is right, I figured that lua_getglobal
would push my user data to index 2 on the stack, but 1 or 2, doesn't matter,
this returns false kills the function
 if(!lua_isuserdata(cState, 2))
  return 0;

 if(!lua_isstring(cState, 1))
  return 0;

 // More stuff here, but it never gets that far

Okay, so oviously I am doing something wrong here, or just missing a basic
concept. Anyone care to make me look the fool and point it out? ;)

Matt Holmes
Lead Developer
The Visual Lua Project