some debug method

This commit is contained in:
leaf corcoran 2011-11-14 19:16:26 -08:00
parent e9c7f56bf4
commit e32f0752b8
2 changed files with 49 additions and 0 deletions

View File

@ -2,7 +2,33 @@ if not moon or not moon.inject then
module("moon", package.seeall)
end
local util = require("moonscript.util")
local lua = {
debug = debug
}
dump = util.dump
debug = {
upvalue = function(fn, k, v)
local upvalues = { }
local i = 1
while true do
local name = lua.debug.getupvalue(fn, i)
if name == nil then
break
end
upvalues[name] = i
i = i + 1
end
if not upvalues[k] then
error("Failed to find upvalue: " .. tostring(k))
end
if not v then
local _, value = lua.debug.getupvalue(fn, upvalues[k])
return value
else
return lua.debug.setupvalue(fn, upvalues[k], v)
end
end
}
run_with_scope = function(fn, scope, ...)
local old_env = getfenv(fn)
local env = setmetatable({ }, {

View File

@ -4,10 +4,33 @@ if not moon or not moon.inject
util = require "moonscript.util"
lua = { :debug }
export *
dump = util.dump
debug = {
upvalue: (fn, k, v) ->
upvalues = {}
i = 1
while true
name = lua.debug.getupvalue(fn, i)
break if name == nil
upvalues[name] = i
i += 1
if not upvalues[k]
error "Failed to find upvalue: " .. tostring k
if not v
_, value = lua.debug.getupvalue fn, upvalues[k]
value
else
lua.debug.setupvalue fn, upvalues[k], v
}
-- run a function with scope injected before its function environment
run_with_scope = (fn, scope, ...) ->
old_env = getfenv fn