Skip to content

Commit

Permalink
print: add callbacks at calling print() function
Browse files Browse the repository at this point in the history
Those callbacks will be set from the console module (which integrates
readline into tarantool), so the print() output won't mix with readline
prompting.

Part of #7169

NO_DOC=no user visible changes, it is internal API
NO_TEST=will be tested in a next commit together with the console
        callbacks itself
NO_CHANGELOG=no user visible changes
  • Loading branch information
Totktonada committed Sep 28, 2022
1 parent 15386e4 commit b6ba22d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -64,6 +64,7 @@ lua_source(lua_sources lua/iconv.lua iconv_lua)
lua_source(lua_sources lua/swim.lua swim_lua)
lua_source(lua_sources lua/datetime.lua datetime_lua)
lua_source(lua_sources lua/timezones.lua timezones_lua)
lua_source(lua_sources lua/print.lua print_lua)

# LuaJIT jit.* library
lua_source(lua_sources ${LUAJIT_SOURCE_ROOT}/src/jit/bc.lua jit_bc_lua)
Expand Down
4 changes: 3 additions & 1 deletion src/lua/init.c
Expand Up @@ -154,7 +154,8 @@ extern char strict_lua[],
sysprof_collapse_lua[],
sysprof_lua[],
datetime_lua[],
timezones_lua[]
timezones_lua[],
print_lua[]
#if defined(EMBED_LUAROCKS)
, luarocks_core_hardcoded_lua[],
luarocks_admin_cache_lua[],
Expand Down Expand Up @@ -308,6 +309,7 @@ static const char *lua_modules[] = {
"sysprof", sysprof_lua,
"timezones", timezones_lua,
"datetime", datetime_lua,
"internal.print", print_lua,
NULL
};

Expand Down
17 changes: 17 additions & 0 deletions src/lua/print.lua
@@ -0,0 +1,17 @@
local M = {}

M.raw_print = print

-- before_cb() and after_cb() must not raise an error, must not
-- yield. It would break expectations about the print() function.
_G.print = function(...)
if M.before_cb ~= nil then
M.before_cb()
end
M.raw_print(...)
if M.after_cb ~= nil then
M.after_cb()
end
end

return M
47 changes: 47 additions & 0 deletions test/luajit-test-init.lua
Expand Up @@ -42,6 +42,53 @@ function _dofile(filename)
return dofile(path_to_sources..filename)
end

-- Tarantool has its own print() function.
--
-- There are tests, which check something around a Lua/C function
-- and use print() as a well known example of such function. The
-- easiest way to mitigate problems in the tests is to replace
-- print() back to the original one.
--
-- Our own print() is a simple wrapper around the original print()
-- and performs some extra actions only in the interactive mode.
-- It seems more or less safe to skip testing of the wrapper on
-- LuaJIT's test suites.
--
-- Examples from LuaJIT test suites that fail with tarantool's
-- print() function:
--
-- | PUC-Rio-Lua-5.1-tests/db.lua
-- | ----------------------------
-- | local a = debug.getinfo(print)
-- | assert(a.what == "C" and a.short_src == "[C]")
-- | -- In tarantool: a.what == "Lua"
-- | -- In tarantool: a.short_src == "builtin/internal.print.lua"
--
-- | lua-Harness-tests/301-basic.t
-- | -----------------------------
-- | setfenv(print, {})
-- | -- Expected error: 'setfenv' cannot change environment of
-- | -- given object.
-- | -- In tarantool: success.
--
-- | lua-Harness-tests/304-string.t
-- | ------------------------------
-- | string.dump(print)
-- | -- Expected error: unable to dump given function.
-- | -- In tarantool: success.
--
-- | lua-Harness-tests/304-string.t
-- | ------------------------------
-- | tostring(print)
-- | Expected: 'function: builtin#29' (or similar).
-- | In tarantool: 'function: 0x40e88018' (or similar).
local print_M = package.loaded['internal.print']
if print_M ~= nil then
rawset(_G, 'print', print_M.raw_print)
assert(print ~= nil)
assert(type(print) == 'function')
end

-- This is workaround introduced for flaky macosx tests reported by
-- https://github.com/tarantool/tarantool/issues/7058
collectgarbage('collect')

0 comments on commit b6ba22d

Please sign in to comment.