Querying the stack

Lua references elements in the stack using indices. The bottom element of the stack is index 1; the indices grow towards the top of the stack where the last element was added. Lua can also index the stack in reverse. An index of -1 refers to the top of the stack, -2 to the element right below the top, and so on:

Using indices, you can check the type of any element on the stack. The functions listed here can be used to query the type of element on the stack. They all return true (1) or false (0). Each function takes a Lua state for its first argument and a stack index for its second:

  • int lua_isnumber(lua_State*, int): Checks whether the element at the provided index is a number
  • int lua_isstring(lua_State*, int): Checks whether the element at the provided index is a string
  • int lua_isboolean(lua_State*, int): Checks whether the element at the provided index is a Boolean
  • int lua_istable(lua_State*, int): Checks whether the element at the provided index is a table
  • int lua_isnil(lua_State*, int): Checks whether the element at the provided index is nil

There is a similar function, int lua_type(lua_State*, int), which returns an enumeration value with the type of object. This function can be useful when used in a switch statement or something similar. The following are valid enumeration values that the function could return:

  • LUA_TNUMBER: Represents a Lua number
  • LUA_TSTRING: Represents a Lua string
  • LUA_TBOOLEAN: Represents a Lua Boolean
  • LUA_TTABLE: Represents a Lua table
  • LUA_TNIL: Represents nil
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.230.81