Last active
March 17, 2017 10:00
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local getlocal, setlocal = debug.getlocal, debug.setlocal | |
local function from_err(k, msg) | |
local info = debug.getinfo(3) | |
io.stderr:write(string.format( | |
"%s:%s:%d: local variable '%s' %s in import block\n", | |
info.source:sub(2),info.name,info.currentline,k,msg)) | |
os.exit() | |
end | |
local function from(t) | |
local i = 1 | |
while true do | |
local k, v = getlocal(2, i) | |
if not k or v == nil then break end | |
i = i + 1 | |
end | |
while true do | |
local k, v = getlocal(2, i) | |
if k == nil then break | |
elseif v ~= nil then from_err(k, "initialized") | |
elseif t[k] == nil then from_err(k, "not found.") | |
end | |
setlocal(2, i, t[k]) | |
i = i + 1 | |
end | |
end | |
local function test(val) | |
local exp, log from (math) | |
local sub from (string) | |
local sin, cos, tan from (math) | |
assert(exp == math.exp) | |
assert(log == math.log) | |
assert(sin == math.sin) | |
assert(cos == math.cos) | |
assert(tan == math.tan) | |
assert(sub == string.sub) | |
return exp(val), tan(val) | |
end | |
--local asin, acos, atan from (math) | |
print(test(1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function getnlocal(stack) | |
local i = 1 | |
repeat | |
local k, v = debug.getlocal(stack + 1, i) | |
if k then | |
i = i + 1 | |
end | |
until nil == k | |
return i | |
end | |
local function within(t, n) | |
local line = debug.getinfo(2).currentline | |
local count = getnlocal(2) | |
for i=count-n, count-1 do | |
local name = debug.getlocal(2, i) | |
debug.setlocal(2, i, t[name]) | |
end | |
end | |
local exp, log, sin, cos, tan within (math, 5) | |
assert(exp == math.exp) | |
assert(log == math.log) | |
assert(sin == math.sin) | |
assert(cos == math.cos) | |
assert(tan == math.tan) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment