Skip to content
Snippets Groups Projects
Commit 01a4af8d authored by Alexander Turenko's avatar Alexander Turenko Committed by Igor Munkin
Browse files

lua: add internal modules initialization status

The flag indicates, whether the loading of built-in modules is finished.
May be useful for override modules to determine, whether it is required
from inside tarantool or from application's code.

Follows up #7774

NO_DOC=It is not intended to be officially supported. I would prefer to
       document ability of loading a replacement module itself, but I
       wouldn't document how to write such a module.
NO_CHANGELOG=see NO_DOC
parent 009d3d9b
No related branches found
No related tags found
No related merge requests found
......@@ -967,6 +967,27 @@ tarantool_lua_init(const char *tarantool_bin, const char *script, int argc,
tarantool_L = L;
}
int
tarantool_lua_postinit(struct lua_State *L)
{
/*
* loaders.initializing = nil
*
* The loaders module set the `initializing` field to
* `true` at first load (during tarantool_lua_init()).
* Now it is time to set it to `nil` to state that all
* the built-in modules are loaded.
*/
lua_getfield(L, LUA_GLOBALSINDEX, "require");
lua_pushstring(L, "internal.loaders");
lua_call(L, 1, 1);
lua_pushnil(L);
lua_setfield(L, -2, "initializing");
/* Pop internal.loaders table. */
lua_pop(L, 1);
return 0;
}
char *history = NULL;
struct slab_cache *
......
......@@ -53,6 +53,12 @@ void
tarantool_lua_init(const char *tarantool_bin, const char *script, int argc,
char **argv);
/**
* A code that runs after loading of all built-in modules.
*/
int
tarantool_lua_postinit(struct lua_State *L);
/** Free Lua subsystem resources. */
void
tarantool_lua_free();
......
......@@ -383,4 +383,7 @@ return {
override_builtin_disable = function()
override_loader_is_enabled = false
end,
-- It is `true` during tarantool initialization, but once all
-- the built-in modules are ready, will be set to `nil`.
initializing = true,
}
......@@ -804,6 +804,7 @@ main(int argc, char **argv)
try {
box_init();
box_lua_init(tarantool_L);
tarantool_lua_postinit(tarantool_L);
/*
* Reserve a fiber to run on_shutdown triggers.
*/
......
local t = require('luatest')
local treegen = require('test.treegen')
local justrun = require('test.justrun')
local g = t.group()
local OVERRIDE_SCRIPT_TEMPLATE = [[
local loaders = require('internal.loaders')
return {
whoami = 'override.<module_name>',
initializing = loaders.initializing,
}
]]
-- Print a result of the require call.
local MAIN_SCRIPT_TEMPLATE = [[
local json = require('json')
local loaders = require('internal.loaders')
print(json.encode({
['<module_name>'] = require('<module_name>'),
['<script>'] = {
whoami = '<script>',
initializing = loaders.initializing,
}
}))
]]
g.before_all(function(g)
treegen.init(g)
treegen.add_template(g, '^override/.*%.lua$', OVERRIDE_SCRIPT_TEMPLATE)
treegen.add_template(g, '^main%.lua$', MAIN_SCRIPT_TEMPLATE)
end)
g.after_all(function(g)
treegen.clean(g)
end)
local function expected_output(module_name)
local res = {
{
[module_name] = {
whoami = ('override.%s'):format(module_name),
initializing = true,
},
['main.lua'] = {
whoami = 'main.lua',
-- initializing is nil
}
}
}
return {
exit_code = 0,
stdout = res,
}
end
g.test_initializing = function(g)
local scripts = {'override/socket.lua', 'main.lua'}
local replacements = {module_name = 'socket'}
local dir = treegen.prepare_directory(g, scripts, replacements)
local res = justrun.tarantool(dir, {}, {'main.lua'})
local exp = expected_output('socket')
t.assert_equals(res, exp)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment