lua-users home
lua-l archive

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

Roberto Ierusalimschy wrote:
> I think there is a misunderstanding here. The Lua 5.0 stack grows
> automatically on demand during recursive calls (unlike Lua 4.0, where
> the stack was fixed), and there is a "polite" error in case of stack
> overflow. What does not grow automatically is the virtual stack that
> each C function sees.

Well... I see I misunderstood something, but... I think I don't fully 
understand this "virtual stack" as opposed to the "real" stack. Why
does this distinction exist? I once, long ago wrote my own stack based 
language, but it had but a single stack, which all functions, builtin
and defined alike used in common. I had assumed it was the same in Lua. 
But , if I understand you correctly, this is different in Lua? 

Does the "virtual" stack of a function mean that every C function 
has it's own LUA stack? How is it then for Lua functions?
Doe they all have a separate stack too? Does that grow automatically 
too? Is there some kind of "common" stack? And how does that "common"
stack interact with the "virtual" stacks of the C functions and the
of the Lua functions? I'm sorry if I sound clueless, but all these
have me a little confused. 

> Usually this virtual stack is for temporary values only (function
> parameters, function results, and "local" variables). For most C
> functions it is quite easy to know in advance how much stack you will
> need. Very few tasks demand that you keep pushing new values on the
> stack without removing them. For these few tasks, you need to call
> lua_checkstack (or more easily luaL_check_stack). See luaB_unpack (in
> lbaselib.c) for an example.

Yes, I see that the amount of return values, arguments and locals is 
usually limited, and relatively well-known. Hmmm..., but what about a 
vararg function like function foo(...) print(arg) end ? Is the "arg"
a single entry on the stack? Or is arg just an alias and all are the 
arguments provided to foo() pushed on the stack one by one? 
I'm just worried about functions with variadic amounts of parameters, 
because then the amount of arguments is unkonwn.

> Finally, it is worth remembering that Lua 4.0 also does not grow the
> stack. The api_incr_top macro only checks the stack size, and raises an
> error in case of overflow.

Hmmm... so Lua 5 does in fact more automatic stack-growing than Lua 4?
Well, then my only remaining worry is that it seems a bit strange that
the "virtual" C stack and the "real" LUA stack behave differently in 
that aspect... I am a big fan of orthogonality, both in the language
and in the API. I would like there to be as little as possible 
differences between "heavy" userdata and LUA tables, and between C 
functions and LUA functions.

"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer