summaryrefslogtreecommitdiff
path: root/tests/test-suite/inprogress
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test-suite/inprogress')
-rw-r--r--tests/test-suite/inprogress/errors.js1323
-rw-r--r--tests/test-suite/inprogress/nextvar.js1183
2 files changed, 2506 insertions, 0 deletions
diff --git a/tests/test-suite/inprogress/errors.js b/tests/test-suite/inprogress/errors.js
new file mode 100644
index 0000000..75c6f81
--- /dev/null
+++ b/tests/test-suite/inprogress/errors.js
@@ -0,0 +1,1323 @@
+"use strict";
+
+const test = require('tape');
+
+global.WEB = false;
+
+const lua = require('../../../src/lua.js');
+const lauxlib = require('../../../src/lauxlib.js');
+const lualib = require('../../../src/lualib.js');
+
+
+const prefix = `
+ -- avoid problems with 'strict' module (which may generate other error messages)
+ local mt = getmetatable(_G) or {}
+ local oldmm = mt.__index
+ mt.__index = nil
+
+ local function checkerr (msg, f, ...)
+ local st, err = pcall(f, ...)
+ assert(not st and string.find(err, msg))
+ end
+
+
+ local function doit (s)
+ local f, msg = load(s)
+ if f == nil then return msg end
+ local cond, msg = pcall(f)
+ return (not cond) and msg
+ end
+
+
+ local function checkmessage (prog, msg)
+ local m = doit(prog)
+ assert(string.find(m, msg, 1, true))
+ end
+
+ local function checksyntax (prog, extra, token, line)
+ local msg = doit(prog)
+ if not string.find(token, "^<%a") and not string.find(token, "^char%(")
+ then token = "'"..token.."'" end
+ token = string.gsub(token, "(%p)", "%%%1")
+ local pt = string.format([[^%%[string ".*"%%]:%d: .- near %s$]],
+ line, token)
+ assert(string.find(msg, pt))
+ assert(string.find(msg, msg, 1, true))
+ end
+`;
+
+test("[test-suite] errors: test error message with no extra info", function (t) {
+ let luaCode = `
+ assert(doit("error('hi', 0)") == 'hi')
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: test error message with no info", function (t) {
+ let luaCode = `
+ assert(doit("error()") == nil)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: test common errors/errors that crashed in the past", function (t) {
+ let luaCode = `
+ assert(doit("table.unpack({}, 1, n=2^30)"))
+ assert(doit("a=math.sin()"))
+ assert(not doit("tostring(1)") and doit("tostring()"))
+ assert(doit"tonumber()")
+ assert(doit"repeat until 1; a")
+ assert(doit"return;;")
+ assert(doit"assert(false)")
+ assert(doit"assert(nil)")
+ assert(doit("function a (... , ...) end"))
+ assert(doit("function a (, ...) end"))
+ assert(doit("local t={}; t = t[#t] + 1"))
+
+ checksyntax([[
+ local a = {4
+
+ ]], "'}' expected (to close '{' at line 1)", "<eof>", 3)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: tests for better error messages", function (t) {
+ let luaCode = `
+ checkmessage("a = {} + 1", "arithmetic")
+ 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("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")
+
+ checkmessage("a = #print", "length of a function value")
+ checkmessage("a = #3", "length of a number value")
+
+ 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'")
+ checkmessage("local a,b,c; (function () a = b+1 end)()", "upvalue 'b'")
+ assert(not doit"local aaa={bbb={ddd=next}}; aaa.bbb:ddd(nil)")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: upvalues being indexed do not go to the stack", function (t) {
+ let luaCode = `
+ checkmessage("local a,b,cc; (function () a = cc[1] end)()", "upvalue 'cc'")
+ 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='a'; x=aaa+b", "local 'aaa'")
+ checkmessage("aaa={}; x=3/aaa", "global 'aaa'")
+ checkmessage("aaa='2'; b=nil;x=aaa*b", "global 'b'")
+ checkmessage("aaa={}; x=-aaa", "global 'aaa'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: short circuit", function (t) {
+ let luaCode = `
+ checkmessage("a=1; local a,bbbb=2,3; a = math.sin(1) and bbbb(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'"))
+ assert(not string.find(doit"aaa={}; (aaa or aaa)()", "'aaa'"))
+
+ checkmessage("print(print < 10)", "function with number")
+ checkmessage("print(print < print)", "two function values")
+ checkmessage("print('10' < 10)", "string with number")
+ checkmessage("print(10 < '23')", "number with string")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: float->integer conversions", function (t) {
+ let luaCode = `
+ checkmessage("local a = 2.0^100; x = a << 2", "local a")
+ checkmessage("local a = 1 >> 2.0^100", "has no integer representation")
+ checkmessage("local a = '10' << 2.0^100", "has no integer representation")
+ checkmessage("local a = 2.0^100 & 1", "has no integer representation")
+ checkmessage("local a = 2.0^100 & '1'", "has no integer representation")
+ checkmessage("local a = 2.0 | 1e40", "has no integer representation")
+ checkmessage("local a = 2e100 ~ 1", "has no integer representation")
+ checkmessage("string.sub('a', 2.0^100)", "has no integer representation")
+ checkmessage("string.rep('a', 3.3)", "has no integer representation")
+ checkmessage("return 6e40 & 7", "has no integer representation")
+ checkmessage("return 34 << 7e30", "has no integer representation")
+ 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'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: passing light userdata instead of full userdata", function (t) {
+ let luaCode = `
+ _G.D = debug
+ checkmessage([[
+ -- create light udata
+ local x = D.upvalueid(function () return debug end, 1)
+ D.setuservalue(x, {})
+ ]], "light userdata")
+ _G.D = nil
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: named objects (field '__name')", function (t) {
+ let luaCode = `
+ do
+ checkmessage("math.sin(io.input())", "(number expected, got FILE*)")
+ _G.XX = setmetatable({}, {__name = "My Type"})
+ assert(string.find(tostring(XX), "^My Type"))
+ checkmessage("io.input(XX)", "(FILE* expected, got My Type)")
+ checkmessage("return XX + 1", "on a My Type value")
+ checkmessage("return ~io.stdin", "on a FILE* value")
+ checkmessage("return XX < XX", "two My Type values")
+ checkmessage("return {} < XX", "table with My Type")
+ checkmessage("return XX < io.stdin", "My Type with FILE*")
+ _G.XX = nil
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: global functions", function (t) {
+ let luaCode = `
+ checkmessage("(io.write or print){}", "io.write")
+ checkmessage("(collectgarbage or print){}", "collectgarbage")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: errors in functions without debug info", function (t) {
+ let luaCode = `
+ do
+ local f = function (a) return a + 1 end
+ f = assert(load(string.dump(f, true)))
+ assert(f(3) == 4)
+ checkerr("^%?:%-1:", f, {})
+
+ -- code with a move to a local var ('OP_MOV A B' with A<B)
+ f = function () local a; a = {}; return a + 2 end
+ -- no debug info (so that 'a' is unknown)
+ f = assert(load(string.dump(f, true)))
+ -- symbolic execution should not get lost
+ checkerr("^%?:%-1:.*table value", f)
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: tests for field accesses after RK limit", function (t) {
+ let luaCode = `
+ local t = {}
+ for i = 1, 1000 do
+ t[i] = "a = 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.."; local t = {}; t:bbb()", "method 'bbb'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: global", function (t) {
+ let luaCode = `
+ checkmessage([[aaa=9
+ repeat until 3==3
+ local x=math.sin(math.cos(3))
+ if math.sin(1) == x then return math.sin(1) end -- tail call
+ local a,b = 1, {
+ {x='a'..'b'..'c', y='b', z=x},
+ {1,2,3,4,5} or 3+3<=3+3,
+ 3+1>3+1,
+ {d = x and aaa[x or y]}}
+ ]], "global 'aaa'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: field", function (t) {
+ let luaCode = `
+ checkmessage([[
+ local x,y = {},1
+ if math.sin(1) == 0 then return 3 end -- return
+ x.a()]], "field 'a'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: global insert", function (t) {
+ let luaCode = `
+ checkmessage([[
+ prefix = nil
+ insert = nil
+ while 1 do
+ local a
+ if nil then break end
+ insert(prefix, a)
+ end]], "global 'insert'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: sin", function (t) {
+ let luaCode = `
+ checkmessage([[ -- tail call
+ return math.sin("a")
+ ]], "'sin'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: concatenate", function (t) {
+ let luaCode = `
+ checkmessage([[x = print .. "a"]], "concatenate")
+ checkmessage([[x = "a" .. false]], "concatenate")
+ checkmessage([[x = {} .. 2]], "concatenate")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: unknown global", function (t) {
+ let luaCode = `
+ checkmessage([[
+ local Var
+ local function main()
+ NoSuchName (function() Var=0 end)
+ end
+ main()
+ ]], "global 'NoSuchName'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: __index", function (t) {
+ let luaCode = `
+ a = {}; setmetatable(a, {__index = string})
+ checkmessage("a: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'")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: tests for errors in coroutines", function (t) {
+ let luaCode = `
+ local function f (n)
+ local c = coroutine.create(f)
+ local a,b = coroutine.resume(c)
+ return b
+ end
+ assert(string.find(f(), "JS stack overflow"))
+
+ checkmessage("coroutine.yield()", "outside a coroutine")
+
+ f = coroutine.wrap(function () table.sort({1,2,3}, coroutine.yield) end)
+ checkerr("yield across", f)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: testing size of 'source' info", function (t) {
+ let luaCode = `
+ idsize = 60 - 1
+ local function checksize (source)
+ -- syntax error
+ local _, msg = load("x", source)
+ msg = string.match(msg, "^([^:]*):") -- get source (1st part before ':')
+ assert(msg:len() <= idsize)
+ end
+
+ for i = 60 - 10, 60 + 10 do -- check border cases around 60
+ checksize("@" .. string.rep("x", i)) -- file names
+ checksize(string.rep("x", i - 10)) -- string sources
+ checksize("=" .. string.rep("x", i)) -- exact sources
+ end
+
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: testing line error", function (t) {
+ let luaCode = `
+ local function lineerror (s, l)
+ local err,msg = pcall(load(s))
+ local line = string.match(msg, ":(%d+):")
+ assert((line and line+0) == l)
+ end
+
+ lineerror("local a\\n for i=1,'a' do \\n print(i) \\n end", 2)
+ lineerror("\\n local a \\n for k,v in 3 \\n do \\n print(k) \\n end", 3)
+ lineerror("\\n\\n for k,v in \\n 3 \\n do \\n print(k) \\n end", 4)
+ lineerror("function a.x.y ()\\na=a+1\\nend", 1)
+
+ lineerror("a = \\na\\n+\\n{}", 3)
+ lineerror("a = \\n3\\n+\\n(\\n4\\n/\\nprint)", 6)
+ lineerror("a = \\nprint\\n+\\n(\\n4\\n/\\n7)", 3)
+
+ lineerror("a\\n=\\n-\\n\\nprint\\n;", 3)
+
+ lineerror([[
+ a
+ (
+ 23)
+ ]], 1)
+
+ lineerror([[
+ local a = {x = 13}
+ a
+ .
+ x
+ (
+ 23
+ )
+ ]], 2)
+
+ lineerror([[
+ local a = {x = 13}
+ a
+ .
+ x
+ (
+ 23 + a
+ )
+ ]], 6)
+
+ local p = [[
+ function g() f() end
+ function f(x) error('a', X) end
+ g()
+ ]]
+ X=3;lineerror((p), 3)
+ X=0;lineerror((p), nil)
+ X=1;lineerror((p), 2)
+ X=2;lineerror((p), 1)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: several tests that exhaust the Lua stack", { skip: true }, function (t) {
+ let luaCode = `
+ C = 0
+ local l = debug.getinfo(1, "l").currentline; function y () C=C+1; y() end
+
+ local function checkstackmessage (m)
+ return (string.find(m, "^.-:%d+: stack overflow"))
+ end
+ -- repeated stack overflows (to check stack recovery)
+ assert(checkstackmessage(doit('y()')))
+ print('+')
+ assert(checkstackmessage(doit('y()')))
+ print('+')
+ assert(checkstackmessage(doit('y()')))
+ print('+')
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: error lines in stack overflow", { skip: true }, function (t) {
+ let luaCode = `
+ C = 0
+ local l1
+ local function g(x)
+ l1 = debug.getinfo(x, "l").currentline; y()
+ end
+ local _, stackmsg = xpcall(g, debug.traceback, 1)
+ print('+')
+ local stack = {}
+ for line in string.gmatch(stackmsg, "[^\\n]*") do
+ local curr = string.match(line, ":(%d+):")
+ if curr then table.insert(stack, tonumber(curr)) end
+ end
+ local i=1
+ while stack[i] ~= l1 do
+ assert(stack[i] == l)
+ i = i+1
+ end
+ assert(i > 15)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: error in error handling", function (t) {
+ let luaCode = `
+ local res, msg = xpcall(error, error)
+ assert(not res and type(msg) == 'string')
+ print('+')
+
+ local function f (x)
+ if x==0 then error('a\\n')
+ else
+ local aux = function () return f(x-1) end
+ local a,b = xpcall(aux, aux)
+ return a,b
+ end
+ end
+ f(3)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: too many results", { skip: true }, function (t) {
+ let luaCode = `
+ local function loop (x,y,z) return 1 + loop(x, y, z) end
+
+ local res, msg = xpcall(loop, function (m)
+ assert(string.find(m, "stack overflow"))
+ checkerr("error handling", loop)
+ assert(math.sin(0) == 0)
+ return 15
+ end)
+ assert(msg == 15)
+
+ local f = function ()
+ for i = 999900, 1000000, 1 do table.unpack({}, 1, i) end
+ end
+ checkerr("too many results", f)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: non string messages", function (t) {
+ let luaCode = `
+ do
+ -- non string messages
+ local t = {}
+ local res, msg = pcall(function () error(t) end)
+ assert(not res and msg == t)
+
+ res, msg = pcall(function () error(nil) end)
+ assert(not res and msg == nil)
+
+ local function f() error{msg='x'} end
+ res, msg = xpcall(f, function (r) return {msg=r.msg..'y'} end)
+ assert(msg.msg == 'xy')
+
+ -- 'assert' with extra arguments
+ res, msg = pcall(assert, false, "X", t)
+ assert(not res and msg == "X")
+
+ -- 'assert' with no message
+ res, msg = pcall(function () assert(false) end)
+ local line = string.match(msg, "%w+%.lua:(%d+): assertion failed!$")
+ assert(tonumber(line) == debug.getinfo(1, "l").currentline - 2)
+
+ -- 'assert' with non-string messages
+ res, msg = pcall(assert, false, t)
+ assert(not res and msg == t)
+
+ res, msg = pcall(assert, nil, nil)
+ assert(not res and msg == nil)
+
+ -- 'assert' without arguments
+ res, msg = pcall(assert)
+ assert(not res and string.find(msg, "value expected"))
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: xpcall with arguments", function (t) {
+ let luaCode = `
+ 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)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: testing tokens in error messages", function (t) {
+ let luaCode = `
+ checksyntax("syntax error", "", "error", 1)
+ checksyntax("1.000", "", "1.000", 1)
+ checksyntax("[[a]]", "", "[[a]]", 1)
+ checksyntax("'aa'", "", "'aa'", 1)
+ checksyntax("while << do end", "", "<<", 1)
+ checksyntax("for >> do end", "", ">>", 1)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: test invalid non-printable char in a chunk", function (t) {
+ let luaCode = `
+ checksyntax("a\\1a = 1", "", "<\\\\1>", 1)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: test 255 as first char in a chunk", function (t) {
+ let luaCode = `
+ checksyntax("\\255a = 1", "", "<\\\\255>", 1)
+
+ doit('I = load("a=9+"); a=3')
+ assert(a==3 and I == nil)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: lots of errors", function (t) {
+ let luaCode = `
+ lim = 1000
+ if _soft then lim = 100 end
+ for i=1,lim do
+ doit('a = ')
+ doit('a = 4+nil')
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: testing syntax limits", function (t) {
+ let luaCode = `
+ local maxClevel = 200 -- LUAI_MAXCCALLS (in llimits.h)
+
+ local function testrep (init, rep, close, repc)
+ local s = init .. string.rep(rep, maxClevel - 10) .. close ..
+ string.rep(repc, maxClevel - 10)
+ assert(load(s)) -- 190 levels is OK
+ s = init .. string.rep(rep, maxClevel + 1)
+ checkmessage(s, "too many C levels")
+ end
+
+ testrep("local a; a", ",a", "= 1", ",1") -- multiple assignment
+ testrep("local a; a=", "{", "0", "}")
+ testrep("local a; a=", "(", "2", ")")
+ testrep("local a; ", "a(", "2", ")")
+ testrep("", "do ", "", " end")
+ testrep("", "while a do ", "", " end")
+ testrep("local a; ", "if a then else ", "", " end")
+ testrep("", "function foo () ", "", " end")
+ testrep("local a; a=", "a..", "a", "")
+ testrep("local a; a=", "a^", "a", "")
+
+ checkmessage("a = f(x" .. string.rep(",x", 260) .. ")", "too many registers")
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: upvalues limit", function (t) {
+ let luaCode = `
+ local lim = 127
+ local s = "local function fooA ()\\n local "
+ for j = 1,lim do
+ s = s.."a"..j..", "
+ end
+ s = s.."b,c\\n"
+ s = s.."local function fooB ()\\n local "
+ for j = 1,lim do
+ s = s.."b"..j..", "
+ end
+ s = s.."b\\n"
+ s = s.."function fooC () return b+c"
+ local c = 1+2
+ for j = 1,lim do
+ s = s.."+a"..j.."+b"..j
+ c = c + 2
+ end
+ s = s.."\\nend end end"
+ local a,b = load(s)
+ assert(c > 255 and string.find(b, "too many upvalues") and
+ string.find(b, "line 5"))
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] errors: local variables limit", function (t) {
+ let luaCode = `
+ s = "\\nfunction foo ()\\n local "
+ for j = 1,300 do
+ s = s.."a"..j..", "
+ end
+ s = s.."b\\n"
+ local a,b = load(s)
+ assert(string.find(b, "line 2") and string.find(b, "too many local variables"))
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
diff --git a/tests/test-suite/inprogress/nextvar.js b/tests/test-suite/inprogress/nextvar.js
new file mode 100644
index 0000000..6cf7c74
--- /dev/null
+++ b/tests/test-suite/inprogress/nextvar.js
@@ -0,0 +1,1183 @@
+"use strict";
+
+const test = require('tape');
+
+global.WEB = false;
+
+const lua = require('../../src/lua.js');
+const lauxlib = require('../../src/lauxlib.js');
+const lualib = require('../../src/lualib.js');
+
+
+const prefix = `
+ local function checkerror (msg, f, ...)
+ local s, err = pcall(f, ...)
+ assert(not s and string.find(err, msg))
+ end
+`;
+
+test("[test-suite] nextvar: testing size operator", function (t) {
+ let luaCode = `
+ local a = {}
+
+ -- make sure table has lots of space in hash part
+ for i=1,100 do a[i.."+"] = true end
+ for i=1,100 do a[i.."+"] = nil end
+ -- fill hash part with numeric indices testing size operator
+ for i=1,100 do
+ a[i] = true
+ assert(#a == i)
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: testing ipairs", function (t) {
+ let luaCode = `
+ local x = 0
+ for k,v in ipairs{10,20,30;x=12} do
+ x = x + 1
+ assert(k == x and v == x * 10)
+ end
+
+ for _ in ipairs{x=12, y=24} do assert(nil) end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: test for 'false' x ipair", function (t) {
+ let luaCode = `
+ x = false
+ local i = 0
+ for k,v in ipairs{true,false,true,false} do
+ i = i + 1
+ x = not x
+ assert(x == v)
+ end
+ assert(i == 4)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: iterator function is always the same", function (t) {
+ let luaCode = `
+ assert(type(ipairs{}) == 'function' and ipairs{} == ipairs{})
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: JS tests", { skip: true }, function (t) {
+ let luaCode = `
+ -- testing table sizes
+
+ local function log2 (x) return math.log(x, 2) end
+
+ local function mp2 (n) -- minimum power of 2 >= n
+ local mp = 2^math.ceil(log2(n))
+ assert(n == 0 or (mp/2 < n and n <= mp))
+ return mp
+ end
+
+ local function fb (n)
+ local r, nn = T.int2fb(n)
+ assert(r < 256)
+ return nn
+ end
+
+ -- test fb function
+ for a = 1, 10000 do -- all numbers up to 10^4
+ local n = fb(a)
+ assert(a <= n and n <= a*1.125)
+ end
+ local a = 1024 -- plus a few up to 2 ^30
+ local lim = 2^30
+ while a < lim do
+ local n = fb(a)
+ assert(a <= n and n <= a*1.125)
+ a = math.ceil(a*1.3)
+ 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
+ for _ in pairs(math) do s = s + 1 end
+ check(math, 0, mp2(s))
+ end
+
+
+ -- testing constructor sizes
+ local lim = 40
+ local s = 'return {'
+ for i=1,lim do
+ s = s..i..','
+ local s = s
+ for k=0,lim do
+ local t = load(s..'}', '')()
+ assert(#t == i)
+ check(t, fb(i), mp2(k))
+ s = string.format('%sa%d=%d,', s, k, k)
+ end
+ end
+
+
+ -- tests with unknown number of elements
+ local a = {}
+ for i=1,lim do a[i] = i end -- build auxiliary table
+ for k=0,lim do
+ local a = {table.unpack(a,1,k)}
+ assert(#a == k)
+ check(a, k, 0)
+ a = {1,2,3,table.unpack(a,1,k)}
+ check(a, k+3, 0)
+ assert(#a == k + 3)
+ end
+
+
+ -- testing tables dynamically built
+ local lim = 130
+ local a = {}; a[2] = 1; check(a, 0, 1)
+ a = {}; a[0] = 1; check(a, 0, 1); a[2] = 1; check(a, 0, 2)
+ a = {}; a[0] = 1; a[1] = 1; check(a, 1, 1)
+ a = {}
+ for i = 1,lim do
+ a[i] = 1
+ assert(#a == i)
+ check(a, mp2(i), 0)
+ end
+
+ a = {}
+ for i = 1,lim do
+ a['a'..i] = 1
+ assert(#a == 0)
+ check(a, 0, mp2(i))
+ end
+
+ a = {}
+ for i=1,16 do a[i] = i end
+ check(a, 16, 0)
+ do
+ for i=1,11 do a[i] = nil end
+ for i=30,50 do a[i] = nil end -- force a rehash (?)
+ check(a, 0, 8) -- only 5 elements in the table
+ a[10] = 1
+ for i=30,50 do a[i] = nil end -- force a rehash (?)
+ check(a, 0, 8) -- only 6 elements in the table
+ for i=1,14 do a[i] = nil end
+ for i=18,50 do a[i] = nil end -- force a rehash (?)
+ check(a, 0, 4) -- only 2 elements ([15] and [16])
+ end
+
+ -- reverse filling
+ for i=1,lim do
+ local a = {}
+ for i=i,1,-1 do a[i] = i end -- fill in reverse
+ check(a, mp2(i), 0)
+ end
+
+ -- size tests for vararg
+ lim = 35
+ function foo (n, ...)
+ local arg = {...}
+ check(arg, n, 0)
+ assert(select('#', ...) == n)
+ arg[n+1] = true
+ check(arg, mp2(n+1), 0)
+ arg.x = true
+ check(arg, mp2(n+1), 1)
+ end
+ local a = {}
+ for i=1,lim do a[i] = true; foo(i, table.unpack(a)) end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: test size operation on empty tables", function (t) {
+ let luaCode = `
+ assert(#{} == 0)
+ assert(#{nil} == 0)
+ assert(#{nil, nil} == 0)
+ assert(#{nil, nil, nil} == 0)
+ assert(#{nil, nil, nil, nil} == 0)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: test size operation on empty tables", function (t) {
+ let luaCode = `
+ assert(#{} == 0)
+ assert(#{nil} == 0)
+ assert(#{nil, nil} == 0)
+ assert(#{nil, nil, nil} == 0)
+ assert(#{nil, nil, nil, nil} == 0)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: next uses always the same iteration function", function (t) {
+ let luaCode = `
+ local nofind = {}
+
+ a,b,c = 1,2,3
+ a,b,c = nil
+
+
+ -- next uses always the same iteraction function
+ assert(next{} == next{})
+
+ local function find (name)
+ local n,v
+ while 1 do
+ n,v = next(_G, n)
+ if not n then return nofind end
+ assert(v ~= nil)
+ if n == name then return v end
+ end
+ end
+
+ local function find1 (name)
+ for n,v in pairs(_G) do
+ if n==name then return v end
+ end
+ return nil -- not found
+ end
+
+
+ assert(print==find("print") and print == find1("print"))
+ assert(_G["print"]==find("print"))
+ assert(assert==find1("assert"))
+ assert(nofind==find("return"))
+ assert(not find1("return"))
+ _G["ret" .. "urn"] = nil
+ assert(nofind==find("return"))
+ _G["xxx"] = 1
+ assert(xxx==find("xxx"))
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: invalid key to 'next'", function (t) {
+ let luaCode = `
+ checkerror("invalid key", next, {10,20}, 3)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: both 'pairs' and 'ipairs' need an argument", function (t) {
+ let luaCode = `
+ checkerror("bad argument", pairs)
+ checkerror("bad argument", ipairs)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: fmod table", function (t) {
+ let luaCode = `
+ a = {}
+ for i=0,10000 do
+ if math.fmod(i,10) ~= 0 then
+ a['x'..i] = i
+ end
+ end
+
+ n = {n=0}
+ for i,v in pairs(a) do
+ n.n = n.n+1
+ assert(i and v and a[i] == v)
+ end
+ assert(n.n == 9000)
+ a = nil
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: check next", function (t) {
+ let luaCode = `
+ local function checknext (a)
+ local b = {}
+ do local k,v = next(a); while k do b[k] = v; k,v = next(a,k) end end
+ for k,v in pairs(b) do assert(a[k] == v) end
+ for k,v in pairs(a) do assert(b[k] == v) end
+ end
+
+ checknext{1,x=1,y=2,z=3}
+ checknext{1,2,x=1,y=2,z=3}
+ checknext{1,2,3,x=1,y=2,z=3}
+ checknext{1,2,3,4,x=1,y=2,z=3}
+ checknext{1,2,3,4,5,x=1,y=2,z=3}
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: # operator", function (t) {
+ let luaCode = `
+ assert(#{} == 0)
+ assert(#{[-1] = 2} == 0)
+ assert(#{1,2,3,nil,nil} == 3)
+ for i=0,40 do
+ local a = {}
+ for j=1,i do a[j]=j end
+ assert(#a == i)
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: maxn", function (t) {
+ let luaCode = `
+ -- 'maxn' is now deprecated, but it is easily defined in Lua
+ function table.maxn (t)
+ local max = 0
+ for k in pairs(t) do
+ max = (type(k) == 'number') and math.max(max, k) or max
+ end
+ return max
+ end
+
+ assert(table.maxn{} == 0)
+ assert(table.maxn{["1000"] = true} == 0)
+ assert(table.maxn{["1000"] = true, [24.5] = 3} == 24.5)
+ assert(table.maxn{[1000] = true} == 1000)
+ assert(table.maxn{[10] = true, [100*math.pi] = print} == 100*math.pi)
+
+ table.maxn = nil
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: int overflow", function (t) {
+ let luaCode = `
+ a = {}
+ for i=0,50 do a[2^i] = true end
+ assert(a[#a])
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: erasing values", function (t) {
+ let luaCode = `
+ local t = {[{1}] = 1, [{2}] = 2, [string.rep("x ", 4)] = 3,
+ [100.3] = 4, [4] = 5}
+
+ local n = 0
+ for k, v in pairs( t ) do
+ n = n+1
+ assert(t[k] == v)
+ t[k] = nil
+ collectgarbage()
+ assert(t[k] == nil)
+ end
+ assert(n == 5)
+
+
+ local function test (a)
+ assert(not pcall(table.insert, a, 2, 20));
+ table.insert(a, 10); table.insert(a, 2, 20);
+ table.insert(a, 1, -1); table.insert(a, 40);
+ table.insert(a, #a+1, 50)
+ table.insert(a, 2, -2)
+ assert(not pcall(table.insert, a, 0, 20));
+ assert(not pcall(table.insert, a, #a + 2, 20));
+ assert(table.remove(a,1) == -1)
+ assert(table.remove(a,1) == -2)
+ assert(table.remove(a,1) == 10)
+ assert(table.remove(a,1) == 20)
+ assert(table.remove(a,1) == 40)
+ assert(table.remove(a,1) == 50)
+ assert(table.remove(a,1) == nil)
+ assert(table.remove(a) == nil)
+ assert(table.remove(a, #a) == nil)
+ end
+
+ a = {n=0, [-7] = "ban"}
+ test(a)
+ assert(a.n == 0 and a[-7] == "ban")
+
+ a = {[-7] = "ban"};
+ test(a)
+ assert(a.n == nil and #a == 0 and a[-7] == "ban")
+
+ a = {[-1] = "ban"}
+ test(a)
+ assert(#a == 0 and table.remove(a) == nil and a[-1] == "ban")
+
+ a = {[0] = "ban"}
+ assert(#a == 0 and table.remove(a) == "ban" and a[0] == nil)
+
+ table.insert(a, 1, 10); table.insert(a, 1, 20); table.insert(a, 1, -1)
+ assert(table.remove(a) == 10)
+ assert(table.remove(a) == 20)
+ assert(table.remove(a) == -1)
+ assert(table.remove(a) == nil)
+
+ a = {'c', 'd'}
+ table.insert(a, 3, 'a')
+ table.insert(a, 'b')
+ assert(table.remove(a, 1) == 'c')
+ assert(table.remove(a, 1) == 'd')
+ assert(table.remove(a, 1) == 'a')
+ assert(table.remove(a, 1) == 'b')
+ assert(table.remove(a, 1) == nil)
+ assert(#a == 0 and a.n == nil)
+
+ a = {10,20,30,40}
+ assert(table.remove(a, #a + 1) == nil)
+ assert(not pcall(table.remove, a, 0))
+ assert(a[#a] == 40)
+ assert(table.remove(a, #a) == 40)
+ assert(a[#a] == 30)
+ assert(table.remove(a, 2) == 20)
+ assert(a[#a] == 30 and #a == 2)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: testing table library with metamethods", function (t) {
+ let luaCode = `
+ do
+ local function test (proxy, t)
+ for i = 1, 10 do
+ table.insert(proxy, 1, i)
+ end
+ assert(#proxy == 10 and #t == 10)
+ for i = 1, 10 do
+ assert(t[i] == 11 - i)
+ end
+ table.sort(proxy)
+ for i = 1, 10 do
+ assert(t[i] == i and proxy[i] == i)
+ end
+ assert(table.concat(proxy, ",") == "1,2,3,4,5,6,7,8,9,10")
+ for i = 1, 8 do
+ assert(table.remove(proxy, 1) == i)
+ end
+ assert(#proxy == 2 and #t == 2)
+ local a, b, c = table.unpack(proxy)
+ assert(a == 9 and b == 10 and c == nil)
+ end
+
+ -- all virtual
+ local t = {}
+ local proxy = setmetatable({}, {
+ __len = function () return #t end,
+ __index = t,
+ __newindex = t,
+ })
+ test(proxy, t)
+
+ -- only __newindex
+ local count = 0
+ t = setmetatable({}, {
+ __newindex = function (t,k,v) count = count + 1; rawset(t,k,v) end})
+ test(t, t)
+ assert(count == 10) -- after first 10, all other sets are not new
+
+ -- no __newindex
+ t = setmetatable({}, {
+ __index = function (_,k) return k + 1 end,
+ __len = function (_) return 5 end})
+ assert(table.concat(t, ";") == "2;3;4;5;6")
+
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: JS tests", { skip: true }, function (t) {
+ let luaCode = `
+ local tab = {10, 20, 30}
+ local mt = {}
+ local u = T.newuserdata(0)
+ checkerror("table expected", table.insert, u, 40)
+ checkerror("table expected", table.remove, u)
+ debug.setmetatable(u, mt)
+ checkerror("table expected", table.insert, u, 40)
+ checkerror("table expected", table.remove, u)
+ mt.__index = tab
+ checkerror("table expected", table.insert, u, 40)
+ checkerror("table expected", table.remove, u)
+ mt.__newindex = tab
+ checkerror("table expected", table.insert, u, 40)
+ checkerror("table expected", table.remove, u)
+ mt.__len = function () return #tab end
+ table.insert(u, 40)
+ assert(#u == 4 and #tab == 4 and u[4] == 40 and tab[4] == 40)
+ assert(table.remove(u) == 40)
+ table.insert(u, 1, 50)
+ assert(#u == 4 and #tab == 4 and u[4] == 30 and tab[1] == 50)
+
+ mt.__newindex = nil
+ mt.__len = nil
+ local tab2 = {}
+ local u2 = T.newuserdata(0)
+ debug.setmetatable(u2, {__newindex = function (_, k, v) tab2[k] = v end})
+ table.move(u, 1, 4, 1, u2)
+ assert(#tab2 == 4 and tab2[1] == tab[1] and tab2[4] == tab[4])
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: next", function (t) {
+ let luaCode = `
+ a = {}
+ for i=1,1000 do
+ a[i] = i; a[i-1] = nil
+ end
+ assert(next(a,nil) == 1000 and next(a,1000) == nil)
+
+ assert(next({}) == nil)
+ assert(next({}, nil) == nil)
+
+ for a,b in pairs{} do error"not here" end
+ for i=1,0 do error'not here' end
+ for i=0,1,-1 do error'not here' end
+ a = nil; for i=1,1 do assert(not a); a=1 end; assert(a)
+ a = nil; for i=1,1,-1 do assert(not a); a=1 end; assert(a)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+// TODO: infinite loop ?
+test("[test-suite] nextvar: testing floats in numeric for", { skip: true }, function (t) {
+ let luaCode = `
+ do
+ local a
+ -- integer count
+ a = 0; for i=1, 1, 1 do a=a+1 end; assert(a==1)
+ a = 0; for i=10000, 1e4, -1 do a=a+1 end; assert(a==1)
+ a = 0; for i=1, 0.99999, 1 do a=a+1 end; assert(a==0)
+ a = 0; for i=9999, 1e4, -1 do a=a+1 end; assert(a==0)
+ a = 0; for i=1, 0.99999, -1 do a=a+1 end; assert(a==1)
+
+ -- float count
+ a = 0; for i=0, 0.999999999, 0.1 do a=a+1 end; assert(a==10)
+ a = 0; for i=1.0, 1, 1 do a=a+1 end; assert(a==1)
+ a = 0; for i=-1.5, -1.5, 1 do a=a+1 end; assert(a==1)
+ a = 0; for i=1e6, 1e6, -1 do a=a+1 end; assert(a==1)
+ a = 0; for i=1.0, 0.99999, 1 do a=a+1 end; assert(a==0)
+ a = 0; for i=99999, 1e5, -1.0 do a=a+1 end; assert(a==0)
+ a = 0; for i=1.0, 0.99999, -1 do a=a+1 end; assert(a==1)
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: conversion", function (t) {
+ let luaCode = `
+ a = 0; for i="10","1","-2" do a=a+1 end; assert(a==5)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: checking types", function (t) {
+ let luaCode = `
+ do
+ local c
+ local function checkfloat (i)
+ assert(math.type(i) == "float")
+ c = c + 1
+ end
+
+ c = 0; for i = 1.0, 10 do checkfloat(i) end
+ assert(c == 10)
+
+ c = 0; for i = -1, -10, -1.0 do checkfloat(i) end
+ assert(c == 10)
+
+ local function checkint (i)
+ assert(math.type(i) == "integer")
+ c = c + 1
+ end
+
+ local m = math.maxinteger
+ c = 0; for i = m, m - 10, -1 do checkint(i) end
+ assert(c == 11)
+
+ c = 0; for i = 1, 10.9 do checkint(i) end
+ assert(c == 10)
+
+ c = 0; for i = 10, 0.001, -1 do checkint(i) end
+ assert(c == 10)
+
+ c = 0; for i = 1, "10.8" do checkint(i) end
+ assert(c == 10)
+
+ c = 0; for i = 9, "3.4", -1 do checkint(i) end
+ assert(c == 6)
+
+ c = 0; for i = 0, " -3.4 ", -1 do checkint(i) end
+ assert(c == 4)
+
+ c = 0; for i = 100, "96.3", -2 do checkint(i) end
+ assert(c == 2)
+
+ c = 0; for i = 1, math.huge do if i > 10 then break end; checkint(i) end
+ assert(c == 10)
+
+ c = 0; for i = -1, -math.huge, -1 do
+ if i < -10 then break end; checkint(i)
+ end
+ assert(c == 10)
+
+
+ for i = math.mininteger, -10e100 do assert(false) end
+ for i = math.maxinteger, 10e100, -1 do assert(false) end
+
+ end
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: testing generic 'for'", function (t) {
+ let luaCode = `
+ local function f (n, p)
+ local t = {}; for i=1,p do t[i] = i*10 end
+ return function (_,n)
+ if n > 0 then
+ n = n-1
+ return n, table.unpack(t)
+ end
+ end, nil, n
+ end
+
+ local x = 0
+ for n,a,b,c,d in f(5,3) do
+ x = x+1
+ assert(a == 10 and b == 20 and c == 30 and d == nil)
+ end
+ assert(x == 5)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: testing __pairs and __ipairs metamethod", function (t) {
+ let luaCode = `
+ a = {}
+ do
+ local x,y,z = pairs(a)
+ assert(type(x) == 'function' and y == a and z == nil)
+ end
+
+ local function foo (e,i)
+ assert(e == a)
+ if i <= 10 then return i+1, i+2 end
+ end
+
+ local function foo1 (e,i)
+ i = i + 1
+ assert(e == a)
+ if i <= e.n then return i,a[i] end
+ end
+
+ setmetatable(a, {__pairs = function (x) return foo, x, 0 end})
+
+ local i = 0
+ for k,v in pairs(a) do
+ i = i + 1
+ assert(k == i and v == k+1)
+ end
+
+ a.n = 5
+ a[3] = 30
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});
+
+
+test("[test-suite] nextvar: testing ipairs with metamethods", function (t) {
+ let luaCode = `
+ a = {n=10}
+ setmetatable(a, { __index = function (t,k)
+ if k <= t.n then return k * 10 end
+ end})
+ i = 0
+ for k,v in ipairs(a) do
+ i = i + 1
+ assert(k == i and v == i * 10)
+ end
+ assert(i == a.n)
+ `, L;
+
+ t.plan(2);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lualib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(prefix + luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+});