locals.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. print('testing local variables and environments')
  2. local debug = require"debug"
  3. -- bug in 5.1:
  4. local function f(x) x = nil; return x end
  5. assert(f(10) == nil)
  6. local function f() local x; return x end
  7. assert(f(10) == nil)
  8. local function f(x) x = nil; local y; return x, y end
  9. assert(f(10) == nil and select(2, f(20)) == nil)
  10. do
  11. local i = 10
  12. do local i = 100; assert(i==100) end
  13. do local i = 1000; assert(i==1000) end
  14. assert(i == 10)
  15. if i ~= 10 then
  16. local i = 20
  17. else
  18. local i = 30
  19. assert(i == 30)
  20. end
  21. end
  22. f = nil
  23. local f
  24. x = 1
  25. a = nil
  26. load('local a = {}')()
  27. assert(a == nil)
  28. function f (a)
  29. local _1, _2, _3, _4, _5
  30. local _6, _7, _8, _9, _10
  31. local x = 3
  32. local b = a
  33. local c,d = a,b
  34. if (d == b) then
  35. local x = 'q'
  36. x = b
  37. assert(x == 2)
  38. else
  39. assert(nil)
  40. end
  41. assert(x == 3)
  42. local f = 10
  43. end
  44. local b=10
  45. local a; repeat local b; a,b=1,2; assert(a+1==b); until a+b==3
  46. assert(x == 1)
  47. f(2)
  48. assert(type(f) == 'function')
  49. local function getenv (f)
  50. local a,b = debug.getupvalue(f, 1)
  51. assert(a == '_ENV')
  52. return b
  53. end
  54. -- test for global table of loaded chunks
  55. assert(getenv(load"a=3") == _G)
  56. local c = {}; local f = load("a = 3", nil, nil, c)
  57. assert(getenv(f) == c)
  58. assert(c.a == nil)
  59. f()
  60. assert(c.a == 3)
  61. -- testing limits for special instructions
  62. if not _soft then
  63. local a
  64. local p = 4
  65. for i=2,31 do
  66. for j=-3,3 do
  67. assert(load(string.format([[local a=%s;
  68. a=a+%s;
  69. assert(a ==2^%s)]], j, p-j, i))) ()
  70. assert(load(string.format([[local a=%s;
  71. a=a-%s;
  72. assert(a==-2^%s)]], -j, p-j, i))) ()
  73. assert(load(string.format([[local a,b=0,%s;
  74. a=b-%s;
  75. assert(a==-2^%s)]], -j, p-j, i))) ()
  76. end
  77. p =2*p
  78. end
  79. end
  80. print'+'
  81. if rawget(_G, "querytab") then
  82. -- testing clearing of dead elements from tables
  83. collectgarbage("stop") -- stop GC
  84. local a = {[{}] = 4, [3] = 0, alo = 1,
  85. a1234567890123456789012345678901234567890 = 10}
  86. local t = querytab(a)
  87. for k,_ in pairs(a) do a[k] = nil end
  88. collectgarbage() -- restore GC and collect dead fiels in `a'
  89. for i=0,t-1 do
  90. local k = querytab(a, i)
  91. assert(k == nil or type(k) == 'number' or k == 'alo')
  92. end
  93. end
  94. -- testing lexical environments
  95. assert(_ENV == _G)
  96. do local _ENV = (function (...) return ... end)(_G, dummy)
  97. do local _ENV = {assert=assert}; assert(true) end
  98. mt = {_G = _G}
  99. local foo,x
  100. do local _ENV = mt
  101. function foo (x)
  102. A = x
  103. do local _ENV = _G; A = 1000 end
  104. return function (x) return A .. x end
  105. end
  106. end
  107. assert(getenv(foo) == mt)
  108. x = foo('hi'); assert(mt.A == 'hi' and A == 1000)
  109. assert(x('*') == mt.A .. '*')
  110. do local _ENV = {assert=assert, A=10};
  111. do local _ENV = {assert=assert, A=20};
  112. assert(A==20);x=A
  113. end
  114. assert(A==10 and x==20)
  115. end
  116. assert(x==20)
  117. print('OK')
  118. return 5,f
  119. end