locals.lua 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. -- $Id: locals.lua,v 1.37 2016/11/07 13:11:28 roberto Exp $
  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 (now just a generic test)
  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, "querytab") 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 = querytab(a)
  89. for k,_ in pairs(a) do a[k] = nil end
  90. collectgarbage() -- restore GC and collect dead fiels 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. end
  96. -- testing lexical environments
  97. assert(_ENV == _G)
  98. do
  99. local dummy
  100. local _ENV = (function (...) return ... end)(_G, dummy) -- {
  101. do local _ENV = {assert=assert}; assert(true) end
  102. mt = {_G = _G}
  103. local foo,x
  104. A = false -- "declare" A
  105. do local _ENV = mt
  106. function foo (x)
  107. A = x
  108. do local _ENV = _G; A = 1000 end
  109. return function (x) return A .. x end
  110. end
  111. end
  112. assert(getenv(foo) == mt)
  113. x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
  114. assert(x('*') == mt.A .. '*')
  115. do local _ENV = {assert=assert, A=10};
  116. do local _ENV = {assert=assert, A=20};
  117. assert(A==20);x=A
  118. end
  119. assert(A==10 and x==20)
  120. end
  121. assert(x==20)
  122. print('OK')
  123. return 5,f
  124. end -- }