lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am writing a debugger that is coroutine-savvy, ie. I also want to able to inspect any coroutines created by the debugged program from the debugger. For this, I need to hook a corresponding debug routine into each coroutine (via debug.sethook()), and I am doing this by intercepting coroutine.create().

I am fairly new to Lua and would like to ask if my approach is OK from the expert's POV. Note that I only want to use Lua, not C.

This is the relevant code snippet of the debugger that I came up with:

local new_coroutine = {}
setmetatable(new_coroutine, new_coroutine)
new_coroutine.__index = coroutine

function new_coroutine.create (...)
  local cr = new_coroutine.__index.create(...)
  -- 'probe.hook' is the debugger's "hook" routine
  debug.sethook(cr, probe.hook, "clr")
  return cr
end

local new_G = {}
setmetatable(new_G, {__index = _G})
new_G.coroutine = new_coroutine

-- 'file' contains the debugged program
local mf, err = loadfile(file)
-- error handling omitted here
setfenv(mf, new_G)										
local mfc = new_coroutine.create(mf)
local result = {coroutine.resume(mfc)}


Is this a suitable approach? Anything that could be improved?

Thanks and regards
- Stefan