closure.lua 5.5 KB

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