Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/CLI/src/Coverage.cpp
2725 views
1
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
2
#include "Luau/Coverage.h"
3
4
#include "lua.h"
5
6
#include <string>
7
#include <vector>
8
9
struct Coverage
10
{
11
lua_State* L = nullptr;
12
std::vector<int> functions;
13
} gCoverage;
14
15
void coverageInit(lua_State* L)
16
{
17
gCoverage.L = lua_mainthread(L);
18
}
19
20
bool coverageActive()
21
{
22
return gCoverage.L != nullptr;
23
}
24
25
void coverageTrack(lua_State* L, int funcindex)
26
{
27
int ref = lua_ref(L, funcindex);
28
gCoverage.functions.push_back(ref);
29
}
30
31
static void coverageCallback(void* context, const char* function, int linedefined, int depth, const int* hits, size_t size)
32
{
33
FILE* f = static_cast<FILE*>(context);
34
35
std::string name;
36
37
if (depth == 0)
38
name = "<main>";
39
else if (function)
40
name = std::string(function) + ":" + std::to_string(linedefined);
41
else
42
name = "<anonymous>:" + std::to_string(linedefined);
43
44
fprintf(f, "FN:%d,%s\n", linedefined, name.c_str());
45
46
for (size_t i = 0; i < size; ++i)
47
if (hits[i] != -1)
48
{
49
fprintf(f, "FNDA:%d,%s\n", hits[i], name.c_str());
50
break;
51
}
52
53
for (size_t i = 0; i < size; ++i)
54
if (hits[i] != -1)
55
fprintf(f, "DA:%d,%d\n", int(i), hits[i]);
56
}
57
58
void coverageDump(const char* path)
59
{
60
lua_State* L = gCoverage.L;
61
62
FILE* f = fopen(path, "wb");
63
if (!f)
64
{
65
fprintf(stderr, "Error opening coverage %s\n", path);
66
return;
67
}
68
69
fprintf(f, "TN:\n");
70
71
for (int fref : gCoverage.functions)
72
{
73
lua_getref(L, fref);
74
75
lua_Debug ar = {};
76
lua_getinfo(L, -1, "s", &ar);
77
78
fprintf(f, "SF:%s\n", ar.short_src);
79
lua_getcoverage(L, -1, f, coverageCallback);
80
fprintf(f, "end_of_record\n");
81
82
lua_pop(L, 1);
83
}
84
85
fclose(f);
86
87
printf("Coverage dump written to %s (%d functions)\n", path, int(gCoverage.functions.size()));
88
}
89
90