summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormpeterv <mpeterval@gmail.com>2015-12-06 18:33:37 +0300
committermpeterv <mpeterval@gmail.com>2015-12-06 20:09:54 +0300
commitad615f63eca42dfd3d3b50069ef295c3d5dbdd0d (patch)
treeae89186589b90a9fb7b79e1390ca987bc9b2bd88
parent146cd4371c1804908e55f31f69c219bdf936b274 (diff)
downloadluacov-ad615f63eca42dfd3d3b50069ef295c3d5dbdd0d.tar.gz
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
-rw-r--r--src/luacov/runner.lua19
1 files changed, 19 insertions, 0 deletions
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
@@ -397,6 +402,18 @@ local function has_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`).
-- If table then config table (see file `luacov.default.lua` for an example)
@@ -444,6 +461,8 @@ function runner.init(configuration)
end
end
end
+
+ initialized = true
end
--------------------------------------------------