lua-users home
lua-l archive

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


On Sun, May 1, 2011 at 09:54, jseb <gmane2010@finiderire.com> wrote:
> Hello,
>
> I'm trying to load a Lua script from a C file, without executing it at load
> time.
> In other words, i don't want to use "luaL_dofile (L, script.lua)".
>
> So i use instead "luaL_loadfile (L, "script.lua") "
>
> But after loading the script file, how can i execute "foo" function from
> inside it ?
>
> i tried this:
>
>    lua_getglobal(L, "foo");
>    lua_pcall (L,0,0,0); //compile function
>    lua_getfield (L, LUA_GLOBALSINDEX,"foo");
>    lua_pcall (L, 0,0,0); //execute function
>
> but the function is not executed.
>
>
> Thank you.
>
>
>

In Lua, writing "function foo() ... end" is really a shortcut for "foo
= function() ... end". Just like any other variable assignment, that
won't do anything until the statement actually gets executed.

Why do you not want to execute the file, but you want to load (and
presumably execute) the functions defined in it? Perhaps you want to
look at sandboxing.

-- 
Sent from my toaster.