lua-users home
lua-l archive

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


Hey watch your stack!

-------------------------------------
lua_newtable(luaVM);

luaopen_string (luaVM);
luaopen_math (luaVM);

lua_pushstring(luaVM, "core");
lua_pushvalue(luaVM, -2);
lua_settable(luaVM, LUA_GLOBALSINDEX); /* register it with given name */
lua_setfenv(luaVM, -2);
-------------------------------------

is vastly different from

-------------------------------------
luaopen_string (luaVM);
luaopen_math (luaVM);

lua_newtable(luaVM);
lua_pushstring(luaVM, "core");
lua_pushvalue(luaVM, -2);
lua_settable(luaVM, LUA_GLOBALSINDEX); /* register it with given name */
lua_setfenv(luaVM, -2);
-------------------------------------



-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of Dmitriy
Iassenev
Sent: Wednesday, September 24, 2003 4:17 PM
To: Lua list
Subject: Re: How to load buffer from C to the particular Lua
namespace(table)


> That's covered by the line:
>
> >>     /* add some data here ... */
>
> See?  :-D
>
> You could simply install the minimal set that you need, or set up a
> metamethod __index that falls back to the globals.  Sandboxing comes at a
> price...

:-) thank you for patience, I am a newbie to Lua.
But how can I "set up a metamethod __index that falls back to the globals"?
And why this piece of code fails on lua_call(VM,0,0) with "attempt to call a
table value" (I've just loaded string and math after creating new table,
otherwise it fails on line 2 in "extension.lua" with "attempt to index
global 'string' (a nil value)")

lua_State* luaVM = lua_open();

luaopen_base (luaVM);
luaopen_table (luaVM);
luaopen_io (luaVM);
luaopen_string (luaVM);
luaopen_math (luaVM);
luaopen_debug (luaVM);

open (luaVM);
function (luaVM,"log",(void (*)(LPCSTR))(Log));

if (luaL_loadfile(luaVM, "x:\\extension.lua"))
    lua_error(luaVM);

lua_newtable(luaVM);

luaopen_string (luaVM);
luaopen_math (luaVM);

lua_pushstring(luaVM, "core");
lua_pushvalue(luaVM, -2);
lua_settable(luaVM, LUA_GLOBALSINDEX); /* register it with given name */
lua_setfenv(luaVM, -2);

lua_call(luaVM, 0, 0);

lua_dofile (luaVM, "x:\\test1.lua");

---------------------------------------------------
file <extension.lua>
function printf(fmt,...)
     log(string.format(fmt,unpack(arg)))
end

---------------------------------------------------
file <test1.lua>
for i=1,6 do
     core.printf("* String %d from test1\n",i)
end

---------------------------------------------------

Best regards,
Dmitriy Iassenev