diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-26 14:59:39 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-26 14:59:39 -0300 |
| commit | b80077b8f3e27a94c6afa895b41a9f8b52c42e61 (patch) | |
| tree | 8a7c21ee0acfed553ef1d92bdfd7b1f728f35a91 /testes | |
| parent | e70f275f32a5339a86be6f8b9d08c20cb874b205 (diff) | |
| download | lua-b80077b8f3e27a94c6afa895b41a9f8b52c42e61.tar.gz lua-b80077b8f3e27a94c6afa895b41a9f8b52c42e61.tar.bz2 lua-b80077b8f3e27a94c6afa895b41a9f8b52c42e61.zip | |
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'.)
Diffstat (limited to 'testes')
| -rw-r--r-- | testes/api.lua | 17 | ||||
| -rw-r--r-- | testes/coroutine.lua | 2 | ||||
| -rw-r--r-- | testes/events.lua | 15 | ||||
| -rw-r--r-- | testes/strings.lua | 7 |
4 files changed, 36 insertions, 5 deletions
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) | |||
| 241 | a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) | 241 | a,b = T.testC("compare LE 5 -6, return 2", a1, 2, 2, a1, 2, 20) |
| 242 | assert(a == 20 and b == true) | 242 | assert(a == 20 and b == true) |
| 243 | 243 | ||
| 244 | |||
| 245 | do -- testing lessthan and lessequal with metamethods | ||
| 246 | local mt = {__lt = function (a,b) return a[1] < b[1] end, | ||
| 247 | __le = function (a,b) return a[1] <= b[1] end, | ||
| 248 | __eq = function (a,b) return a[1] == b[1] end} | ||
| 249 | local function O (x) | ||
| 250 | return setmetatable({x}, mt) | ||
| 251 | end | ||
| 252 | |||
| 253 | local a, b = T.testC("compare LT 2 3; pushint 10; return 2", O(1), O(2)) | ||
| 254 | assert(a == true and b == 10) | ||
| 255 | local a, b = T.testC("compare LE 2 3; pushint 10; return 2", O(3), O(2)) | ||
| 256 | assert(a == false and b == 10) | ||
| 257 | local a, b = T.testC("compare EQ 2 3; pushint 10; return 2", O(3), O(3)) | ||
| 258 | assert(a == true and b == 10) | ||
| 259 | end | ||
| 260 | |||
| 244 | -- testing length | 261 | -- testing length |
| 245 | local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) | 262 | local t = setmetatable({x = 20}, {__len = function (t) return t.x end}) |
| 246 | a,b,c = T.testC([[ | 263 | 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 () | |||
| 809 | -- tests for coroutine API | 809 | -- tests for coroutine API |
| 810 | if T==nil then | 810 | if T==nil then |
| 811 | (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') | 811 | (Message or print)('\n >>> testC not active: skipping coroutine API tests <<<\n') |
| 812 | return | 812 | print "OK"; return |
| 813 | end | 813 | end |
| 814 | 814 | ||
| 815 | print('testing coroutine API') | 815 | 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) | |||
| 217 | return a<=b, "dummy" | 217 | return a<=b, "dummy" |
| 218 | end | 218 | end |
| 219 | 219 | ||
| 220 | t.__eq = function (a,b,c) | ||
| 221 | assert(c == nil) | ||
| 222 | if type(a) == 'table' then a = a.x end | ||
| 223 | if type(b) == 'table' then b = b.x end | ||
| 224 | return a == b, "dummy" | ||
| 225 | end | ||
| 226 | |||
| 220 | function Op(x) return setmetatable({x=x}, t) end | 227 | function Op(x) return setmetatable({x=x}, t) end |
| 221 | 228 | ||
| 222 | local function test () | 229 | local function test (a, b, c) |
| 223 | assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1))) | 230 | assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1))) |
| 224 | assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1))) | 231 | assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1))) |
| 225 | assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a'))) | 232 | assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a'))) |
| @@ -232,9 +239,13 @@ local function test () | |||
| 232 | assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) | 239 | assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1)) |
| 233 | assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) | 240 | assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a'))) |
| 234 | assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) | 241 | assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a'))) |
| 242 | assert(Op(1) == Op(1) and Op(1) ~= Op(2)) | ||
| 243 | assert(Op('a') == Op('a') and Op('a') ~= Op('b')) | ||
| 244 | assert(a == a and a ~= b) | ||
| 245 | assert(Op(3) == c) | ||
| 235 | end | 246 | end |
| 236 | 247 | ||
| 237 | test() | 248 | test(Op(1), Op(2), Op(3)) |
| 238 | 249 | ||
| 239 | 250 | ||
| 240 | -- test `partial order' | 251 | -- 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 | |||
| 167 | local t1 = {}; local t2 = {} | 167 | local t1 = {}; local t2 = {} |
| 168 | assert(string.format("%p", t1) ~= string.format("%p", t2)) | 168 | assert(string.format("%p", t1) ~= string.format("%p", t2)) |
| 169 | end | 169 | end |
| 170 | assert(string.format("%p", string.rep("a", 10)) == | 170 | do -- short strings |
| 171 | string.format("%p", string.rep("a", 10))) -- short strings | 171 | local s1 = string.rep("a", 10) |
| 172 | local s2 = string.rep("a", 10) | ||
| 173 | assert(string.format("%p", s1) == string.format("%p", s2)) | ||
| 174 | end | ||
| 172 | do -- long strings | 175 | do -- long strings |
| 173 | local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) | 176 | local s1 = string.rep("a", 300); local s2 = string.rep("a", 300) |
| 174 | assert(string.format("%p", s1) ~= string.format("%p", s2)) | 177 | assert(string.format("%p", s1) ~= string.format("%p", s2)) |
