Path: blob/main/src/resources/pandoc/datadir/luacov/hook.lua
12923 views
------------------------1-- Hook module, creates debug hook used by LuaCov.2-- @class module3-- @name luacov.hook4local hook = {}56----------------------------------------------------------------7local dir_sep = package.config:sub(1, 1)8if not dir_sep:find("[/\\]") then9dir_sep = "/"10end1112--- Creates a new debug hook.13-- @param runner runner module.14-- @return debug hook function that uses runner fields and functions15-- and sets `runner.data`.16function hook.new(runner)17local ignored_files = {}18local steps_after_save = 01920return function(_, line_nr, level)21-- Do not use string metamethods within the debug hook:22-- they may be absent if it's called from a sandboxed environment23-- or because of carelessly implemented monkey-patching.24level = level or 225if not runner.initialized then26return27end2829-- Get name of processed file.30local name = debug.getinfo(level, "S").source31local prefixed_name = string.match(name, "^@(.*)")32if prefixed_name then33name = prefixed_name:gsub("^%.[/\\]", ""):gsub("[/\\]", dir_sep)34elseif not runner.configuration.codefromstrings then35-- Ignore Lua code loaded from raw strings by default.36return37end3839local data = runner.data40local file = data[name]4142if not file then43-- New or ignored file.44if ignored_files[name] then45return46elseif runner.file_included(name) then47file = {max = 0, max_hits = 0}48data[name] = file49else50ignored_files[name] = true51return52end53end5455if line_nr > file.max then56file.max = line_nr57end5859local hits = (file[line_nr] or 0) + 160file[line_nr] = hits6162if hits > file.max_hits then63file.max_hits = hits64end6566if runner.tick then67steps_after_save = steps_after_save + 16869if steps_after_save == runner.configuration.savestepsize then70steps_after_save = 07172if not runner.paused then73runner.save_stats()74end75end76end77end78end7980return hook818283