Pushing to the stack

The first step in working with the Lua stack is usually to push some data onto it. The Lua C API provides several functions to push different types of data onto the stack. The following functions can be used to push data:

  •  lua_pushnil(lua_State*): Pushes nil onto the stack
  •  lua_pushboolean(lua_State*, bool): Pushes a Boolean value onto the stack
  • lua_pushnumber(lua_State*, lua_Number): Pushes a double onto the stack
  • lua_pushinteger(lua_State*, lua_Integer): Pushes a signed integer onto the stack
  • lua_pushstring (lua_State*, const char*): Pushes a NULL terminated string onto the stack

When you push a string onto the stack, Lua creates its own copy of that string. As soon as the push operation is finished, you can modify or even free the copy of the string you have.

The stack is not infinite. Before pushing data onto the stack, it's a good idea to check whether there is actually room for the data or not. To check how much room the stack has, you can use the int lua_checkstack(lua_State*, int) function. The function takes two arguments: the Lua state and the number of items you want to add to the stack. If there is enough room, the function returns true (1). If there is not enough room, the function returns false (0). This function may actually grow the stack if needed.

..................Content has been hidden....................

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