2

Take for example a simple C function for lua:

int luacfunc(lua_State *L)
{
    printf("%g\n", lua_tonumber(L, 1) + lua_tonumber(L, 2));
}

In this case, the parameters show up as index 1 and 2. Since positive number represent the stack bottom-up, it would mean that the parameters are located at the bottom of the stack, not the top.

Why is that the case? Wouldn't it require shifting the entire stack on every function call to make room for the parameters?

Therhang
  • 825
  • 1
  • 9
  • 15
  • I'm sure you have read this - http://www.lua.org/pil/24.2.html - It does mention the stack is LIFO so I'm not sure why you are seeing the above behavior. – Neil Jul 30 '15 at 08:09
  • Please read this also - http://stackoverflow.com/questions/5448654/lua-c-api-handling-and-storing-additional-arguments – Neil Jul 30 '15 at 08:16
  • Customary the stack grows down, while the heap grows up. – Paul Ogilvie Jul 30 '15 at 08:52

1 Answers1

5

The Lua stack is specific to your function call. Each C function call gets its own "stack" (it's actually a slice of a bigger stack, but that's hidden from you). Your arguments are both at the top and at the bottom, because they're the only thing on your stack.

user253751
  • 57,427
  • 7
  • 48
  • 90