[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: problems while using my loader function?
- From: Rob Hoelz <rob@...>
- Date: Mon, 5 Nov 2012 10:32:21 +0100
Ah, but it does:
/* create `package' table */
luaL_register(L, LUA_LOADLIBNAME, pk_funcs);
#if defined(LUA_COMPAT_LOADLIB)
lua_getfield(L, -1, "loadlib");
lua_setfield(L, LUA_GLOBALSINDEX, "loadlib");
#endif
lua_pushvalue(L, -1);
lua_replace(L, LUA_ENVIRONINDEX);
/* create `loaders' table */
lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0);
/* fill it with pre-defined loaders */
for (i=0; loaders[i] != NULL; i++) {
lua_pushcfunction(L, loaders[i]);
lua_rawseti(L, -2, i+1);
}
C functions inherit their function environment from the functions that
register them. Honestly, using function environments with C functions
is a bit obscure, and I don't think it's portable to 5.2; you should
probably store package in an upvalue instead.
-Rob
On Mon, 5 Nov 2012 17:22:28 +0800
lj_enjoylife <lslj0516@gmail.com> wrote:
> I replaced the loader_lua, and "require (test_module)" step into my
> loader. In my loader i want to get the path value in
> table(LUA_ENVIRONINDEX), then add them to my resource path.
>
> But i got null.
>
> You said i should set the function environment. The problem is: the
> loader_lua doesn`t set the function envitonment, and it get the “path”
> value;
>
> i want to know differences betown them.
> thank you.
>
>
> 2012/11/5 Rob Hoelz <rob@hoelz.ro>
>
> > On Mon, 5 Nov 2012 16:49:03 +0800
> > lj_enjoylife <lslj0516@gmail.com> wrote:
> >
> > > first
> > > just replace the loader_lua loader.
> > > and also it works.
> >
> > Oh, it'll work! There's nothing "wrong" with replacing it; I was
> > just suggesting adding a "packed" loader so you didn't need to
> > reinvent the wheel.
> >
> > >
> > > 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).
> >
> > Sorry, I'm confused; I see the following line in the code you
> > posted:
> >
> > 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
> > > >
> >
> >
Attachment:
signature.asc
Description: PGP signature