closure.lua 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. print ("testing closures")
  2. local A,B = 0,{g=10}
  3. function f(x)
  4. local a = {}
  5. for i=1,1000 do
  6. local y = 0
  7. do
  8. a[i] = function () B.g = B.g+1; y = y+x; return y+A end
  9. end
  10. end
  11. local dummy = function () return a[A] end
  12. collectgarbage()
  13. A = 1; assert(dummy() == a[1]); A = 0;
  14. assert(a[1]() == x)
  15. assert(a[3]() == x)
  16. collectgarbage()
  17. assert(B.g == 12)
  18. return a
  19. end
  20. local a = f(10)
  21. -- force a GC in this level
  22. local x = {[1] = {}} -- to detect a GC
  23. setmetatable(x, {__mode = 'kv'})
  24. while x[1] do -- repeat until GC
  25. local a = A..A..A..A -- create garbage
  26. A = A+1
  27. end
  28. assert(a[1]() == 20+A)
  29. assert(a[1]() == 30+A)
  30. assert(a[2]() == 10+A)
  31. collectgarbage()
  32. assert(a[2]() == 20+A)
  33. assert(a[2]() == 30+A)
  34. assert(a[3]() == 20+A)
  35. assert(a[8]() == 10+A)
  36. assert(getmetatable(x).__mode == 'kv')
  37. assert(B.g == 19)
  38. -- testing equality
  39. a = {}
  40. for i = 1, 5 do a[i] = function (x) return x + a + _ENV end end
  41. assert(a[3] == a[4] and a[4] == a[5])
  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. local function f()
  45. return function (x) return math.sin(_ENV[x]) end
  46. end
  47. assert(f() == f())
  48. -- testing closures with 'for' control variable
  49. a = {}
  50. for i=1,10 do
  51. a[i] = {set = function(x) i=x end, get = function () return i end}
  52. if i == 3 then break end
  53. end
  54. assert(a[4] == nil)
  55. a[1].set(10)
  56. assert(a[2].get() == 2)
  57. a[2].set('a')
  58. assert(a[3].get() == 3)
  59. assert(a[2].get() == 'a')
  60. a = {}
  61. local t = {"a", "b"}
  62. for i = 1, #t do
  63. local k = t[i]
  64. a[i] = {set = function(x, y) i=x; k=y end,
  65. get = function () return i, k end}
  66. if i == 2 then break end
  67. end
  68. a[1].set(10, 20)
  69. local r,s = a[2].get()
  70. assert(r == 2 and s == 'b')
  71. r,s = a[1].get()
  72. assert(r == 10 and s == 20)
  73. a[2].set('a', 'b')
  74. r,s = a[2].get()
  75. assert(r == "a" and s == "b")
  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 repeat-until
  130. local a = {}
  131. local i = 1
  132. repeat
  133. local x = i
  134. a[i] = function () i = x+1; return x end
  135. until i > 10 or a[i]() ~= x
  136. assert(i == 11 and a[1]() == 1 and a[3]() == 3 and i == 4)
  137. -- testing closures created in 'then' and 'else' parts of 'if's
  138. a = {}
  139. for i = 1, 10 do
  140. if i % 3 == 0 then
  141. local y = 0
  142. a[i] = function (x) local t = y; y = x; return t end
  143. elseif i % 3 == 1 then
  144. goto L1
  145. error'not here'
  146. ::L1::
  147. local y = 1
  148. a[i] = function (x) local t = y; y = x; return t end
  149. elseif i % 3 == 2 then
  150. local t
  151. goto l4
  152. ::l4a:: a[i] = t; goto l4b
  153. error("should never be here!")
  154. ::l4::
  155. local y = 2
  156. t = function (x) local t = y; y = x; return t end
  157. goto l4a
  158. error("should never be here!")
  159. ::l4b::
  160. end
  161. end
  162. for i = 1, 10 do
  163. assert(a[i](i * 10) == i % 3 and a[i]() == i * 10)
  164. end
  165. print'+'
  166. -- test for correctly closing upvalues in tail calls of vararg functions
  167. local function t ()
  168. local function c(a,b) assert(a=="test" and b=="OK") end
  169. local function v(f, ...) c("test", f() ~= 1 and "FAILED" or "OK") end
  170. local x = 1
  171. return v(function() return x end)
  172. end
  173. t()
  174. -- test for debug manipulation of upvalues
  175. local debug = require'debug'
  176. do
  177. local a , b, c = 3, 5, 7
  178. foo1 = function () return a+b end;
  179. foo2 = function () return b+a end;
  180. do
  181. local a = 10
  182. foo3 = function () return a+b end;
  183. end
  184. end
  185. assert(debug.upvalueid(foo1, 1))
  186. assert(debug.upvalueid(foo1, 2))
  187. assert(not pcall(debug.upvalueid, foo1, 3))
  188. assert(debug.upvalueid(foo1, 1) == debug.upvalueid(foo2, 2))
  189. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo2, 1))
  190. assert(debug.upvalueid(foo3, 1))
  191. assert(debug.upvalueid(foo1, 1) ~= debug.upvalueid(foo3, 1))
  192. assert(debug.upvalueid(foo1, 2) == debug.upvalueid(foo3, 2))
  193. assert(debug.upvalueid(string.gmatch("x", "x"), 1) ~= nil)
  194. assert(foo1() == 3 + 5 and foo2() == 5 + 3)
  195. debug.upvaluejoin(foo1, 2, foo2, 2)
  196. assert(foo1() == 3 + 3 and foo2() == 5 + 3)
  197. assert(foo3() == 10 + 5)
  198. debug.upvaluejoin(foo3, 2, foo2, 1)
  199. assert(foo3() == 10 + 5)
  200. debug.upvaluejoin(foo3, 2, foo2, 2)
  201. assert(foo3() == 10 + 3)
  202. assert(not pcall(debug.upvaluejoin, foo1, 3, foo2, 1))
  203. assert(not pcall(debug.upvaluejoin, foo1, 1, foo2, 3))
  204. assert(not pcall(debug.upvaluejoin, foo1, 0, foo2, 1))
  205. assert(not pcall(debug.upvaluejoin, print, 1, foo2, 1))
  206. assert(not pcall(debug.upvaluejoin, {}, 1, foo2, 1))
  207. assert(not pcall(debug.upvaluejoin, foo1, 1, print, 1))
  208. print('OK')