From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 0C36B6E0CA; Mon, 27 Dec 2021 13:27:20 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 0C36B6E0CA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1640600840; bh=KIYnCMVcvgQBxebjw+BsJLQhYY8e877Ofl8vr3QUne0=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=PmRyrf2x2UBeccphWQ/bEfbDDFDgz1GOKtQX7b42uX7385z36bJCX0fyhxd4xgUom bzTCVDET7PGCHy9u0gi2Rw8ZB8WFV+5SBqCpStsDrG5JJ9QTUXse1ElARVrlPX3KVo WZ0gZYz4NmdGaQ5Rg6JcZ5r2mIxpBy7tJNdPM+l4= Received: from smtp36.i.mail.ru (smtp36.i.mail.ru [94.100.177.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 48BEC6E0CA for ; Mon, 27 Dec 2021 13:27:17 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 48BEC6E0CA Received: by smtp36.i.mail.ru with esmtpa (envelope-from ) id 1n1nDo-0003JJ-7n; Mon, 27 Dec 2021 13:27:16 +0300 To: Sergey Ostanevich , Igor Munkin Date: Mon, 27 Dec 2021 13:25:24 +0300 Message-Id: <20211227102524.9410-1-skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD94D5EF110843E6A67488A001FE7488196A3A9897144145DE2182A05F53808504007F26BF621C2F6A303032AC8FF92463208EF3C93E005FA4F1FCDC6844EFD143B X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7C4FDA87F74E4A9F6EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F79006372B7E36617B0C54A78638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D86A1B3CFBC621AF53B42D788F34276441117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC3A703B70628EAD7BA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F446042972877693876707352033AC447995A7AD182CC0D3CB04F14752D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B60A62CEF541B197C8089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: C20DE7B7AB408E4181F030C43753B8186998911F362727C414F749A5E30D975C5B1EC9170548837293A5A7EFFEC0666875726A23FB6FD7EF9C2B6934AE262D3EE7EAB7254005DCED221D5A8161B58CB51E0A4E2319210D9B64D260DF9561598F01A9E91200F654B0588BE6097D4A97A98E8E86DC7131B365E7726E8460B7C23C X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D3468964757141B82E03421F7448CBC4944933476836EBEA1FEE8F6F3105B03570F7121C342E03F6EE61D7E09C32AA3244C2F264AF9BAB1C7625FE51380D741F3494DBEAD0ED6C55A80927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojAMkO8M8XKUvfCgLPZkmJ8A== X-Mailru-Sender: 3B9A0136629DC91206CBC582EFEF4CB4844C66FB2E1EFCC5BF8472E5FAF514E5349F599D2452B6F1F2400F607609286E924004A7DEC283833C7120B22964430C52B393F8C72A41A84198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit] Add stricter check for print() vs. tostring() shortcut. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall (cherry picked from 46a1b268eb0534182eda0447303c344a071632fe) `print()` function uses tostring to convert all incoming values to the strings. So, if element to convert has `__tostring` metamethod it must be used. In LuaJIT `print()` base library function has a shortcut to detect fast path for strings and numbers. This shortcut doesn't check metatable presence for numbers, so `__tostring` metamethod is ignored. This patch adds the corresponding check for metatable on base number type, when the aforementioned shortcut is set. Sergey Kaplun: * added the description and the test for the problem Part of tarantool/tarantool#6548 --- Issue: https://github.com/tarantool/tarantool/issues/6548 Related issue: https://github.com/tarantool/tarantool/issues/6746 Branch: https://github.com/tarantool/luajit/tree/skaplun/gh-noticket-print-tostring-number-mt-full-ci Tarantool branch: https://github.com/tarantool/tarantool/tree/skaplun/gh-noticket-print-tostring-number-mt-full-ci CI is red due to integration tests failes (same as on master) or due to connection errors. src/lib_base.c | 3 +- .../print-tostring-number.test.lua | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/print-tostring-number.test.lua diff --git a/src/lib_base.c b/src/lib_base.c index 3a757870..d58de10b 100644 --- a/src/lib_base.c +++ b/src/lib_base.c @@ -493,7 +493,8 @@ LJLIB_CF(print) lua_gettable(L, LUA_GLOBALSINDEX); tv = L->top-1; } - shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring); + shortcut = (tvisfunc(tv) && funcV(tv)->c.ffid == FF_tostring) + && !gcrefu(basemt_it(G(L), LJ_TNUMX)); for (i = 0; i < nargs; i++) { cTValue *o = &L->base[i]; const char *str; diff --git a/test/tarantool-tests/print-tostring-number.test.lua b/test/tarantool-tests/print-tostring-number.test.lua new file mode 100644 index 00000000..862bd0f9 --- /dev/null +++ b/test/tarantool-tests/print-tostring-number.test.lua @@ -0,0 +1,45 @@ +local tap = require('tap') + +local test = tap.test('print-tostring-number') + +-- First field is type, second -- content of cmd. +local test_data = { + {'nil', 'nil'}, + {'boolean', 'true'}, + {'userdata', 'newproxy()'}, + {'number', '42'}, + -- FIXME: This test case is disabled, because __tostring + -- metamethod isn't checked for string base metatable. + -- See also https://github.com/tarantool/tarantool/issues/6746. + -- {'string', '"teststr"'}, + {'table', '{}'}, + {'function', 'function() end'}, + {'thread', 'coroutine.create(function() end)'}, +} + +local NTEST = #test_data +test:plan(NTEST) + +local i = 0 +while arg[i] do i = i - 1 end +local luabin = arg[i + 1] + +for j = 1, NTEST do + local prefix = '__tostring reloaded for ' + local datatype = test_data[j][1] + local expected = prefix .. datatype + + local cmd = luabin .. ([[ -e ' + local testvar = %s + debug.setmetatable(testvar, {__tostring = function(a) + return "%s" .. type(a) + end}) + print(testvar) + ']]):format(test_data[j][2], prefix) + + local proc = io.popen(cmd) + local res = proc:read('*all'):gsub('%s+$', '') + test:ok(res == expected, expected) +end + +os.exit(test:check() and 0 or 1) -- 2.34.1