2
0

events.lua 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. -- $Id: testes/events.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing metatables')
  4. local debug = require'debug'
  5. X = 20; B = 30
  6. _ENV = setmetatable({}, {__index=_G})
  7. collectgarbage()
  8. X = X+10
  9. assert(X == 30 and _G.X == 20)
  10. B = false
  11. assert(B == false)
  12. _ENV["B"] = undef
  13. assert(B == 30)
  14. assert(getmetatable{} == nil)
  15. assert(getmetatable(4) == nil)
  16. assert(getmetatable(nil) == nil)
  17. a={name = "NAME"}; setmetatable(a, {__metatable = "xuxu",
  18. __tostring=function(x) return x.name end})
  19. assert(getmetatable(a) == "xuxu")
  20. assert(tostring(a) == "NAME")
  21. -- cannot change a protected metatable
  22. assert(pcall(setmetatable, a, {}) == false)
  23. a.name = "gororoba"
  24. assert(tostring(a) == "gororoba")
  25. local a, t = {10,20,30; x="10", y="20"}, {}
  26. assert(setmetatable(a,t) == a)
  27. assert(getmetatable(a) == t)
  28. assert(setmetatable(a,nil) == a)
  29. assert(getmetatable(a) == nil)
  30. assert(setmetatable(a,t) == a)
  31. function f (t, i, e)
  32. assert(not e)
  33. local p = rawget(t, "parent")
  34. return (p and p[i]+3), "dummy return"
  35. end
  36. t.__index = f
  37. a.parent = {z=25, x=12, [4] = 24}
  38. assert(a[1] == 10 and a.z == 28 and a[4] == 27 and a.x == "10")
  39. collectgarbage()
  40. a = setmetatable({}, t)
  41. function f(t, i, v) rawset(t, i, v-3) end
  42. setmetatable(t, t) -- causes a bug in 5.1 !
  43. t.__newindex = f
  44. a[1] = 30; a.x = "101"; a[5] = 200
  45. assert(a[1] == 27 and a.x == 98 and a[5] == 197)
  46. do -- bug in Lua 5.3.2
  47. local mt = {}
  48. mt.__newindex = mt
  49. local t = setmetatable({}, mt)
  50. t[1] = 10 -- will segfault on some machines
  51. assert(mt[1] == 10)
  52. end
  53. local c = {}
  54. a = setmetatable({}, t)
  55. t.__newindex = c
  56. t.__index = c
  57. a[1] = 10; a[2] = 20; a[3] = 90;
  58. for i = 4, 20 do a[i] = i * 10 end
  59. assert(a[1] == 10 and a[2] == 20 and a[3] == 90)
  60. for i = 4, 20 do assert(a[i] == i * 10) end
  61. assert(next(a) == nil)
  62. do
  63. local a;
  64. a = setmetatable({}, {__index = setmetatable({},
  65. {__index = setmetatable({},
  66. {__index = function (_,n) return a[n-3]+4, "lixo" end})})})
  67. a[0] = 20
  68. for i=0,10 do
  69. assert(a[i*3] == 20 + i*4)
  70. end
  71. end
  72. do -- newindex
  73. local foi
  74. local a = {}
  75. for i=1,10 do a[i] = 0; a['a'..i] = 0; end
  76. setmetatable(a, {__newindex = function (t,k,v) foi=true; rawset(t,k,v) end})
  77. foi = false; a[1]=0; assert(not foi)
  78. foi = false; a['a1']=0; assert(not foi)
  79. foi = false; a['a11']=0; assert(foi)
  80. foi = false; a[11]=0; assert(foi)
  81. foi = false; a[1]=undef; assert(not foi)
  82. a[1] = undef
  83. foi = false; a[1]=nil; assert(foi)
  84. end
  85. setmetatable(t, nil)
  86. function f (t, ...) return t, {...} end
  87. t.__call = f
  88. do
  89. local x,y = a(table.unpack{'a', 1})
  90. assert(x==a and y[1]=='a' and y[2]==1 and y[3]==undef)
  91. x,y = a()
  92. assert(x==a and y[1]==undef)
  93. end
  94. local b = setmetatable({}, t)
  95. setmetatable(b,t)
  96. function f(op)
  97. return function (...) cap = {[0] = op, ...} ; return (...) end
  98. end
  99. t.__add = f("add")
  100. t.__sub = f("sub")
  101. t.__mul = f("mul")
  102. t.__div = f("div")
  103. t.__idiv = f("idiv")
  104. t.__mod = f("mod")
  105. t.__unm = f("unm")
  106. t.__pow = f("pow")
  107. t.__len = f("len")
  108. t.__band = f("band")
  109. t.__bor = f("bor")
  110. t.__bxor = f("bxor")
  111. t.__shl = f("shl")
  112. t.__shr = f("shr")
  113. t.__bnot = f("bnot")
  114. t.__lt = f("lt")
  115. t.__le = f("le")
  116. local function checkcap (t)
  117. assert(#cap + 1 == #t)
  118. for i = 1, #t do
  119. assert(cap[i - 1] == t[i])
  120. assert(math.type(cap[i - 1]) == math.type(t[i]))
  121. end
  122. end
  123. -- Some tests are done inside small anonymous functions to ensure
  124. -- that constants go to constant table even in debug compilation,
  125. -- when the constant table is very small.
  126. assert(b+5 == b); checkcap{"add", b, 5}
  127. assert(5.2 + b == 5.2); checkcap{"add", 5.2, b}
  128. assert(b+'5' == b); checkcap{"add", b, '5'}
  129. assert(5+b == 5); checkcap{"add", 5, b}
  130. assert('5'+b == '5'); checkcap{"add", '5', b}
  131. b=b-3; assert(getmetatable(b) == t); checkcap{"sub", b, 3}
  132. assert(5-a == 5); checkcap{"sub", 5, a}
  133. assert('5'-a == '5'); checkcap{"sub", '5', a}
  134. assert(a*a == a); checkcap{"mul", a, a}
  135. assert(a/0 == a); checkcap{"div", a, 0}
  136. assert(a/0.0 == a); checkcap{"div", a, 0.0}
  137. assert(a%2 == a); checkcap{"mod", a, 2}
  138. assert(a // (1/0) == a); checkcap{"idiv", a, 1/0}
  139. ;(function () assert(a & "hi" == a) end)(); checkcap{"band", a, "hi"}
  140. ;(function () assert(10 & a == 10) end)(); checkcap{"band", 10, a}
  141. ;(function () assert(a | 10 == a) end)(); checkcap{"bor", a, 10}
  142. assert(a | "hi" == a); checkcap{"bor", a, "hi"}
  143. assert("hi" ~ a == "hi"); checkcap{"bxor", "hi", a}
  144. ;(function () assert(10 ~ a == 10) end)(); checkcap{"bxor", 10, a}
  145. assert(-a == a); checkcap{"unm", a, a}
  146. assert(a^4.0 == a); checkcap{"pow", a, 4.0}
  147. assert(a^'4' == a); checkcap{"pow", a, '4'}
  148. assert(4^a == 4); checkcap{"pow", 4, a}
  149. assert('4'^a == '4'); checkcap{"pow", '4', a}
  150. assert(#a == a); checkcap{"len", a, a}
  151. assert(~a == a); checkcap{"bnot", a, a}
  152. assert(a << 3 == a); checkcap{"shl", a, 3}
  153. assert(1.5 >> a == 1.5); checkcap{"shr", 1.5, a}
  154. -- for comparsion operators, all results are true
  155. assert(5.0 > a); checkcap{"lt", a, 5.0}
  156. assert(a >= 10); checkcap{"le", 10, a}
  157. assert(a <= -10.0); checkcap{"le", a, -10.0}
  158. assert(a < -10); checkcap{"lt", a, -10}
  159. -- test for rawlen
  160. t = setmetatable({1,2,3}, {__len = function () return 10 end})
  161. assert(#t == 10 and rawlen(t) == 3)
  162. assert(rawlen"abc" == 3)
  163. assert(not pcall(rawlen, io.stdin))
  164. assert(not pcall(rawlen, 34))
  165. assert(not pcall(rawlen))
  166. -- rawlen for long strings
  167. assert(rawlen(string.rep('a', 1000)) == 1000)
  168. t = {}
  169. t.__lt = function (a,b,c)
  170. collectgarbage()
  171. assert(c == nil)
  172. if type(a) == 'table' then a = a.x end
  173. if type(b) == 'table' then b = b.x end
  174. return a<b, "dummy"
  175. end
  176. t.__le = function (a,b,c)
  177. assert(c == nil)
  178. if type(a) == 'table' then a = a.x end
  179. if type(b) == 'table' then b = b.x end
  180. return a<=b, "dummy"
  181. end
  182. function Op(x) return setmetatable({x=x}, t) end
  183. local function test ()
  184. assert(not(Op(1)<Op(1)) and (Op(1)<Op(2)) and not(Op(2)<Op(1)))
  185. assert(not(1 < Op(1)) and (Op(1) < 2) and not(2 < Op(1)))
  186. assert(not(Op('a')<Op('a')) and (Op('a')<Op('b')) and not(Op('b')<Op('a')))
  187. assert(not('a' < Op('a')) and (Op('a') < 'b') and not(Op('b') < Op('a')))
  188. assert((Op(1)<=Op(1)) and (Op(1)<=Op(2)) and not(Op(2)<=Op(1)))
  189. assert((Op('a')<=Op('a')) and (Op('a')<=Op('b')) and not(Op('b')<=Op('a')))
  190. assert(not(Op(1)>Op(1)) and not(Op(1)>Op(2)) and (Op(2)>Op(1)))
  191. assert(not(Op('a')>Op('a')) and not(Op('a')>Op('b')) and (Op('b')>Op('a')))
  192. assert((Op(1)>=Op(1)) and not(Op(1)>=Op(2)) and (Op(2)>=Op(1)))
  193. assert((1 >= Op(1)) and not(1 >= Op(2)) and (Op(2) >= 1))
  194. assert((Op('a')>=Op('a')) and not(Op('a')>=Op('b')) and (Op('b')>=Op('a')))
  195. assert(('a' >= Op('a')) and not(Op('a') >= 'b') and (Op('b') >= Op('a')))
  196. end
  197. test()
  198. -- test `partial order'
  199. local function rawSet(x)
  200. local y = {}
  201. for _,k in pairs(x) do y[k] = 1 end
  202. return y
  203. end
  204. local function Set(x)
  205. return setmetatable(rawSet(x), t)
  206. end
  207. t.__lt = function (a,b)
  208. for k in pairs(a) do
  209. if not b[k] then return false end
  210. b[k] = undef
  211. end
  212. return next(b) ~= nil
  213. end
  214. t.__le = function (a,b)
  215. for k in pairs(a) do
  216. if not b[k] then return false end
  217. end
  218. return true
  219. end
  220. assert(Set{1,2,3} < Set{1,2,3,4})
  221. assert(not(Set{1,2,3,4} < Set{1,2,3,4}))
  222. assert((Set{1,2,3,4} <= Set{1,2,3,4}))
  223. assert((Set{1,2,3,4} >= Set{1,2,3,4}))
  224. assert(not (Set{1,3} <= Set{3,5}))
  225. assert(not(Set{1,3} <= Set{3,5}))
  226. assert(not(Set{1,3} >= Set{3,5}))
  227. t.__eq = function (a,b)
  228. for k in pairs(a) do
  229. if not b[k] then return false end
  230. b[k] = undef
  231. end
  232. return next(b) == nil
  233. end
  234. local s = Set{1,3,5}
  235. assert(s == Set{3,5,1})
  236. assert(not rawequal(s, Set{3,5,1}))
  237. assert(rawequal(s, s))
  238. assert(Set{1,3,5,1} == rawSet{3,5,1})
  239. assert(rawSet{1,3,5,1} == Set{3,5,1})
  240. assert(Set{1,3,5} ~= Set{3,5,1,6})
  241. -- '__eq' is not used for table accesses
  242. t[Set{1,3,5}] = 1
  243. assert(t[Set{1,3,5}] == undef)
  244. if not T then
  245. (Message or print)('\n >>> testC not active: skipping tests for \z
  246. userdata <<<\n')
  247. else
  248. local u1 = T.newuserdata(0, 1)
  249. local u2 = T.newuserdata(0, 1)
  250. local u3 = T.newuserdata(0, 1)
  251. assert(u1 ~= u2 and u1 ~= u3)
  252. debug.setuservalue(u1, 1);
  253. debug.setuservalue(u2, 2);
  254. debug.setuservalue(u3, 1);
  255. debug.setmetatable(u1, {__eq = function (a, b)
  256. return debug.getuservalue(a) == debug.getuservalue(b)
  257. end})
  258. debug.setmetatable(u2, {__eq = function (a, b)
  259. return true
  260. end})
  261. assert(u1 == u3 and u3 == u1 and u1 ~= u2)
  262. assert(u2 == u1 and u2 == u3 and u3 == u2)
  263. assert(u2 ~= {}) -- different types cannot be equal
  264. local mirror = {}
  265. debug.setmetatable(u3, {__index = mirror, __newindex = mirror})
  266. for i = 1, 10 do u3[i] = i end
  267. for i = 1, 10 do assert(u3[i] == i) end
  268. end
  269. t.__concat = function (a,b,c)
  270. assert(c == nil)
  271. if type(a) == 'table' then a = a.val end
  272. if type(b) == 'table' then b = b.val end
  273. if A then return a..b
  274. else
  275. return setmetatable({val=a..b}, t)
  276. end
  277. end
  278. c = {val="c"}; setmetatable(c, t)
  279. d = {val="d"}; setmetatable(d, t)
  280. A = true
  281. assert(c..d == 'cd')
  282. assert(0 .."a".."b"..c..d.."e".."f"..(5+3).."g" == "0abcdef8g")
  283. A = false
  284. assert((c..d..c..d).val == 'cdcd')
  285. x = c..d
  286. assert(getmetatable(x) == t and x.val == 'cd')
  287. x = 0 .."a".."b"..c..d.."e".."f".."g"
  288. assert(x.val == "0abcdefg")
  289. -- concat metamethod x numbers (bug in 5.1.1)
  290. c = {}
  291. local x
  292. setmetatable(c, {__concat = function (a,b)
  293. assert(type(a) == "number" and b == c or type(b) == "number" and a == c)
  294. return c
  295. end})
  296. assert(c..5 == c and 5 .. c == c)
  297. assert(4 .. c .. 5 == c and 4 .. 5 .. 6 .. 7 .. c == c)
  298. -- test comparison compatibilities
  299. local t1, t2, c, d
  300. t1 = {}; c = {}; setmetatable(c, t1)
  301. d = {}
  302. t1.__eq = function () return true end
  303. t1.__lt = function () return true end
  304. t1.__le = function () return false end
  305. setmetatable(d, t1)
  306. assert(c == d and c < d and not(d <= c))
  307. t2 = {}
  308. t2.__eq = t1.__eq
  309. t2.__lt = t1.__lt
  310. setmetatable(d, t2)
  311. assert(c == d and c < d and not(d <= c))
  312. -- test for several levels of calls
  313. local i
  314. local tt = {
  315. __call = function (t, ...)
  316. i = i+1
  317. if t.f then return t.f(...)
  318. else return {...}
  319. end
  320. end
  321. }
  322. local a = setmetatable({}, tt)
  323. local b = setmetatable({f=a}, tt)
  324. local c = setmetatable({f=b}, tt)
  325. i = 0
  326. x = c(3,4,5)
  327. assert(i == 3 and x[1] == 3 and x[3] == 5)
  328. assert(_G.X == 20)
  329. print'+'
  330. local _g = _G
  331. _ENV = setmetatable({}, {__index=function (_,k) return _g[k] end})
  332. a = {}
  333. rawset(a, "x", 1, 2, 3)
  334. assert(a.x == 1 and rawget(a, "x", 3) == 1)
  335. print '+'
  336. -- testing metatables for basic types
  337. mt = {__index = function (a,b) return a+b end,
  338. __len = function (x) return math.floor(x) end}
  339. debug.setmetatable(10, mt)
  340. assert(getmetatable(-2) == mt)
  341. assert((10)[3] == 13)
  342. assert((10)["3"] == 13)
  343. assert(#3.45 == 3)
  344. debug.setmetatable(23, nil)
  345. assert(getmetatable(-2) == nil)
  346. debug.setmetatable(true, mt)
  347. assert(getmetatable(false) == mt)
  348. mt.__index = function (a,b) return a or b end
  349. assert((true)[false] == true)
  350. assert((false)[false] == false)
  351. debug.setmetatable(false, nil)
  352. assert(getmetatable(true) == nil)
  353. debug.setmetatable(nil, mt)
  354. assert(getmetatable(nil) == mt)
  355. mt.__add = function (a,b) return (a or 1) + (b or 2) end
  356. assert(10 + nil == 12)
  357. assert(nil + 23 == 24)
  358. assert(nil + nil == 3)
  359. debug.setmetatable(nil, nil)
  360. assert(getmetatable(nil) == nil)
  361. debug.setmetatable(nil, {})
  362. -- loops in delegation
  363. a = {}; setmetatable(a, a); a.__index = a; a.__newindex = a
  364. assert(not pcall(function (a,b) return a[b] end, a, 10))
  365. assert(not pcall(function (a,b,c) a[b] = c end, a, 10, true))
  366. -- bug in 5.1
  367. T, K, V = nil
  368. grandparent = {}
  369. grandparent.__newindex = function(t,k,v) T=t; K=k; V=v end
  370. parent = {}
  371. parent.__newindex = parent
  372. setmetatable(parent, grandparent)
  373. child = setmetatable({}, parent)
  374. child.foo = 10 --> CRASH (on some machines)
  375. assert(T == parent and K == "foo" and V == 10)
  376. print 'OK'
  377. return 12