示例工程 28-release-and-compat

28-release-and-compat/02-feature-detection/main.lua

lua · 0.7 KB

-- @runtimes lua54 luajit
--- 演示:特性检测优于版本字符串。
local M = {}
 
local function has_goto()
	return load("do goto done; ::done:: end") ~= nil
end
 
--- @return table { integer = boolean, utf8 = boolean, pack = boolean, goto = boolean }
function M.run(log)
	log = log or print
	local flags = {
		integer = math.type ~= nil and math.type(1) == "integer",
		utf8 = utf8 ~= nil,
		pack = table.pack ~= nil,
		["goto"] = has_goto(),
	}
	log(
		("integer=%s utf8=%s pack=%s goto=%s"):format(
			tostring(flags.integer),
			tostring(flags.utf8),
			tostring(flags.pack),
			tostring(flags["goto"])
		)
	)
	return flags
end
if not ... then
	M.run()
end
return M