lua-users home
lua-l archive

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


On Mon, 5 Nov 2012 16:22:35 +0800
lj_enjoylife <lslj0516@gmail.com> wrote:

> I want to substitude the load_lua function witch my loader, and then
> i can control the process, such as loading lua file from packed files.
> i do it like this:
> 
> lua_getfield(l, LUA_GLOBALSINDEX, LUA_LOADLIBNAME);
> lua_pushstring(l, "loaders");
> lua_gettable(l, -2);
> 
> lua_pushnumber(l, 2);
> lua_pushcfunction(l, my_loader); // set my loader
> lua_settable(l, -3);
> 
> *my loader:*
> 
> int __cdecl my_module_loader(lua_State* l)
> {
>         const char *name = luaL_checkstring(l, 1);
>         lua_getfield(l, LUA_ENVIRONINDEX, "path");
> ---------------------> here, i want to get the path value. but i got
> null. const char* temp1 = lua_tostring(l, -1);
> ---------------------> temp1  == NULL
>         printf("load module");
>         return 1;
> }
> 
> i want to get the path value. but i got null in my loader.
> i compare my loader with de loader_lua, there is no difference between
> them. but in loader_lua it works.
> 
> so, i was comfused by this.

Hello!

Are you sure you want to be replacing the Lua loader entirely?  If all
you want to do is load packed Lua files, the loader mechanism falls
back to other loaders until one succeeds.

I also noticed you're using LUA_ENVIRONINDEX.  If you're trying to
mimic the behavior of the Lua loader, you need to actually set the
function environment for my_module_loader in order for that to work.
Either that, or use an upvalue.

-Rob

Attachment: signature.asc
Description: PGP signature