示例工程 19-modules-and-env

19-modules-and-env/01-require-cache/main.lua

lua · 0.5 KB

-- @runtimes lua54 luajit
--- 演示:require 缓存与 package.loaded。
local M = {}
 
--- @return table { same = boolean, loaded = boolean }
function M.run(log)
	log = log or print
	local first = require("counter")
	local second = require("counter")
	local same = first == second
	local loaded = package.loaded["counter"] == first
	log(("same=%s loaded=%s"):format(tostring(same), tostring(loaded)))
	return { same = same, loaded = loaded }
end
 
if not ... then
	M.run()
end
return M