aboutsummaryrefslogtreecommitdiff
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-65434b4d1b5509e95940939e28fd90d4558da12e.tar.gz
lua-65434b4d1b5509e95940939e28fd90d4558da12e.tar.bz2
lua-65434b4d1b5509e95940939e28fd90d4558da12e.zip
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) {
89 lua_writestringerror( 89 lua_writestringerror(
90 "usage: %s [options] [script [args]]\n" 90 "usage: %s [options] [script [args]]\n"
91 "Available options are:\n" 91 "Available options are:\n"
92 " -e stat execute string 'stat'\n" 92 " -e stat execute string 'stat'\n"
93 " -i enter interactive mode after executing 'script'\n" 93 " -i enter interactive mode after executing 'script'\n"
94 " -l name require library 'name' into global 'name'\n" 94 " -l mod require library 'mod' into global 'mod'\n"
95 " -v show version information\n" 95 " -l g=mod require library 'mod' into global 'g'\n"
96 " -E ignore environment variables\n" 96 " -v show version information\n"
97 " -W turn warnings on\n" 97 " -E ignore environment variables\n"
98 " -- stop handling options\n" 98 " -W turn warnings on\n"
99 " - stop handling options and execute stdin\n" 99 " -- stop handling options\n"
100 " - stop handling options and execute stdin\n"
100 , 101 ,
101 progname); 102 progname);
102} 103}
@@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) {
207 208
208 209
209/* 210/*
210** Calls 'require(name)' and stores the result in a global variable 211** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
211** with the given name.
212*/ 212*/
213static int dolibrary (lua_State *L, const char *name) { 213static int dolibrary (lua_State *L, char *globname) {
214 int status; 214 int status;
215 char *modname = strchr(globname, '=');
216 if (modname == NULL) /* no explicit name? */
217 modname = globname; /* module name is equal to global name */
218 else {
219 *modname = '\0'; /* global name ends here */
220 modname++; /* module name starts after the '=' */
221 }
215 lua_getglobal(L, "require"); 222 lua_getglobal(L, "require");
216 lua_pushstring(L, name); 223 lua_pushstring(L, modname);
217 status = docall(L, 1, 1); /* call 'require(name)' */ 224 status = docall(L, 1, 1); /* call 'require(modname)' */
218 if (status == LUA_OK) 225 if (status == LUA_OK)
219 lua_setglobal(L, name); /* global[name] = require return */ 226 lua_setglobal(L, globname); /* globname = require(modname) */
220 return report(L, status); 227 return report(L, status);
221} 228}
222 229
@@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) {
327 switch (option) { 334 switch (option) {
328 case 'e': case 'l': { 335 case 'e': case 'l': {
329 int status; 336 int status;
330 const char *extra = argv[i] + 2; /* both options need an argument */ 337 char *extra = argv[i] + 2; /* both options need an argument */
331 if (*extra == '\0') extra = argv[++i]; 338 if (*extra == '\0') extra = argv[++i];
332 lua_assert(extra != NULL); 339 lua_assert(extra != NULL);
333 status = (option == 'e') 340 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)
190RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out) 190RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
191checkout("1\n2\n15\n2\n15\n") 191checkout("1\n2\n15\n2\n15\n")
192 192
193-- test explicit global names in -l
194prepfile("print(str.upper'alo alo', m.max(10, 20))")
195RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
196checkout("0.0\nALO ALO\t20\n")
197
193-- test 'arg' table 198-- test 'arg' table
194local a = [[ 199local a = [[
195 assert(#arg == 3 and arg[1] == 'a' and 200 assert(#arg == 3 and arg[1] == 'a' and