示例工程 01-mental-model
01-mental-model/04-require-vs-dofile/main.lua
-- @runtimes lua54 luajit
--- 演示:`require` 有缓存,`dofile` 每次重新执行文件。
local M = {}
--- 比较两次 require 与两次 dofile 的返回表身份。
--- @return table { require_same = boolean, dofile_same = boolean }
function M.run(log)
log = log or print
local first = require("counter")
local second = require("counter")
local third = dofile("counter.lua")
local fourth = dofile("counter.lua")
log(("require_same=%s"):format(tostring(first == second)))
log(("dofile_same=%s"):format(tostring(third == fourth)))
return { require_same = first == second, dofile_same = third == fourth }
end
if not ... then
M.run()
end
return M