1

silly/patch.lua at 9794239f0df92fcb2931758ac40b741a98424d45 · findstr/silly · Gi...

 2 years ago
source link: https://github.com/findstr/silly/blob/9794239f0df92fcb2931758ac40b741a98424d45/lualib/silly/patch.lua
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

silly/patch.lua at 9794239f0df92fcb2931758ac40b741a98424d45 · findstr/silly · GitHub

Permalink

66 lines (61 sloc) 1.28 KB

local dgetinfo = debug.getinfo local dgetupval = debug.getupvalue local dupvaljoin = debug.upvaluejoin

local function collectup(func, tbl) local info = dgetinfo(func, "u") for i = 1, info.nups do local name, val = dgetupval(func, i) local up if type(val) == "function" then up = { idx = i, uname = name, utype = type(val), val = val, up = {} } collectup(val, up.up) else up = { idx = i, uname = name, uptype = type(val), val = val, up = val, } end tbl[name] = up end end

local function patch(run, runup, fix, fixup) assert(type(run) == "function") assert(type(fix) == "function") local info = dgetinfo(run, "u") for i = 1, info.nups do local name, val = dgetupval(run, i) if fixup[name] then local rup = runup[name] local fup = fixup[name] assert(rup.idx == i) if type(val) == "function" then assert(runup[name].val == val) patch(rup.val, rup.up, fup.val, fup.up) else dupvaljoin(fix, fup.idx, run, rup.idx) end end end end

return function(runfunc, fixfunc, fixenv) local runup = {} local fixup = {} collectup(runfunc, runup) collectup(fixfunc, fixup) patch(runfunc, runup, fixfunc, fixup) for k, v in pairs(fixenv) do if not _ENV[k] then _ENV[k] = v end end return fixfunc end


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK