Skip to content

Commit 09f3c23

Browse files
committed
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.
1 parent c197885 commit 09f3c23

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

loadlib.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@
2424
#include "lualib.h"
2525

2626

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-
3627
/*
3728
** LUA_CSUBSEP is the character that replaces dots in submodule names
3829
** when searching for a C loader.

lua.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,29 @@ static int dostring (lua_State *L, const char *s, const char *name) {
210210

211211
/*
212212
** 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.
213215
*/
214216
static int dolibrary (lua_State *L, char *globname) {
215217
int status;
218+
char *suffix = NULL;
216219
char *modname = strchr(globname, '=');
217-
if (modname == NULL) /* no explicit name? */
220+
if (modname == NULL) { /* no explicit name? */
218221
modname = globname; /* module name is equal to global name */
222+
suffix = strchr(modname, *LUA_IGMARK); /* look for a suffix mark */
223+
}
219224
else {
220225
*modname = '\0'; /* global name ends here */
221226
modname++; /* module name starts after the '=' */
222227
}
223228
lua_getglobal(L, "require");
224229
lua_pushstring(L, modname);
225230
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 */
227234
lua_setglobal(L, globname); /* globname = require(modname) */
235+
}
228236
return report(L, status);
229237
}
230238

luaconf.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@
257257

258258
#endif
259259

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+
260269
/* }================================================================== */
261270

262271

testes/main.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ prepfile("print(str.upper'alo alo', m.max(10, 20))")
225225
RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
226226
checkout("0.0\nALO ALO\t20\n")
227227

228+
229+
-- test module names with version sufix ("libs/lib2-v2")
230+
RUN("env LUA_CPATH='./libs/?.so' lua -l lib2-v2 -e 'print(lib2.id())' > %s",
231+
out)
232+
checkout("true\n")
233+
234+
228235
-- test 'arg' table
229236
local a = [[
230237
assert(#arg == 3 and arg[1] == 'a' and

0 commit comments

Comments
 (0)