-- @runtimes lua54 --- 演示: 禁止重新赋值, 在作用域退出时清理。 local M = {} --- @return table { value = integer, close_called = boolean } function M.run(log) log = log or print local value = 42 local close_called = false do local guard = setmetatable({}, { __close = function() close_called = true end, }) log(("guard_is_table=%s"):format(tostring(type(guard) == "table"))) end log(("close_called=%s"):format(tostring(close_called))) local bad, err = load("local x = 1; x = 2", "=const") log(("const_compile_error=%s"):format(tostring(bad == nil))) log( ("const_error_mentions_const=%s"):format( tostring(err ~= nil and err:find("const", 1, true) ~= nil) ) ) return { value = value, close_called = close_called, const_error = bad == nil } end if not ... then M.run() end return M