Add __lt, __le, __eq metatag processing.

This commit is contained in:
James Roseborough
2010-08-18 22:17:13 +00:00
parent 4d4517dd58
commit 4acf8ed2ce
2 changed files with 95 additions and 22 deletions

View File

@@ -8,9 +8,15 @@ for i=1,#values do
print( debug.getmetatable( values[i] ) )
end
local ts = tostring
local tb,count = {},0
tostring = function(o)
local t = type(o)
return (t=='thread' or t=='function') and t or ts(o)
if t~='thread' and t~='function' then return ts(o) end
if not tb[o] then
count = count + 1
tb[o] = t..'.'..count
end
return tb[o]
end
local buildunop = function(name)
@@ -40,6 +46,9 @@ local mt = {
__mod=buildbinop('mod'),
__unm=buildunop('unm'),
__len=buildunop('neg'),
__eq=buildbinop('eq'),
__lt=buildbinop('lt'),
__le=buildbinop('le'),
}
-- pcall a function and check for a pattern in the error string
@@ -107,4 +116,48 @@ for i=1,#values do
print( debug.setmetatable( values[i], nil ) )
end
print( '---- __eq, __lt, __le, same types' )
local bfunction = function() end
local bthread = coroutine.create( bfunction )
local groups
groups = { {afunction, bfunction}, {true, true}, {true, false}, {afunction, bfunction}, {athread, bthread}, }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
print( type(values[i]), 'before', pcall( function() return a==b end ) )
print( type(values[i]), 'before', pcall( function() return a~=b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a<b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a>b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, mt ) )
print( debug.setmetatable( b, mt ) )
print( type(values[i]), 'after', pcall( function() return a==b end ) )
print( type(values[i]), 'after', pcall( function() return a~=b end ) )
print( type(values[i]), 'after', pcall( function() return a<b end ) )
print( type(values[i]), 'after', pcall( function() return a<=b end ) )
print( type(values[i]), 'after', pcall( function() return a>b end ) )
print( type(values[i]), 'after', pcall( function() return a>=b end ) )
print( debug.setmetatable( a, nil ) )
print( debug.setmetatable( b, nil ) )
end
print( '---- __eq, __lt, __le, different types' )
groups = { {aboolean, athread}, }
for i=1,#groups do
local a,b = groups[i][1], groups[i][2]
print( type(values[i]), 'before', pcall( function() return a==b end ) )
print( type(values[i]), 'before', pcall( function() return a~=b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a<b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a>b end ) )
print( type(values[i]), 'before', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, mt ) )
print( debug.setmetatable( b, mt ) )
print( type(values[i]), 'after-a', pcall( function() return a==b end ) )
print( type(values[i]), 'after-a', pcall( function() return a~=b end ) )
print( type(values[i]), 'after-a', ecall( 'attempt to compare', function() return a<b end ) )
print( type(values[i]), 'after-a', ecall( 'attempt to compare', function() return a<=b end ) )
print( type(values[i]), 'after-a', ecall( 'attempt to compare', function() return a>b end ) )
print( type(values[i]), 'after-a', ecall( 'attempt to compare', function() return a>=b end ) )
print( debug.setmetatable( a, nil ) )
print( debug.setmetatable( b, nil ) )
end