summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-08-16 13:57:19 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2021-08-16 13:57:19 -0300
commit65434b4d1b5509e95940939e28fd90d4558da12e (patch)
treecb4f78a69bd64f8fbbf1108add57e447a85b0ee5
parent59acd79c05b78950fe03279d60b015aeed5348ab (diff)
downloadlua-github-65434b4d1b5509e95940939e28fd90d4558da12e.tar.gz
Option '-l' can give a name for the global variable.
Sintax for this option now is '-l [globname=]modname'.
-rw-r--r--lua.c37
-rw-r--r--testes/main.lua5
2 files changed, 27 insertions, 15 deletions
diff --git a/lua.c b/lua.c
index 46b48dba..0f190044 100644
--- a/lua.c
+++ b/lua.c
@@ -89,14 +89,15 @@ static void print_usage (const char *badoption) {
lua_writestringerror(
"usage: %s [options] [script [args]]\n"
"Available options are:\n"
- " -e stat execute string 'stat'\n"
- " -i enter interactive mode after executing 'script'\n"
- " -l name require library 'name' into global 'name'\n"
- " -v show version information\n"
- " -E ignore environment variables\n"
- " -W turn warnings on\n"
- " -- stop handling options\n"
- " - stop handling options and execute stdin\n"
+ " -e stat execute string 'stat'\n"
+ " -i enter interactive mode after executing 'script'\n"
+ " -l mod require library 'mod' into global 'mod'\n"
+ " -l g=mod require library 'mod' into global 'g'\n"
+ " -v show version information\n"
+ " -E ignore environment variables\n"
+ " -W turn warnings on\n"
+ " -- stop handling options\n"
+ " - stop handling options and execute stdin\n"
,
progname);
}
@@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) {
/*
-** Calls 'require(name)' and stores the result in a global variable
-** with the given name.
+** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
*/
-static int dolibrary (lua_State *L, const char *name) {
+static int dolibrary (lua_State *L, char *globname) {
int status;
+ char *modname = strchr(globname, '=');
+ if (modname == NULL) /* no explicit name? */
+ modname = globname; /* module name is equal to global name */
+ else {
+ *modname = '\0'; /* global name ends here */
+ modname++; /* module name starts after the '=' */
+ }
lua_getglobal(L, "require");
- lua_pushstring(L, name);
- status = docall(L, 1, 1); /* call 'require(name)' */
+ lua_pushstring(L, modname);
+ status = docall(L, 1, 1); /* call 'require(modname)' */
if (status == LUA_OK)
- lua_setglobal(L, name); /* global[name] = require return */
+ lua_setglobal(L, globname); /* globname = require(modname) */
return report(L, status);
}
@@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) {
switch (option) {
case 'e': case 'l': {
int status;
- const char *extra = argv[i] + 2; /* both options need an argument */
+ char *extra = argv[i] + 2; /* both options need an argument */
if (*extra == '\0') extra = argv[++i];
lua_assert(extra != NULL);
status = (option == 'e')
diff --git a/testes/main.lua b/testes/main.lua
index 56959abd..52c77954 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -190,6 +190,11 @@ prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
checkout("1\n2\n15\n2\n15\n")
+-- test explicit global names in -l
+prepfile("print(str.upper'alo alo', m.max(10, 20))")
+RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
+checkout("0.0\nALO ALO\t20\n")
+
-- test 'arg' table
local a = [[
assert(#arg == 3 and arg[1] == 'a' and