lua-users home
lua-l archive

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


It was thus said that the Great liam mail once stated:
> On 31 August 2011 16:49, Sean Conner <sean@conman.org> wrote:
> > It was thus said that the Great Sean Conner once stated:
> >>
> >>   I think you gave the solution in another email---lua_gettop()!  Here:
> >>
> >> int mylua_isacceptable(lua_State *L,int idx)
> >> {
> >>   return abs(idx) <= lua_gettop(L);
> >> }
> >>
> >>   -spc (Or am I missing something?)
> >
> >  Okay, in re-reading the thread, I see I am missing something.  Oliver
> > wants to check that an index is acceptable and thus, feels the need to know
> > the physical size of the stack at that instance, instead of the current top
> > of the stack.
> >
> >  But in thinking about it, the code above is what you really want.  If you
> > (as programmer) are trying to access stack entries you know nothing about
> > (outside of lua_gettop()) then you are doing something wrong, regardless of
> > the size of the stack (but I am willing to conceed that this issue has never
> > bitten me, probably because of all my years of programming in assembly,
> > where one is working with a raw, system controlled, stack).
> >
> luaL_optint(L,2,0) on a stack which has one entry would therefore be
> an error and not a valid request using the function you posted
> previously with the incorrect name 'mylua_isacceptable'

  The stack is always going to be at least 20 entries.

static int atest(lua_State *L)
{
  int v1;
  int v2;
  
  v1 = luaL_optint(L,1,-10);
  v2 = luaL_optint(L,2,-50);
  printf("VALUE: %d %d\n",v1,v2);
  return 0;
}

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "atest"
> atest()
VALUE: -10 -50
> atest(3)
VALUE: 3 -50
> atest(3,1)
VALUE: 3 1
> 

  -spc