From 86045a9910e83aa0e47cfc0c9d3f79d910571e94 Mon Sep 17 00:00:00 2001 From: Alexander Turenko Date: Wed, 18 Jan 2023 02:09:46 +0300 Subject: [PATCH] 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 #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`, because the whole module is internal. Part of #7774 NO_DOC=user visible behavior is unchanged, pure refactoring change NO_TEST=see NO_DOC NO_CHANGELOG=see NO_DOC --- src/box/lua/console.c | 2 +- src/box/lua/console.lua | 2 +- src/box/lua/init.c | 2 +- src/box/lua/xlog.c | 3 +-- src/box/lua/xlog.lua | 2 +- src/lua/digest.c | 2 +- src/lua/digest.lua | 2 +- src/lua/errno.c | 2 +- src/lua/errno.lua | 2 +- src/lua/httpc.c | 2 +- src/lua/httpc.lua | 2 +- src/lua/socket.c | 2 +- src/lua/socket.lua | 2 +- src/lua/swim.c | 2 +- src/lua/swim.lua | 2 +- src/lua/uri.c | 17 ++++------------- src/lua/uri.lua | 6 +++--- 17 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/box/lua/console.c b/src/box/lua/console.c index e90e63da413bc..9784bd63c44c8 100644 --- a/src/box/lua/console.c +++ b/src/box/lua/console.c @@ -883,7 +883,7 @@ tarantool_lua_console_init(struct lua_State *L) {"run_on_eval", lbox_console_run_on_eval}, {NULL, NULL} }; - luaT_newmodule(L, "console", consolelib); + luaT_newmodule(L, "console.lib", consolelib); /* readline() func needs a ref to completion_handler (in upvalue) */ lua_getfield(L, -1, "completion_handler"); diff --git a/src/box/lua/console.lua b/src/box/lua/console.lua index 403ceaea85ef7..60defbe6c5a03 100644 --- a/src/box/lua/console.lua +++ b/src/box/lua/console.lua @@ -17,7 +17,7 @@ ffi.cdef[[ console_set_output_format(enum output_format output_format); ]] -local internal = require('console') +local internal = require('console.lib') local session_internal = box.internal.session local fiber = require('fiber') local socket = require('socket') diff --git a/src/box/lua/init.c b/src/box/lua/init.c index 333ed0b0dfccc..7c9ee6e3ce0a9 100644 --- a/src/box/lua/init.c +++ b/src/box/lua/init.c @@ -159,11 +159,11 @@ static const char *lua_sources[] = { #if ENABLE_SECURITY "box/security", security_lua, #endif + "box/xlog", xlog_lua, "box/upgrade", upgrade_lua, "box/net_box", net_box_lua, "box/console", console_lua, "box/load_cfg", load_cfg_lua, - "box/xlog", xlog_lua, "box/key_def", key_def_lua, "box/merger", merger_lua, NULL diff --git a/src/box/lua/xlog.c b/src/box/lua/xlog.c index 698e72fecd574..ed8a8ecea2a96 100644 --- a/src/box/lua/xlog.c +++ b/src/box/lua/xlog.c @@ -47,7 +47,6 @@ /* {{{ Helpers */ static uint32_t CTID_STRUCT_XLOG_CURSOR_REF = 0; -static const char *xloglib_name = "xlog"; static int lbox_pushcursor(struct lua_State *L, struct xlog_cursor *cur) @@ -328,7 +327,7 @@ box_lua_xlog_init(struct lua_State *L) CTID_STRUCT_XLOG_CURSOR_REF = luaL_ctypeid(L, "struct xlog_cursor&"); assert(CTID_STRUCT_XLOG_CURSOR_REF != 0); - luaT_newmodule(L, xloglib_name, lbox_xlog_parser_lib); + luaT_newmodule(L, "xlog.lib", lbox_xlog_parser_lib); lua_newtable(L); lua_setmetatable(L, -2); diff --git a/src/box/lua/xlog.lua b/src/box/lua/xlog.lua index 0145092715602..9e78c0cc0df7a 100644 --- a/src/box/lua/xlog.lua +++ b/src/box/lua/xlog.lua @@ -1,4 +1,4 @@ -local internal = require('xlog') +local internal = require('xlog.lib') local fun = require('fun') local function xlog_pairs(...) return fun.wrap(internal.pairs(...)) diff --git a/src/lua/digest.c b/src/lua/digest.c index ac532ff639cb0..b8aa3fb350a3d 100644 --- a/src/lua/digest.c +++ b/src/lua/digest.c @@ -163,6 +163,6 @@ tarantool_lua_digest_init(struct lua_State *L) lua_pushcfunction(L, luaopen_crc32_internal); lua_setfield(L, -2, "crc32.internal"); lua_pop(L, 1); - luaT_newmodule(L, "digest", lua_digest_methods); + luaT_newmodule(L, "digest.lib", lua_digest_methods); lua_pop(L, 1); }; diff --git a/src/lua/digest.lua b/src/lua/digest.lua index c58e2f5cde6a8..4e04ce60b672f 100644 --- a/src/lua/digest.lua +++ b/src/lua/digest.lua @@ -90,7 +90,7 @@ local digest_shortcuts = { md5 = 'MD5', md4 = 'MD4', } -local internal = require("digest") +local internal = require("digest.lib") local PMurHash local PMurHash_methods = { diff --git a/src/lua/errno.c b/src/lua/errno.c index 843672c2afff9..a4e2de0643233 100644 --- a/src/lua/errno.c +++ b/src/lua/errno.c @@ -286,7 +286,7 @@ tarantool_lua_errno_init(struct lua_State *L) static const luaL_Reg errnolib[] = { { NULL, NULL} }; - luaT_newmodule(L, "errno", errnolib); + luaT_newmodule(L, "errno.lib", errnolib); for (int i = 0; i < (int)lengthof(elist); i++) { lua_pushstring(L, elist[i].name); lua_pushinteger(L, elist[i].value); diff --git a/src/lua/errno.lua b/src/lua/errno.lua index ebc41a6aa02a1..48733e14dda90 100644 --- a/src/lua/errno.lua +++ b/src/lua/errno.lua @@ -1,5 +1,5 @@ local ffi = require('ffi') -local errno_list = require('errno') +local errno_list = require('errno.lib') ffi.cdef[[ const char *tt_strerror(int errnum); diff --git a/src/lua/httpc.c b/src/lua/httpc.c index 22e017d3a9452..90ce110df92b7 100644 --- a/src/lua/httpc.c +++ b/src/lua/httpc.c @@ -452,6 +452,6 @@ LUA_API int luaopen_http_client_driver(lua_State *L) { luaL_register_type(L, DRIVER_LUA_UDATA_NAME, Client); - luaT_newmodule(L, "http.client", Module); + luaT_newmodule(L, "http.client.lib", Module); return 1; } diff --git a/src/lua/httpc.lua b/src/lua/httpc.lua index 1aea6cfe768ea..6f5a2f5101ad3 100644 --- a/src/lua/httpc.lua +++ b/src/lua/httpc.lua @@ -29,7 +29,7 @@ -- SUCH DAMAGE. -- -local driver = require('http.client') +local driver = require('http.client.lib') local json = require('json') local yaml = require('yaml') diff --git a/src/lua/socket.c b/src/lua/socket.c index 78ea4059cd307..f8256db49abc1 100644 --- a/src/lua/socket.c +++ b/src/lua/socket.c @@ -1035,7 +1035,7 @@ tarantool_lua_socket_init(struct lua_State *L) { NULL, NULL } }; - luaT_newmodule(L, "socket", internal_methods); + luaT_newmodule(L, "socket.lib", internal_methods); /* domains table */ lua_pushliteral(L, "DOMAIN"); diff --git a/src/lua/socket.lua b/src/lua/socket.lua index 43c37a84be9a9..56671728a4732 100644 --- a/src/lua/socket.lua +++ b/src/lua/socket.lua @@ -5,7 +5,7 @@ local LIMIT_INFINITY = 2147483647 local ffi = require('ffi') local boxerrno = require('errno') -local internal = require('socket') +local internal = require('socket.lib') local fiber = require('fiber') local fio = require('fio') local log = require('log') diff --git a/src/lua/swim.c b/src/lua/swim.c index c1e3eaf2eca53..836fd95ff8e6b 100644 --- a/src/lua/swim.c +++ b/src/lua/swim.c @@ -117,6 +117,6 @@ tarantool_lua_swim_init(struct lua_State *L) {"swim_on_member_event", lua_swim_on_member_event}, {NULL, NULL} }; - luaT_newmodule(L, "swim", lua_swim_internal_methods); + luaT_newmodule(L, "swim.lib", lua_swim_internal_methods); lua_pop(L, 1); } diff --git a/src/lua/swim.lua b/src/lua/swim.lua index 027f07038c12e..e39f877d60b14 100644 --- a/src/lua/swim.lua +++ b/src/lua/swim.lua @@ -4,7 +4,7 @@ local buffer = require('buffer') local msgpack = require('msgpack') local crypto = require('crypto') local fiber = require('fiber') -local internal = require('swim') +local internal = require('swim.lib') local schedule_task = fiber._internal.schedule_task local cord_ibuf_take = buffer.internal.cord_ibuf_take local cord_ibuf_put = buffer.internal.cord_ibuf_put diff --git a/src/lua/uri.c b/src/lua/uri.c index 141ac022b738b..eb24194b730fd 100644 --- a/src/lua/uri.c +++ b/src/lua/uri.c @@ -301,7 +301,7 @@ luaT_uri_create_internal(lua_State *L) { struct uri *uri = (struct uri *)lua_topointer(L, 1); if (uri == NULL) - luaL_error(L, "Usage: uri.internal.uri_create(string|table)"); + luaL_error(L, "Usage: uri_lib.uri_create(string|table)"); if (luaT_uri_create(L, 2, uri) != 0) luaT_error(L); return 0; @@ -312,7 +312,7 @@ luaT_uri_set_create_internal(lua_State *L) { struct uri_set *uri_set = (struct uri_set *)lua_topointer(L, 1); if (uri_set == NULL) - luaL_error(L, "Usage: uri.internal.uri_set_create(string|table)"); + luaL_error(L, "Usage: uri_lib.uri_set_create(string|table)"); if (luaT_uri_set_create(L, 2, uri_set) != 0) luaT_error(L); return 0; @@ -321,21 +321,12 @@ luaT_uri_set_create_internal(lua_State *L) void tarantool_lua_uri_init(struct lua_State *L) { - static const struct luaL_Reg uri_methods[] = { - {NULL, NULL} - }; - luaT_newmodule(L, "uri", uri_methods); - /* internal table */ - lua_pushliteral(L, "internal"); - lua_newtable(L); - static const struct luaL_Reg uri_internal_methods[] = { + static const struct luaL_Reg uri_methods[] = { {"uri_create", luaT_uri_create_internal}, {"uri_set_create", luaT_uri_set_create_internal}, {NULL, NULL} }; - luaT_setfuncs(L, uri_internal_methods); - lua_settable(L, -3); - + luaT_newmodule(L, "uri.lib", uri_methods); lua_pop(L, 1); }; diff --git a/src/lua/uri.lua b/src/lua/uri.lua index 1a9c449906ea1..709a92b7822b5 100644 --- a/src/lua/uri.lua +++ b/src/lua/uri.lua @@ -2,7 +2,7 @@ local ffi = require('ffi') local buffer = require('buffer') -local uri = require('uri') +local internal = require('uri.lib') local uri_cdef = [[ struct uri_param { @@ -165,7 +165,7 @@ local function parse(str) error("Usage: uri.parse(string|table)") end local uribuf = uri_stash_take() - local status, errmsg = pcall(uri.internal.uri_create, uribuf, str) + local status, errmsg = pcall(internal.uri_create, uribuf, str) if not status then uri_stash_put(uribuf) return nil, errmsg @@ -181,7 +181,7 @@ local function parse_many(str) error("Usage: uri.parse_many(string|table)") end local uri_set_buf = uri_set_stash_take() - local status, errmsg = pcall(uri.internal.uri_set_create, uri_set_buf, str) + local status, errmsg = pcall(internal.uri_set_create, uri_set_buf, str) if not status then uri_set_stash_put(uri_set_buf) return nil, errmsg