Skip to content

Instantly share code, notes, and snippets.

@cloudwu
Created October 26, 2015 12:05
Show Gist options
  • Select an option

  • Save cloudwu/f9bd2a5f7c22b90cfd2b to your computer and use it in GitHub Desktop.

Select an option

Save cloudwu/f9bd2a5f7c22b90cfd2b to your computer and use it in GitHub Desktop.

Revisions

  1. cloudwu created this gist Oct 26, 2015.
    7 changes: 7 additions & 0 deletions mymod.user.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    local M = {}

    function M.test(...)
    print(...)
    end

    return M
    28 changes: 28 additions & 0 deletions requirenv.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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)
    15 changes: 15 additions & 0 deletions test.lua
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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