locals.lua 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. -- $Id: testes/locals.lua $
  2. -- See Copyright Notice in file all.lua
  3. print('testing local variables and environments')
  4. local debug = require"debug"
  5. -- bug in 5.1:
  6. local function f(x) x = nil; return x end
  7. assert(f(10) == nil)
  8. local function f() local x; return x end
  9. assert(f(10) == nil)
  10. local function f(x) x = nil; local y; return x, y end
  11. assert(f(10) == nil and select(2, f(20)) == nil)
  12. do
  13. local i = 10
  14. do local i = 100; assert(i==100) end
  15. do local i = 1000; assert(i==1000) end
  16. assert(i == 10)
  17. if i ~= 10 then
  18. local i = 20
  19. else
  20. local i = 30
  21. assert(i == 30)
  22. end
  23. end
  24. f = nil
  25. local f
  26. x = 1
  27. a = nil
  28. load('local a = {}')()
  29. assert(a == nil)
  30. function f (a)
  31. local _1, _2, _3, _4, _5
  32. local _6, _7, _8, _9, _10
  33. local x = 3
  34. local b = a
  35. local c,d = a,b
  36. if (d == b) then
  37. local x = 'q'
  38. x = b
  39. assert(x == 2)
  40. else
  41. assert(nil)
  42. end
  43. assert(x == 3)
  44. local f = 10
  45. end
  46. local b=10
  47. local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3
  48. assert(x == 1)
  49. f(2)
  50. assert(type(f) == 'function')
  51. local function getenv (f)
  52. local a,b = debug.getupvalue(f, 1)
  53. assert(a == '_ENV')
  54. return b
  55. end
  56. -- test for global table of loaded chunks
  57. assert(getenv(load"a=3") == _G)
  58. local c = {}; local f = load("a = 3", nil, nil, c)
  59. assert(getenv(f) == c)
  60. assert(c.a == nil)
  61. f()
  62. assert(c.a == 3)
  63. -- old test for limits for special instructions
  64. do
  65. local i = 2
  66. local p = 4 -- p == 2^i
  67. repeat
  68. for j=-3,3 do
  69. assert(load(string.format([[local a=%s;
  70. a=a+%s;
  71. assert(a ==2^%s)]], j, p-j, i), '')) ()
  72. assert(load(string.format([[local a=%s;
  73. a=a-%s;
  74. assert(a==-2^%s)]], -j, p-j, i), '')) ()
  75. assert(load(string.format([[local a,b=0,%s;
  76. a=b-%s;
  77. assert(a==-2^%s)]], -j, p-j, i), '')) ()
  78. end
  79. p = 2 * p; i = i + 1
  80. until p <= 0
  81. end
  82. print'+'
  83. if rawget(_G, "T") then
  84. -- testing clearing of dead elements from tables
  85. collectgarbage("stop") -- stop GC
  86. local a = {[{}] = 4, [3] = 0, alo = 1,
  87. a1234567890123456789012345678901234567890 = 10}
  88. local t = T.querytab(a)
  89. for k,_ in pairs(a) do a[k] = undef end
  90. collectgarbage() -- restore GC and collect dead fields in 'a'
  91. for i=0,t-1 do
  92. local k = querytab(a, i)
  93. assert(k == nil or type(k) == 'number' or k == 'alo')
  94. end
  95. -- testing allocation errors during table insertions
  96. local a = {}
  97. local function additems ()
  98. a.x = true; a.y = true; a.z = true
  99. a[1] = true
  100. a[2] = true
  101. end
  102. for i = 1, math.huge do
  103. T.alloccount(i)
  104. local st, msg = pcall(additems)
  105. T.alloccount()
  106. local count = 0
  107. for k, v in pairs(a) do
  108. assert(a[k] == v)
  109. count = count + 1
  110. end
  111. if st then assert(count == 5); break end
  112. end
  113. end
  114. -- testing lexical environments
  115. assert(_ENV == _G)
  116. do
  117. local dummy
  118. local _ENV = (function (...) return ... end)(_G, dummy) -- {
  119. do local _ENV = {assert=assert}; assert(true) end
  120. mt = {_G = _G}
  121. local foo,x
  122. A = false -- "declare" A
  123. do local _ENV = mt
  124. function foo (x)
  125. A = x
  126. do local _ENV = _G; A = 1000 end
  127. return function (x) return A .. x end
  128. end
  129. end
  130. assert(getenv(foo) == mt)
  131. x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
  132. assert(x('*') == mt.A .. '*')
  133. do local _ENV = {assert=assert, A=10};
  134. do local _ENV = {assert=assert, A=20};
  135. assert(A==20);x=A
  136. end
  137. assert(A==10 and x==20)
  138. end
  139. assert(x==20)
  140. do -- constants
  141. local a<const>, b, c<const> = 10, 20, 30
  142. b = a + c + b -- 'b' is not constant
  143. assert(a == 10 and b == 60 and c == 30)
  144. local function checkro (name, code)
  145. local st, msg = load(code)
  146. local gab = string.format("attempt to assign to const variable '%s'", name)
  147. assert(not st and string.find(msg, gab))
  148. end
  149. checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12")
  150. checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11")
  151. checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11")
  152. checkro("z", [[
  153. local a, z <const>, b = 10;
  154. function foo() a = 20; z = 32; end
  155. ]])
  156. checkro("var1", [[
  157. local a, var1 <const> = 10;
  158. function foo() a = 20; z = function () var1 = 12; end end
  159. ]])
  160. end
  161. print"testing to-be-closed variables"
  162. local function stack(n) n = ((n == 0) or stack(n - 1)) end
  163. local function func2close (f, x, y)
  164. local obj = setmetatable({}, {__close = f})
  165. if x then
  166. return x, obj, y
  167. else
  168. return obj
  169. end
  170. end
  171. do
  172. local a = {}
  173. do
  174. local b <close> = false -- not to be closed
  175. local x <close> = setmetatable({"x"}, {__close = function (self)
  176. a[#a + 1] = self[1] end})
  177. local w, y <close>, z = func2close(function (self, err)
  178. assert(err == nil); a[#a + 1] = "y"
  179. end, 10, 20)
  180. local c <close> = nil -- not to be closed
  181. a[#a + 1] = "in"
  182. assert(w == 10 and z == 20)
  183. end
  184. a[#a + 1] = "out"
  185. assert(a[1] == "in" and a[2] == "y" and a[3] == "x" and a[4] == "out")
  186. end
  187. do
  188. local X = false
  189. local x, closescope = func2close(function (_, msg)
  190. stack(10);
  191. assert(msg == nil)
  192. X = true
  193. end, 100)
  194. assert(x == 100); x = 101; -- 'x' is not read-only
  195. -- closing functions do not corrupt returning values
  196. local function foo (x)
  197. local _ <close> = closescope
  198. return x, X, 23
  199. end
  200. local a, b, c = foo(1.5)
  201. assert(a == 1.5 and b == false and c == 23 and X == true)
  202. X = false
  203. foo = function (x)
  204. local _<close> = func2close(function (_, msg)
  205. -- without errors, enclosing function should be still active when
  206. -- __close is called
  207. assert(debug.getinfo(2).name == "foo")
  208. assert(msg == nil)
  209. end)
  210. local _<close> = closescope
  211. local y = 15
  212. return y
  213. end
  214. assert(foo() == 15 and X == true)
  215. X = false
  216. foo = function ()
  217. local x <close> = closescope
  218. return x
  219. end
  220. assert(foo() == closescope and X == true)
  221. end
  222. -- testing to-be-closed x compile-time constants
  223. -- (there were some bugs here in Lua 5.4-rc3, due to a confusion
  224. -- between compile levels and stack levels of variables)
  225. do
  226. local flag = false
  227. local x = setmetatable({},
  228. {__close = function() assert(flag == false); flag = true end})
  229. local y <const> = nil
  230. local z <const> = nil
  231. do
  232. local a <close> = x
  233. end
  234. assert(flag) -- 'x' must be closed here
  235. end
  236. do
  237. -- similar problem, but with implicit close in for loops
  238. local flag = false
  239. local x = setmetatable({},
  240. {__close = function () assert(flag == false); flag = true end})
  241. -- return an empty iterator, nil, nil, and 'x' to be closed
  242. local function a ()
  243. return (function () return nil end), nil, nil, x
  244. end
  245. local v <const> = 1
  246. local w <const> = 1
  247. local x <const> = 1
  248. local y <const> = 1
  249. local z <const> = 1
  250. for k in a() do
  251. a = k
  252. end -- ending the loop must close 'x'
  253. assert(flag) -- 'x' must be closed here
  254. end
  255. do
  256. -- calls cannot be tail in the scope of to-be-closed variables
  257. local X, Y
  258. local function foo ()
  259. local _ <close> = func2close(function () Y = 10 end)
  260. assert(X == true and Y == nil) -- 'X' not closed yet
  261. return 1,2,3
  262. end
  263. local function bar ()
  264. local _ <close> = func2close(function () X = false end)
  265. X = true
  266. do
  267. return foo() -- not a tail call!
  268. end
  269. end
  270. local a, b, c, d = bar()
  271. assert(a == 1 and b == 2 and c == 3 and X == false and Y == 10 and d == nil)
  272. end
  273. do print("testing errors in __close")
  274. -- original error is in __close
  275. local function foo ()
  276. local x <close> =
  277. func2close(function (self, msg)
  278. assert(string.find(msg, "@y"))
  279. error("@x")
  280. end)
  281. local x1 <close> =
  282. func2close(function (self, msg)
  283. assert(string.find(msg, "@y"))
  284. end)
  285. local gc <close> = func2close(function () collectgarbage() end)
  286. local y <close> =
  287. func2close(function (self, msg)
  288. assert(string.find(msg, "@z")) -- error in 'z'
  289. error("@y")
  290. end)
  291. local z <close> =
  292. func2close(function (self, msg)
  293. assert(msg == nil)
  294. error("@z")
  295. end)
  296. return 200
  297. end
  298. local stat, msg = pcall(foo, false)
  299. assert(string.find(msg, "@x"))
  300. -- original error not in __close
  301. local function foo ()
  302. local x <close> =
  303. func2close(function (self, msg)
  304. -- after error, 'foo' was discarded, so caller now
  305. -- must be 'pcall'
  306. assert(debug.getinfo(2).name == "pcall")
  307. assert(string.find(msg, "@x1"))
  308. end)
  309. local x1 <close> =
  310. func2close(function (self, msg)
  311. assert(debug.getinfo(2).name == "pcall")
  312. assert(string.find(msg, "@y"))
  313. error("@x1")
  314. end)
  315. local gc <close> = func2close(function () collectgarbage() end)
  316. local y <close> =
  317. func2close(function (self, msg)
  318. assert(debug.getinfo(2).name == "pcall")
  319. assert(string.find(msg, "@z"))
  320. error("@y")
  321. end)
  322. local first = true
  323. local z <close> =
  324. func2close(function (self, msg)
  325. assert(debug.getinfo(2).name == "pcall")
  326. -- 'z' close is called once
  327. assert(first and msg == 4)
  328. first = false
  329. error("@z")
  330. end)
  331. error(4) -- original error
  332. end
  333. local stat, msg = pcall(foo, true)
  334. assert(string.find(msg, "@x1"))
  335. -- error leaving a block
  336. local function foo (...)
  337. do
  338. local x1 <close> =
  339. func2close(function (self, msg)
  340. assert(string.find(msg, "@X"))
  341. error("@Y")
  342. end)
  343. local x123 <close> =
  344. func2close(function (_, msg)
  345. assert(msg == nil)
  346. error("@X")
  347. end)
  348. end
  349. os.exit(false) -- should not run
  350. end
  351. local st, msg = xpcall(foo, debug.traceback)
  352. assert(string.match(msg, "^[^ ]* @Y"))
  353. -- error in toclose in vararg function
  354. local function foo (...)
  355. local x123 <close> = func2close(function () error("@x123") end)
  356. end
  357. local st, msg = xpcall(foo, debug.traceback)
  358. assert(string.match(msg, "^[^ ]* @x123"))
  359. assert(string.find(msg, "in metamethod 'close'"))
  360. end
  361. do -- errors due to non-closable values
  362. local function foo ()
  363. local x <close> = {}
  364. os.exit(false) -- should not run
  365. end
  366. local stat, msg = pcall(foo)
  367. assert(not stat and
  368. string.find(msg, "variable 'x' got a non%-closable value"))
  369. local function foo ()
  370. local xyz <close> = setmetatable({}, {__close = print})
  371. getmetatable(xyz).__close = nil -- remove metamethod
  372. end
  373. local stat, msg = pcall(foo)
  374. assert(not stat and string.find(msg, "metamethod 'close'"))
  375. local function foo ()
  376. local a1 <close> = func2close(function (_, msg)
  377. assert(string.find(msg, "number value"))
  378. error(12)
  379. end)
  380. local a2 <close> = setmetatable({}, {__close = print})
  381. local a3 <close> = func2close(function (_, msg)
  382. assert(msg == nil)
  383. error(123)
  384. end)
  385. getmetatable(a2).__close = 4 -- invalidate metamethod
  386. end
  387. local stat, msg = pcall(foo)
  388. assert(not stat and msg == 12)
  389. end
  390. do -- tbc inside close methods
  391. local track = {}
  392. local function foo ()
  393. local x <close> = func2close(function ()
  394. local xx <close> = func2close(function (_, msg)
  395. assert(msg == nil)
  396. track[#track + 1] = "xx"
  397. end)
  398. track[#track + 1] = "x"
  399. end)
  400. track[#track + 1] = "foo"
  401. return 20, 30, 40
  402. end
  403. local a, b, c, d = foo()
  404. assert(a == 20 and b == 30 and c == 40 and d == nil)
  405. assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx")
  406. -- again, with errors
  407. local track = {}
  408. local function foo ()
  409. local x0 <close> = func2close(function (_, msg)
  410. assert(msg == 202)
  411. track[#track + 1] = "x0"
  412. end)
  413. local x <close> = func2close(function ()
  414. local xx <close> = func2close(function (_, msg)
  415. assert(msg == 101)
  416. track[#track + 1] = "xx"
  417. error(202)
  418. end)
  419. track[#track + 1] = "x"
  420. error(101)
  421. end)
  422. track[#track + 1] = "foo"
  423. return 20, 30, 40
  424. end
  425. local st, msg = pcall(foo)
  426. assert(not st and msg == 202)
  427. assert(track[1] == "foo" and track[2] == "x" and track[3] == "xx" and
  428. track[4] == "x0")
  429. end
  430. if rawget(_G, "T") then
  431. -- memory error inside closing function
  432. local function foo ()
  433. local y <close> = func2close(function () T.alloccount() end)
  434. local x <close> = setmetatable({}, {__close = function ()
  435. T.alloccount(0); local x = {} -- force a memory error
  436. end})
  437. error(1000) -- common error inside the function's body
  438. end
  439. stack(5) -- ensure a minimal number of CI structures
  440. -- despite memory error, 'y' will be executed and
  441. -- memory limit will be lifted
  442. local _, msg = pcall(foo)
  443. assert(msg == "not enough memory")
  444. local closemsg
  445. local close = func2close(function (self, msg)
  446. T.alloccount()
  447. closemsg = msg
  448. end)
  449. -- set a memory limit and return a closing object to remove the limit
  450. local function enter (count)
  451. stack(10) -- reserve some stack space
  452. T.alloccount(count)
  453. closemsg = nil
  454. return close
  455. end
  456. local function test ()
  457. local x <close> = enter(0) -- set a memory limit
  458. -- creation of previous upvalue will raise a memory error
  459. assert(false) -- should not run
  460. end
  461. local _, msg = pcall(test)
  462. assert(msg == "not enough memory" and closemsg == "not enough memory")
  463. -- repeat test with extra closing upvalues
  464. local function test ()
  465. local xxx <close> = func2close(function (self, msg)
  466. assert(msg == "not enough memory");
  467. error(1000) -- raise another error
  468. end)
  469. local xx <close> = func2close(function (self, msg)
  470. assert(msg == "not enough memory");
  471. end)
  472. local x <close> = enter(0) -- set a memory limit
  473. -- creation of previous upvalue will raise a memory error
  474. os.exit(false) -- should not run
  475. end
  476. local _, msg = pcall(test)
  477. assert(msg == 1000 and closemsg == "not enough memory")
  478. do -- testing 'toclose' in C string buffer
  479. collectgarbage()
  480. local s = string.rep('a', 10000) -- large string
  481. local m = T.totalmem()
  482. collectgarbage("stop")
  483. s = string.upper(s) -- allocate buffer + new string (10K each)
  484. -- ensure buffer was deallocated
  485. assert(T.totalmem() - m <= 11000)
  486. collectgarbage("restart")
  487. end
  488. do -- now some tests for freeing buffer in case of errors
  489. local lim = 10000 -- some size larger than the static buffer
  490. local extra = 2000 -- some extra memory (for callinfo, etc.)
  491. local s = string.rep("a", lim)
  492. -- concat this table needs two buffer resizes (one for each 's')
  493. local a = {s, s}
  494. collectgarbage()
  495. m = T.totalmem()
  496. collectgarbage("stop")
  497. -- error in the first buffer allocation
  498. T. totalmem(m + extra)
  499. assert(not pcall(table.concat, a))
  500. -- first buffer was not even allocated
  501. assert(T.totalmem() - m <= extra)
  502. -- error in the second buffer allocation
  503. T. totalmem(m + lim + extra)
  504. assert(not pcall(table.concat, a))
  505. -- first buffer was released by 'toclose'
  506. assert(T.totalmem() - m <= extra)
  507. -- error in creation of final string
  508. T.totalmem(m + 2 * lim + extra)
  509. assert(not pcall(table.concat, a))
  510. -- second buffer was released by 'toclose'
  511. assert(T.totalmem() - m <= extra)
  512. -- userdata, upvalue, buffer, buffer, final string
  513. T.totalmem(m + 4*lim + extra)
  514. assert(#table.concat(a) == 2*lim)
  515. T.totalmem(0) -- remove memory limit
  516. collectgarbage("restart")
  517. print'+'
  518. end
  519. end
  520. print "to-be-closed variables in coroutines"
  521. do
  522. -- yielding inside closing metamethods
  523. local function checktable (t1, t2)
  524. assert(#t1 == #t2)
  525. for i = 1, #t1 do
  526. assert(t1[i] == t2[i])
  527. end
  528. end
  529. local trace = {}
  530. local co = coroutine.wrap(function ()
  531. trace[#trace + 1] = "nowX"
  532. -- will be closed after 'y'
  533. local x <close> = func2close(function (_, msg)
  534. assert(msg == nil)
  535. trace[#trace + 1] = "x1"
  536. coroutine.yield("x")
  537. trace[#trace + 1] = "x2"
  538. end)
  539. return pcall(function ()
  540. do -- 'z' will be closed first
  541. local z <close> = func2close(function (_, msg)
  542. assert(msg == nil)
  543. trace[#trace + 1] = "z1"
  544. coroutine.yield("z")
  545. trace[#trace + 1] = "z2"
  546. end)
  547. end
  548. trace[#trace + 1] = "nowY"
  549. -- will be closed after 'z'
  550. local y <close> = func2close(function(_, msg)
  551. assert(msg == nil)
  552. trace[#trace + 1] = "y1"
  553. coroutine.yield("y")
  554. trace[#trace + 1] = "y2"
  555. end)
  556. return 10, 20, 30
  557. end)
  558. end)
  559. assert(co() == "z")
  560. assert(co() == "y")
  561. assert(co() == "x")
  562. checktable({co()}, {true, 10, 20, 30})
  563. checktable(trace, {"nowX", "z1", "z2", "nowY", "y1", "y2", "x1", "x2"})
  564. end
  565. do
  566. -- yielding inside closing metamethods after an error
  567. local co = coroutine.wrap(function ()
  568. local function foo (err)
  569. local z <close> = func2close(function(_, msg)
  570. assert(msg == nil or msg == err + 20)
  571. coroutine.yield("z")
  572. return 100, 200
  573. end)
  574. local y <close> = func2close(function(_, msg)
  575. -- still gets the original error (if any)
  576. assert(msg == err or (msg == nil and err == 1))
  577. coroutine.yield("y")
  578. if err then error(err + 20) end -- creates or changes the error
  579. end)
  580. local x <close> = func2close(function(_, msg)
  581. assert(msg == err or (msg == nil and err == 1))
  582. coroutine.yield("x")
  583. return 100, 200
  584. end)
  585. if err == 10 then error(err) else return 10, 20 end
  586. end
  587. coroutine.yield(pcall(foo, nil)) -- no error
  588. coroutine.yield(pcall(foo, 1)) -- error in __close
  589. return pcall(foo, 10) -- 'foo' will raise an error
  590. end)
  591. local a, b = co() -- first foo: no error
  592. assert(a == "x" and b == nil) -- yields inside 'x'; Ok
  593. a, b = co()
  594. assert(a == "y" and b == nil) -- yields inside 'y'; Ok
  595. a, b = co()
  596. assert(a == "z" and b == nil) -- yields inside 'z'; Ok
  597. local a, b, c = co()
  598. assert(a and b == 10 and c == 20) -- returns from 'pcall(foo, nil)'
  599. local a, b = co() -- second foo: error in __close
  600. assert(a == "x" and b == nil) -- yields inside 'x'; Ok
  601. a, b = co()
  602. assert(a == "y" and b == nil) -- yields inside 'y'; Ok
  603. a, b = co()
  604. assert(a == "z" and b == nil) -- yields inside 'z'; Ok
  605. local st, msg = co() -- reports the error in 'y'
  606. assert(not st and msg == 21)
  607. local a, b = co() -- third foo: error in function body
  608. assert(a == "x" and b == nil) -- yields inside 'x'; Ok
  609. a, b = co()
  610. assert(a == "y" and b == nil) -- yields inside 'y'; Ok
  611. a, b = co()
  612. assert(a == "z" and b == nil) -- yields inside 'z'; Ok
  613. local st, msg = co() -- gets final error
  614. assert(not st and msg == 10 + 20)
  615. end
  616. do
  617. -- an error in a wrapped coroutine closes variables
  618. local x = false
  619. local y = false
  620. local co = coroutine.wrap(function ()
  621. local xv <close> = func2close(function () x = true end)
  622. do
  623. local yv <close> = func2close(function () y = true end)
  624. coroutine.yield(100) -- yield doesn't close variable
  625. end
  626. coroutine.yield(200) -- yield doesn't close variable
  627. error(23) -- error does
  628. end)
  629. local b = co()
  630. assert(b == 100 and not x and not y)
  631. b = co()
  632. assert(b == 200 and not x and y)
  633. local a, b = pcall(co)
  634. assert(not a and b == 23 and x and y)
  635. end
  636. do
  637. -- error in a wrapped coroutine raising errors when closing a variable
  638. local x = 0
  639. local co = coroutine.wrap(function ()
  640. local xx <close> = func2close(function (_, msg)
  641. x = x + 1;
  642. assert(string.find(msg, "@XXX"))
  643. error("@YYY")
  644. end)
  645. local xv <close> = func2close(function () x = x + 1; error("@XXX") end)
  646. coroutine.yield(100)
  647. error(200)
  648. end)
  649. assert(co() == 100); assert(x == 0)
  650. local st, msg = pcall(co); assert(x == 2)
  651. assert(not st and string.find(msg, "@YYY")) -- should get error raised
  652. local x = 0
  653. local y = 0
  654. co = coroutine.wrap(function ()
  655. local xx <close> = func2close(function (_, err)
  656. y = y + 1;
  657. assert(string.find(err, "XXX"))
  658. error("YYY")
  659. end)
  660. local xv <close> = func2close(function ()
  661. x = x + 1; error("XXX")
  662. end)
  663. coroutine.yield(100)
  664. return 200
  665. end)
  666. assert(co() == 100); assert(x == 0)
  667. local st, msg = pcall(co)
  668. assert(x == 1 and y == 1)
  669. -- should get first error raised
  670. assert(not st and string.find(msg, "%w+%.%w+:%d+: YYY"))
  671. end
  672. -- a suspended coroutine should not close its variables when collected
  673. local co
  674. co = coroutine.wrap(function()
  675. -- should not run
  676. local x <close> = func2close(function () os.exit(false) end)
  677. co = nil
  678. coroutine.yield()
  679. end)
  680. co() -- start coroutine
  681. assert(co == nil) -- eventually it will be collected
  682. collectgarbage()
  683. -- to-be-closed variables in generic for loops
  684. do
  685. local numopen = 0
  686. local function open (x)
  687. numopen = numopen + 1
  688. return
  689. function () -- iteraction function
  690. x = x - 1
  691. if x > 0 then return x end
  692. end,
  693. nil, -- state
  694. nil, -- control variable
  695. func2close(function () numopen = numopen - 1 end) -- closing function
  696. end
  697. local s = 0
  698. for i in open(10) do
  699. s = s + i
  700. end
  701. assert(s == 45 and numopen == 0)
  702. local s = 0
  703. for i in open(10) do
  704. if i < 5 then break end
  705. s = s + i
  706. end
  707. assert(s == 35 and numopen == 0)
  708. local s = 0
  709. for i in open(10) do
  710. for j in open(10) do
  711. if i + j < 5 then goto endloop end
  712. s = s + i
  713. end
  714. end
  715. ::endloop::
  716. assert(s == 375 and numopen == 0)
  717. end
  718. print('OK')
  719. return 5,f
  720. end -- }