From f58cc96f2798ddc5f492223897d0b77e4f902128 Mon Sep 17 00:00:00 2001
From: Nikolay Shirokovskiy <nshirokovskiy@tarantool.org>
Date: Thu, 31 Aug 2023 14:39:28 +0300
Subject: [PATCH] lua: provide tarantool build info before loading lua modules

This way we will have access to build info in those modules. In
particularly build.asan flag is going to be used in buffer.lua in scope
of #7327.

Part of #7327

NO_TEST=internal
NO_DOC=internal
NO_CHANGELOG=internal
---
 src/lua/init.c   | 14 ++------------
 src/lua/init.lua | 11 ++++++++---
 src/lua/utils.c  |  5 +++--
 3 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/src/lua/init.c b/src/lua/init.c
index 12600f80d2..5ffdaf86a9 100644
--- a/src/lua/init.c
+++ b/src/lua/init.c
@@ -795,15 +795,7 @@ luaopen_tarantool(lua_State *L)
 	lua_pushstring(L, tarantool_version());
 	lua_setfield(L, LUA_GLOBALSINDEX, "_TARANTOOL");
 
-	/*
-	 * Get tarantool module.
-	 *
-	 * src/lua/init.lua is already registered as tarantool
-	 * module, so we `require` it here, not create.
-	 */
-	lua_getfield(L, LUA_GLOBALSINDEX, "require");
-	lua_pushstring(L, "tarantool");
-	lua_call(L, 1, 1);
+	luaT_newmodule(L, "tarantool", NULL);
 
 	/* package */
 	lua_pushstring(L, tarantool_package());
@@ -998,6 +990,7 @@ tarantool_lua_init(const char *tarantool_bin, const char *script, int argc,
 #if defined(ENABLE_BACKTRACE)
 	backtrace_lua_init();
 #endif /* defined(ENABLE_BACKTRACE) */
+	luaopen_tarantool(L);
 	for (const char **s = lua_modules; *s; s += 2) {
 		const char *modname = *s;
 		const char *modsrc = *(s + 1);
@@ -1018,9 +1011,6 @@ tarantool_lua_init(const char *tarantool_bin, const char *script, int argc,
 		builtin_modcache_put(modname, modsrc);
 	}
 	lua_pop(L, 1); /* _PRELOAD */
-
-	luaopen_tarantool(L);
-
 #ifdef NDEBUG
 	/* Unload strict after boot in release mode */
 	if (luaL_dostring(L, "require('strict').off()") != 0)
diff --git a/src/lua/init.lua b/src/lua/init.lua
index f42274a1e4..352e7a8c37 100644
--- a/src/lua/init.lua
+++ b/src/lua/init.lua
@@ -1,8 +1,8 @@
-
 -- init.lua -- internal file
 
 local ffi = require('ffi')
 local loaders = require('internal.loaders')
+local tarantool = require('tarantool')
 
 ffi.cdef[[
 struct method_info;
@@ -211,7 +211,7 @@ local mt = {
     __serialize = filter_out_private_fields,
 }
 
-return setmetatable({
+local tarantool_lua = {
     uptime = uptime,
     pid = pid,
     _internal = {
@@ -219,4 +219,9 @@ return setmetatable({
         module_name_from_filename = module_name_from_filename,
         run_preload = run_preload,
     },
-}, mt)
+}
+-- tarantool module is already registered by src/lua/init.c
+for k, v in pairs(tarantool_lua) do
+    tarantool[k] = v
+end
+return setmetatable(tarantool, mt)
diff --git a/src/lua/utils.c b/src/lua/utils.c
index ad6fe27849..66d24bc1cb 100644
--- a/src/lua/utils.c
+++ b/src/lua/utils.c
@@ -406,7 +406,7 @@ luaT_newmodule(struct lua_State *L, const char *modname,
 	       const struct luaL_Reg *funcs)
 {
 	say_debug("%s(%s)", __func__, modname);
-	assert(modname != NULL && funcs != NULL);
+	assert(modname != NULL);
 
 	/* Get loaders.builtin. */
 	lua_getfield(L, LUA_REGISTRYINDEX, "_TARANTOOL_BUILTIN");
@@ -422,7 +422,8 @@ luaT_newmodule(struct lua_State *L, const char *modname,
 	lua_newtable(L);
 
 	/* Fill the module table with functions. */
-	luaL_setfuncs(L, funcs, 0);
+	if (funcs != NULL)
+		luaL_setfuncs(L, funcs, 0);
 
 	/* Copy the module table. */
 	lua_pushvalue(L, -1);
-- 
GitLab