Skip to content

Instantly share code, notes, and snippets.

@xebecnan
Forked from cloudwu/mymod.user.lua
Created October 26, 2015 12:48
Show Gist options
  • Save xebecnan/e47e9d431db032af5ea2 to your computer and use it in GitHub Desktop.
Save xebecnan/e47e9d431db032af5ea2 to your computer and use it in GitHub Desktop.
user defined loader
local M = {}
function M.test(...)
print(...)
end
return M
local package = package
local debug = debug
local function load_env(filename)
local f,err = loadfile(filename)
if f == nil then
return err
end
return function()
return function(env)
if env then
debug.setupvalue(f, 1, env)
end
return f(filename)
end
end
end
local function searcher_env(name)
local filename, err = package.searchpath(name, package.upath)
if filename == nil then
return err
else
return load_env(filename)
end
end
table.insert(package.searchers, searcher_env)
require "requirenv"
package.upath = "./?.user.lua"
local myprint = print
local env = {
print = function (...)
myprint("hook", ...)
end
}
local s = require "mymod"(env)
s.test "hello world" -- hook hello world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment