summaryrefslogtreecommitdiff
path: root/src/luacov.lua
blob: 166c8be59d239080bf706d854ffd60fe1747fe41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

module("luacov", package.seeall)

local stats = require("luacov.stats")

data = stats.load()

statsfile = stats.start()

tick = package.loaded["luacov.tick"]
ctr = 0

local function on_line(_, line_nr)
   if tick then
      ctr = ctr + 1
      if ctr == 100 then
         ctr = 0
         stats.save(data, statsfile)
      end
   end

   local name = debug.getinfo(2, "S").source
   if name:match("^@") then
      name = name:sub(2)
      local file = data[name]
      if not file then
         file = {}
         file.max = 0
         data[name] = file
      end
      if line_nr > file.max then
         file.max = line_nr
      end
      local current = file[line_nr]
      if not current then
         file[line_nr] = 1
      else
         file[line_nr] = current + 1
      end
   end
end

local luacovlock = os.tmpname()

function on_exit()
   os.remove(luacovlock)
   stats.save(data, statsfile)
   stats.stop(statsfile)
end

if not tick then
   on_exit_trick = io.open(luacovlock, "w")
   debug.setmetatable(on_exit_trick, { __gc = on_exit } )
end

debug.sethook(on_line, "l")

rawcoroutinecreate = coroutine.create

function coroutine.create(...)
  local co = rawcoroutinecreate(...)
  debug.sethook(co, on_line, "l")
  return co
end

function coroutine.wrap(...)
  local co = rawcoroutinecreate(...)
  debug.sethook(co, on_line, "l")
  return function()
    local r = { coroutine.resume(co) }
    if not r[1] then
      error(r[2])
    end
    return unpack(r, 2)
  end
end


local rawexit = os.exit
function os.exit(...)
  on_exit()
  rawexit(...)
end