closure.lua 5.6 KB

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