lua-users home
lua-l archive

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


On Mon, Mar 16, 2009 at 10:51 PM, kathrin_69@gmx.de <kathrin_69@gmx.de> wrote:
> Hi,
>
> Dolphin schrieb:
>>
>> kathrin_69 <at> gmx.de <kathrin_69 <at> gmx.de> writes:
>>
>>
>>>
>>> Hi,
>>>
>>> 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:
>>>
>>> function START()
>>>  print("test")
>>> end
>>>
>>> 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.
>>> Any ideas?
>>>
>>> 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
>> equivalent of:
>>
>> function()
>>  START = function()
>>    print("Hello")
>>  end
>> end
>>
>> onto the stack, which must then be executed with pcall.
>
> 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).

There is nothing called START until your code runs and defines 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.

It's not the compiler's job to define variables.  In a dynamic
language, functions are defined at runtime.  This is not unique to Lua
by any means.

> 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.

Code assigns values to them when it runs.  There's no magic, other
than some syntax which can make it look a little less like assignment
to a variable.

> 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.

pcall executes the code that defines START.  loadstring just gets it
read to execute.

> 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?

Nothing "hinders" it.  That's not its job.  Defining functions, or
other variables, is the job of the code, and the code doesn't do
anything until it is executed (e.g., by pcall).

> 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 executed?

No, as it's the code that updates the table.  If you want to assign
values to variables, you'll need to run code.  That's just how dynamic
languages usually work, and Lua is no exception in this regard.

Loading the code parses it, and will check for parse errors.  It
converts to a compiled form.  It doesn't run the code, it doesn't set
any variables, it just leaves a code object ready to run.

-- James