--- source: src/main.rs expression: compiled input_file: test-data/lua5.4-tests/attrib.lua --- print("testing require"); assert(require("string") == string); assert(require("math") == math); assert(require("table") == table); assert(require("io") == io); assert(require("os") == os); assert(require("coroutine") == coroutine); assert(type(package.path) == "string"); assert(type(package.cpath) == "string"); assert(type(package.loaded) == "table"); assert(type(package.preload) == "table"); assert(type(package.config) == "string"); print("package config: " .. string.gsub(package.config, "\n", "|")); { local max = _soft && 100 || 2000 local t = {} for i = 1, max { t[(i)] = string.rep("?", i % 10 + 1) } t[(#t + 1)] = ";" local path = table.concat(t, ";") local s, err = package.searchpath("xuxu", path) assert(!s && string.find(err, string.rep("xuxu", 10)) && #string.gsub(err, "[^\n]", "") >= max); local path = string.rep("?", max) local s, err = package.searchpath("xuxu", path) assert(!s && string.find(err, string.rep('xuxu', max))); } { local oldpath = package.path package.path = {} local s, err = pcall(require, "no-such-file") assert(!s && string.find(err, "package.path")); package.path = oldpath } { print("testing 'require' message"); local oldpath = package.path local oldcpath = package.cpath package.path = "?.lua;?/?" package.cpath = "?.so;?/init" local st, msg = pcall(require, 'XXX') local expected = `module 'XXX' not found: no field package.preload['XXX'] no file 'XXX.lua' no file 'XXX/XXX' no file 'XXX.so' no file 'XXX/init'` assert(msg == expected); package.path = oldpath package.cpath = oldcpath } print('+'); if !_port { local dirsep = string.match(package.config, "^([^\n]+)\n") local DIR = "libs" .. dirsep local fn D(x) { x = string.gsub(x, "/", dirsep) return DIR .. x } local fn DC(x) { local ext = (dirsep == '\\') && ".dll" || ".so" return D(x .. ext) } local fn createfiles(files, preextras, posextras) { for n, c with pairs(files) { io.output(D(n)); io.write(string.format(preextras, n)); io.write(c); io.write(string.format(posextras, n)); io.close(io.output()); } } global fn removefiles(files) { for n with pairs(files) { os.remove(D(n)); } } local files = { "names.lua" = "do return {...} end\n", "err.lua" = "B = 15; a = a + 1;", "synerr.lua" = "B =", "A.lua" = "", "B.lua" = "assert(...=='B');require 'A'", "A.lc" = "", "A" = "", "L" = "", "XXxX" = "", "C.lua" = "package.loaded[...] = 25; require'C'" } AA = nil local extras = ` NAME = '%s' REQUIRED = ... return AA` createfiles(files, "", extras); assert(package.searchpath("C.lua", D("?"), "", "") == D("C.lua")); assert(package.searchpath("C.lua", D("?"), ".", ".") == D("C.lua")); assert(package.searchpath("--x-", D("?"), "-", "X") == D("XXxX")); assert(package.searchpath("---xX", D("?"), "---", "XX") == D("XXxX")); assert(package.searchpath(D("C.lua"), "?", dirsep) == D("C.lua")); assert(package.searchpath(".\\C.lua", D("?"), "\\") == D("./C.lua")); local oldpath = package.path package.path = string.gsub("D/?.lua;D/?.lc;D/?;D/??x?;D/L", "D/", DIR) local try = fn (p, n, r, ext) { NAME = nil local rr, x = require(p) assert(NAME == n); assert(REQUIRED == p); assert(rr == r); assert(ext == x); } a = require("names") assert(a[(1)] == "names" && a[(2)] == D("names.lua")); _G.a = nil local st, msg = pcall(require, "err") assert(!st && string.find(msg, "arithmetic") && B == 15); st, msg = pcall(require, "synerr") assert(!st && string.find(msg, "error loading module")); assert(package.searchpath("C", package.path) == D("C.lua")); assert(require("C") == 25); assert(require("C") == 25); AA = nil try('B', 'B.lua', true, "libs/B.lua"); assert(package.loaded.B); assert(require("B") == true); assert(package.loaded.A); assert(require("C") == 25); package.loaded.A = nil try('B', nil, true, nil); try('A', 'A.lua', true, "libs/A.lua"); package.loaded.A = nil os.remove(D('A.lua')); AA = {} try('A', 'A.lc', AA, "libs/A.lc"); assert(package.searchpath("A", package.path) == D("A.lc")); assert(require("A") == AA); AA = false try('K', 'L', false, "libs/L"); try('K', 'L', false, "libs/L"); assert(rawget(_G, "_REQUIREDNAME") == nil); AA = "x" try("X", "XXxX", AA, "libs/XXxX"); removefiles(files); local _G = _G package.path = string.gsub("D/?.lua;D/?/init.lua", "D/", DIR) files = { "P1/init.lua" = "AA = 10", "P1/xuxu.lua" = "AA = 20" } createfiles(files, "_ENV = {}\n", "\nreturn _ENV\n"); AA = 0 local m, ext = assert(require("P1")) assert(ext == "libs/P1/init.lua"); assert(AA == 0 && m.AA == 10); assert(require("P1") == m); assert(require("P1") == m); assert(package.searchpath("P1.xuxu", package.path) == D("P1/xuxu.lua")); m.xuxu, ext = assert(require("P1.xuxu")) assert(AA == 0 && m.xuxu.AA == 20); assert(ext == "libs/P1/xuxu.lua"); assert(require("P1.xuxu") == m.xuxu); assert(require("P1.xuxu") == m.xuxu); assert(require("P1") == m && m.AA == 10); removefiles(files); package.path = "" assert(!pcall(require, "file_does_not_exist")); package.path = "??\0?" assert(!pcall(require, "file_does_not_exist1")); package.path = oldpath local fname = "file_does_not_exist2" local m, err = pcall(require, fname) for t with string.gmatch(package.path .. ";" .. package.cpath, "[^;]+") { t = string.gsub(t, "?", fname) assert(string.find(err, t, 1, true)); } { local searchers = package.searchers package.searchers = 3 local st, msg = pcall(require, 'a') assert(!st && string.find(msg, "must be a table")); package.searchers = searchers } local fn import(...) { local f = { ... } return fn (m) { for i = 1, #f { m[(f[(i)])] = _G[(f[(i)])] } } } assert(!pcall(module, 'XUXU')); local p = "" local st, err, when = package.loadlib(DC("lib1"), "*") if !st { local f, err, when = package.loadlib("donotexist", p .. "xuxu") assert(!f && type(err) == "string" && when == "absent"); (Message || print)('\n >>> cannot load dynamic library <<<\n'); print(err, when); } else { local f = assert(package.loadlib(DC("lib1"), p .. "onefunction")) local a, b = f(15, 25) assert(a == 25 && b == 15); f = assert(package.loadlib(DC("lib1"), p .. "anotherfunc")) assert(f(10, 20) == "10%20\n"); local f, err, when = package.loadlib(DC("lib1"), p .. "xuxu") assert(!f && type(err) == "string" && when == "init"); f, err, when = package.loadlib("donotexist", p .. "xuxu") assert(!f && type(err) == "string" && when == "open"); f = assert(package.loadlib(DC("lib11"), p .. "luaopen_lib11")) assert(f() == "exported"); package.cpath = DC("?") local lib2, ext = require("lib2-v2") assert(string.find(ext, "libs/lib2-v2", 1, true)); assert(_ENV.x == "lib2-v2" && _ENV.y == DC("lib2-v2")); assert(lib2.id("x") == true); local fs, ext = require("lib1.sub") assert(_ENV.x == "lib1.sub" && _ENV.y == DC("lib1")); assert(string.find(ext, "libs/lib1", 1, true)); assert(fs.id(45) == 45); } _ENV = _G { local p = package package = {} p.preload.pl = fn (...) { local _ENV = { ... } global fn xuxu(x) { return x + 20 } return _ENV } local pl, ext = require("pl") assert(require("pl") == pl); assert(pl.xuxu(10) == 30); assert(pl[(1)] == "pl" && pl[(2)] == ":preload:" && ext == ":preload:"); package = p assert(type(package.path) == "string"); } print('+'); } print("testing assignments, logical operators, and constructors"); local res, res2 = 27 a, b = 1, 2 + 3 assert(a == 1 && b == 5); a = {} global fn f() { return 10, 11, 12 } a.x, b, a[(1)] = 1, 2, f() assert(a.x == 1 && b == 2 && a[(1)] == 10); a[(f())], b, a[(f() + 3)] = f(), a, 'x' assert(a[(10)] == 10 && b == a && a[(13)] == 'x'); { local f = fn (n) { local x = {} for i = 1, n { x[(i)] = i } return table.unpack(x) } local a, b, c a, b = 0, f(1) assert(a == 0 && b == 1); A, b = 0, f(1) assert(A == 0 && b == 1); a, b, c = 0, 5, f(4) assert(a == 0 && b == 5 && c == 1); a, b, c = 0, 5, f(0) assert(a == 0 && b == 5 && c == nil); } a, b, c, d = 1 && nil, 1 || nil, (1 && (nil || 1)), 6 assert(!a && b && c && d == 6); d = 20 a, b, c, d = f() assert(a == 10 && b == 11 && c == 12 && d == nil); a, b = f(), 1, 2, 3, f() assert(a == 10 && b == 1); assert(a < b == false && a > b == true); assert((10 && 2) == 2); assert((10 || 2) == 10); assert((10 || assert(nil)) == 10); assert(!(nil && assert(nil))); assert((nil || "alo") == "alo"); assert((nil && 10) == nil); assert((false && 10) == false); assert((true || 10) == true); assert((false || 10) == 10); assert(false != nil); assert(nil != false); assert(!nil == true); assert(!!nil == false); assert(!!1 == true); assert(!!a == true); assert(!!(6 || nil) == true); assert(!!(nil && 56) == false); assert(!!(nil && true) == false); assert(!10 == false); assert(!{} == false); assert(!0.5 == false); assert(!"x" == false); assert({} != {}); print('+'); a = {} a[(true)] = 20 a[(false)] = 10 assert(a[(1 < 2)] == 20 && a[(1 > 2)] == 10); global fn f(a) { return a } local a = {} for i = 3000, -3000, -1 { a[(i + 0.0)] = i } a[(10e30)] = "alo" a[(true)] = 10 a[(false)] = 20 assert(a[(10e30)] == 'alo' && a[(!1)] == 20 && a[(10 < 20)] == 10); for i = 3000, -3000, -1 { assert(a[(i)] == i); } a[(print)] = assert a[(f)] = print a[(a)] = a assert(a[(a)][(a)][(a)][(a)][(print)] == assert); a[(print)](a[(a[(f)])] == a[(print)]); assert(!pcall(fn () { local a = {} a[(nil)] = 10 })); assert(!pcall(fn () { local a = { nil = 10 } })); assert(a[(nil)] == undef); a = nil 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 && a[(-3)] == a.a && a[(f)] == print && a.x == 'a' && !a.y); a[(1)], f(a)[(2)], b, c = { 'alo' = assert }, 10, a[(1)], a[(f)], 6, 10, 23, f(a), 2 a[(1)].alo(a[(2)] == 10 && b == 10 && c == print); a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 = 10 local fn foo() { return a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 } assert(foo() == 10 && a.aVeryLongName012345678901234567890123456789012345678901234567890123456789 == 10); local maxint = math.maxinteger while maxint != (maxint + 0.0) || (maxint - 1) != (maxint - 1.0) { maxint = maxint /_ 2 } maxintF = maxint + 0.0 assert(maxintF == maxint && math.type(maxintF) == "float" && maxintF >= 2.0 ^ 14); a[(maxintF)] = 10 a[(maxintF - 1.0)] = 11 a[(-maxintF)] = 12 a[(-maxintF + 1.0)] = 13 assert(a[(maxint)] == 10 && a[(maxint - 1)] == 11 && a[(-maxint)] == 12 && a[(-maxint + 1)] == 13); a[(maxint)] = 20 a[(-maxint)] = 22 assert(a[(maxintF)] == 20 && a[(maxintF - 1.0)] == 11 && a[(-maxintF)] == 22 && a[(-maxintF + 1.0)] == 13); a = nil { local a, i, j, b a = { 'a', 'b' } i = 1 j = 2 b = a i, a[(i)], a, j, a[(j)], a[(i + j)] = j, i, i, b, j, i assert(i == 2 && b[(1)] == 1 && a == 1 && j == b && b[(2)] == 2 && b[(3)] == 1); a = {} local fn foo() { b, a.x, a = a, 10, 20 } foo(); assert(a == 20 && b.x == 10); } { local a, i, j, b a = { 'a', 'b' } i = 1 j = 2 b = a local fn foo() { i, a[(i)], a, j, a[(j)], a[(i + j)] = j, i, i, b, j, i } foo(); assert(i == 2 && b[(1)] == 1 && a == 1 && j == b && b[(2)] == 2 && b[(3)] == 1); local t = {}(fn (a) { t[(a)], a = 10, 20 })(1) assert(t[(1)] == 10); } local fn foo() { local a return fn () { local b a, b = 3, 14 return a, b } } local a, b = foo()() assert(a == 3 && b == 14); print('OK'); return res