[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [FEATURE REQUEST] Custom-naming of preloaded modules
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 17 Feb 2017 17:12:35 +0200
2017-02-17 14:25 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> Patch to lua.c attached. Compiles warning-free as a Lua 5.2.4 patch but
>> gives a warning for incompatible types on 5.3.4 that I can't fix. Still works,
>> though.
>
>> 261,262c261,264
>> < lua_getglobal(L, "require");
>> < lua_pushstring(L, name);
>> ---
>> > char *eq = strchr(name,'=');
>> > lua_getglobal(L, "require");
>> > if (eq==name) lua_pushstring(L, name);
>> > else lua_pushstring(L, eq+1);
>
> The test 'eq==name' seems weird. Wouldn't it be true only when 'name'
> starts with "="?
>
>
>> 264,265c266,269
>> < if (status == LUA_OK)
>> < lua_setglobal(L, name); /* global[name] = require return */
>> ---
>> > if (status == LUA_OK) {
>> > if (eq==name) lua_setglobal(L, name); /* global[name] = require return */
>> > else lua_setglobal(L, strndup(name,eq-name));
>> > }
>
> 'strndup' is not ANSI, and it leaks memory when used like here.
My present version is:
static int dolibrary (lua_State *L, const char *name) {
int status;
char *eq = strchr(name,'=');
lua_getglobal(L, "require");
if (eq) lua_pushstring(L, eq+1);
else lua_pushstring(L, name);
status = docall(L, 1, 1); /* call 'require(name)' */
if (status == LUA_OK) {
if (eq) *eq = 0;
lua_setglobal(L, name); /* global[name] = require return */
if (eq) *eq = '=';
}
return report(L, status);
}
Luiz has neither approved nor rejected the temporary modification of
a character inside a "const char*", but the compiler is happy :-)