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'.)
This commit is contained in:
@@ -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([[
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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 (Op(1)<Op(2)) and not(Op(2)<Op(1)))
|
||||
assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
|
||||
assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
|
||||
@@ -232,9 +239,13 @@ local function test ()
|
||||
assert((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'
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user