closure.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. local 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. local f
  81. for i=1,3 do
  82. f = function () return i end
  83. break
  84. end
  85. assert(f() == 1)
  86. for k = 1, #t do
  87. local v = t[k]
  88. f = function () return k, v end
  89. break
  90. end
  91. assert(({f()})[1] == 1)
  92. assert(({f()})[2] == "a")
  93. -- testing closure x break x return x errors
  94. local b
  95. function f(x)
  96. local first = 1
  97. while 1 do
  98. if x == 3 and not first then return end
  99. local a = 'xuxu'
  100. b = function (op, y)
  101. if op == 'set' then
  102. a = x+y
  103. else
  104. return a
  105. end
  106. end
  107. if x == 1 then do break end
  108. elseif x == 2 then return
  109. else if x ~= 3 then error() end
  110. end
  111. first = nil
  112. end
  113. end
  114. for i=1,3 do
  115. f(i)
  116. assert(b('get') == 'xuxu')
  117. b('set', 10); assert(b('get') == 10+i)
  118. b = nil
  119. end
  120. pcall(f, 4);
  121. assert(b('get') == 'xuxu')
  122. b('set', 10); assert(b('get') == 14)
  123. local y, w
  124. -- testing multi-level closure
  125. function f(x)
  126. return function (y)
  127. return function (z) return w+x+y+z end
  128. end
  129. end
  130. y = f(10)
  131. w = 1.345
  132. assert(y(20)(30) == 60+w)
  133. -- testing closures x break
  134. do
  135. local X, Y
  136. local a = math.sin(0)
  137. while a do
  138. local b = 10
  139. X = function () return b end -- closure with upvalue
  140. if a then break end
  141. end
  142. do
  143. local b = 20
  144. Y = function () return b end -- closure with upvalue
  145. end
  146. -- upvalues must be different
  147. assert(X() == 10 and Y() == 20)
  148. end
  149. -- testing closures x repeat-until
  150. local a = {}
  151. local i = 1
  152. repeat
  153. local x = i
  154. a[i] = function () i = x+1; return x end
  155. until i > 10 or a[i]() ~= x
  156. assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4)
  157. -- testing closures created in 'then' and 'else' parts of 'if's
  158. a = {}
  159. for i = 1, 10 do
  160. if i % 3 == 0 then
  161. local y = 0
  162. a[i] = function (x) local t = y; y = x; return t end
  163. elseif i % 3 == 1 then
  164. goto L1
  165. error'not here'
  166. ::L1::
  167. local y = 1
  168. a[i] = function (x) local t = y; y = x; return t end
  169. elseif i % 3 == 2 then
  170. local t
  171. goto l4
  172. ::l4a:: a[i] = t; goto l4b
  173. error("should never be here!")
  174. ::l4::
  175. local y = 2
  176. t = function (x) local t = y; y = x; return t end
  177. goto l4a
  178. error("should never be here!")
  179. ::l4b::
  180. end
  181. end
  182. for i = 1, 10 do
  183. assert(a[i](i * 10) == i % 3 and a[i]() == i * 10)
  184. end
  185. print'+'
  186. -- test for correctly closing upvalues in tail calls of vararg functions
  187. local function t ()
  188. local function c(a,b) assert(a=="test" and b=="OK") end
  189. local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end
  190. local x = 1
  191. return v(function() return x end)
  192. end
  193. t()
  194. -- test for debug manipulation of upvalues
  195. local debug = require'debug'
  196. local foo1, foo2, foo3
  197. do
  198. local a , b, c = 3, 5, 7
  199. foo1 = function () return a+b end;
  200. foo2 = function () return b+a end;
  201. do
  202. local a = 10
  203. foo3 = function () return a+b end;
  204. end
  205. end
  206. assert(debug.upvalueid(foo1, 1))
  207. assert(debug.upvalueid(foo1, 2))
  208. assert(not debug.upvalueid(foo1, 3))
  209. assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
  210. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
  211. assert(debug.upvalueid(foo3, 1))
  212. assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1))
  213. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2))
  214. assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil)
  215. assert(foo1() == 3 + 5 and foo2() == 5 + 3)
  216. debug.upvaluejoin(foo1, 2, foo2, 2)
  217. assert(foo1() == 3 + 3 and foo2() == 5 + 3)
  218. assert(foo3() == 10 + 5)
  219. debug.upvaluejoin(foo3, 2, foo2, 1)
  220. assert(foo3() == 10 + 5)
  221. debug.upvaluejoin(foo3, 2, foo2, 2)
  222. assert(foo3() == 10 + 3)
  223. assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1))
  224. assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3))
  225. assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1))
  226. assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1))
  227. assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1))
  228. assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1))
  229. print'OK'