lua-users home
lua-l archive

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

this wasn't instantly obvious to me either.  could I ask this-

say you just do

> loadfile 'test.lua'

the stuff is sitting on the stack, but you haven't named it.
is there any way in lua to execute the top of the stack, without the label?
what happens to it, does it get over-written by the next call to loadfile(),
or does it get garbage-collected?



Message: 5
Date: Fri, 17 Feb 2006 15:31:32 -0000
From: "Guy Davidson" <>
Subject: RE: Loading chunks
Content-Type: text/plain;    charset="iso-8859-1"

Thanks, that's exactly what I needed to realise.  Syntactic sugar never tasted so bittersweet...

-----Original Message-----
[]On Behalf Of Javier Guerra
Sent: 17 February 2006 12:52
To: Lua list
Subject: Re: Loading chunks

On Friday 17 February 2006 7:45 am, Alex Queiroz wrote:


On 2/17/06, Guy Davidson <> wrote:

My client C++ code calls luaL_loadfile with this filename.  My stack
diagnostics tell me a Lua function is on the top of the stack now.

     Did you run this function?

i'll try to clarify Alex's suggestion:  luaL_loadfile() loads the chunk and 
compiles it, but it doesn't execute the chunk.  remember that a code like 

function foo()
-- ...

is just syntactic sugar for:

foo = function ()
-- ...

IOW, it constructs a function and then assigns it to a variable.  that 
assignment is what must be executed.  to execute that, you have to execute 
the chunk itself.  the function that you got in the stack is the whole chunk 
compiled and unexecuted.  only after running it you'll find your function(s 
and variables) in the global table