lua-users home
lua-l archive

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


On Tue, Jun 03, 2008 at 06:28:17PM -0700, M.C. wrote:
> On Tue, Jun 3, 2008 at 4:20 AM, Etan Reisner
> <deryni@unreliablesource.net> wrote:
<snip>
> So I am to think of this luaopen_array() function as something Lua
> calls and when it returns, it resets the stack? When the function
> first starts, lua_gettop() reports 1. The function returns 1. What is
> the function returning back to Lua. And why don't I need to concern
> myself with the fact that all those intermediate calls like
> lua_newmetatable and lua_register continue to increase the stack size
> but don't get returned, and Lua is going to effectively zero the
> stack? I'm wondering how these values survive to do anything useful or
> don't blow up the stack.

Yes, luaopen_array is being called by lua it is the module init function
for the C module. It is what is called by require("array"). The reference
manual discusses this in the section on the default package.loaders
entries (http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders).

The module init function is called with the name of the module (that is
why gettop returns one when the function starts).

What the function returns depends on what is in the top X stack locations
(where X is the return value of the C function itself). Generally modules
return the created module tables. (See
http://www.lua.org/manual/5.1/manual.html#pdf-require for information
about what require does with the return value of the function.)

You only need to worry about the stack size when you push a large number
of items in a row (that is a number greater than the default stack size,
http://www.lua.org/manual/5.1/manual.html#3.2).

The reason you don't need to worry about the items that are left-over on
the stack is because, as I said last time, lua makes sure to clean the
stack up before and after a C function is called. Which means that
anything not returned from the C function (below the X top stack items
that are returned) will be cleaned up and subject to any appropriate
garbage collection.

They don't last long enough to be useful, anything not stored anywhere
else will be gone, and they don't blow up the stack because they cease
existing.

To be clear here, you *do* need to worry about stack size and balancing
the stack while you are in your C code. It is only at the transitions that
lua manages things for you.

<snip>
> I was thinking I might want to think of a library as a large piece of
> functionality (like the math library). I was thinking a little bit
> about window kits and they seem to have a bunch of types like size,
> point, rect, etc. If I wanted to write a library like 'window', it
> seemed like I would want all those separate data types together, yes?

Yeah, that would be a reasonable thing to do. I had assumed your module
was more targeted than that, sorry.

<snip>
> Thank you for your helpful answers.

You are quite welcome.

	-Etan