1
0

locals.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. print('testing local variables plus some extra stuff')
  2. do
  3. local i = 10
  4. do local i = 100; assert(i==100) end
  5. do local i = 1000; assert(i==1000) end
  6. assert(i == 10)
  7. if i ~= 10 then
  8. local i = 20
  9. else
  10. local i = 30
  11. assert(i == 30)
  12. end
  13. end
  14. f = nil
  15. local f
  16. x = 1
  17. a = nil
  18. loadstring('local a = {}')()
  19. assert(type(a) ~= 'table')
  20. function f (a)
  21. local _1, _2, _3, _4, _5
  22. local _6, _7, _8, _9, _10
  23. local x = 3
  24. local b = a
  25. local c,d = a,b
  26. if (d == b) then
  27. local x = 'q'
  28. x = b
  29. assert(x == 2)
  30. else
  31. assert(nil)
  32. end
  33. assert(x == 3)
  34. local f = 10
  35. end
  36. local b=10
  37. local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3
  38. assert(x == 1)
  39. f(2)
  40. assert(type(f) == 'function')
  41. -- testing globals ;-)
  42. do
  43. local f = {}
  44. local _G = _G
  45. for i=1,10 do f[i] = function (x) A=A+1; return A, _G.getfenv(x) end end
  46. A=10; assert(f[1]() == 11)
  47. for i=1,10 do assert(setfenv(f[i], {A=i}) == f[i]) end
  48. assert(f[3]() == 4 and A == 11)
  49. local a,b = f[8](1)
  50. assert(b.A == 9)
  51. a,b = f[8](0)
  52. assert(b.A == 11) -- `real' global
  53. local g
  54. local function f () assert(setfenv(2, {a='10'}) == g) end
  55. g = function () f(); _G.assert(_G.getfenv(1).a == '10') end
  56. g(); assert(getfenv(g).a == '10')
  57. end
  58. -- test for global table of loaded chunks
  59. local function foo (s)
  60. return loadstring(s)
  61. end
  62. assert(getfenv(foo("")) == _G)
  63. local a = {loadstring = loadstring}
  64. setfenv(foo, a)
  65. assert(getfenv(foo("")) == _G)
  66. setfenv(0, a) -- change global environment
  67. assert(getfenv(foo("")) == a)
  68. setfenv(0, _G)
  69. -- testing limits for special instructions
  70. local a
  71. local p = 4
  72. for i=2,31 do
  73. for j=-3,3 do
  74. assert(loadstring(string.format([[local a=%s;a=a+
  75. %s;
  76. assert(a
  77. ==2^%s)]], j, p-j, i))) ()
  78. assert(loadstring(string.format([[local a=%s;
  79. a=a-%s;
  80. assert(a==-2^%s)]], -j, p-j, i))) ()
  81. assert(loadstring(string.format([[local a,b=0,%s;
  82. a=b-%s;
  83. assert(a==-2^%s)]], -j, p-j, i))) ()
  84. end
  85. p =2*p
  86. end
  87. print'+'
  88. if rawget(_G, "querytab") then
  89. -- testing clearing of dead elements from tables
  90. collectgarbage("stop") -- stop GC
  91. local a = {[{}] = 4, [3] = 0, alo = 1,
  92. a1234567890123456789012345678901234567890 = 10}
  93. local t = querytab(a)
  94. for k,_ in pairs(a) do a[k] = nil end
  95. collectgarbage() -- restore GC and collect dead fiels in `a'
  96. for i=0,t-1 do
  97. local k = querytab(a, i)
  98. assert(k == nil or type(k) == 'number' or k == 'alo')
  99. end
  100. end
  101. print('OK')
  102. return 5,f