lua-users home
lua-l archive

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


On Fri, Oct 06, 2006 at 09:09:49AM +1000, Nick Gammon wrote:
> 
> On 05/10/2006, at 1:11 PM, Glenn Maynard wrote:
> 
> >The Lua binding I'm using for C++ classes removes the "self" parameter
> >from the stack.  It's always passed to the bound class, after type
> >checking.  This allows arguments to be referenced more naturally, with
> >the first argument at 1, rather than 2.
> >
> >This causes a problem: it confuses luaL_argerror, since it expects the
> >self parameter to be there; it subtracts one, giving the wrong offset
> >for error messages (and "bad self" for errors on argument 1).
> >
> >Any suggestions on how to deal with this?
> 
> I had the same situation in my code. What I did is pull out the  
> "self" argument (if it existed) and then did:
> 
> lua_remove (L, 1);
> 
> This then makes subsequent processing take each argument in a natural  
> way (now the 1st argument after self is argument 1, and the errors  
> are correct).

That's exactly what I'm doing: the type is pulled out of the first argument
(for passing to the actual C++ method) and removed from the stack, but
that's what causing the problem.

luaL_argerror() assumes that if a function is a method call (according
to lua_getinfo), then the first argument is "self", and reports
accordingly.  I've removed that argument, and it doesn't know that,
so when I say "foo:PrintString(nil)", I get errors like "calling PrintString
on bad self (got nil, expected string)", instead of "bad argument #1".

(But libraries aren't necessarily doing the same thing, so if I just
remove that "self" trick from luaL_argerror, then everything else will
be wrong, in the opposite direction.)

-- 
Glenn Maynard