Skip to content

Commit 9617f31

Browse files
committed
lua: don't use public module name as internal one
There are modules that are implemented as two parts: a Lua/C module for internal use and a public module written on Lua. There is a practice to name both parts the same: just capture the internal part within the public part and rewrite `package.loaded` then. This name overlap is confusing at reading the sources and complicates debugging. And it conflicts with a built-in module loading logic that will be implemented for tarantool#7774. Let's use `foo.lib` for the internal part and `foo` for the public one. This approach is already used in some built-in modules. src/box/lua/upgrade.lua requires src/box/lua/xlog.lua, so changed the loading order. Eliminated extra `internal` field in `uri.lib`, becase the whole module is internal. Part of tarantool#7774 NO_DOC=user visible behavior is unchanged, pure refactoring change NO_TEST=see NO_DOC NO_CHANGELOG=see NO_DOC
1 parent 675d05b commit 9617f31

17 files changed

+22
-32
lines changed

src/box/lua/console.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ tarantool_lua_console_init(struct lua_State *L)
883883
{"run_on_eval", lbox_console_run_on_eval},
884884
{NULL, NULL}
885885
};
886-
luaT_newmodule(L, "console", consolelib);
886+
luaT_newmodule(L, "console.lib", consolelib);
887887

888888
/* readline() func needs a ref to completion_handler (in upvalue) */
889889
lua_getfield(L, -1, "completion_handler");

src/box/lua/console.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ffi.cdef[[
1717
console_set_output_format(enum output_format output_format);
1818
]]
1919

20-
local internal = require('console')
20+
local internal = require('console.lib')
2121
local session_internal = box.internal.session
2222
local fiber = require('fiber')
2323
local socket = require('socket')

src/box/lua/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ static const char *lua_sources[] = {
159159
#if ENABLE_SECURITY
160160
"box/security", security_lua,
161161
#endif
162+
"box/xlog", xlog_lua,
162163
"box/upgrade", upgrade_lua,
163164
"box/net_box", net_box_lua,
164165
"box/console", console_lua,
165166
"box/load_cfg", load_cfg_lua,
166-
"box/xlog", xlog_lua,
167167
"box/key_def", key_def_lua,
168168
"box/merger", merger_lua,
169169
NULL

src/box/lua/xlog.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
/* {{{ Helpers */
4848

4949
static uint32_t CTID_STRUCT_XLOG_CURSOR_REF = 0;
50-
static const char *xloglib_name = "xlog";
5150

5251
static int
5352
lbox_pushcursor(struct lua_State *L, struct xlog_cursor *cur)
@@ -328,7 +327,7 @@ box_lua_xlog_init(struct lua_State *L)
328327
CTID_STRUCT_XLOG_CURSOR_REF = luaL_ctypeid(L, "struct xlog_cursor&");
329328
assert(CTID_STRUCT_XLOG_CURSOR_REF != 0);
330329

331-
luaT_newmodule(L, xloglib_name, lbox_xlog_parser_lib);
330+
luaT_newmodule(L, "xlog.lib", lbox_xlog_parser_lib);
332331

333332
lua_newtable(L);
334333
lua_setmetatable(L, -2);

src/box/lua/xlog.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local internal = require('xlog')
1+
local internal = require('xlog.lib')
22
local fun = require('fun')
33
local function xlog_pairs(...)
44
return fun.wrap(internal.pairs(...))

src/lua/digest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,6 @@ tarantool_lua_digest_init(struct lua_State *L)
163163
lua_pushcfunction(L, luaopen_crc32_internal);
164164
lua_setfield(L, -2, "crc32.internal");
165165
lua_pop(L, 1);
166-
luaT_newmodule(L, "digest", lua_digest_methods);
166+
luaT_newmodule(L, "digest.lib", lua_digest_methods);
167167
lua_pop(L, 1);
168168
};

src/lua/digest.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ local digest_shortcuts = {
9090
md5 = 'MD5',
9191
md4 = 'MD4',
9292
}
93-
local internal = require("digest")
93+
local internal = require("digest.lib")
9494

