-- Conformance suite: exercises the supported Lua subset and returns an array -- of "name=value" strings so the compiled module can be compared byte for byte -- with the reference Lua interpreter. local results = {} local function add(name, value) results[#results + 1] = name .. "=" .. tostring(value) end -- numbers add("add", 2 + 3) add("div", 1 / 2) add("idiv", 7 // 2) add("mod", 7 % 3) add("pow", 2 ^ 10) add("unm", -3) add("band", 0xFF & 0x0F) add("shl", 1 << 10) add("int_type", math.type(7)) add("float_type", math.type(7.0)) add("floor", math.floor(3.9)) -- strings local s = "Buffy" add("upper", s:upper()) add("sub", string.sub(s, 2, 4)) add("slen", #s) add("concat", s .. " " .. 42) add("escape", "a\tb\nc") add("fmt", string.format("%s-%d-%.2f", "x", 7, 1.5)) -- tables local t = { 10, 20, 30, x = 5 } t[#t + 1] = 40 add("tlen", #t) add("tidx", t[4]) add("tfield", t.x) local keyed = { ["a" .. "b"] = "ab", [2 + 3] = "five" } add("keyed", keyed.ab .. "/" .. keyed[5]) local nested = { a = { b = { c = 1 } } } add("nested", nested.a.b.c) -- control flow local sum = 0 for i = 1, 10 do sum = sum + i end add("for_sum", sum) local wsum, wi = 0, 0 while wi < 5 do wi = wi + 1 wsum = wsum + wi end add("while_sum", wsum) local rsum, rj = 0, 0 repeat rj = rj + 1 rsum = rsum + rj until rj >= 3 add("repeat_sum", rsum) local found for _, v in ipairs({ 5, 6, 7 }) do if v == 6 then found = v break end end add("break", found) local fsum = 0 for _, v in pairs({ a = 1, b = 2, c = 3 }) do fsum = fsum + v end add("pairs_sum", fsum) local total = 0 for i = 10, 1, -3 do total = total + i end add("for_neg", total) -- functions, varargs, multiple returns local function two() return 1, 2 end local a, b = two() add("multi_return", a + b) local function sum_all(...) local acc = 0 for _, v in ipairs({ ... }) do acc = acc + v end return acc end add("varargs", sum_all(1, 2, 3, 4)) local function swap(x, y) return y, x end local p, q = swap("left", "right") add("swap_returns", p .. "/" .. q) local function with_default(x, y) y = y or 10 return x + y end add("default_arg", with_default(5)) local function fact(n) if n <= 1 then return 1 end return n * fact(n - 1) end add("recursion", fact(6)) -- closures capture mutable locals local function counter() local n = 0 return function() n = n + 1 return n end end local c = counter() c() c() add("closure", c()) local function adder(x) return function(y) return x + y end end add("closure_nested", adder(10)(5)) -- methods and metatables local point = { x = 1, y = 2 } function point:norm2() return self.x * self.x + self.y * self.y end add("method", point:norm2()) local mt = {} mt.__index = mt function mt.new(v) return setmetatable({ value = v }, mt) end function mt:double() return self.value * 2 end mt.__add = function(lhs, rhs) return mt.new(lhs.value + rhs.value) end mt.__tostring = function(obj) return "box(" .. obj.value .. ")" end local box1, box2 = mt.new(3), mt.new(4) add("meta_method", (box1 + box2):double()) add("meta_tostring", tostring(box1)) -- table library driven by a Lua callback local list = { 5, 2, 9, 1 } table.sort(list, function(x, y) return x > y end) add("sort", table.concat(list, ",")) -- errors local ok, err = pcall(function() error("boom") end) add("pcall", tostring(ok) .. ":" .. tostring(err):match("boom%a*")) -- multiple assignment swap local u, v = 1, 2 u, v = v, u add("swap_vars", u .. "/" .. v) -- goto / labels local visited = 0 for k = 1, 5 do if k == 3 then goto continue end visited = visited + 1 ::continue:: end add("goto", visited) add("tonumber", tonumber("42") + 1) add("select_count", select("#", 1, 2, 3)) return results