[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Possible documentation improvement on Lua 5.1 Reference Manual, lua_call()
- From: Michal Kottman <k0mpjut0r@...>
- Date: Thu, 20 Jan 2011 15:17:51 +0100
On Thu, 2011-01-20 at 09:03 -0500, Steve Litt wrote:
> On Thursday 20 January 2011 08:39:28 Michal Kottman wrote:
> > On Thu, 2011-01-20 at 08:23 -0500, Steve Litt wrote:
>
> > > You get this at runtime:
> > > =================
> > > slitt@mydesk:~$ ./callfunc
> > > In C, calling Lua
> > >
> > > FATAL ERROR:
> > > lua_pcall() failed: attempt to call a nil value
> >
> > This is because there is no global named "tellme" (whose value should be
> > a function).
>
> What would make tellme a global? Here's my Lua script:
>
> =======================
> function tellme()
> io.write("This is coming from lua.tellme.\n")
> end
>
> function tellme2()
> io.write("This is coming from lua.tellmetwo.\n")
> end
>
> function witharg(n)
> io.write("Number within Lua.witharg=")
> io.write(tonumber(n))
> io.write("\n")
> end
>
> print("You shouldnt see this!")
> =======================
>
> Am I missing something? I thought the existence of a non-local function called
> "tellme" within the script called by lua_pcall() would be the global function.
Well, this should work. But there is something you maybe don't
understand. Functions don't exist, until you actually create them. In
Lua, function abcd() end is syntax sugar for this:
abcd = function() end
So in order to "have" the functions in the global table, you need to run
the code (so that they are created and assigned). You will actually need
to see the "You shouldnt see this!" text to have them available.
In C, you can call luaL_dofile(filename) to run the code prior to your
calling of "tellme". lua_loadfile actually only compiles the file as a
function and prepares for you to run it (maybe that is why you had a
function in your stack available).