Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65434b4

Browse files
committed
Option '-l' can give a name for the global variable.
Sintax for this option now is '-l [globname=]modname'.
1 parent 59acd79 commit 65434b4

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lua.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,15 @@ static void print_usage (const char *badoption) {
8989
lua_writestringerror(
9090
"usage: %s [options] [script [args]]\n"
9191
"Available options are:\n"
92-
" -e stat execute string 'stat'\n"
93-
" -i enter interactive mode after executing 'script'\n"
94-
" -l name require library 'name' into global 'name'\n"
95-
" -v show version information\n"
96-
" -E ignore environment variables\n"
97-
" -W turn warnings on\n"
98-
" -- stop handling options\n"
99-
" - stop handling options and execute stdin\n"
92+
" -e stat execute string 'stat'\n"
93+
" -i enter interactive mode after executing 'script'\n"
94+
" -l mod require library 'mod' into global 'mod'\n"
95+
" -l g=mod require library 'mod' into global 'g'\n"
96+
" -v show version information\n"
97+
" -E ignore environment variables\n"
98+
" -W turn warnings on\n"
99+
" -- stop handling options\n"
100+
" - stop handling options and execute stdin\n"
100101
,
101102
progname);
102103
}
@@ -207,16 +208,22 @@ static int dostring (lua_State *L, const char *s, const char *name) {
207208

208209

209210
/*
210-
** Calls 'require(name)' and stores the result in a global variable
211-
** with the given name.
211+
** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
212212
*/
213-
static int dolibrary (lua_State *L, const char *name) {
213+
static int dolibrary (lua_State *L, char *globname) {
214214
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+
}
215222
lua_getglobal(L, "require");
216-
lua_pushstring(L, name);
217-
status = docall(L, 1, 1); /* call 'require(name)' */
223+
lua_pushstring(L, modname);
224+
status = docall(L, 1, 1); /* call 'require(modname)' */
218225
if (status == LUA_OK)
219-
lua_setglobal(L, name); /* global[name] = require return */
226+
lua_setglobal(L, globname); /* globname = require(modname) */
220227
return report(L, status);
221228
}
222229

@@ -327,7 +334,7 @@ static int runargs (lua_State *L, char **argv, int n) {
327334
switch (option) {
328335
case 'e': case 'l': {
329336
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 */
331338
if (*extra == '\0') extra = argv[++i];
332339
lua_assert(extra != NULL);
333340
status = (option == 'e')

testes/main.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
190190
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
191191
checkout("1\n2\n15\n2\n15\n")
192192

193+
-- test explicit global names in -l
194+
prepfile("print(str.upper'alo alo', m.max(10, 20))")
195+
RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
196+
checkout("0.0\nALO ALO\t20\n")
197+
193198
-- test 'arg' table
194199
local a = [[
195200
assert(#arg == 3 and arg[1] == 'a' and

0 commit comments

Comments
 (0)