From ad615f63eca42dfd3d3b50069ef295c3d5dbdd0d Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sun, 6 Dec 2015 18:33:37 +0300 Subject: Implement luacov.runner.with_luacov Useful when using luacov with coroutines created via C API. It's possible to get and set the luacov debug hook manually, but the debug.sethook call itself won't be covered. When with_luacov() is used, the call happens inside luacov. runner.with_luacov can be called before runner.init, triggering a hook call before luacov state is ready. Add a guard for that. Ref #38 --- src/luacov/runner.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/luacov/runner.lua b/src/luacov/runner.lua index 7e7e7b4..70668fc 100644 --- a/src/luacov/runner.lua +++ b/src/luacov/runner.lua @@ -24,6 +24,7 @@ local data local statsfile local tick local paused = true +local initialized = false local ctr = 0 local filelist = {} @@ -89,6 +90,10 @@ end -- they may be absent if it's called from a sandboxed environment -- or because of carelessly implemented monkey-patching. local function on_line(_, line_nr) + if not initialized then + return + end + if tick then ctr = ctr + 1 if ctr == runner.configuration.savestepsize then @@ -396,6 +401,18 @@ local function has_hook_per_thread() return hook_per_thread end +-------------------------------------------------- +-- Wraps a function to be used in a coroutine created via C API. +-- @param f function +-- @return function that enables coverage gathering for current thread +-- and calls original function. +function runner.with_luacov(f) + return function(...) + debug.sethook(on_line, "l") + return f(...) + end +end + -------------------------------------------------- -- Initializes LuaCov runner to start collecting data. -- @param[opt] configuration if string, filename of config file (used to call `load_config`). @@ -444,6 +461,8 @@ function runner.init(configuration) end end end + + initialized = true end -------------------------------------------------- -- cgit v1.2.1