Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Custom Lua module loader for Lua 5.2
-- This allows a more restricted module() style; the key feature
-- is that each module is loaded in a custom environment, and the actual module
-- table is a copy of that environment after initial load.
-- clone _G so that globals from the program don't invade the module
local lua_libs = {}
for k,v in pairs(package.loaded._G) do
lua_libs[k] = v
local function loader(name, modpath)
local env = {}
env.module = function (name)
env._NAME = name
return env
setmetatable(env, {
__index = lua_libs, -- resolve known globals
local fh = assert(, 'rb'))
local source = fh:read'*a'
local ret = assert(load(source, modpath, 'bt', env))(name)
if ret then return ret end -- explicit table was returned
local mod = {}
-- the module is a copy of the environment after initial loading
for k,v in pairs(env) do
mod[k] = v
return mod
-- replace Lua loader
package.searchers[2] = function (name)
local modpath, msg = package.searchpath(name, package.path)
if modpath then
return loader, modpath
return nil, msg
Copy link

matthiesenj commented Mar 10, 2020

I think you have a mistake; a searcher returns a string message as its sole return value when it doesn't find anything, so line 39 should be "return msg".


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment