Skip to content

Commit

Permalink
lua: don't use public module name as internal one
Browse files Browse the repository at this point in the history
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`, because 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
  • Loading branch information
Totktonada committed Jan 18, 2023
1 parent c70df22 commit 86045a9
Show file tree
Hide file tree
Showing 17 changed files with 22 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/box/lua/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/box/lua/console.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/box/lua/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/box/lua/xlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/box/lua/xlog.lua
Original file line number Diff line number Diff line change
@@ -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(...))
Expand Down
2 changes: 1 addition & 1 deletion src/lua/digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
2 changes: 1 addition & 1 deletion src/lua/digest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion src/lua/errno.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lua/errno.lua
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lua/httpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/lua/httpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/lua/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/lua/socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/lua/swim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion src/lua/swim.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions src/lua/uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
};
6 changes: 3 additions & 3 deletions src/lua/uri.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 86045a9

Please sign in to comment.