aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-05-15 13:46:38 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-05-15 13:46:38 -0300
commit09f3c2372f5dbeaec9f50614a26c1b5761726a88 (patch)
tree83469dd02dba1c59ead0eb0cba3a35bd39ec899d
parentc197885cb00b85251c35cffdc4057efaee2d7a88 (diff)
downloadlua-09f3c2372f5dbeaec9f50614a26c1b5761726a88.tar.gz
lua-09f3c2372f5dbeaec9f50614a26c1b5761726a88.tar.bz2
lua-09f3c2372f5dbeaec9f50614a26c1b5761726a88.zip
Option '-l' discards version sufix from file name
Like 'require', the command-line option '-l' discards an optional version suffix (everything after an hyphen) from a file name when creating the module name.
-rw-r--r--loadlib.c9
-rw-r--r--lua.c12
-rw-r--r--luaconf.h9
-rw-r--r--testes/main.lua7
4 files changed, 26 insertions, 11 deletions
diff --git a/loadlib.c b/loadlib.c
index d792dffa..6d289fce 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -25,15 +25,6 @@
25 25
26 26
27/* 27/*
28** LUA_IGMARK is a mark to ignore all before it when building the
29** luaopen_ function name.
30*/
31#if !defined (LUA_IGMARK)
32#define LUA_IGMARK "-"
33#endif
34
35
36/*
37** LUA_CSUBSEP is the character that replaces dots in submodule names 28** LUA_CSUBSEP is the character that replaces dots in submodule names
38** when searching for a C loader. 29** when searching for a C loader.
39** LUA_LSUBSEP is the character that replaces dots in submodule names 30** LUA_LSUBSEP is the character that replaces dots in submodule names
diff --git a/lua.c b/lua.c
index 0ff88454..3af5ce6a 100644
--- a/lua.c
+++ b/lua.c
@@ -210,12 +210,17 @@ static int dostring (lua_State *L, const char *s, const char *name) {
210 210
211/* 211/*
212** Receives 'globname[=modname]' and runs 'globname = require(modname)'. 212** Receives 'globname[=modname]' and runs 'globname = require(modname)'.
213** If there is no explicit modname and globname contains a '-', cut
214** the sufix after '-' (the "version") to make the global name.
213*/ 215*/
214static int dolibrary (lua_State *L, char *globname) { 216static int dolibrary (lua_State *L, char *globname) {
215 int status; 217 int status;
218 char *suffix = NULL;
216 char *modname = strchr(globname, '='); 219 char *modname = strchr(globname, '=');
217 if (modname == NULL) /* no explicit name? */ 220 if (modname == NULL) { /* no explicit name? */
218 modname = globname; /* module name is equal to global name */ 221 modname = globname; /* module name is equal to global name */
222 suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */
223 }
219 else { 224 else {
220 *modname = '\0'; /* global name ends here */ 225 *modname = '\0'; /* global name ends here */
221 modname++; /* module name starts after the '=' */ 226 modname++; /* module name starts after the '=' */
@@ -223,8 +228,11 @@ static int dolibrary (lua_State *L, char *globname) {
223 lua_getglobal(L, "require"); 228 lua_getglobal(L, "require");
224 lua_pushstring(L, modname); 229 lua_pushstring(L, modname);
225 status = docall(L, 1, 1); /* call 'require(modname)' */ 230 status = docall(L, 1, 1); /* call 'require(modname)' */
226 if (status == LUA_OK) 231 if (status == LUA_OK) {
232 if (suffix != NULL) /* is there a suffix mark? */
233 *suffix = '\0'; /* remove sufix from global name */
227 lua_setglobal(L, globname); /* globname = require(modname) */ 234 lua_setglobal(L, globname); /* globname = require(modname) */
235 }
228 return report(L, status); 236 return report(L, status);
229} 237}
230 238
diff --git a/luaconf.h b/luaconf.h
index 137103ed..acebe29c 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -257,6 +257,15 @@
257 257
258#endif 258#endif
259 259
260
261/*
262** LUA_IGMARK is a mark to ignore all after it when building the
263** module name (e.g., used to build the luaopen_ function name).
264** Typically, the sufix after the mark is the module version,
265** as in "mod-v1.2.so".
266*/
267#define LUA_IGMARK "-"
268
260/* }================================================================== */ 269/* }================================================================== */
261 270
262 271
diff --git a/testes/main.lua b/testes/main.lua
index 3fa94e97..11b14b44 100644
--- a/testes/main.lua
+++ b/testes/main.lua
@@ -225,6 +225,13 @@ prepfile("print(str.upper'alo alo', m.max(10, 20))")
225RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out) 225RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
226checkout("0.0\nALO ALO\t20\n") 226checkout("0.0\nALO ALO\t20\n")
227 227
228
229-- test module names with version sufix ("libs/lib2-v2")
230RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s",
231 out)
232checkout("true\n")
233
234
228-- test 'arg' table 235-- test 'arg' table
229local a = [[ 236local a = [[
230 assert(#arg == 3 and arg[1] == 'a' and 237 assert(#arg == 3 and arg[1] == 'a' and