closure.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. -- $Id: testes/closure.lua $
  2. -- See Copyright Notice in file all.lua
  3. print "testing closures"
  4. local A,B = 0,{g=10}
  5. function f(x)
  6. local a = {}
  7. for i=1,1000 do
  8. local y = 0
  9. do
  10. a[i] = function () B.g = B.g+1; y = y+x; return y+A end
  11. end
  12. end
  13. local dummy = function () return a[A] end
  14. collectgarbage()
  15. A = 1; assert(dummy() == a[1]); A = 0;
  16. assert(a[1]() == x)
  17. assert(a[3]() == x)
  18. collectgarbage()
  19. assert(B.g == 12)
  20. return a
  21. end
  22. local a = f(10)
  23. -- force a GC in this level
  24. local x = {[1] = {}} -- to detect a GC
  25. setmetatable(x, {__mode = 'kv'})
  26. while x[1] do -- repeat until GC
  27. local a = A..A..A..A -- create garbage
  28. A = A+1
  29. end
  30. assert(a[1]() == 20+A)
  31. assert(a[1]() == 30+A)
  32. assert(a[2]() == 10+A)
  33. collectgarbage()
  34. assert(a[2]() == 20+A)
  35. assert(a[2]() == 30+A)
  36. assert(a[3]() == 20+A)
  37. assert(a[8]() == 10+A)
  38. assert(getmetatable(x).__mode == 'kv')
  39. assert(B.g == 19)
  40. -- testing equality
  41. a = {}
  42. for i = 1, 5 do a[i] = function (x) return i + a + _ENV end end
  43. assert(a[3] ~= a[4] and a[4] ~= a[5])
  44. do
  45. local a = function (x) return math.sin(_ENV[x]) end
  46. local function f()
  47. return a
  48. end
  49. assert(f() == f())
  50. end
  51. -- testing closures with 'for' control variable
  52. a = {}
  53. for i=1,10 do
  54. a[i] = {set = function(x) i=x end, get = function () return i end}
  55. if i == 3 then break end
  56. end
  57. assert(a[4] == undef)
  58. a[1].set(10)
  59. assert(a[2].get() == 2)
  60. a[2].set('a')
  61. assert(a[3].get() == 3)
  62. assert(a[2].get() == 'a')
  63. a = {}
  64. local t = {"a", "b"}
  65. for i = 1, #t do
  66. local k = t[i]
  67. a[i] = {set = function(x, y) i=x; k=y end,
  68. get = function () return i, k end}
  69. if i == 2 then break end
  70. end
  71. a[1].set(10, 20)
  72. local r,s = a[2].get()
  73. assert(r == 2 and s == 'b')
  74. r,s = a[1].get()
  75. assert(r == 10 and s == 20)
  76. a[2].set('a', 'b')
  77. r,s = a[2].get()
  78. assert(r == "a" and s == "b")
  79. -- testing closures with 'for' control variable x break
  80. for i=1,3 do
  81. f = function () return i end
  82. break
  83. end
  84. assert(f() == 1)
  85. for k = 1, #t do
  86. local v = t[k]
  87. f = function () return k, v end
  88. break
  89. end
  90. assert(({f()})[1] == 1)
  91. assert(({f()})[2] == "a")
  92. -- testing closure x break x return x errors
  93. local b
  94. function f(x)
  95. local first = 1
  96. while 1 do
  97. if x == 3 and not first then return end
  98. local a = 'xuxu'
  99. b = function (op, y)
  100. if op == 'set' then
  101. a = x+y
  102. else
  103. return a
  104. end
  105. end
  106. if x == 1 then do break end
  107. elseif x == 2 then return
  108. else if x ~= 3 then error() end
  109. end
  110. first = nil
  111. end
  112. end
  113. for i=1,3 do
  114. f(i)
  115. assert(b('get') == 'xuxu')
  116. b('set', 10); assert(b('get') == 10+i)
  117. b = nil
  118. end
  119. pcall(f, 4);
  120. assert(b('get') == 'xuxu')
  121. b('set', 10); assert(b('get') == 14)
  122. local w
  123. -- testing multi-level closure
  124. function f(x)
  125. return function (y)
  126. return function (z) return w+x+y+z end
  127. end
  128. end
  129. y = f(10)
  130. w = 1.345
  131. assert(y(20)(30) == 60+w)
  132. -- testing closures x break
  133. do
  134. local X, Y
  135. local a = math.sin(0)
  136. while a do
  137. local b = 10
  138. X = function () return b end -- closure with upvalue
  139. if a then break end
  140. end
  141. do
  142. local b = 20
  143. Y = function () return b end -- closure with upvalue
  144. end
  145. -- upvalues must be different
  146. assert(X() == 10 and Y() == 20)
  147. end
  148. -- testing closures x repeat-until
  149. local a = {}
  150. local i = 1
  151. repeat
  152. local x = i
  153. a[i] = function () i = x+1; return x end
  154. until i > 10 or a[i]() ~= x
  155. assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4)
  156. -- testing closures created in 'then' and 'else' parts of 'if's
  157. a = {}
  158. for i = 1, 10 do
  159. if i % 3 == 0 then
  160. local y = 0
  161. a[i] = function (x) local t = y; y = x; return t end
  162. elseif i % 3 == 1 then
  163. goto L1
  164. error'not here'
  165. ::L1::
  166. local y = 1
  167. a[i] = function (x) local t = y; y = x; return t end
  168. elseif i % 3 == 2 then
  169. local t
  170. goto l4
  171. ::l4a:: a[i] = t; goto l4b
  172. error("should never be here!")
  173. ::l4::
  174. local y = 2
  175. t = function (x) local t = y; y = x; return t end
  176. goto l4a
  177. error("should never be here!")
  178. ::l4b::
  179. end
  180. end
  181. for i = 1, 10 do
  182. assert(a[i](i * 10) == i % 3 and a[i]() == i * 10)
  183. end
  184. print'+'
  185. -- test for correctly closing upvalues in tail calls of vararg functions
  186. local function t ()
  187. local function c(a,b) assert(a=="test" and b=="OK") end
  188. local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end
  189. local x = 1
  190. return v(function() return x end)
  191. end
  192. t()
  193. -- test for debug manipulation of upvalues
  194. local debug = require'debug'
  195. do
  196. local a , b, c = 3, 5, 7
  197. foo1 = function () return a+b end;
  198. foo2 = function () return b+a end;
  199. do
  200. local a = 10
  201. foo3 = function () return a+b end;
  202. end
  203. end
  204. assert(debug.upvalueid(foo1, 1))
  205. assert(debug.upvalueid(foo1, 2))
  206. assert(not pcall(debug.upvalueid, foo1, 3))
  207. assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
  208. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
  209. assert(debug.upvalueid(foo3, 1))
  210. assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1))
  211. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2))
  212. assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil)
  213. assert(foo1() == 3 + 5 and foo2() == 5 + 3)
  214. debug.upvaluejoin(foo1, 2, foo2, 2)
  215. assert(foo1() == 3 + 3 and foo2() == 5 + 3)
  216. assert(foo3() == 10 + 5)
  217. debug.upvaluejoin(foo3, 2, foo2, 1)
  218. assert(foo3() == 10 + 5)
  219. debug.upvaluejoin(foo3, 2, foo2, 2)
  220. assert(foo3() == 10 + 3)
  221. assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1))
  222. assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3))
  223. assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1))
  224. assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1))
  225. assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1))
  226. assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1))
  227. print'OK'