|
On Mon, 5 Nov 2012 16:49:03 +0800Oh, it'll work! There's nothing "wrong" with replacing it; I was just
lj_enjoylife <lslj0516@gmail.com> wrote:
> first
> just replace the loader_lua loader.
> and also it works.
suggesting adding a "packed" loader so you didn't need to reinvent the
wheel.
Sorry, I'm confused; I see the following line in the code you posted:
>
> second
> i`m not using LUA_ENVIRONINDEX. I operate the "package" table
> directly. What i was confused by is that i couldn`t get the "path"
> from the table(LUA_ENVIRONINDEX).
lua_getfield(l, LUA_ENVIRONINDEX, "path");
>
>
> 2012/11/5 Rob Hoelz <rob@hoelz.ro>
>
> > 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
> >