Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pr #573

Merged
merged 4 commits into from
Feb 10, 2017
Merged

Pr #573

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lualib/snax/hotfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ local function collect_all_uv(funcs)
end

local function loader(source)
return function (filename, ...)
return load(source, "=patch", ...)
return function (path, name, G)
return load(source, "=patch", "bt", G)
end
end

Expand All @@ -68,10 +68,10 @@ local function find_func(funcs, group , name)
end

local dummy_env = {}
for k,v in pairs(_ENV) do dummy_env[k] = v end

local function patch_func(funcs, global, group, name, f)
local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name))
local i = 1
local function _patch(global, f)
local i = 1
while true do
local name, value = debug.getupvalue(f, i)
if name == nil then
Expand All @@ -81,9 +81,18 @@ local function patch_func(funcs, global, group, name, f)
if old_uv then
debug.upvaluejoin(f, i, old_uv.func, old_uv.index)
end
else
if type(value) == "function" then
_patch(global, value)
end
end
i = i + 1
end
end

local function patch_func(funcs, global, group, name, f)
local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name))
_patch(global, f)
desc[4] = f
end

Expand Down
45 changes: 21 additions & 24 deletions lualib/snax/interface.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
local skynet = require "skynet"

local function dft_loader(path, name, G)
local errlist = {}

for pat in string.gmatch(path,"[^;]+") do
local filename = string.gsub(pat, "?", name)
local f , err = loadfile(filename, "bt", G)
if f then
return f, pat
else
table.insert(errlist, err)
end
end

error(table.concat(errlist, "\n"))
end

return function (name , G, loader)
loader = loader or loadfile
local mainfunc
loader = loader or dft_loader
local mainfunc

local function func_id(id, group)
local tmp = {}
Expand Down Expand Up @@ -61,30 +77,11 @@ return function (name , G, loader)

local pattern

do
local path = assert(skynet.getenv "snax" , "please set snax in config file")

local errlist = {}

for pat in string.gmatch(path,"[^;]+") do
local filename = string.gsub(pat, "?", name)
local f , err = loader(filename, "bt", G)
if f then
pattern = pat
mainfunc = f
break
else
table.insert(errlist, err)
end
end

if mainfunc == nil then
error(table.concat(errlist, "\n"))
end
end
local path = assert(skynet.getenv "snax" , "please set snax in config file")
mainfunc, pattern = loader(path, name, G)

setmetatable(G, { __index = env , __newindex = init_system })
local ok, err = pcall(mainfunc)
local ok, err = xpcall(mainfunc, debug.traceback)
setmetatable(G, nil)
assert(ok,err)

Expand Down
4 changes: 3 additions & 1 deletion service/snaxd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ local profile = require "profile"
local snax = require "snax"

local snax_name = tostring(...)
local func, pattern = snax_interface(snax_name, _ENV)
local loaderpath = skynet.getenv"snax_loader"
local loader = loaderpath and assert(dofile(loaderpath)) or require"snax.loader"
local func, pattern = snax_interface(snax_name, _ENV, loader)
local snax_path = pattern:sub(1,pattern:find("?", 1, true)-1) .. snax_name .. "/"
package.path = snax_path .. "?.lua;" .. package.path

Expand Down