lua-users home
lua-l archive

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


On Thursday 20 January 2011 09:17:51 Michal Kottman wrote:
> 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

THE KEY POINT FOLLOWS...
 
> 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).

THAT SOLVED IT!

You need to run the script once in order to create and assign the functions as 
globals.

Thank you Michal. It was driving me nuts.

I did this:

/* Execute script once to create and assign functions */
if (lua_pcall(L, 0, 0, 0))
	bail(L, "lua_pcall() failed"); 

printf("In C, calling Lua\n");

/* Specify which function to run */
lua_getfield(L, LUA_GLOBALSINDEX, "tellme");

/* Now run it */
if (lua_pcall(L, 0, 0, 0))
	bail(L, "lua_pcall() failed");

printf("Back in C again\n");


Once again for the sake of newbies, I think it would help to add a sentence to 
the lua_call() docs in the manual saying you must run dofile() or lua_pcall() 
first without a function to get all the functions registered as globals. 
Nobody on the Internet says that explicitly, at least nobody I was able to 
Google within 2 hours.

Again, thank you -- it was driving me nuts.

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt