From b80077b8f3e27a94c6afa895b41a9f8b52c42e61 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 26 Jul 2019 14:59:39 -0300 Subject: Change in the handling of 'L->top' when calling metamethods Instead of updating 'L->top' in every place that may call a metamethod, the metamethod functions themselves (luaT_trybinTM and luaT_callorderTM) correct the top. (When calling metamethods from the C API, however, the callers must preserve 'L->top'.) --- testes/api.lua | 17 +++++++++++++++++ testes/coroutine.lua | 2 +- testes/events.lua | 15 +++++++++++++-- testes/strings.lua | 7 +++++-- 4 files changed, 36 insertions(+), 5 deletions(-) (limited to 'testes') diff --git a/testes/api.lua b/testes/api.lua index 5da03641..0966ed19 100644 --- a/testes/api.lua +++ b/testes/api.lua @@ -241,6 +241,23 @@ assert(a == 20 and b == false) a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) assert(a == 20 and b == true) + +do -- testing lessthan and lessequal with metamethods + local mt = {__lt = function (a,b) return a[1] < b[1] end, + __le = function (a,b) return a[1] <= b[1] end, + __eq = function (a,b) return a[1] == b[1] end} + local function O (x) + return setmetatable({x}, mt) + end + + local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2)) + assert(a == true and b == 10) + local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2)) + assert(a == false and b == 10) + local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3)) + assert(a == true and b == 10) +end + -- testing length local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) a,b,c = T.testC([[ diff --git a/testes/coroutine.lua b/testes/coroutine.lua index e04207c8..00531d8e 100644 --- a/testes/coroutine.lua +++ b/testes/coroutine.lua @@ -809,7 +809,7 @@ assert(run(function () -- tests for coroutine API if T==nil then (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') - return + print "OK"; return end print('testing coroutine API') diff --git a/testes/events.lua b/testes/events.lua index cf68d1e9..7fb54c9a 100644 --- a/testes/events.lua +++ b/testes/events.lua @@ -217,9 +217,16 @@ t.__le = function (a,b,c) return a<=b, "dummy" end +t.__eq = function (a,b,c) + assert(c == nil) + if type(a) == 'table' then a = a.x end + if type(b) == 'table' then b = b.x end + return a == b, "dummy" +end + function Op(x) return setmetatable({x=x}, t) end -local function test () +local function test (a, b, c) assert(not(Op(1)= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) + assert(Op(1) == Op(1) and Op(1) ~= Op(2)) + assert(Op('a') == Op('a') and Op('a') ~= Op('b')) + assert(a == a and a ~= b) + assert(Op(3) == c) end -test() +test(Op(1), Op(2), Op(3)) -- test `partial order' diff --git a/testes/strings.lua b/testes/strings.lua index 0e7874bf..aa039c4f 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -167,8 +167,11 @@ do -- tests for '%p' format local t1 = {}; local t2 = {} assert(string.format("%p", t1) ~= string.format("%p", t2)) end - assert(string.format("%p", string.rep("a", 10)) == - string.format("%p", string.rep("a", 10))) -- short strings + do -- short strings + local s1 = string.rep("a", 10) + local s2 = string.rep("a", 10) + assert(string.format("%p", s1) == string.format("%p", s2)) + end do -- long strings local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) assert(string.format("%p", s1) ~= string.format("%p", s2)) -- cgit v1.2.3-55-g6feb