9595
local PMurHash
9696
local PMurHash_methods = {

src/lua/errno.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ tarantool_lua_errno_init(struct lua_State *L)
286286
static const luaL_Reg errnolib[] = {
287287
{ NULL, NULL}
288288
};
289-
luaT_newmodule(L, "errno", errnolib);
289+
luaT_newmodule(L, "errno.lib", errnolib);
290290
for (int i = 0; i < (int)lengthof(elist); i++) {
291291
lua_pushstring(L, elist[i].name);
292292
lua_pushinteger(L, elist[i].value);

src/lua/errno.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local ffi = require('ffi')
2-
local errno_list = require('errno')
2+
local errno_list = require('errno.lib')
33

44
ffi.cdef[[
55
const char *tt_strerror(int errnum);

src/lua/httpc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,6 @@ LUA_API int
452452
luaopen_http_client_driver(lua_State *L)
453453
{
454454
luaL_register_type(L, DRIVER_LUA_UDATA_NAME, Client);
455-
luaT_newmodule(L, "http.client", Module);
455+
luaT_newmodule(L, "http.client.lib", Module);
456456
return 1;
457457
}

src/lua/httpc.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
-- SUCH DAMAGE.
3030
--
3131

32-
local driver = require('http.client')
32+
local driver = require('http.client.lib')
3333

3434
local json = require('json')
3535
local yaml = require('yaml')

src/lua/socket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ tarantool_lua_socket_init(struct lua_State *L)
10351035
{ NULL, NULL }
10361036
};
10371037

1038-
luaT_newmodule(L, "socket", internal_methods);
1038+
luaT_newmodule(L, "socket.lib", internal_methods);
10391039

10401040
/* domains table */
10411041
lua_pushliteral(L, "DOMAIN");

src/lua/socket.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ local LIMIT_INFINITY = 2147483647
55

66
local ffi = require('ffi')
77
local boxerrno = require('errno')
8-
local internal = require('socket')
8+
local internal = require('socket.lib')
99
local fiber = require('fiber')
1010
local fio = require('fio')
1111
local log = require('log')

src/lua/swim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,6 @@ tarantool_lua_swim_init(struct lua_State *L)
117117
{"swim_on_member_event", lua_swim_on_member_event},
118118
{NULL, NULL}
119119
};
120-
luaT_newmodule(L, "swim", lua_swim_internal_methods);
120+
luaT_newmodule(L, "swim.lib", lua_swim_internal_methods);
121121
lua_pop(L, 1);
122122
}

src/lua/swim.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local buffer = require('buffer')
44
local msgpack = require('msgpack')
55
local crypto = require('crypto')
66
local fiber = require('fiber')
7-
local internal = require('swim')
7+
local internal = require('swim.lib')
88
local schedule_task = fiber._internal.schedule_task
99
local cord_ibuf_take = buffer.internal.cord_ibuf_take
1010
local cord_ibuf_put = buffer.internal.cord_ibuf_put

src/lua/uri.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ luaT_uri_create_internal(lua_State *L)
301301
{
302302
struct uri *uri = (struct uri *)lua_topointer(L, 1);
303303
if (uri == NULL)
304-
luaL_error(L, "Usage: uri.internal.uri_create(string|table)");
304+
luaL_error(L, "Usage: uri_lib.uri_create(string|table)");
305305
if (luaT_uri_create(L, 2, uri) != 0)
306306
luaT_error(L);
307307
return 0;
@@ -312,7 +312,7 @@ luaT_uri_set_create_internal(lua_State *L)
312312
{
313313
struct uri_set *uri_set = (struct uri_set *)lua_topointer(L, 1);
314314
if (uri_set == NULL)
315-
luaL_error(L, "Usage: uri.internal.uri_set_create(string|table)");
315+
luaL_error(L, "Usage: uri_lib.uri_set_create(string|table)");
316316
if (luaT_uri_set_create(L, 2, uri_set) != 0)
317317
luaT_error(L);
318318
return 0;
@@ -321,21 +321,12 @@ luaT_uri_set_create_internal(lua_State *L)
321321
void
322322
tarantool_lua_uri_init(struct lua_State *L)
323323
{
324-
static const struct luaL_Reg uri_methods[] = {
325-
{NULL, NULL}
326-
};
327-
luaT_newmodule(L, "uri", uri_methods);
328-
329324
/* internal table */
330-
lua_pushliteral(L, "internal");
331-
lua_newtable(L);
332-
static const struct luaL_Reg uri_internal_methods[] = {
325+
static const struct luaL_Reg uri_methods[] = {
333326
{"uri_create", luaT_uri_create_internal},
334327
{"uri_set_create", luaT_uri_set_create_internal},
335328
{NULL, NULL}
336329
};
337-
luaT_setfuncs(L, uri_internal_methods);
338-
lua_settable(L, -3);
339-
330+
luaT_newmodule(L, "uri.lib", uri_methods);
340331
lua_pop(L, 1);
341332
};

src/lua/uri.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
local ffi = require('ffi')
44
local buffer = require('buffer')
5-
local uri = require('uri')
5+
local internal = require('uri.lib')
66

77
local uri_cdef = [[
88
struct uri_param {
@@ -165,7 +165,7 @@ local function parse(str)
165165
error("Usage: uri.parse(string|table)")
166166
end
167167
local uribuf = uri_stash_take()
168-
local status, errmsg = pcall(uri.internal.uri_create, uribuf, str)
168+
local status, errmsg = pcall(internal.uri_create, uribuf, str)
169169
if not status then
170170
uri_stash_put(uribuf)
171171
return nil, errmsg
@@ -181,7 +181,7 @@ local function parse_many(str)
181181
error("Usage: uri.parse_many(string|table)")
182182
end
183183
local uri_set_buf = uri_set_stash_take()
184-
local status, errmsg = pcall(uri.internal.uri_set_create, uri_set_buf, str)
184+
local status, errmsg = pcall(internal.uri_set_create, uri_set_buf, str)
185185
if not status then
186186
uri_set_stash_put(uri_set_buf)
187187
return nil, errmsg

0 commit comments

Comments
 (0)