[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: When are global names registered within th VM?
- From: "kathrin_69@..." <kathrin_69@...>
- Date: Tue, 17 Mar 2009 06:51:01 +0100
Ok and what I wanted to do is to take that loaded (not started) program,
use lua_getglobal() to look for a function called START and if it's
found then call it (but don't call the anonymous function around it).
But that doesn't seem to be possible because for some strange reason
lua_loadstring() doesn't care about putting the symbol START onto the
global index table.
I don't see how I've a misunderstanding of loading and running a
program. Rather I seem to have a misunderstanding about how/when global
symbols are registered into the global index table. Couldn't find
anything in the docs about that.
kathrin_69 <at> gmx.de <kathrin_69 <at> gmx.de> writes:
I compile the scripts within my engine using luaL_loadstring.
After that I want to check if a function named "START" is defined and if
so - call resume with that function on the top of the stack.
so if I have that script:
i check and try to start it using this code:
Problem is: lua_getglobal(mpLuaVm, "START"); gets me "nil" on top of the
stack and I don't know why.
Thank you in advance!
It sounds like you have a misunderstanding in how loadstring works.
luaL_loadstring does not actually execute any of the lua code. It basically
wraps whatever you are loading into an unnamed function and pushes it on to
the top of the stack. Loading and running are two independent steps.
loaL_loadstring(L, "function START() print("Hello") end") pushes the
START = function()
onto the stack, which must then be executed with pcall.
That works is to pcall the anonymous function (if the deceleration of
START is the only thing there of course nothing happens). After that
lua_getglobal does find START and puts it on top of the stack and I can
do a lua_resume to execute START like I did intend it.
So some how pcall does register START within the global index table but
not loadstring and this is what I don't get. lua_loadstring actually
does analyze the code because it is able to find syntax errors. So what
hinders it to also add the symbols to the global index table?
Is there maybe some mechanism between loadstring and pcall which goes
that far, that the global index table is updated but the program is not
As mentioned above, luaL_dostring does both steps in one line of C code. It
is actually a macro defined as:
(luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))