[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: [FEATURE REQUEST] Custom-naming of preloaded modules
- From: Dirk Laurie <dirk.laurie@...>
- Date: Fri, 17 Feb 2017 10:25:52 +0200
At present (from `man lua`):
-l name
execute the equivalent of name=require('name') before executing
script.
Request:
-l [loaded=]name
execute the equivalent of loaded=require('name'), or if
not supplied,
name=require('name') before executing script.
Inspired by the fact that "lua -l pl.stringio" creates `_G.["pl.stringio"]` and
Steve has promised more of the same. (Yes, I know I can solve that
problem with "-e".)
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);
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));
> }