Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/pandoc/datadir/luacov/hook.lua
12923 views
1
------------------------
2
-- Hook module, creates debug hook used by LuaCov.
3
-- @class module
4
-- @name luacov.hook
5
local hook = {}
6
7
----------------------------------------------------------------
8
local dir_sep = package.config:sub(1, 1)
9
if not dir_sep:find("[/\\]") then
10
dir_sep = "/"
11
end
12
13
--- Creates a new debug hook.
14
-- @param runner runner module.
15
-- @return debug hook function that uses runner fields and functions
16
-- and sets `runner.data`.
17
function hook.new(runner)
18
local ignored_files = {}
19
local steps_after_save = 0
20
21
return function(_, line_nr, level)
22
-- Do not use string metamethods within the debug hook:
23
-- they may be absent if it's called from a sandboxed environment
24
-- or because of carelessly implemented monkey-patching.
25
level = level or 2
26
if not runner.initialized then
27
return
28
end
29
30
-- Get name of processed file.
31
local name = debug.getinfo(level, "S").source
32
local prefixed_name = string.match(name, "^@(.*)")
33
if prefixed_name then
34
name = prefixed_name:gsub("^%.[/\\]", ""):gsub("[/\\]", dir_sep)
35
elseif not runner.configuration.codefromstrings then
36
-- Ignore Lua code loaded from raw strings by default.
37
return
38
end
39
40
local data = runner.data
41
local file = data[name]
42
43
if not file then
44
-- New or ignored file.
45
if ignored_files[name] then
46
return
47
elseif runner.file_included(name) then
48
file = {max = 0, max_hits = 0}
49
data[name] = file
50
else
51
ignored_files[name] = true
52
return
53
end
54
end
55
56
if line_nr > file.max then
57
file.max = line_nr
58
end
59
60
local hits = (file[line_nr] or 0) + 1
61
file[line_nr] = hits
62
63
if hits > file.max_hits then
64
file.max_hits = hits
65
end
66
67
if runner.tick then
68
steps_after_save = steps_after_save + 1
69
70
if steps_after_save == runner.configuration.savestepsize then
71
steps_after_save = 0
72
73
if not runner.paused then
74
runner.save_stats()
75
end
76
end
77
end
78
end
79
end
80
81
return hook
82
83