Bring Lua to 5.4.6. (#1214)

This essentially re-does the work of #875 on top of master.

This is what I did to check that Cosmo's Lua extensions still worked:

```
$ build/bootstrap/make MODE=aarch64 o/aarch64/third_party/lua/lua
$ ape o/aarch64/third_party/lua/lua
>: 10
10
>: 010
8
>: 0b10
2
>: string.byte("\e")
27
>: "Hello, %s" % {"world"}
Hello, world
>: "*" * 3
***
```

`luaL_traceback2` was used to show the stack trace with parameter
values; it's used in `LuaCallWithTrace`, which is used in Redbean to run
Lua code. You should be able to see the extended stack trace by running
something like this: `redbean -e "function a(b)c()end a(2)"` (with
"params" indicating the extended stack trace):

```
stack traceback:
 [string "function a(b)c()end a(2)"]:1: in function 'a', params: b = 2;
 [string "function a(b)c()end a(2)"]:1: in main chunk
```
@pkulchenko confirmed that I get the expected result with the updated
code.

This is what I did to check that Lua itself still worked:

```
$ cd third_party/lua/test/
$ ape ../../../o/aarch64/third_party/lua/lua all.lua
```

There's one test failure, in `files.lua`:

```
***** FILE 'files.lua'*****
testing i/o
../../../o/aarch64/third_party/lua/lua: files.lua:84: assertion failed!
stack traceback:
[C]: in function 'assert'
files.lua:84: in main chunk
(...tail calls...)
all.lua:195: in main chunk
[C]: in ?
.>>> closing state <<<
```

That isn't a result of these changes; the same test is failing in
master.

The failure is here:

```lua
if not _port then   -- invalid seek
  local status, msg, code = io.stdin:seek("set", 1000)
  assert(not status and type(msg) == "string" and type(code) == "number")
end
```

The test expects a seek to offset 1,000 on stdin to fail — but it
doesn't. `status` ends up being the new offset rather than `nil`.

If I comment out that one test, the remaining tests succeed.
This commit is contained in:
Michael Lenaghan 2024-06-15 20:13:08 -04:00 committed by GitHub
parent 3a599bfbe1
commit 0dbf01bf1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
90 changed files with 2741 additions and 1376 deletions

View file

@ -163,6 +163,7 @@ f()
dofile('db.lua')
assert(dofile('calls.lua') == deep and deep)
_G.deep = nil
olddofile('strings.lua')
olddofile('literals.lua')
dofile('tpack.lua')

View file

@ -14,7 +14,7 @@ local pack = table.pack
-- standard error message for memory errors
local MEMERRMSG = "not enough memory"
function tcheck (t1, t2)
local function tcheck (t1, t2)
assert(t1.n == (t2.n or #t2) + 1)
for i = 2, t1.n do assert(t1[i] == t2[i - 1]) end
end
@ -28,7 +28,7 @@ end
print('testing C API')
a = T.testC("pushvalue R; return 1")
local a = T.testC("pushvalue R; return 1")
assert(a == debug.getregistry())
@ -43,10 +43,10 @@ a = T.d2s(12458954321123.0)
assert(a == string.pack("d", 12458954321123.0))
assert(T.s2d(a) == 12458954321123.0)
a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
local a,b,c = T.testC("pushnum 1; pushnum 2; pushnum 3; return 2")
assert(a == 2 and b == 3 and not c)
f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
local f = T.makeCfunc("pushnum 1; pushnum 2; pushnum 3; return 2")
a,b,c = f()
assert(a == 2 and b == 3 and not c)
@ -61,7 +61,7 @@ assert(a==false and b==true and c==false)
a,b,c = T.testC("gettop; return 2", 10, 20, 30, 40)
assert(a == 40 and b == 5 and not c)
t = pack(T.testC("settop 5; return *", 2, 3))
local t = pack(T.testC("settop 5; return *", 2, 3))
tcheck(t, {n=4,2,3})
t = pack(T.testC("settop 0; settop 15; return 10", 3, 1, 23))
@ -166,16 +166,17 @@ end
-- testing globals
_G.a = 14; _G.b = "a31"
_G.AA = 14; _G.BB = "a31"
local a = {T.testC[[
getglobal a;
getglobal b;
getglobal b;
setglobal a;
getglobal AA;
getglobal BB;
getglobal BB;
setglobal AA;
return *
]]}
assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.a == "a31")
assert(a[2] == 14 and a[3] == "a31" and a[4] == nil and _G.AA == "a31")
_G.AA, _G.BB = nil
-- testing arith
assert(T.testC("pushnum 10; pushnum 20; arith /; return 1") == 0.5)
@ -199,13 +200,14 @@ a,b,c = T.testC([[pushnum 1;
pushstring 10; arith _;
pushstring 5; return 3]])
assert(a == 1 and b == -10 and c == "5")
mt = {__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
local mt = {
__add = function (a,b) return setmetatable({a[1] + b[1]}, mt) end,
__mod = function (a,b) return setmetatable({a[1] % b[1]}, mt) end,
__unm = function (a) return setmetatable({a[1]* 2}, mt) end}
a,b,c = setmetatable({4}, mt),
setmetatable({8}, mt),
setmetatable({-3}, mt)
x,y,z = T.testC("arith +; return 2", 10, a, b)
local x,y,z = T.testC("arith +; return 2", 10, a, b)
assert(x == 10 and y[1] == 12 and z == nil)
assert(T.testC("arith %; return 1", a, c)[1] == 4%-3)
assert(T.testC("arith _; arith +; arith %; return 1", b, a, c)[1] ==
@ -312,9 +314,9 @@ assert(T.testC("concat 1; return 1", "xuxu") == "xuxu")
-- testing lua_is
function B(x) return x and 1 or 0 end
local function B (x) return x and 1 or 0 end
function count (x, n)
local function count (x, n)
n = n or 2
local prog = [[
isnumber %d;
@ -345,7 +347,7 @@ assert(count(nil, 15) == 100)
-- testing lua_to...
function to (s, x, n)
local function to (s, x, n)
n = n or 2
return T.testC(string.format("%s %d; return 1", s, n), x)
end
@ -486,11 +488,12 @@ a = T.testC([[
pushvalue 3; insert -2; pcall 1 1 0;
pcall 0 0 0;
return 1
]], "x=150", function (a) assert(a==nil); return 3 end)
]], "XX=150", function (a) assert(a==nil); return 3 end)
assert(type(a) == 'string' and x == 150)
assert(type(a) == 'string' and XX == 150)
_G.XX = nil
function check3(p, ...)
local function check3(p, ...)
local arg = {...}
assert(#arg == 3)
assert(string.find(arg[3], p))
@ -500,7 +503,7 @@ check3("%.", T.testC("loadfile 2; return *", "."))
check3("xxxx", T.testC("loadfile 2; return *", "xxxx"))
-- test errors in non protected threads
function checkerrnopro (code, msg)
local function checkerrnopro (code, msg)
local th = coroutine.create(function () end) -- create new thread
local stt, err = pcall(T.testC, th, code) -- run code there
assert(not stt and string.find(err, msg))
@ -510,8 +513,9 @@ if not _soft then
collectgarbage("stop") -- avoid __gc with full stack
checkerrnopro("pushnum 3; call 0 0", "attempt to call")
print"testing stack overflow in unprotected thread"
function f () f() end
checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow")
function F () F() end
checkerrnopro("getglobal 'F'; call 0 0;", "stack overflow")
F = nil
collectgarbage("restart")
end
print"+"
@ -588,7 +592,7 @@ assert(a[a] == "x")
b = setmetatable({p = a}, {})
getmetatable(b).__index = function (t, i) return t.p[i] end
k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
local k, x = T.testC("gettable 3, return 2", 4, b, 20, 35, "x")
assert(x == 15 and k == 35)
k = T.testC("getfield 2 y, return 1", b)
assert(k == 12)
@ -748,8 +752,8 @@ local i = T.ref{}
T.unref(i)
assert(T.ref{} == i)
Arr = {}
Lim = 100
local Arr = {}
local Lim = 100
for i=1,Lim do -- lock many objects
Arr[i] = T.ref({})
end
@ -761,7 +765,7 @@ for i=1,Lim do -- unlock all them
T.unref(Arr[i])
end
function printlocks ()
local function printlocks ()
local f = T.makeCfunc("gettable R; return 1")
local n = f("n")
print("n", n)
@ -793,8 +797,8 @@ assert(type(T.getref(a)) == 'table')
-- colect in cl the `val' of all collected userdata
tt = {}
cl = {n=0}
local tt = {}
local cl = {n=0}
A = nil; B = nil
local F
F = function (x)
@ -804,20 +808,20 @@ F = function (x)
d = nil
assert(debug.getmetatable(x).__gc == F)
assert(load("table.insert({}, {})"))() -- create more garbage
collectgarbage() -- force a GC during GC
assert(debug.getmetatable(x).__gc == F) -- previous GC did not mess this?
assert(not collectgarbage()) -- GC during GC (no op)
local dummy = {} -- create more garbage during GC
if A ~= nil then
assert(type(A) == "userdata")
assert(T.udataval(A) == B)
debug.getmetatable(A) -- just access it
end
A = x -- ressucita userdata
A = x -- ressurect userdata
B = udval
return 1,2,3
end
tt.__gc = F
-- test whether udate collection frees memory in the right time
do
collectgarbage();
@ -854,9 +858,9 @@ end
collectgarbage("stop")
-- create 3 userdatas with tag `tt'
a = T.newuserdata(0); debug.setmetatable(a, tt); na = T.udataval(a)
b = T.newuserdata(0); debug.setmetatable(b, tt); nb = T.udataval(b)
c = T.newuserdata(0); debug.setmetatable(c, tt); nc = T.udataval(c)
a = T.newuserdata(0); debug.setmetatable(a, tt); local na = T.udataval(a)
b = T.newuserdata(0); debug.setmetatable(b, tt); local nb = T.udataval(b)
c = T.newuserdata(0); debug.setmetatable(c, tt); local nc = T.udataval(c)
-- create userdata without meta table
x = T.newuserdata(4)
@ -867,9 +871,9 @@ checkerr("FILE%* expected, got userdata", io.input, x)
assert(debug.getmetatable(x) == nil and debug.getmetatable(y) == nil)
d=T.ref(a);
e=T.ref(b);
f=T.ref(c);
local d = T.ref(a);
local e = T.ref(b);
local f = T.ref(c);
t = {T.getref(d), T.getref(e), T.getref(f)}
assert(t[1] == a and t[2] == b and t[3] == c)
@ -889,7 +893,7 @@ tt=nil -- frees tt for GC
A = nil
b = nil
T.unref(d);
n5 = T.newuserdata(0)
local n5 = T.newuserdata(0)
debug.setmetatable(n5, {__gc=F})
n5 = T.udataval(n5)
collectgarbage()
@ -960,11 +964,11 @@ print'+'
-- testing changing hooks during hooks
_G.t = {}
_G.TT = {}
T.sethook([[
# set a line hook after 3 count hooks
sethook 4 0 '
getglobal t;
getglobal TT;
pushvalue -3; append -2
pushvalue -2; append -2
']], "c", 3)
@ -974,12 +978,13 @@ a = 1 -- count hook (set line hook)
a = 1 -- line hook
a = 1 -- line hook
debug.sethook()
t = _G.t
local t = _G.TT
assert(t[1] == "line")
line = t[2]
local line = t[2]
assert(t[3] == "line" and t[4] == line + 1)
assert(t[5] == "line" and t[6] == line + 2)
assert(t[7] == nil)
_G.TT = nil
-------------------------------------------------------------------------
@ -1004,6 +1009,7 @@ do -- testing errors during GC
collectgarbage("restart")
warn("@on")
end
_G.A = nil
-------------------------------------------------------------------------
-- test for userdata vals
do
@ -1033,8 +1039,8 @@ assert(a == 'alo' and b == '3')
T.doremote(L1, "_ERRORMESSAGE = nil")
-- error: `sin' is not defined
a, _, b = T.doremote(L1, "return sin(1)")
assert(a == nil and b == 2) -- 2 == run-time error
a, b, c = T.doremote(L1, "return sin(1)")
assert(a == nil and c == 2) -- 2 == run-time error
-- error: syntax error
a, b, c = T.doremote(L1, "return a+")
@ -1205,7 +1211,7 @@ T.alloccount() -- remove limit
-- o that we get memory errors in all allocations of a given
-- task, until there is enough memory to complete the task without
-- errors.
function testbytes (s, f)
local function testbytes (s, f)
collectgarbage()
local M = T.totalmem()
local oldM = M
@ -1230,7 +1236,7 @@ end
-- task, until there is enough allocations to complete the task without
-- errors.
function testalloc (s, f)
local function testalloc (s, f)
collectgarbage()
local M = 0
local a,b = nil
@ -1297,12 +1303,12 @@ end)
-- testing threads
-- get main thread from registry (at index LUA_RIDX_MAINTHREAD == 1)
mt = T.testC("rawgeti R 1; return 1")
local mt = T.testC("rawgeti R 1; return 1")
assert(type(mt) == "thread" and coroutine.running() == mt)
function expand (n,s)
local function expand (n,s)
if n==0 then return "" end
local e = string.rep("=", n)
return string.format("T.doonnewstack([%s[ %s;\n collectgarbage(); %s]%s])\n",
@ -1312,9 +1318,10 @@ end
G=0; collectgarbage(); a =collectgarbage("count")
load(expand(20,"G=G+1"))()
assert(G==20); collectgarbage(); -- assert(gcinfo() <= a+1)
G = nil
testamem("running code on new thread", function ()
return T.doonnewstack("x=1") == 0 -- try to create thread
return T.doonnewstack("local x=1") == 0 -- try to create thread
end)
@ -1328,13 +1335,13 @@ end)
local testprog = [[
local function foo () return end
local t = {"x"}
a = "aaa"
for i = 1, #t do a=a..t[i] end
AA = "aaa"
for i = 1, #t do AA = AA .. t[i] end
return true
]]
-- testing memory x dofile
_G.a = nil
_G.AA = nil
local t =os.tmpname()
local f = assert(io.open(t, "w"))
f:write(testprog)
@ -1344,7 +1351,7 @@ testamem("dofile", function ()
return a and a()
end)
assert(os.remove(t))
assert(_G.a == "aaax")
assert(_G.AA == "aaax")
-- other generic tests
@ -1361,6 +1368,8 @@ testamem("dump/undump", function ()
return a and a()
end)
_G.AA = nil
local t = os.tmpname()
testamem("file creation", function ()
local f = assert(io.open(t, 'w'))
@ -1382,7 +1391,7 @@ testamem("constructors", function ()
end)
local a = 1
close = nil
local close = nil
testamem("closure creation", function ()
function close (b)
return function (x) return b + x end

View file

@ -85,7 +85,7 @@ local DIR = "libs" .. dirsep
-- prepend DIR to a name and correct directory separators
local function D (x)
x = string.gsub(x, "/", dirsep)
local x = string.gsub(x, "/", dirsep)
return DIR .. x
end
@ -106,7 +106,7 @@ local function createfiles (files, preextras, posextras)
end
end
function removefiles (files)
local function removefiles (files)
for n in pairs(files) do
os.remove(D(n))
end
@ -154,10 +154,9 @@ local try = function (p, n, r, ext)
assert(ext == x)
end
a = require"names"
local a = require"names"
assert(a[1] == "names" and a[2] == D"names.lua")
_G.a = nil
local st, msg = pcall(require, "err")
assert(not st and string.find(msg, "arithmetic") and B == 15)
st, msg = pcall(require, "synerr")
@ -191,6 +190,7 @@ try("X", "XXxX", AA, "libs/XXxX")
removefiles(files)
NAME, REQUIRED, AA, B = nil
-- testing require of sub-packages
@ -223,7 +223,7 @@ assert(require"P1" == m and m.AA == 10)
removefiles(files)
AA = nil
package.path = ""
assert(not pcall(require, "file_does_not_exist"))
@ -305,6 +305,7 @@ else
assert(_ENV.x == "lib1.sub" and _ENV.y == DC"lib1")
assert(string.find(ext, "libs/lib1", 1, true))
assert(fs.id(45) == 45)
_ENV.x, _ENV.y = nil
end
_ENV = _G
@ -338,10 +339,10 @@ print("testing assignments, logical operators, and constructors")
local res, res2 = 27
a, b = 1, 2+3
local a, b = 1, 2+3
assert(a==1 and b==5)
a={}
function f() return 10, 11, 12 end
local function f() return 10, 11, 12 end
a.x, b, a[1] = 1, 2, f()
assert(a.x==1 and b==2 and a[1]==10)
a[f()], b, a[f()+3] = f(), a, 'x'
@ -353,15 +354,15 @@ do
local a,b,c
a,b = 0, f(1)
assert(a == 0 and b == 1)
A,b = 0, f(1)
assert(A == 0 and b == 1)
a,b = 0, f(1)
assert(a == 0 and b == 1)
a,b,c = 0,5,f(4)
assert(a==0 and b==5 and c==1)
a,b,c = 0,5,f(0)
assert(a==0 and b==5 and c==nil)
end
a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
local a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
assert(not a and b and c and d==6)
d = 20
@ -419,6 +420,7 @@ assert(not pcall(function () local a = {[nil] = 10} end))
assert(a[nil] == undef)
a = nil
local a, b, c
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
a, a.x, a.y = a, a[-3]
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
@ -434,6 +436,16 @@ a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 ==
10)
do
-- _ENV constant
local function foo ()
local _ENV <const> = 11
X = "hi"
end
local st, msg = pcall(foo)
assert(not st and string.find(msg, "number"))
end
-- test of large float/integer indices
@ -445,7 +457,7 @@ while maxint ~= (maxint + 0.0) or (maxint - 1) ~= (maxint - 1.0) do
maxint = maxint // 2
end
maxintF = maxint + 0.0 -- float version
local maxintF = maxint + 0.0 -- float version
assert(maxintF == maxint and math.type(maxintF) == "float" and
maxintF >= 2.0^14)

View file

@ -32,7 +32,7 @@ setmetatable(env, {
})
X = nil
co = coroutine.wrap(f)
local co = coroutine.wrap(f)
assert(co() == 's')
assert(co() == 'g')
assert(co() == 'g')

View file

@ -38,6 +38,18 @@ d = d << 32
assert(a | b ~ c & d == 0xF4000000 << 32)
assert(~~a == a and ~a == -1 ~ a and -d == ~d + 1)
do -- constant folding
local code = string.format("return -1 >> %d", math.maxinteger)
assert(load(code)() == 0)
local code = string.format("return -1 >> %d", math.mininteger)
assert(load(code)() == 0)
local code = string.format("return -1 << %d", math.maxinteger)
assert(load(code)() == 0)
local code = string.format("return -1 << %d", math.mininteger)
assert(load(code)() == 0)
end
assert(-1 >> 1 == (1 << (numbits - 1)) - 1 and 1 << 31 == 0x80000000)
assert(-1 >> (numbits - 1) == 1)
assert(-1 >> numbits == 0 and
@ -45,6 +57,11 @@ assert(-1 >> numbits == 0 and
-1 << numbits == 0 and
-1 << -numbits == 0)
assert(1 >> math.mininteger == 0)
assert(1 >> math.maxinteger == 0)
assert(1 << math.mininteger == 0)
assert(1 << math.maxinteger == 0)
assert((2^30 - 1) << 2^30 == 0)
assert((2^30 - 1) >> 2^30 == 0)

View file

@ -16,7 +16,7 @@ assert(type(nil) == 'nil'
and type(type) == 'function')
assert(type(assert) == type(print))
function f (x) return a:x (x) end
local function f (x) return a:x (x) end
assert(type(f) == 'function')
assert(not pcall(type))
@ -33,10 +33,11 @@ do
assert(fact(5) == 120)
end
assert(fact == false)
fact = nil
-- testing declarations
a = {i = 10}
self = 20
local a = {i = 10}
local self = 20
function a:x (x) return x+self.i end
function a.y (x) return x+self end
@ -72,6 +73,8 @@ f(1,2, -- this one too
3,4)
assert(t[1] == 1 and t[2] == 2 and t[3] == 3 and t[4] == 'a')
t = nil -- delete 't'
function fat(x)
if x <= 1 then return 1
else return x*load("return fat(" .. x-1 .. ")", "")()
@ -80,26 +83,29 @@ end
assert(load "load 'assert(fat(6)==720)' () ")()
a = load('return fat(5), 3')
a,b = a()
local a,b = a()
assert(a == 120 and b == 3)
fat = nil
print('+')
function err_on_n (n)
local function err_on_n (n)
if n==0 then error(); exit(1);
else err_on_n (n-1); exit(1);
end
end
do
function dummy (n)
local function dummy (n)
if n > 0 then
assert(not pcall(err_on_n, n))
dummy(n-1)
end
end
dummy(10)
end
dummy(10)
_G.deep = nil -- "declaration" (used by 'all.lua')
function deep (n)
if n>0 then deep(n-1) end
@ -151,6 +157,16 @@ do -- tail calls x varargs
end
do -- C-stack overflow while handling C-stack overflow
local function loop ()
assert(pcall(loop))
end
local err, msg = xpcall(loop, loop)
assert(not err and string.find(msg, "error"))
end
do -- tail calls x chain of __call
local n = 10000 -- depth
@ -199,7 +215,7 @@ assert(a == 23 and (function (x) return x*2 end)(20) == 40)
-- testing closures
-- fixed-point operator
Z = function (le)
local Z = function (le)
local function a (f)
return le(function (x) return f(f)(x) end)
end
@ -209,14 +225,14 @@ Z = function (le)
-- non-recursive factorial
F = function (f)
local F = function (f)
return function (n)
if n == 0 then return 1
else return n*f(n-1) end
end
end
fat = Z(F)
local fat = Z(F)
assert(fat(0) == 1 and fat(4) == 24 and Z(F)(5)==5*Z(F)(4))
@ -227,22 +243,21 @@ local function g (z)
return f(z,z+1,z+2,z+3)
end
f = g(10)
local f = g(10)
assert(f(9, 16) == 10+11+12+13+10+9+16+10)
Z, F, f = nil
print('+')
-- testing multiple returns
function unlpack (t, i)
local function unlpack (t, i)
i = i or 1
if (i <= #t) then
return t[i], unlpack(t, i+1)
end
end
function equaltab (t1, t2)
local function equaltab (t1, t2)
assert(#t1 == #t2)
for i = 1, #t1 do
assert(t1[i] == t2[i])
@ -251,8 +266,8 @@ end
local pack = function (...) return (table.pack(...)) end
function f() return 1,2,30,4 end
function ret2 (a,b) return a,b end
local function f() return 1,2,30,4 end
local function ret2 (a,b) return a,b end
local a,b,c,d = unlpack{1,2,3}
assert(a==1 and b==2 and c==3 and d==nil)
@ -281,7 +296,7 @@ table.sort({10,9,8,4,19,23,0,0}, function (a,b) return a<b end, "extra arg")
local x = "-- a comment\0\0\0\n x = 10 + \n23; \
local a = function () x = 'hi' end; \
return '\0'"
function read1 (x)
local function read1 (x)
local i = 0
return function ()
collectgarbage()
@ -290,7 +305,7 @@ function read1 (x)
end
end
function cannotload (msg, a,b)
local function cannotload (msg, a,b)
assert(not a and string.find(b, msg))
end
@ -327,11 +342,26 @@ do -- another bug (in 5.4.0)
end
do -- another bug (since 5.2)
-- corrupted binary dump: list of upvalue names is larger than number
-- of upvalues, overflowing the array of upvalues.
local code =
"\x1b\x4c\x75\x61\x54\x00\x19\x93\x0d\x0a\x1a\x0a\x04\x08\x08\x78\x56\z
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x28\x77\x40\x00\x86\x40\z
\x74\x65\x6d\x70\x81\x81\x01\x00\x02\x82\x48\x00\x02\x00\xc7\x00\x01\z
\x00\x80\x80\x80\x82\x00\x00\x80\x81\x82\x78\x80\x82\x81\x86\x40\x74\z
\x65\x6d\x70"
assert(load(code)) -- segfaults in previous versions
end
x = string.dump(load("x = 1; return x"))
a = assert(load(read1(x), nil, "b"))
assert(a() == 1 and _G.x == 1)
cannotload("attempt to load a binary chunk", load(read1(x), nil, "t"))
cannotload("attempt to load a binary chunk", load(x, nil, "t"))
_G.x = nil
assert(not pcall(string.dump, print)) -- no dump of C functions
@ -356,7 +386,7 @@ debug.setupvalue(x, 2, _G)
assert(x() == 123)
assert(assert(load("return XX + ...", nil, nil, {XX = 13}))(4) == 17)
XX = nil
-- test generic load with nested functions
x = [[

View file

@ -4,7 +4,7 @@
print "testing closures"
local A,B = 0,{g=10}
function f(x)
local function f(x)
local a = {}
for i=1,1000 do
local y = 0
@ -89,6 +89,7 @@ assert(r == "a" and s == "b")
-- testing closures with 'for' control variable x break
local f
for i=1,3 do
f = function () return i end
break
@ -139,7 +140,7 @@ assert(b('get') == 'xuxu')
b('set', 10); assert(b('get') == 14)
local w
local y, w
-- testing multi-level closure
function f(x)
return function (y)
@ -230,6 +231,7 @@ t()
-- test for debug manipulation of upvalues
local debug = require'debug'
local foo1, foo2, foo3
do
local a , b, c = 3, 5, 7
foo1 = function () return a+b end;

View file

@ -69,10 +69,24 @@ foo = function (f, a)
checkKlist(foo, {100000, 100000.0, -100000, -100000.0})
-- floats x integers
foo = function (t, a)
t[a] = 1; t[a] = 1.0
t[a] = 1; t[a] = 1.0
t[a] = 2; t[a] = 2.0
t[a] = 0; t[a] = 0.0
t[a] = 1; t[a] = 1.0
t[a] = 2; t[a] = 2.0
t[a] = 0; t[a] = 0.0
end
checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0})
-- testing opcodes
-- check that 'f' opcodes match '...'
function check (f, ...)
local function check (f, ...)
local arg = {...}
local c = T.listcode(f)
for i=1, #arg do
@ -85,7 +99,7 @@ end
-- check that 'f' opcodes match '...' and that 'f(p) == r'.
function checkR (f, p, r, ...)
local function checkR (f, p, r, ...)
local r1 = f(p)
assert(r == r1 and math.type(r) == math.type(r1))
check(f, ...)
@ -93,7 +107,7 @@ end
-- check that 'a' and 'b' has the same opcodes
function checkequal (a, b)
local function checkequal (a, b)
a = T.listcode(a)
b = T.listcode(b)
assert(#a == #b)

View file

@ -11,6 +11,7 @@ local function checkload (s, msg)
end
-- testing semicollons
local a
do ;;; end
; do ; a = 3; assert(a == 3) end;
;
@ -49,10 +50,10 @@ assert((((nil and true) or false) and true) == false)
local a,b = 1,nil;
assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
local x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
x,y=1,2;
local x, y = 1, 2;
assert((x>y) and x or y == 2);
x,y=2,1;
assert((x>y) and x or y == 2);
@ -77,13 +78,13 @@ do -- testing operators with diffent kinds of constants
local gab = f(o1, o2)
_ENV.XX = o1
code = string.format("return XX %s %s", op, o2)
res = assert(load(code))()
local code = string.format("return XX %s %s", op, o2)
local res = assert(load(code))()
assert(res == gab)
_ENV.XX = o2
local code = string.format("return (%s) %s XX", o1, op)
local res = assert(load(code))()
code = string.format("return (%s) %s XX", o1, op)
res = assert(load(code))()
assert(res == gab)
code = string.format("return (%s) %s %s", o1, op, o2)
@ -92,6 +93,7 @@ do -- testing operators with diffent kinds of constants
end
end
end
_ENV.XX = nil
end
@ -100,10 +102,35 @@ repeat until 1; repeat until true;
while false do end; while nil do end;
do -- test old bug (first name could not be an `upvalue')
local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
local a; local function f(x) x={a=1}; x={x=1}; x={G=1} end
end
function f (i)
do -- bug since 5.4.0
-- create code with a table using more than 256 constants
local code = {"local x = {"}
for i = 1, 257 do
code[#code + 1] = i .. ".1,"
end
code[#code + 1] = "};"
code = table.concat(code)
-- add "ret" to the end of that code and checks that
-- it produces the expected value "val"
local function check (ret, val)
local code = code .. ret
code = load(code)
assert(code() == val)
end
check("return (1 ~ (2 or 3))", 1 ~ 2)
check("return (1 | (2 or 3))", 1 | 2)
check("return (1 + (2 or 3))", 1 + 2)
check("return (1 << (2 or 3))", 1 << 2)
end
local function f (i)
if type(i) ~= 'number' then return i,'jojo'; end;
if i > 0 then return i, f(i-1); end;
end
@ -129,10 +156,10 @@ end
assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
for i=1,1000 do break; end;
n=100;
i=3;
t = {};
a=nil
local n=100;
local i=3;
local t = {};
local a=nil
while not a do
a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
end
@ -175,14 +202,14 @@ a={y=1}
x = {a.y}
assert(x[1] == 1)
function f(i)
local function f (i)
while 1 do
if i>0 then i=i-1;
else return; end;
end;
end;
function g(i)
local function g(i)
while 1 do
if i>0 then i=i-1
else return end
@ -247,7 +274,7 @@ function g (a,b,c,d,e)
if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
end
function h (a,b,c,d,e)
local function h (a,b,c,d,e)
while (a>=b or c or (d and e) or nil) do return 1; end;
return 0;
end;
@ -275,7 +302,7 @@ do
assert(a==2)
end
function F(a)
local function F (a)
assert(debug.getinfo(1, "n").name == 'F')
return a,2,3
end
@ -368,6 +395,8 @@ for n = 1, level do
if i % 60000 == 0 then print('+') end
end
end
IX = nil
_G.GLOB1 = nil
------------------------------------------------------------------
-- testing some syntax errors (chosen through 'gcov')

View file

@ -30,7 +30,8 @@ local function eqtab (t1, t2)
end
_G.x = nil -- declare x
function foo (a, ...)
_G.f = nil -- declare f
local function foo (a, ...)
local x, y = coroutine.running()
assert(x == f and y == false)
-- next call should not corrupt coroutine (but must fail,
@ -67,10 +68,11 @@ assert(coroutine.status(f) == "dead")
s, a = coroutine.resume(f, "xuxu")
assert(not s and string.find(a, "dead") and coroutine.status(f) == "dead")
_G.f = nil
-- yields in tail calls
local function foo (i) return coroutine.yield(i) end
f = coroutine.wrap(function ()
local f = coroutine.wrap(function ()
for i=1,10 do
assert(foo(i) == _G.x)
end
@ -79,8 +81,10 @@ end)
for i=1,10 do _G.x = i; assert(f(i) == i) end
_G.x = 'xuxu'; assert(f('xuxu') == 'a')
_G.x = nil
-- recursive
function pf (n, i)
local function pf (n, i)
coroutine.yield(n)
pf(n*i, i+1)
end
@ -93,14 +97,14 @@ for i=1,10 do
end
-- sieve
function gen (n)
local function gen (n)
return coroutine.wrap(function ()
for i=2,n do coroutine.yield(i) end
end)
end
function filter (p, g)
local function filter (p, g)
return coroutine.wrap(function ()
while 1 do
local n = g()
@ -136,6 +140,10 @@ do
assert(coroutine.status(co) == "dead")
local st, msg = coroutine.close(co)
assert(st and msg == nil)
-- also ok to close it again
st, msg = coroutine.close(co)
assert(st and msg == nil)
-- cannot close the running coroutine
local st, msg = pcall(coroutine.close, coroutine.running())
@ -149,6 +157,22 @@ do
assert(not st and string.find(msg, "normal"))
end))()
-- cannot close a coroutine while closing it
do
local co
co = coroutine.create(
function()
local x <close> = func2close(function()
coroutine.close(co) -- try to close it again
end)
coroutine.yield(20)
end)
local st, msg = coroutine.resume(co)
assert(st and msg == 20)
st, msg = coroutine.close(co)
assert(not st and string.find(msg, "running coroutine"))
end
-- to-be-closed variables in coroutines
local X
@ -158,6 +182,9 @@ do
assert(not st and msg == 100)
st, msg = coroutine.close(co)
assert(not st and msg == 100)
-- after closing, no more errors
st, msg = coroutine.close(co)
assert(st and msg == nil)
co = coroutine.create(function ()
local x <close> = func2close(function (self, err)
@ -189,23 +216,26 @@ do
local st, msg = coroutine.close(co)
assert(st == false and coroutine.status(co) == "dead" and msg == 200)
assert(x == 200)
-- after closing, no more errors
st, msg = coroutine.close(co)
assert(st and msg == nil)
end
do
-- <close> versus pcall in coroutines
local X = false
local Y = false
function foo ()
local function foo ()
local x <close> = func2close(function (self, err)
Y = debug.getinfo(2)
X = err
end)
error(43)
end
co = coroutine.create(function () return pcall(foo) end)
local co = coroutine.create(function () return pcall(foo) end)
local st1, st2, err = coroutine.resume(co)
assert(st1 and not st2 and err == 43)
assert(X == 43 and Y.name == "pcall")
assert(X == 43 and Y.what == "C")
-- recovering from errors in __close metamethods
local track = {}
@ -249,7 +279,7 @@ end
-- yielding across C boundaries
co = coroutine.wrap(function()
local co = coroutine.wrap(function()
assert(not pcall(table.sort,{1,2,3}, coroutine.yield))
assert(coroutine.isyieldable())
coroutine.yield(20)
@ -277,15 +307,15 @@ local r1, r2, v = f1(nil)
assert(r1 and not r2 and v[1] == (10 + 1)*10/2)
function f (a, b) a = coroutine.yield(a); error{a + b} end
function g(x) return x[1]*2 end
local function f (a, b) a = coroutine.yield(a); error{a + b} end
local function g(x) return x[1]*2 end
co = coroutine.wrap(function ()
coroutine.yield(xpcall(f, g, 10, 20))
end)
assert(co() == 10)
r, msg = co(100)
local r, msg = co(100)
assert(not r and msg == 240)
@ -347,9 +377,10 @@ assert(not a and b == foo and coroutine.status(x) == "dead")
a,b = coroutine.resume(x)
assert(not a and string.find(b, "dead") and coroutine.status(x) == "dead")
goo = nil
-- co-routines x for loop
function all (a, n, k)
local function all (a, n, k)
if k == 0 then coroutine.yield(a)
else
for i=1,n do
@ -389,7 +420,7 @@ assert(f() == 43 and f() == 53)
-- old bug: attempt to resume itself
function co_func (current_co)
local function co_func (current_co)
assert(coroutine.running() == current_co)
assert(coroutine.resume(current_co) == false)
coroutine.yield(10, 20)
@ -419,7 +450,7 @@ do
local X = false
A = coroutine.wrap(function()
local _ <close> = setmetatable({}, {__close = function () X = true end})
local _ <close> = func2close(function () X = true end)
return pcall(A, 1)
end)
st, res = A()
@ -427,6 +458,22 @@ do
end
-- bug in 5.4.1
do
-- coroutine ran close metamethods with invalid status during a
-- reset.
local co
co = coroutine.wrap(function()
local x <close> = func2close(function() return pcall(co) end)
error(111)
end)
local st, errobj = pcall(co)
assert(not st and errobj == 111)
st, errobj = pcall(co)
assert(not st and string.find(errobj, "dead coroutine"))
end
-- attempt to resume 'normal' coroutine
local co1, co2
co1 = coroutine.create(function () return co2() end)
@ -449,15 +496,16 @@ a = nil
-- access to locals of erroneous coroutines
local x = coroutine.create (function ()
local a = 10
_G.f = function () a=a+1; return a end
_G.F = function () a=a+1; return a end
error('x')
end)
assert(not coroutine.resume(x))
-- overwrite previous position of local `a'
assert(not coroutine.resume(x, 1, 1, 1, 1, 1, 1, 1))
assert(_G.f() == 11)
assert(_G.f() == 12)
assert(_G.F() == 11)
assert(_G.F() == 12)
_G.F = nil
if not T then
@ -468,7 +516,7 @@ else
local turn
function fact (t, x)
local function fact (t, x)
assert(turn == t)
if x == 0 then return 1
else return x*fact(t, x-1)
@ -537,6 +585,7 @@ else
_G.X = nil; co(); assert(_G.X == line + 2 and _G.XX == nil)
_G.X = nil; co(); assert(_G.X == line + 3 and _G.XX == 20)
assert(co() == 10)
_G.X = nil
-- testing yields in count hook
co = coroutine.wrap(function ()
@ -614,6 +663,8 @@ else
assert(X == 'a a a' and Y == 'OK')
X, Y = nil
-- resuming running coroutine
C = coroutine.create(function ()
@ -659,7 +710,7 @@ else
X = function (x) coroutine.yield(x, 'BB'); return 'CC' end;
return 'ok']]))
t = table.pack(T.testC(state, [[
local t = table.pack(T.testC(state, [[
rawgeti R 1 # get main thread
pushstring 'XX'
getglobal X # get function for body
@ -688,31 +739,28 @@ end
-- leaving a pending coroutine open
_X = coroutine.wrap(function ()
_G.TO_SURVIVE = coroutine.wrap(function ()
local a = 10
local x = function () a = a+1 end
coroutine.yield()
end)
_X()
_G.TO_SURVIVE()
if not _soft then
-- bug (stack overflow)
local j = 2^9
local lim = 1000000 -- (C stack limit; assume 32-bit machine)
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1}
local lim = 1000000 -- stack limit; assume 32-bit machine
local t = {lim - 10, lim - 5, lim - 1, lim, lim + 1, lim + 5}
for i = 1, #t do
local j = t[i]
co = coroutine.create(function()
local t = {}
for i = 1, j do t[i] = i end
return table.unpack(t)
local co = coroutine.create(function()
return table.unpack({}, 1, j)
end)
local r, msg = coroutine.resume(co)
assert(not r)
-- must fail for unpacking larger than stack limit
assert(j < lim or not r)
end
co = nil
end
@ -896,7 +944,7 @@ assert(run(function ()
do local _ENV = _ENV
f = function () AAA = BBB + 1; return AAA end
end
g = new(10); g.k.BBB = 10;
local g = new(10); g.k.BBB = 10;
debug.setupvalue(f, 1, g)
assert(run(f, {"idx", "nidx", "idx"}) == 11)
assert(g.k.AAA == 11)
@ -1036,6 +1084,8 @@ assert(#a == 3 and a[1] == a[2] and a[2] == a[3] and a[3] == 34)
-- testing yields with continuations
local y
co = coroutine.wrap(function (...) return
T.testC([[ # initial function
yieldk 1 2
@ -1088,6 +1138,9 @@ assert(x == "YIELD" and y == 4)
assert(not pcall(co)) -- coroutine should be dead
_G.ctx = nil
_G.status = nil
-- bug in nCcalls
local co = coroutine.wrap(function ()

View file

@ -84,6 +84,32 @@ do -- bug in 5.4.0
end
do -- bug since 5.4.0
local count = 0
print("chain of 'coroutine.close'")
-- create N coroutines forming a list so that each one, when closed,
-- closes the previous one. (With a large enough N, previous Lua
-- versions crash in this test.)
local coro = false
for i = 1, 1000 do
local previous = coro
coro = coroutine.create(function()
local cc <close> = setmetatable({}, {__close=function()
count = count + 1
if previous then
assert(coroutine.close(previous))
end
end})
coroutine.yield() -- leaves 'cc' pending to be closed
end)
assert(coroutine.resume(coro)) -- start it and run until it yields
end
local st, msg = coroutine.close(coro)
assert(not st and string.find(msg, "C stack overflow"))
print("final count: ", count)
end
do
print("nesting of resuming yielded coroutines")
local count = 0
@ -103,6 +129,20 @@ do
end
do -- bug in 5.4.2
print("nesting coroutines running after recoverable errors")
local count = 0
local function foo()
count = count + 1
pcall(1) -- create an error
-- running now inside 'precover' ("protected recover")
coroutine.wrap(foo)() -- call another coroutine
end
checkerror("C stack overflow", foo)
print("final count: ", count)
end
if T then
print("testing stack recovery")
local N = 0 -- trace number of calls

View file

@ -16,7 +16,7 @@ end
assert(not debug.gethook())
local testline = 19 -- line where 'test' is defined
function test (s, l, p) -- this must be line 19
local function test (s, l, p) -- this must be line 19
collectgarbage() -- avoid gc during trace
local function f (event, line)
assert(event == 'line')
@ -50,7 +50,7 @@ end
-- test file and string names truncation
a = "function f () end"
local a = "function f () end"
local function dostring (s, x) return load(s, x)() end
dostring(a)
assert(debug.getinfo(f).short_src == string.format('[string "%s"]', a))
@ -72,7 +72,8 @@ dostring(a, string.format("=%s", string.rep('x', 500)))
assert(string.find(debug.getinfo(f).short_src, "^x*$"))
dostring(a, "=")
assert(debug.getinfo(f).short_src == "")
a = nil; f = nil;
_G.a = nil; _G.f = nil;
_G[string.rep("p", 400)] = nil
repeat
@ -120,6 +121,7 @@ else
end
]], {2,3,4,7})
test([[
local function foo()
end
@ -128,6 +130,7 @@ A = 1
A = 2
A = 3
]], {2, 3, 2, 4, 5, 6})
_G.A = nil
test([[--
@ -175,6 +178,8 @@ end
test([[for i=1,4 do a=1 end]], {1,1,1,1})
_G.a = nil
do -- testing line info/trace with large gaps in source
@ -194,6 +199,50 @@ do -- testing line info/trace with large gaps in source
end
end
end
_G.a = nil
do -- testing active lines
local function checkactivelines (f, lines)
local t = debug.getinfo(f, "SL")
for _, l in pairs(lines) do
l = l + t.linedefined
assert(t.activelines[l])
t.activelines[l] = undef
end
assert(next(t.activelines) == nil) -- no extra lines
end
checkactivelines(function (...) -- vararg function
-- 1st line is empty
-- 2nd line is empty
-- 3th line is empty
local a = 20
-- 5th line is empty
local b = 30
-- 7th line is empty
end, {4, 6, 8})
checkactivelines(function (a)
-- 1st line is empty
-- 2nd line is empty
local a = 20
local b = 30
-- 5th line is empty
end, {3, 4, 6})
checkactivelines(function (a, b, ...) end, {0})
checkactivelines(function (a, b)
end, {1})
for _, n in pairs{0, 1, 2, 10, 50, 100, 1000, 10000} do
checkactivelines(
load(string.format("%s return 1", string.rep("\n", n))),
{n + 1})
end
end
print'+'
@ -244,7 +293,6 @@ foo(200, 3, 4)
local a = {}
for i = 1, (_soft and 100 or 1000) do a[i] = i end
foo(table.unpack(a))
a = nil
@ -264,13 +312,14 @@ do -- test hook presence in debug info
debug.sethook()
assert(count == 4)
end
_ENV.a = nil
-- hook table has weak keys
assert(getmetatable(debug.getregistry()._HOOKKEY).__mode == 'k')
a = {}; L = nil
a = {}; local L = nil
local glob = 1
local oldglob = glob
debug.sethook(function (e,l)
@ -311,7 +360,7 @@ function foo()
end; foo() -- set L
-- check line counting inside strings and empty lines
_ = 'alo\
local _ = 'alo\
alo' .. [[
]]
@ -360,6 +409,7 @@ function g(a,b) return (a+1) + f() end
assert(g(0,0) == 30)
_G.f, _G.g = nil
debug.sethook(nil);
assert(not debug.gethook())
@ -403,7 +453,7 @@ local function collectlocals (level)
end
X = nil
local X = nil
a = {}
function a:f (a, b, ...) local arg = {...}; local c = 13 end
debug.sethook(function (e)
@ -426,6 +476,7 @@ a:f(1,2,3,4,5)
assert(X.self == a and X.a == 1 and X.b == 2 and X.c == nil)
assert(XX == 12)
assert(not debug.gethook())
_G.XX = nil
-- testing access to local variables in return hook (bug in 5.2)
@ -550,6 +601,7 @@ end
debug.sethook()
local g, g1
-- tests for tail calls
local function f (x)
@ -595,7 +647,7 @@ h(false)
debug.sethook()
assert(b == 2) -- two tail calls
lim = _soft and 3000 or 30000
local lim = _soft and 3000 or 30000
local function foo (x)
if x==0 then
assert(debug.getinfo(2).what == "main")
@ -844,7 +896,7 @@ do -- testing debug info for finalizers
-- create a piece of garbage with a finalizer
setmetatable({}, {__gc = function ()
local t = debug.getinfo(2) -- get callee information
local t = debug.getinfo(1) -- get function information
assert(t.namewhat == "metamethod")
name = t.name
end})
@ -897,7 +949,7 @@ end
print("testing debug functions on chunk without debug info")
prog = [[-- program to be loaded without debug information (strip)
local prog = [[-- program to be loaded without debug information (strip)
local debug = require'debug'
local a = 12 -- a local variable

View file

@ -26,7 +26,7 @@ end
local function checkmessage (prog, msg, debug)
local m = doit(prog)
if debug then print(m) end
if debug then print(m, msg) end
assert(string.find(m, msg, 1, true))
end
@ -114,12 +114,14 @@ checkmessage("a = {} | 1", "bitwise operation")
checkmessage("a = {} < 1", "attempt to compare")
checkmessage("a = {} <= 1", "attempt to compare")
checkmessage("a=1; bbbb=2; a=math.sin(3)+bbbb(3)", "global 'bbbb'")
checkmessage("a={}; do local a=1 end a:bbbb(3)", "method 'bbbb'")
checkmessage("aaa=1; bbbb=2; aaa=math.sin(3)+bbbb(3)", "global 'bbbb'")
checkmessage("aaa={}; do local aaa=1 end aaa:bbbb(3)", "method 'bbbb'")
checkmessage("local a={}; a.bbbb(3)", "field 'bbbb'")
assert(not string.find(doit"a={13}; local bbbb=1; a[bbbb](3)", "'bbbb'"))
checkmessage("a={13}; local bbbb=1; a[bbbb](3)", "number")
checkmessage("a=(1)..{}", "a table value")
assert(not string.find(doit"aaa={13}; local bbbb=1; aaa[bbbb](3)", "'bbbb'"))
checkmessage("aaa={13}; local bbbb=1; aaa[bbbb](3)", "number")
checkmessage("aaa=(1)..{}", "a table value")
_G.aaa, _G.bbbb = nil
-- calls
checkmessage("local a; a(13)", "local 'a'")
@ -134,12 +136,13 @@ checkmessage([[
-- tail calls
checkmessage("local a={}; return a.bbbb(3)", "field 'bbbb'")
checkmessage("a={}; do local a=1 end; return a:bbbb(3)", "method 'bbbb'")
checkmessage("aaa={}; do local aaa=1 end; return aaa:bbbb(3)", "method 'bbbb'")
checkmessage("a = #print", "length of a function value")
checkmessage("a = #3", "length of a number value")
checkmessage("aaa = #print", "length of a function value")
checkmessage("aaa = #3", "length of a number value")
_G.aaa = nil
aaa = nil
checkmessage("aaa.bbb:ddd(9)", "global 'aaa'")
checkmessage("local aaa={bbb=1}; aaa.bbb:ddd(9)", "field 'bbb'")
checkmessage("local aaa={bbb={}}; aaa.bbb:ddd(9)", "method 'ddd'")
@ -152,15 +155,16 @@ checkmessage("local a,b,cc; (function () a.x = 1 end)()", "upvalue 'a'")
checkmessage("local _ENV = {x={}}; a = a + 1", "global 'a'")
checkmessage("b=1; local aaa={}; x=aaa+b", "local 'aaa'")
checkmessage("BB=1; local aaa={}; x=aaa+BB", "local 'aaa'")
checkmessage("aaa={}; x=3.3/aaa", "global 'aaa'")
checkmessage("aaa=2; b=nil;x=aaa*b", "global 'b'")
checkmessage("aaa=2; BB=nil;x=aaa*BB", "global 'BB'")
checkmessage("aaa={}; x=-aaa", "global 'aaa'")
-- short circuit
checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(3)",
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = math.sin(1) and bbbb(3)",
"local 'bbbb'")
checkmessage("aaa=1; local aaa,bbbb=2,3; aaa = bbbb(1) or aaa(3)",
"local 'bbbb'")
checkmessage("a=1; local a,bbbb=2,3; a = bbbb(1) or a(3)", "local 'bbbb'")
checkmessage("local a,b,c,f = 1,1,1; f((a and b) or c)", "local 'f'")
checkmessage("local a,b,c = 1,1,1; ((a and b) or c)()", "call a number value")
assert(not string.find(doit"aaa={}; x=(aaa or aaa)+(aaa and aaa)", "'aaa'"))
@ -187,8 +191,8 @@ checkmessage("return ~-3e40", "has no integer representation")
checkmessage("return ~-3.009", "has no integer representation")
checkmessage("return 3.009 & 1", "has no integer representation")
checkmessage("return 34 >> {}", "table value")
checkmessage("a = 24 // 0", "divide by zero")
checkmessage("a = 1 % 0", "'n%0'")
checkmessage("aaa = 24 // 0", "divide by zero")
checkmessage("aaa = 1 % 0", "'n%0'")
-- type error for an object which is neither in an upvalue nor a register.
@ -228,6 +232,22 @@ do -- named objects (field '__name')
checkmessage("return {} < XX", "table with My Type")
checkmessage("return XX < io.stdin", "My Type with FILE*")
_G.XX = nil
if T then -- extra tests for 'luaL_tolstring'
-- bug in 5.4.3; 'luaL_tolstring' with negative indices
local x = setmetatable({}, {__name="TABLE"})
assert(T.testC("Ltolstring -1; return 1", x) == tostring(x))
local a, b = T.testC("pushint 10; Ltolstring -2; return 2", x)
assert(a == 10 and b == tostring(x))
setmetatable(x, {__tostring=function (o)
assert(o == x)
return "ABC"
end})
local a, b, c = T.testC("pushint 10; Ltolstring -2; return 3", x)
assert(a == x and b == 10 and c == "ABC")
end
end
-- global functions
@ -253,13 +273,13 @@ end
-- tests for field accesses after RK limit
local t = {}
for i = 1, 1000 do
t[i] = "a = x" .. i
t[i] = "aaa = x" .. i
end
local s = table.concat(t, "; ")
t = nil
checkmessage(s.."; a = bbb + 1", "global 'bbb'")
checkmessage("local _ENV=_ENV;"..s.."; a = bbb + 1", "global 'bbb'")
checkmessage(s.."; local t = {}; a = t.bbb + 1", "field 'bbb'")
checkmessage(s.."; aaa = bbb + 1", "global 'bbb'")
checkmessage("local _ENV=_ENV;"..s.."; aaa = bbb + 1", "global 'bbb'")
checkmessage(s.."; local t = {}; aaa = t.bbb + 1", "field 'bbb'")
checkmessage(s.."; local t = {}; t:bbb()", "method 'bbb'")
checkmessage([[aaa=9
@ -289,7 +309,7 @@ end]], "global 'insert'")
checkmessage([[ -- tail call
return math.sin("a")
]], "'sin'")
]], "sin")
checkmessage([[collectgarbage("nooption")]], "invalid option")
@ -308,14 +328,17 @@ main()
]], "global 'NoSuchName'")
print'+'
a = {}; setmetatable(a, {__index = string})
checkmessage("a:sub()", "bad self")
aaa = {}; setmetatable(aaa, {__index = string})
checkmessage("aaa:sub()", "bad self")
checkmessage("string.sub('a', {})", "#2")
checkmessage("('a'):sub{}", "#1")
checkmessage("table.sort({1,2,3}, table.sort)", "'table.sort'")
checkmessage("string.gsub('s', 's', setmetatable)", "'setmetatable'")
_G.aaa = nil
-- tests for errors in coroutines
local function f (n)
@ -333,7 +356,7 @@ checkerr("yield across", f)
-- testing size of 'source' info; size of buffer for that info is
-- LUA_IDSIZE, declared as 60 in luaconf. Get one position for '\0'.
idsize = 60 - 1
local idsize = 60 - 1
local function checksize (source)
-- syntax error
local _, msg = load("x", source)
@ -395,13 +418,14 @@ x
local p = [[
function g() f() end
function f(x) error('a', X) end
function f(x) error('a', XX) end
g()
]]
X=3;lineerror((p), 3)
X=0;lineerror((p), false)
X=1;lineerror((p), 2)
X=2;lineerror((p), 1)
XX=3;lineerror((p), 3)
XX=0;lineerror((p), false)
XX=1;lineerror((p), 2)
XX=2;lineerror((p), 1)
_G.XX, _G.g, _G.f = nil
lineerror([[
@ -420,6 +444,14 @@ if not b then
end
end]], 5)
-- bug in 5.4.0
lineerror([[
local a = 0
local b = 1
local c = b % a
]], 3)
do
-- Force a negative estimate for base line. Error in instruction 2
-- (after VARARGPREP, GETGLOBAL), with first absolute line information
@ -433,11 +465,11 @@ if not _soft then
-- several tests that exaust the Lua stack
collectgarbage()
print"testing stack overflow"
C = 0
local C = 0
-- get line where stack overflow will happen
local l = debug.getinfo(1, "l").currentline + 1
local function auxy () C=C+1; auxy() end -- produce a stack overflow
function y ()
function YY ()
collectgarbage("stop") -- avoid running finalizers without stack space
auxy()
collectgarbage("restart")
@ -449,9 +481,11 @@ if not _soft then
return (string.find(m, "stack overflow"))
end
-- repeated stack overflows (to check stack recovery)
assert(checkstackmessage(doit('y()')))
assert(checkstackmessage(doit('y()')))
assert(checkstackmessage(doit('y()')))
assert(checkstackmessage(doit('YY()')))
assert(checkstackmessage(doit('YY()')))
assert(checkstackmessage(doit('YY()')))
_G.YY = nil
-- error lines in stack overflow
@ -545,7 +579,7 @@ do
end
-- xpcall with arguments
a, b, c = xpcall(string.find, error, "alo", "al")
local a, b, c = xpcall(string.find, error, "alo", "al")
assert(a and b == 1 and c == 2)
a, b, c = xpcall(string.find, function (x) return {} end, true, "al")
assert(not a and type(b) == "table" and c == nil)
@ -565,11 +599,12 @@ checksyntax("a\1a = 1", "", "<\\1>", 1)
-- test 255 as first char in a chunk
checksyntax("\255a = 1", "", "<\\255>", 1)
doit('I = load("a=9+"); a=3')
assert(a==3 and not I)
doit('I = load("a=9+"); aaa=3')
assert(_G.aaa==3 and not _G.I)
_G.I,_G.aaa = nil
print('+')
lim = 1000
local lim = 1000
if _soft then lim = 100 end
for i=1,lim do
doit('a = ')

View file

@ -420,6 +420,9 @@ assert(i == 3 and x[1] == 3 and x[3] == 5)
assert(_G.X == 20)
_G.X, _G.B = nil
print'+'
local _g = _G

View file

@ -507,15 +507,17 @@ load((io.lines(file, 1)))()
assert(_G.X == 4)
load((io.lines(file, 3)))()
assert(_G.X == 8)
_G.X = nil
print('+')
local x1 = "string\n\n\\com \"\"''coisas [[estranhas]] ]]'"
io.output(file)
assert(io.write(string.format("x2 = %q\n-- comment without ending EOS", x1)))
assert(io.write(string.format("X2 = %q\n-- comment without ending EOS", x1)))
io.close()
assert(loadfile(file))()
assert(x1 == x2)
assert(x1 == _G.X2)
_G.X2 = nil
print('+')
assert(os.remove(file))
assert(not os.remove(file))
@ -610,6 +612,7 @@ do
assert(os.remove(file))
end
io.output(file)
assert(io.write("qualquer coisa\n"))
assert(io.write("mais qualquer coisa"))
@ -743,7 +746,7 @@ if not _port then
{"exit 129", "exit", 129},
{"kill -s HUP $$", "signal", 1},
{"kill -s KILL $$", "signal", 9},
{"sh -c 'kill -s HUP $$'", "signal", 1},
{"sh -c 'kill -s HUP $$'", "signal", 1}, -- [jart]
{progname .. ' -e " "', "ok"},
{progname .. ' -e "os.exit(0, true)"', "ok"},
{progname .. ' -e "os.exit(20, true)"', "exit", 20},
@ -833,8 +836,17 @@ checkerr("missing", os.time, {hour = 12}) -- missing date
if string.packsize("i") == 4 then -- 4-byte ints
checkerr("field 'year' is out-of-bound", os.time,
{year = -(1 << 31) + 1899, month = 1, day = 1})
checkerr("field 'year' is out-of-bound", os.time,
{year = -(1 << 31), month = 1, day = 1})
if math.maxinteger > 2^31 then -- larger lua_integer?
checkerr("field 'year' is out-of-bound", os.time,
{year = (1 << 31) + 1900, month = 1, day = 1})
end
end
if not _port then
-- test Posix-specific modifiers
assert(type(os.date("%Ex")) == 'string')

View file

@ -125,7 +125,7 @@ do
end
a:test()
_G.temp = nil
end
@ -134,7 +134,7 @@ do local f = function () end end
print("functions with errors")
prog = [[
local prog = [[
do
a = 10;
function foo(x,y)
@ -153,22 +153,25 @@ do
end
end
end
rawset(_G, "a", nil)
_G.x = nil
do
foo = nil
print('long strings')
x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
local x = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"
assert(string.len(x)==80)
s = ''
k = math.min(300, (math.maxinteger // 80) // 2)
for n = 1, k do s = s..x; j=tostring(n) end
local s = ''
local k = math.min(300, (math.maxinteger // 80) // 2)
for n = 1, k do s = s..x; local j=tostring(n) end
assert(string.len(s) == k*80)
s = string.sub(s, 1, 10000)
s, i = string.gsub(s, '(%d%d%d%d)', '')
local s, i = string.gsub(s, '(%d%d%d%d)', '')
assert(i==10000 // 4)
s = nil
x = nil
assert(_G["while"] == 234)
_G["while"] = nil
end
--
@ -227,8 +230,8 @@ end
print("clearing tables")
lim = 15
a = {}
local lim = 15
local a = {}
-- fill a with `collectable' indices
for i=1,lim do a[{}] = i end
b = {}
@ -371,7 +374,7 @@ if T then
warn("@on"); warn("@store")
collectgarbage()
assert(string.find(_WARN, "error in __gc metamethod"))
assert(string.find(_WARN, "error in __gc"))
assert(string.match(_WARN, "@(.-)@") == "expected"); _WARN = false
for i = 8, 10 do assert(s[i]) end
@ -552,6 +555,7 @@ do
for i=1,1000 do _ENV.a = {} end -- no collection during the loop
until gcinfo() > 2 * x
collectgarbage"restart"
_ENV.a = nil
end
@ -676,11 +680,13 @@ end
-- just to make sure
assert(collectgarbage'isrunning')
do -- check that the collector is reentrant in incremental mode
do -- check that the collector is not reentrant in incremental mode
local res = true
setmetatable({}, {__gc = function ()
collectgarbage()
res = collectgarbage()
end})
collectgarbage()
assert(not res)
end

View file

@ -11,17 +11,17 @@ CFLAGS = -Wall -std=gnu99 -O2 -I$(LUA_DIR) -fPIC -shared
all: lib1.so lib11.so lib2.so lib21.so lib2-v2.so
touch all
lib1.so: lib1.c $(LUA_DIR)/luaconf.h
lib1.so: lib1.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
$(CC) $(CFLAGS) -o lib1.so lib1.c
lib11.so: lib11.c $(LUA_DIR)/luaconf.h
lib11.so: lib11.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
$(CC) $(CFLAGS) -o lib11.so lib11.c
lib2.so: lib2.c $(LUA_DIR)/luaconf.h
lib2.so: lib2.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
$(CC) $(CFLAGS) -o lib2.so lib2.c
lib21.so: lib21.c $(LUA_DIR)/luaconf.h
lib21.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
$(CC) $(CFLAGS) -o lib21.so lib21.c
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h
lib2-v2.so: lib21.c $(LUA_DIR)/luaconf.h $(LUA_DIR)/lua.h
$(CC) $(CFLAGS) -o lib2-v2.so lib22.c

View file

@ -10,6 +10,7 @@ local function dostring (x) return assert(load(x), "")() end
dostring("x \v\f = \t\r 'a\0a' \v\f\f")
assert(x == 'a\0a' and string.len(x) == 3)
_G.x = nil
-- escape sequences
assert('\n\"\'\\' == [[
@ -129,16 +130,16 @@ end
-- long variable names
var1 = string.rep('a', 15000) .. '1'
var2 = string.rep('a', 15000) .. '2'
prog = string.format([[
local var1 = string.rep('a', 15000) .. '1'
local var2 = string.rep('a', 15000) .. '2'
local prog = string.format([[
%s = 5
%s = %s + 1
return function () return %s - %s end
]], var1, var2, var1, var1, var2)
local f = dostring(prog)
assert(_G[var1] == 5 and _G[var2] == 6 and f() == -1)
var1, var2, f = nil
_G[var1], _G[var2] = nil
print('+')
-- escapes --
@ -150,13 +151,13 @@ assert([[
$debug]] == "\n $debug")
assert([[ [ ]] ~= [[ ] ]])
-- long strings --
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
assert(string.len(b) == 960)
prog = [=[
print('+')
a1 = [["this is a 'string' with several 'quotes'"]]
a2 = "'quotes'"
local a1 = [["this is a 'string' with several 'quotes'"]]
local a2 = "'quotes'"
assert(string.find(a1, a2) == 34)
print('+')
@ -164,12 +165,13 @@ print('+')
a1 = [==[temp = [[an arbitrary value]]; ]==]
assert(load(a1))()
assert(temp == 'an arbitrary value')
_G.temp = nil
-- long strings --
b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
local b = "001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789001234567890123456789012345678901234567891234567890123456789012345678901234567890012345678901234567890123456789012345678912345678901234567890123456789012345678900123456789012345678901234567890123456789123456789012345678901234567890123456789"
assert(string.len(b) == 960)
print('+')
a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
local a = [[00123456789012345678901234567890123456789123456789012345678901234567890123456789
00123456789012345678901234567890123456789123456789012345678901234567890123456789
00123456789012345678901234567890123456789123456789012345678901234567890123456789
00123456789012345678901234567890123456789123456789012345678901234567890123456789
@ -199,19 +201,41 @@ x = 1
]=]
print('+')
x = nil
_G.x = nil
dostring(prog)
assert(x)
_G.x = nil
prog = nil
a = nil
b = nil
do -- reuse of long strings
-- get the address of a string
local function getadd (s) return string.format("%p", s) end
local s1 <const> = "01234567890123456789012345678901234567890123456789"
local s2 <const> = "01234567890123456789012345678901234567890123456789"
local s3 = "01234567890123456789012345678901234567890123456789"
local function foo() return s1 end
local function foo1() return s3 end
local function foo2()
return "01234567890123456789012345678901234567890123456789"
end
local a1 = getadd(s1)
assert(a1 == getadd(s2))
assert(a1 == getadd(foo()))
assert(a1 == getadd(foo1()))
assert(a1 == getadd(foo2()))
local sd = "0123456789" .. "0123456789012345678901234567890123456789"
assert(sd == s1 and getadd(sd) ~= a1)
end
-- testing line ends
prog = [[
a = 1 -- a comment
b = 2
local a = 1 -- a comment
local b = 2
x = [=[
@ -228,10 +252,11 @@ for _, n in pairs{"\n", "\r", "\n\r", "\r\n"} do
assert(dostring(prog) == nn)
assert(_G.x == "hi\n" and _G.y == "\nhello\r\n\n")
end
_G.x, _G.y = nil
-- testing comments and strings with long brackets
a = [==[]=]==]
local a = [==[]=]==]
assert(a == "]=")
a = [==[[===[[=[]]=][====[]]===]===]==]

View file

@ -37,7 +37,7 @@ end
f = nil
local f
x = 1
local x = 1
a = nil
load('local a = {}')()
@ -152,7 +152,7 @@ local dummy
local _ENV = (function (...) return ... end)(_G, dummy) -- {
do local _ENV = {assert=assert}; assert(true) end
mt = {_G = _G}
local mt = {_G = _G}
local foo,x
A = false -- "declare" A
do local _ENV = mt
@ -174,6 +174,8 @@ do local _ENV = {assert=assert, A=10};
end
assert(x==20)
A = nil
do -- constants
local a<const>, b, c<const> = 10, 20, 30
@ -187,6 +189,8 @@ do -- constants
checkro("y", "local x, y <const>, z = 10, 20, 30; x = 11; y = 12")
checkro("x", "local x <const>, y, z <const> = 10, 20, 30; x = 11")
checkro("z", "local x <const>, y, z <const> = 10, 20, 30; y = 10; z = 11")
checkro("foo", "local foo <const> = 10; function foo() end")
checkro("foo", "local foo <const> = {}; function foo() end")
checkro("z", [[
local a, z <const>, b = 10;
@ -335,6 +339,49 @@ do
end
do
-- bug in 5.4.3: previous condition (calls cannot be tail in the
-- scope of to-be-closed variables) must be valid for tbc variables
-- created by 'for' loops.
local closed = false
local function foo ()
return function () return true end, 0, 0,
func2close(function () closed = true end)
end
local function tail() return closed end
local function foo1 ()
for k in foo() do return tail() end
end
assert(foo1() == false)
assert(closed == true)
end
do
-- bug in 5.4.4: 'break' may generate wrong 'close' instruction when
-- leaving a loop block.
local closed = false
local o1 = setmetatable({}, {__close=function() closed = true end})
local function test()
for k, v in next, {}, nil, o1 do
local function f() return k end -- create an upvalue
break
end
assert(closed)
end
test()
end
do print("testing errors in __close")
-- original error is in __close
@ -567,6 +614,28 @@ end
if rawget(_G, "T") then
do
-- bug in 5.4.3
-- 'lua_settop' may use a pointer to stack invalidated by 'luaF_close'
-- reduce stack size
collectgarbage(); collectgarbage(); collectgarbage()
-- force a stack reallocation
local function loop (n)
if n < 400 then loop(n + 1) end
end
-- close metamethod will reallocate the stack
local o = setmetatable({}, {__close = function () loop(0) end})
local script = [[toclose 2; settop 1; return 1]]
assert(T.testC(script, o) == script)
end
-- memory error inside closing function
local function foo ()
local y <close> = func2close(function () T.alloccount() end)
@ -644,7 +713,7 @@ if rawget(_G, "T") then
collectgarbage(); collectgarbage()
m = T.totalmem()
local m = T.totalmem()
collectgarbage("stop")
-- error in the first buffer allocation
@ -790,6 +859,65 @@ do
end
do
-- yielding inside closing metamethods while returning
-- (bug in 5.4.3)
local extrares -- result from extra yield (if any)
local function check (body, extra, ...)
local t = table.pack(...) -- expected returns
local co = coroutine.wrap(body)
if extra then
extrares = co() -- runs until first (extra) yield
end
local res = table.pack(co()) -- runs until yield inside '__close'
assert(res.n == 2 and res[2] == nil)
local res2 = table.pack(co()) -- runs until end of function
assert(res2.n == t.n)
for i = 1, #t do
if t[i] == "x" then
assert(res2[i] == res[1]) -- value that was closed
else
assert(res2[i] == t[i])
end
end
end
local function foo ()
local x <close> = func2close(coroutine.yield)
local extra <close> = func2close(function (self)
assert(self == extrares)
coroutine.yield(100)
end)
extrares = extra
return table.unpack{10, x, 30}
end
check(foo, true, 10, "x", 30)
assert(extrares == 100)
local function foo ()
local x <close> = func2close(coroutine.yield)
return
end
check(foo, false)
local function foo ()
local x <close> = func2close(coroutine.yield)
local y, z = 20, 30
return x
end
check(foo, false, "x")
local function foo ()
local x <close> = func2close(coroutine.yield)
local extra <close> = func2close(coroutine.yield)
return table.unpack({}, 1, 100) -- 100 nils
end
check(foo, true, table.unpack({}, 1, 100))
end
do
-- yielding inside closing metamethods after an error

View file

@ -94,6 +94,33 @@ RUN('echo "print(10)\nprint(2)\n" | lua > %s', out)
checkout("10\n2\n")
-- testing BOM
prepfile("\xEF\xBB\xBF")
RUN('lua %s > %s', prog, out)
checkout("")
prepfile("\xEF\xBB\xBFprint(3)")
RUN('lua %s > %s', prog, out)
checkout("3\n")
prepfile("\xEF\xBB\xBF# comment!!\nprint(3)")
RUN('lua %s > %s', prog, out)
checkout("3\n")
-- bad BOMs
prepfile("\xEF")
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
prepfile("\xEF\xBB")
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
prepfile("\xEFprint(3)")
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
prepfile("\xEF\xBBprint(3)")
NoRun("unexpected symbol", 'lua %s > %s', prog, out)
-- test option '-'
RUN('echo "print(arg[1])" | lua - -h > %s', out)
checkout("-h\n")
@ -190,6 +217,11 @@ prepfile(("print(a); print(_G['%s'].x)"):format(prog), otherprog)
RUN('env LUA_PATH="?;;" lua -l %s -l%s -lstring -l io %s > %s', prog, otherprog, otherprog, out)
checkout("1\n2\n15\n2\n15\n")
-- test explicit global names in -l
prepfile("print(str.upper'alo alo', m.max(10, 20))")
RUN("lua -l 'str=string' '-lm=math' -e 'print(m.sin(0))' %s > %s", prog, out)
checkout("0.0\nALO ALO\t20\n")
-- test 'arg' table
local a = [[
assert(#arg == 3 and arg[1] == 'a' and
@ -256,6 +288,34 @@ u2 = setmetatable({}, {__gc = function () error("ZYX") end})
RUN('lua -W %s 2> %s', prog, out)
checkprogout("ZYX)\nXYZ)\n")
-- bug since 5.2: finalizer called when closing a state could
-- subvert finalization order
prepfile[[
-- should be called last
print("creating 1")
setmetatable({}, {__gc = function () print(1) end})
print("creating 2")
setmetatable({}, {__gc = function ()
print("2")
print("creating 3")
-- this finalizer should not be called, as object will be
-- created after 'lua_close' has been called
setmetatable({}, {__gc = function () print(3) end})
print(collectgarbage()) -- cannot call collector here
os.exit(0, true)
end})
]]
RUN('lua -W %s > %s', prog, out)
checkout[[
creating 1
creating 2
2
creating 3
nil
1
]]
-- test many arguments
prepfile[[print(({...})[30])]]
@ -279,7 +339,7 @@ prepfile("a = [[b\nc\nd\ne]]\n=a")
RUN([[lua -e"_PROMPT='' _PROMPT2=''" -i < %s > %s]], prog, out)
checkprogout("b\nc\nd\ne\n\n")
prompt = "alo"
local prompt = "alo"
prepfile[[ --
a = 2
]]
@ -339,7 +399,7 @@ NoRun("error object is a table value", [[lua %s]], prog)
-- chunk broken in many lines
s = [=[ --
local s = [=[ --
function f ( x )
local a = [[
xuxu
@ -361,12 +421,10 @@ checkprogout("101\n13\t22\n\n")
prepfile[[#comment in 1st line without \n at the end]]
RUN('lua %s', prog)
prepfile[[#test line number when file starts with comment line
debug = require"debug"
print(debug.getinfo(1).currentline)
]]
-- first-line comment with binary file
prepfile("#comment\n" .. string.dump(load("print(3)")))
RUN('lua %s > %s', prog, out)
checkprogout('3\n')
checkout('3\n')
-- close Lua with an open file
prepfile(string.format([[io.output(%q); io.write('alo')]], out))

View file

@ -50,7 +50,7 @@ end
local msgf2i = "number.* has no integer representation"
-- float equality
function eq (a,b,limit)
local function eq (a,b,limit)
if not limit then
if floatbits >= 50 then limit = 1E-11
else limit = 1E-5
@ -62,7 +62,7 @@ end
-- equality with types
function eqT (a,b)
local function eqT (a,b)
return a == b and math.type(a) == math.type(b)
end
@ -83,7 +83,7 @@ end
do
local x = -1
local mz = 0/x -- minus zero
t = {[0] = 10, 20, 30, 40, 50}
local t = {[0] = 10, 20, 30, 40, 50}
assert(t[mz] == t[0] and t[-0] == t[0])
end
@ -849,6 +849,7 @@ do
math.randomseed(x, y) -- again should repeat the state
assert(math.random(0) == res)
-- keep the random seed for following tests
print(string.format("random seeds: %d, %d", x, y))
end
do -- test random for floats

View file

@ -9,6 +9,16 @@ local function checkerror (msg, f, ...)
end
local function check (t, na, nh)
if not T then return end
local a, h = T.querytab(t)
if a ~= na or h ~= nh then
print(na, nh, a, h)
assert(nil)
end
end
local a = {}
-- make sure table has lots of space in hash part
@ -20,6 +30,25 @@ for i=1,100 do
assert(#a == i)
end
do -- rehash moving elements from array to hash
local a = {}
for i = 1, 100 do a[i] = i end
check(a, 128, 0)
for i = 5, 95 do a[i] = nil end
check(a, 128, 0)
a.x = 1 -- force a re-hash
check(a, 4, 8)
for i = 1, 4 do assert(a[i] == i) end
for i = 5, 95 do assert(a[i] == nil) end
for i = 96, 100 do assert(a[i] == i) end
assert(a.x == 1)
end
-- testing ipairs
local x = 0
for k,v in ipairs{10,20,30;x=12} do
@ -43,6 +72,14 @@ assert(i == 4)
assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
do -- overflow (must wrap-around)
local f = ipairs{}
local k, v = f({[math.mininteger] = 10}, math.maxinteger)
assert(k == math.mininteger and v == 10)
k, v = f({[math.mininteger] = 10}, k)
assert(k == nil)
end
if not T then
(Message or print)
('\n >>> testC not active: skipping tests for table sizes <<<\n')
@ -57,15 +94,6 @@ local function mp2 (n) -- minimum power of 2 >= n
end
local function check (t, na, nh)
local a, h = T.querytab(t)
if a ~= na or h ~= nh then
print(na, nh, a, h)
assert(nil)
end
end
-- testing C library sizes
do
local s = 0
@ -161,7 +189,7 @@ end
-- size tests for vararg
lim = 35
function foo (n, ...)
local function foo (n, ...)
local arg = {...}
check(arg, n, 0)
assert(select('#', ...) == n)
@ -499,6 +527,15 @@ do -- testing table library with metamethods
end
do -- testing overflow in table.insert (must wrap-around)
local t = setmetatable({},
{__len = function () return math.maxinteger end})
table.insert(t, 20)
local k, v = next(t)
assert(k == math.mininteger and v == 20)
end
if not T then
(Message or print)
('\n >>> testC not active: skipping tests for table library on non-tables <<<\n')
@ -764,4 +801,25 @@ for k,v in ipairs(a) do
end
assert(i == a.n)
-- testing yield inside __pairs
do
local t = setmetatable({10, 20, 30}, {__pairs = function (t)
local inc = coroutine.yield()
return function (t, i)
if i > 1 then return i - inc, t[i - inc] else return nil end
end, t, #t + 1
end})
local res = {}
local co = coroutine.wrap(function ()
for i,p in pairs(t) do res[#res + 1] = p end
end)
co() -- start coroutine
co(1) -- continue after yield
assert(res[1] == 30 and res[2] == 20 and res[3] == 10 and #res == 3)
end
print"OK"

View file

@ -9,12 +9,12 @@ local function checkerror (msg, f, ...)
end
function f(s, p)
local function f (s, p)
local i,e = string.find(s, p)
if i then return string.sub(s, i, e) end
end
a,b = string.find('', '') -- empty patterns are tricky
local a,b = string.find('', '') -- empty patterns are tricky
assert(a == 1 and b == 0);
a,b = string.find('alo', '')
assert(a == 1 and b == 0)
@ -88,7 +88,7 @@ assert(f("alo alo", "%C+") == "alo alo")
print('+')
function f1(s, p)
local function f1 (s, p)
p = string.gsub(p, "%%([0-9])", function (s)
return "%" .. (tonumber(s)+1)
end)
@ -113,7 +113,7 @@ local abc = string.char(range(0, 127)) .. string.char(range(128, 255));
assert(string.len(abc) == 256)
function strset (p)
local function strset (p)
local res = {s=''}
string.gsub(abc, p, function (c) res.s = res.s .. c end)
return res.s
@ -147,7 +147,7 @@ assert(string.gsub('
assert(string.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim
assert(string.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim
assert(string.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ')
t = "abç d"
local t = "abç d"
a, b = string.gsub(t, '(.)', '%1@')
assert('@'..a == string.gsub(t, '', '@') and b == 5)
a, b = string.gsub('abçd', '(.)', '%0@', 2)
@ -184,6 +184,7 @@ do
local function setglobal (n,v) rawset(_G, n, v) end
string.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal)
assert(_G.a=="roberto" and _G.roberto=="a")
_G.a = nil; _G.roberto = nil
end
function f(a,b) return string.gsub(a,'.',b) end
@ -195,20 +196,21 @@ assert(string.gsub("alo $a='x'$ novamente $return a$",
"$([^$]*)%$",
dostring) == "alo novamente x")
x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
local x = string.gsub("$x=string.gsub('alo', '.', string.upper)$ assim vai para $return x$",
"$([^$]*)%$", dostring)
assert(x == ' assim vai para ALO')
_G.a, _G.x = nil
t = {}
s = 'a alo jose joao'
r = string.gsub(s, '()(%w+)()', function (a,w,b)
assert(string.len(w) == b-a);
t[a] = b-a;
end)
local t = {}
local s = 'a alo jose joao'
local r = string.gsub(s, '()(%w+)()', function (a,w,b)
assert(string.len(w) == b-a);
t[a] = b-a;
end)
assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4)
function isbalanced (s)
local function isbalanced (s)
return not string.find(string.gsub(s, "%b()", ""), "[()]")
end
@ -251,7 +253,7 @@ if not _soft then
end
-- recursive nest of gsubs
function rev (s)
local function rev (s)
return string.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end)
end

View file

@ -20,7 +20,7 @@ end
checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
local x,y,z,a,n
a = {}; lim = _soft and 200 or 2000
a = {}; local lim = _soft and 200 or 2000
for i=1, lim do a[i]=i end
assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
x = unpack(a)
@ -222,7 +222,7 @@ a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
table.sort(a)
check(a)
function perm (s, n)
local function perm (s, n)
n = n or #s
if n == 1 then
local t = {unpack(s)}
@ -248,7 +248,7 @@ perm{1,2,3,3,5}
perm{1,2,3,4,5,6}
perm{2,2,3,3,5,6}
function timesort (a, n, func, msg, pre)
local function timesort (a, n, func, msg, pre)
local x = os.clock()
table.sort(a, func)
x = (os.clock() - x) * 1000
@ -257,7 +257,7 @@ function timesort (a, n, func, msg, pre)
check(a, func)
end
limit = 50000
local limit = 50000
if _soft then limit = 5000 end
a = {}
@ -274,7 +274,7 @@ for i=1,limit do
a[i] = math.random()
end
x = os.clock(); i=0
local x = os.clock(); local i = 0
table.sort(a, function(x,y) i=i+1; return y<x end)
x = (os.clock() - x) * 1000
print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
@ -289,18 +289,19 @@ timesort(a, limit, function(x,y) return nil end, "equal")
for i,v in pairs(a) do assert(v == false) end
A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
table.sort(A)
check(A)
AA = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
table.sort(AA)
check(AA)
table.sort(A, function (x, y)
load(string.format("A[%q] = ''", x), "")()
table.sort(AA, function (x, y)
load(string.format("AA[%q] = ''", x), "")()
collectgarbage()
return x<y
end)
_G.AA = nil
tt = {__lt = function (a,b) return a.val < b.val end}
local tt = {__lt = function (a,b) return a.val < b.val end}
a = {}
for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
table.sort(a)

View file

@ -52,7 +52,7 @@ assert(("\000123456789"):sub(8) == "789")
-- testing string.find
assert(string.find("123456789", "345") == 3)
a,b = string.find("123456789", "345")
local a,b = string.find("123456789", "345")
assert(string.sub("123456789", a, b) == "345")
assert(string.find("1234567890123456789", "345", 3) == 3)
assert(string.find("1234567890123456789", "345", 4) == 13)
@ -192,7 +192,7 @@ do -- tests for '%p' format
end
end
x = '"ílo"\n\\'
local x = '"ílo"\n\\'
assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\')
assert(string.format('%q', "\0") == [["\0"]])
assert(load(string.format('return %q', x))() == x)
@ -202,13 +202,11 @@ assert(string.format("\0%c\0%c%x\0", string.byte("\xe4"), string.byte("b"), 140)
"\0\xe4\0b8c\0")
assert(string.format('') == "")
assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) ==
string.format("%c%c%c%c", 34, 48, 90, 100))
string.format("%1c%-c%-1c%c", 34, 48, 90, 100))
assert(string.format("%s\0 is not \0%s", 'not be', 'be') == 'not be\0 is not \0be')
assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023")
assert(tonumber(string.format("%f", 10.3)) == 10.3)
x = string.format('"%-50s"', 'a')
assert(#x == 52)
assert(string.sub(x, 1, 4) == '"a ')
assert(string.format('"%-50s"', 'a') == '"a' .. string.rep(' ', 49) .. '"')
assert(string.format("-%.20s.20s", string.rep("%", 2000)) ==
"-"..string.rep("%", 20)..".20s")
@ -237,7 +235,6 @@ end
assert(string.format("\0%s\0", "\0\0\1") == "\0\0\0\1\0")
checkerror("contains zeros", string.format, "%10s", "\0")
checkerror("cannot have modifiers", string.format, "%10q", "1")
-- format x tostring
assert(string.format("%s %s", nil, true) == "nil true")
@ -341,6 +338,26 @@ do print("testing 'format %a %A'")
end
-- testing some flags (all these results are required by ISO C)
assert(string.format("%#12o", 10) == " 012")
assert(string.format("%#10x", 100) == " 0x64")
assert(string.format("%#-17X", 100) == "0X64 ")
assert(string.format("%013i", -100) == "-000000000100")
assert(string.format("%2.5d", -100) == "-00100")
assert(string.format("%.u", 0) == "")
assert(string.format("%+#014.0f", 100) == "+000000000100.")
assert(string.format("%-16c", 97) == "a ")
assert(string.format("%+.3G", 1.5) == "+1.5")
assert(string.format("%.0s", "alo") == "")
assert(string.format("%.s", "alo") == "")
-- ISO C89 says that "The exponent always contains at least two digits",
-- but unlike ISO C99 it does not ensure that it contains "only as many
-- more digits as necessary".
assert(string.match(string.format("% 1.0E", 100), "^ 1E%+0+2$"))
assert(string.match(string.format("% .1g", 2^10), "^ 1e%+0+3$"))
-- errors in format
local function check (fmt, msg)
@ -348,13 +365,21 @@ local function check (fmt, msg)
end
local aux = string.rep('0', 600)
check("%100.3d", "too long")
check("%100.3d", "invalid conversion")
check("%1"..aux..".3d", "too long")
check("%1.100d", "too long")
check("%1.100d", "invalid conversion")
check("%10.1"..aux.."004d", "too long")
check("%t", "invalid conversion")
check("%"..aux.."d", "repeated flags")
check("%"..aux.."d", "too long")
check("%d %d", "no value")
check("%010c", "invalid conversion")
check("%.10c", "invalid conversion")
check("%0.34s", "invalid conversion")
check("%#i", "invalid conversion")
check("%3.1p", "invalid conversion")
check("%0.s", "invalid conversion")
check("%10q", "cannot have modifiers")
check("%F", "invalid conversion") -- useless and not in C89
assert(load("return 1\n--comment without ending EOL")() == 1)
@ -427,7 +452,7 @@ end
do
local f = string.gmatch("1 2 3 4 5", "%d+")
assert(f() == "1")
co = coroutine.wrap(f)
local co = coroutine.wrap(f)
assert(co() == "2")
end

View file

@ -35,7 +35,7 @@ print("\talignment: " .. align)
-- check errors in arguments
function checkerror (msg, f, ...)
local function checkerror (msg, f, ...)
local status, err = pcall(f, ...)
-- print(status, err, msg)
assert(not status and string.find(err, msg))

View file

@ -97,9 +97,15 @@ do -- error indication in utf8.len
assert(not a and b == p)
end
check("abc\xE3def", 4)
check("汉字\x80", #("汉字") + 1)
check("\xF4\x9F\xBF", 1)
check("\xF4\x9F\xBF\xBF", 1)
-- spurious continuation bytes
check("汉字\x80", #("汉字") + 1)
check("\x80hello", 1)
check("hel\x80lo", 4)
check("汉字\xBF", #("汉字") + 1)
check("\xBFhello", 1)
check("hel\xBFlo", 4)
end
-- errors in utf8.codes
@ -112,6 +118,16 @@ do
end
errorcodes("ab\xff")
errorcodes("\u{110000}")
errorcodes("in\x80valid")
errorcodes("\xbfinvalid")
errorcodes("αλφ\xBFα")
-- calling interation function with invalid arguments
local f = utf8.codes("")
assert(f("", 2) == nil)
assert(f("", -1) == nil)
assert(f("", math.mininteger) == nil)
end
-- error in initial position for offset
@ -214,7 +230,7 @@ do
check(s, {0x10000, 0x1FFFFF}, true)
end
x = "日本語a-4\0éó"
local x = "日本語a-4\0éó"
check(x, {26085, 26412, 35486, 97, 45, 52, 0, 233, 243})

View file

@ -3,13 +3,13 @@
print('testing vararg')
function f(a, ...)
local function f (a, ...)
local x = {n = select('#', ...), ...}
for i = 1, x.n do assert(a[i] == x[i]) end
return x.n
end
function c12 (...)
local function c12 (...)
assert(arg == _G.arg) -- no local 'arg'
local x = {...}; x.n = #x
local res = (x.n==2 and x[1] == 1 and x[2] == 2)
@ -17,7 +17,7 @@ function c12 (...)
return res, 2
end
function vararg (...) return {n = select('#', ...), ...} end
local function vararg (...) return {n = select('#', ...), ...} end
local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
@ -29,7 +29,7 @@ assert(vararg().n == 0)
assert(vararg(nil, nil).n == 2)
assert(c12(1,2)==55)
a,b = assert(call(c12, {1,2}))
local a,b = assert(call(c12, {1,2}))
assert(a == 55 and b == 2)
a = call(c12, {1,2;n=2})
assert(a == 55 and b == 2)
@ -49,7 +49,7 @@ function t:f (...) local arg = {...}; return self[...]+#arg end
assert(t:f(1,4) == 3 and t:f(2) == 11)
print('+')
lim = 20
local lim = 20
local i, a = 1, {}
while i <= lim do a[i] = i+0.3; i=i+1 end
@ -59,7 +59,7 @@ function f(a, b, c, d, ...)
more[lim-4] == lim+0.3 and not more[lim-3])
end
function g(a,b,c)
local function g (a,b,c)
assert(a == 1.3 and b == 2.3 and c == 3.3)
end
@ -76,7 +76,7 @@ print("+")
-- new-style varargs
function oneless (a, ...) return ... end
local function oneless (a, ...) return ... end
function f (n, a, ...)
local b
@ -99,8 +99,8 @@ assert(a==nil and b==nil and c==nil and d==nil and e==nil)
-- varargs for main chunks
f = load[[ return {...} ]]
x = f(2,3)
local f = load[[ return {...} ]]
local x = f(2,3)
assert(x[1] == 2 and x[2] == 3 and x[3] == undef)

View file

@ -52,7 +52,7 @@ if _soft then return 10 end
print "testing large programs (>64k)"
-- template to create a very big test file
prog = [[$
local prog = [[$
local a,b
@ -85,7 +85,7 @@ function b:xxx (a,b) return a+b end
assert(b:xxx(10, 12) == 22) -- pushself with non-constant index
b["xxx"] = undef
s = 0; n=0
local s = 0; local n=0
for a,b in pairs(b) do s=s+b; n=n+1 end
-- with 32-bit floats, exact value of 's' depends on summation order
assert(81800000.0 < s and s < 81860000 and n == 70001)
@ -93,7 +93,7 @@ assert(81800000.0 < s and s < 81860000 and n == 70001)
a = nil; b = nil
print'+'
function f(x) b=x end
local function f(x) b=x end
a = f{$3$} or 10
@ -118,7 +118,7 @@ local function sig (x)
return (x % 2 == 0) and '' or '-'
end
F = {
local F = {
function () -- $1$
for i=10,50009 do
io.write('a', i, ' = ', sig(i), 5+((i-10)/2), ',\n')
@ -138,14 +138,14 @@ function () -- $3$
end,
}
file = os.tmpname()
local file = os.tmpname()
io.output(file)
for s in string.gmatch(prog, "$([^$]+)") do
local n = tonumber(s)
if not n then io.write(s) else F[n]() end
end
io.close()
result = dofile(file)
local result = dofile(file)
assert(os.remove(file))
print'OK'
return result