Discussion:
Loading chunks
Guy Davidson
2006-02-17 12:41:30 UTC
Permalink
I'm having trouble invoking a loaded chunk.

Here's a simple piece of Lua:

function print_loop()
for i=1,10 do
print(i)
end
end

This is kept in a text file called test.lua.

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.

Then it calls

lua_pushstring(lua, "print_loop");

(where lua is the lua_State*) which pushes "print_loop", the function name declared in the chunk, on the stack. I then call

lua_gettable(lua, LUA_GLOBALSINDEX);

which exchanges the string "print_loop" for a function. However, NIL is appearing at the top of the stack instead. Now, my decision to call lua_gettable with LUA_GLOBALSINDEX is informed by the sample code in section 3.14 of the documentation. I am assuming that, when the chunk is loaded, all its declared objects (functions and variables) are added to the LUA_GLOBALSINDEX table, using the identifier as the key. I'm having trouble finding similar examples from the library code.

Any clues where I'm going wrong here?

While I'm waiting I'll write table diagnostics...

Cheers,
Guy Davidson
Alex Queiroz
2006-02-17 12:45:46 UTC
Permalink
Hallo,
Post by Guy Davidson
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?

--
-alex
http://www.ventonegro.org/
Javier Guerra
2006-02-17 12:51:56 UTC
Permalink
Post by Alex Queiroz
Hallo,
Post by Guy Davidson
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
yours:

function foo()
-- ...
end

is just syntactic sugar for:

foo = function ()
-- ...
end

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
--
Javier
Romulo Bahiense
2006-02-17 13:51:27 UTC
Permalink
Post by Guy Davidson
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.
Right. The chunk is at the top of the stack. You will have to call it,
so it will be able to register print_loop in the globals table.

// Load the code
luaL_loadfile(L, "test.lua");

// Execute the chunk
lua_call(L, 0, 0);

// You know what to do from now on :)
lua_pushstring(L, "print_loop");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_call(L, 0, 0);
Post by Guy Davidson
local chunk = loadfile("test.lua")
print(type(chunk))
function: ...
Post by Guy Davidson
print(print_loop)
nil
Post by Guy Davidson
chunk()
print(print_loop)
function: ...

Hope it helps

--rb
Guy Davidson
2006-02-17 15:31:32 UTC
Permalink
Thanks, that's exactly what I needed to realise. Syntactic sugar never tasted so bittersweet...

-----Original Message-----
From: lua-***@bazar2.conectiva.com.br
[mailto:lua-***@bazar2.conectiva.com.br]On Behalf Of Javier Guerra
Sent: 17 February 2006 12:52
To: Lua list
Subject: Re: Loading chunks
Post by Alex Queiroz
Hallo,
Post by Guy Davidson
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
yours:

function foo()
-- ...
end

is just syntactic sugar for:

foo = function ()
-- ...
end

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
--
Javier
Rob Shaw
2006-02-20 12:25:52 UTC
Permalink
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?

thanks,

rob
Message: 5
Date: Fri, 17 Feb 2006 15:31:32 -0000
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-----
Sent: 17 February 2006 12:52
To: Lua list
Subject: Re: Loading chunks
Post by Alex Queiroz
Hallo,
Post by Guy Davidson
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()
-- ...
end
foo = function ()
-- ...
end
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
--
Javier
Alex Queiroz
2006-02-20 12:35:11 UTC
Permalink
Hallo,
Post by Rob Shaw
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?
(loadfile"test.lua")()
If you don't assign it or don't execute it, it'll be garbage collected.

--
-alex
http://www.ventonegro.org/
Romulo Bahiense
2006-02-20 12:36:25 UTC
Permalink
Post by Rob Shaw
is there any way in lua to execute the top of the stack, without the label?
In Lua, no. You can only access stack in C the side. Sometimes I miss
this kind of 'feature', but it is just temporarily :)
Post by Rob Shaw
what happens to it, does it get over-written by the next call to loadfile(),
or does it get garbage-collected?
Garbage collected.

--rb

Loading...