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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
const bun = @import("root").bun;
const std = @import("std");
const LineOffsetTable = bun.sourcemap.LineOffsetTable;
const SourceMap = bun.sourcemap;
const Bitset = bun.bit_set.DynamicBitSetUnmanaged;
const Output = bun.Output;
const prettyFmt = Output.prettyFmt;
/// Our code coverage currently only deals with lines of code, not statements or branches.
/// JSC doesn't expose function names in their coverage data, so we don't include that either :(.
/// Since we only need to store line numbers, our job gets simpler
///
/// We can use two bitsets to store code coverage data for a given file
/// 1. executable_lines
/// 2. lines_which_have_executed
///
/// Not all lines of code are executable. Comments, whitespace, empty lines, etc. are not executable.
/// It's not a problem for anyone if comments, whitespace, empty lines etc are not executed, so those should always be omitted from coverage reports
///
/// We use two bitsets since the typical size will be decently small,
/// bitsets are simple and bitsets are relatively fast to construct and query
///
pub const CodeCoverageReport = struct {
source_url: bun.JSC.ZigString.Slice,
executable_lines: Bitset,
lines_which_have_executed: Bitset,
functions: std.ArrayListUnmanaged(Block),
functions_which_have_executed: Bitset,
stmts_which_have_executed: Bitset,
stmts: std.ArrayListUnmanaged(Block),
total_lines: u32 = 0,
pub const Block = struct {
start_line: u32 = 0,
end_line: u32 = 0,
};
pub fn linesCoverageFraction(this: *const CodeCoverageReport) f64 {
var intersected = this.executable_lines.clone(bun.default_allocator) catch @panic("OOM");
defer intersected.deinit(bun.default_allocator);
intersected.setIntersection(this.lines_which_have_executed);
const total_count: f64 = @floatFromInt(this.executable_lines.count());
if (total_count == 0) {
return 1.0;
}
const intersected_count: f64 = @floatFromInt(intersected.count());
return (intersected_count / total_count);
}
pub fn stmtsCoverageFraction(this: *const CodeCoverageReport) f64 {
const total_count: f64 = @floatFromInt(this.stmts.items.len);
if (total_count == 0) {
return 1.0;
}
return ((@as(f64, @floatFromInt(this.stmts_which_have_executed.count()))) / (total_count));
}
pub fn functionCoverageFraction(this: *const CodeCoverageReport) f64 {
const total_count: f64 = @floatFromInt(this.functions.items.len);
if (total_count == 0) {
return 1.0;
}
return (@as(f64, @floatFromInt(this.functions_which_have_executed.count())) / total_count);
}
pub fn writeFormatWithValues(
filename: []const u8,
max_filename_length: usize,
vals: CoverageFraction,
failing: CoverageFraction,
failed: bool,
writer: anytype,
indent_name: bool,
comptime enable_colors: bool,
) !void {
if (comptime enable_colors) {
if (failed) {
try writer.writeAll(comptime prettyFmt("<r><b><red>", true));
} else {
try writer.writeAll(comptime prettyFmt("<r><b><green>", true));
}
}
if (indent_name) {
try writer.writeAll(" ");
}
try writer.writeAll(filename);
try writer.writeByteNTimes(' ', (max_filename_length - filename.len + @as(usize, @intFromBool(!indent_name))));
try writer.writeAll(comptime prettyFmt("<r><d> | <r>", enable_colors));
if (comptime enable_colors) {
if (vals.functions < failing.functions) {
try writer.writeAll(comptime prettyFmt("<b><red>", true));
} else {
try writer.writeAll(comptime prettyFmt("<b><green>", true));
}
}
try writer.print("{d: >7.2}", .{vals.functions * 100.0});
// try writer.writeAll(comptime prettyFmt("<r><d> | <r>", enable_colors));
// if (comptime enable_colors) {
// // if (vals.stmts < failing.stmts) {
// try writer.writeAll(comptime prettyFmt("<d>", true));
// // } else {
// // try writer.writeAll(comptime prettyFmt("<d>", true));
// // }
// }
// try writer.print("{d: >8.2}", .{vals.stmts * 100.0});
try writer.writeAll(comptime prettyFmt("<r><d> | <r>", enable_colors));
if (comptime enable_colors) {
if (vals.lines < failing.lines) {
try writer.writeAll(comptime prettyFmt("<b><red>", true));
} else {
try writer.writeAll(comptime prettyFmt("<b><green>", true));
}
}
try writer.print("{d: >7.2}", .{vals.lines * 100.0});
}
pub fn writeFormat(
report: *const CodeCoverageReport,
max_filename_length: usize,
fraction: *CoverageFraction,
base_path: []const u8,
writer: anytype,
comptime enable_colors: bool,
) !void {
var failing = fraction.*;
const fns = report.functionCoverageFraction();
const lines = report.linesCoverageFraction();
const stmts = report.stmtsCoverageFraction();
fraction.functions = fns;
fraction.lines = lines;
fraction.stmts = stmts;
const failed = fns < failing.functions or lines < failing.lines; // or stmts < failing.stmts;
fraction.failing = failed;
var filename = report.source_url.slice();
if (base_path.len > 0) {
filename = bun.path.relative(base_path, filename);
}
try writeFormatWithValues(
filename,
max_filename_length,
fraction.*,
failing,
failed,
writer,
true,
enable_colors,
);
try writer.writeAll(comptime prettyFmt("<r><d> | <r>", enable_colors));
var executable_lines_that_havent_been_executed = report.lines_which_have_executed.clone(bun.default_allocator) catch @panic("OOM");
defer executable_lines_that_havent_been_executed.deinit(bun.default_allocator);
executable_lines_that_havent_been_executed.toggleAll();
// This sets statements in executed scopes
executable_lines_that_havent_been_executed.setIntersection(report.executable_lines);
var iter = executable_lines_that_havent_been_executed.iterator(.{});
var start_of_line_range: usize = 0;
var prev_line: usize = 0;
var is_first = true;
while (iter.next()) |next_line| {
if (next_line == (prev_line + 1)) {
prev_line = next_line;
continue;
} else if (is_first and start_of_line_range == 0 and prev_line == 0) {
start_of_line_range = next_line;
prev_line = next_line;
continue;
}
if (is_first) {
is_first = false;
} else {
try writer.print(comptime prettyFmt("<r><d>,<r>", enable_colors), .{});
}
if (start_of_line_range == prev_line) {
try writer.print(comptime prettyFmt("<red>{d}", enable_colors), .{start_of_line_range + 1});
} else {
try writer.print(comptime prettyFmt("<red>{d}-{d}", enable_colors), .{ start_of_line_range + 1, prev_line + 1 });
}
prev_line = next_line;
start_of_line_range = next_line;
}
if (prev_line != start_of_line_range) {
if (is_first) {
is_first = false;
} else {
try writer.print(comptime prettyFmt("<r><d>,<r>", enable_colors), .{});
}
if (start_of_line_range == prev_line) {
try writer.print(comptime prettyFmt("<red>{d}", enable_colors), .{start_of_line_range + 1});
} else {
try writer.print(comptime prettyFmt("<red>{d}-{d}", enable_colors), .{ start_of_line_range + 1, prev_line + 1 });
}
}
}
pub fn deinit(this: *CodeCoverageReport, allocator: std.mem.Allocator) void {
this.executable_lines.deinit(allocator);
this.lines_which_have_executed.deinit(allocator);
this.functions.deinit(allocator);
this.stmts.deinit(allocator);
this.functions_which_have_executed.deinit(allocator);
this.stmts_which_have_executed.deinit(allocator);
}
extern fn CodeCoverage__withBlocksAndFunctions(
*bun.JSC.VM,
i32,
*anyopaque,
bool,
*const fn (
*Generator,
[*]const BasicBlockRange,
usize,
usize,
bool,
) callconv(.C) void,
) bool;
const Generator = struct {
allocator: std.mem.Allocator,
byte_range_mapping: *ByteRangeMapping,
result: *?CodeCoverageReport,
pub fn do(
this: *@This(),
blocks_ptr: [*]const BasicBlockRange,
blocks_len: usize,
function_start_offset: usize,
ignore_sourcemap: bool,
) callconv(.C) void {
const blocks: []const BasicBlockRange = blocks_ptr[0..function_start_offset];
var function_blocks: []const BasicBlockRange = blocks_ptr[function_start_offset..blocks_len];
if (function_blocks.len > 1) {
function_blocks = function_blocks[1..];
}
if (blocks.len == 0) {
return;
}
this.result.* = this.byte_range_mapping.generateCodeCoverageReportFromBlocks(
this.allocator,
this.byte_range_mapping.source_url,
blocks,
function_blocks,
ignore_sourcemap,
) catch null;
}
};
pub fn generate(
globalThis: *bun.JSC.JSGlobalObject,
allocator: std.mem.Allocator,
byte_range_mapping: *ByteRangeMapping,
ignore_sourcemap_: bool,
) ?CodeCoverageReport {
bun.JSC.markBinding(@src());
var vm = globalThis.vm();
var result: ?CodeCoverageReport = null;
var generator = Generator{
.result = &result,
.allocator = allocator,
.byte_range_mapping = byte_range_mapping,
};
if (!CodeCoverage__withBlocksAndFunctions(
vm,
byte_range_mapping.source_id,
&generator,
ignore_sourcemap_,
&Generator.do,
)) {
return null;
}
return result;
}
};
const BasicBlockRange = extern struct {
startOffset: c_int = 0,
endOffset: c_int = 0,
hasExecuted: bool = false,
executionCount: usize = 0,
};
pub const ByteRangeMapping = struct {
line_offset_table: LineOffsetTable.List = .{},
source_id: i32,
source_url: bun.JSC.ZigString.Slice,
pub fn isLessThan(_: void, a: ByteRangeMapping, b: ByteRangeMapping) bool {
return bun.strings.order(a.source_url.slice(), b.source_url.slice()) == .lt;
}
pub const HashMap = std.HashMap(u64, ByteRangeMapping, bun.IdentityContext(u64), std.hash_map.default_max_load_percentage);
pub fn deinit(this: *ByteRangeMapping) void {
this.line_offset_table.deinit(bun.default_allocator);
}
pub threadlocal var map: ?*HashMap = null;
pub fn generate(str: bun.String, source_contents_str: bun.String, source_id: i32) callconv(.C) void {
var _map = map orelse brk: {
map = bun.JSC.VirtualMachine.get().allocator.create(HashMap) catch @panic("OOM");
map.?.* = HashMap.init(bun.JSC.VirtualMachine.get().allocator);
break :brk map.?;
};
var slice = str.toUTF8(bun.default_allocator);
const hash = bun.hash(slice.slice());
var entry = _map.getOrPut(hash) catch @panic("Out of memory");
if (entry.found_existing) {
entry.value_ptr.deinit();
}
var source_contents = source_contents_str.toUTF8(bun.default_allocator);
defer source_contents.deinit();
entry.value_ptr.* = compute(source_contents.slice(), source_id, slice);
}
pub fn getSourceID(this: *ByteRangeMapping) callconv(.C) i32 {
return this.source_id;
}
pub fn find(path: bun.String) callconv(.C) ?*ByteRangeMapping {
var slice = path.toUTF8(bun.default_allocator);
defer slice.deinit();
var map_ = map orelse return null;
const hash = bun.hash(slice.slice());
var entry = map_.getPtr(hash) orelse return null;
return entry;
}
pub fn generateCodeCoverageReportFromBlocks(
this: *ByteRangeMapping,
allocator: std.mem.Allocator,
source_url: bun.JSC.ZigString.Slice,
blocks: []const BasicBlockRange,
function_blocks: []const BasicBlockRange,
ignore_sourcemap: bool,
) !CodeCoverageReport {
var line_starts = this.line_offset_table.items(.byte_offset_to_start_of_line);
var executable_lines: Bitset = Bitset{};
var lines_which_have_executed: Bitset = Bitset{};
const parsed_mappings_ = bun.JSC.VirtualMachine.get().source_mappings.get(
source_url.slice(),
);
var functions = std.ArrayListUnmanaged(CodeCoverageReport.Block){};
try functions.ensureTotalCapacityPrecise(allocator, function_blocks.len);
errdefer functions.deinit(allocator);
var functions_which_have_executed: Bitset = try Bitset.initEmpty(allocator, function_blocks.len);
errdefer functions_which_have_executed.deinit(allocator);
var stmts_which_have_executed: Bitset = try Bitset.initEmpty(allocator, blocks.len);
errdefer stmts_which_have_executed.deinit(allocator);
var stmts = std.ArrayListUnmanaged(CodeCoverageReport.Block){};
try stmts.ensureTotalCapacityPrecise(allocator, function_blocks.len);
errdefer stmts.deinit(allocator);
errdefer executable_lines.deinit(allocator);
errdefer lines_which_have_executed.deinit(allocator);
var line_count: u32 = 0;
if (ignore_sourcemap or parsed_mappings_ == null) {
line_count = @truncate(line_starts.len);
executable_lines = try Bitset.initEmpty(allocator, line_count);
lines_which_have_executed = try Bitset.initEmpty(allocator, line_count);
for (blocks, 0..) |block, i| {
const min: usize = @intCast(@min(block.startOffset, block.endOffset));
const max: usize = @intCast(@max(block.startOffset, block.endOffset));
var min_line: u32 = std.math.maxInt(u32);
var max_line: u32 = 0;
const has_executed = block.hasExecuted or block.executionCount > 0;
for (min..max) |byte_offset| {
const new_line_index = LineOffsetTable.findIndex(line_starts, .{ .start = @intCast(byte_offset) }) orelse continue;
const line_start_byte_offset = line_starts[new_line_index];
if (line_start_byte_offset >= byte_offset) {
continue;
}
const line: u32 = @intCast(new_line_index);
min_line = @min(min_line, line);
max_line = @max(max_line, line);
executable_lines.set(@intCast(new_line_index));
if (has_executed) {
lines_which_have_executed.set(@intCast(new_line_index));
}
}
if (min_line != std.math.maxInt(u32)) {
if (has_executed)
stmts_which_have_executed.set(i);
try stmts.append(allocator, .{
.start_line = min_line,
.end_line = max_line,
});
}
}
for (function_blocks, 0..) |function, i| {
const min: usize = @intCast(@min(function.startOffset, function.endOffset));
const max: usize = @intCast(@max(function.startOffset, function.endOffset));
var min_line: u32 = std.math.maxInt(u32);
var max_line: u32 = 0;
for (min..max) |byte_offset| {
const new_line_index = LineOffsetTable.findIndex(line_starts, .{ .start = @intCast(byte_offset) }) orelse continue;
const line_start_byte_offset = line_starts[new_line_index];
if (line_start_byte_offset >= byte_offset) {
continue;
}
const line: u32 = @intCast(new_line_index);
min_line = @min(min_line, line);
max_line = @max(max_line, line);
}
const did_fn_execute = function.executionCount > 0 or function.hasExecuted;
// only mark the lines as executable if the function has not executed
// functions that have executed have non-executable lines in them and thats fine.
if (!did_fn_execute) {
const end = @min(max_line, line_count);
for (min_line..end) |line| {
executable_lines.set(line);
lines_which_have_executed.unset(line);
}
}
try functions.append(allocator, .{
.start_line = min_line,
.end_line = max_line,
});
if (did_fn_execute)
functions_which_have_executed.set(i);
}
} else if (parsed_mappings_) |parsed_mapping| {
line_count = @as(u32, @truncate(parsed_mapping.input_line_count)) + 1;
executable_lines = try Bitset.initEmpty(allocator, line_count);
lines_which_have_executed = try Bitset.initEmpty(allocator, line_count);
for (blocks, 0..) |block, i| {
const min: usize = @intCast(@min(block.startOffset, block.endOffset));
const max: usize = @intCast(@max(block.startOffset, block.endOffset));
var min_line: u32 = std.math.maxInt(u32);
var max_line: u32 = 0;
const has_executed = block.hasExecuted or block.executionCount > 0;
for (min..max) |byte_offset| {
const new_line_index = LineOffsetTable.findIndex(line_starts, .{ .start = @intCast(byte_offset) }) orelse continue;
const line_start_byte_offset = line_starts[new_line_index];
if (line_start_byte_offset >= byte_offset) {
continue;
}
const column_position = byte_offset -| line_start_byte_offset;
if (SourceMap.Mapping.find(parsed_mapping.mappings, @intCast(new_line_index), @intCast(column_position))) |point| {
if (point.original.lines < 0) continue;
const line: u32 = @as(u32, @intCast(point.original.lines));
executable_lines.set(line);
if (has_executed) {
lines_which_have_executed.set(line);
}
min_line = @min(min_line, line);
max_line = @max(max_line, line);
}
}
if (min_line != std.math.maxInt(u32)) {
try stmts.append(allocator, .{
.start_line = min_line,
.end_line = max_line,
});
if (has_executed)
stmts_which_have_executed.set(i);
}
}
for (function_blocks, 0..) |function, i| {
const min: usize = @intCast(@min(function.startOffset, function.endOffset));
const max: usize = @intCast(@max(function.startOffset, function.endOffset));
var min_line: u32 = std.math.maxInt(u32);
var max_line: u32 = 0;
for (min..max) |byte_offset| {
const new_line_index = LineOffsetTable.findIndex(line_starts, .{ .start = @intCast(byte_offset) }) orelse continue;
const line_start_byte_offset = line_starts[new_line_index];
if (line_start_byte_offset >= byte_offset) {
continue;
}
const column_position = byte_offset -| line_start_byte_offset;
if (SourceMap.Mapping.find(parsed_mapping.mappings, @intCast(new_line_index), @intCast(column_position))) |point| {
if (point.original.lines < 0) continue;
const line: u32 = @as(u32, @intCast(point.original.lines));
min_line = @min(min_line, line);
max_line = @max(max_line, line);
}
}
// no sourcemaps? ignore it
if (min_line == std.math.maxInt(u32) and max_line == 0) {
continue;
}
const did_fn_execute = function.executionCount > 0 or function.hasExecuted;
// only mark the lines as executable if the function has not executed
// functions that have executed have non-executable lines in them and thats fine.
if (!did_fn_execute) {
const end = @min(max_line, line_count);
for (min_line..end) |line| {
executable_lines.set(line);
lines_which_have_executed.unset(line);
}
}
try functions.append(allocator, .{
.start_line = min_line,
.end_line = max_line,
});
if (did_fn_execute)
functions_which_have_executed.set(i);
}
} else {
unreachable;
}
return CodeCoverageReport{
.source_url = source_url,
.functions = functions,
.executable_lines = executable_lines,
.lines_which_have_executed = lines_which_have_executed,
.total_lines = line_count,
.stmts = stmts,
.functions_which_have_executed = functions_which_have_executed,
.stmts_which_have_executed = stmts_which_have_executed,
};
}
pub fn findExecutedLines(
globalThis: *bun.JSC.JSGlobalObject,
source_url: bun.String,
blocks_ptr: [*]const BasicBlockRange,
blocks_len: usize,
function_start_offset: usize,
ignore_sourcemap: bool,
) callconv(.C) bun.JSC.JSValue {
var this = ByteRangeMapping.find(source_url) orelse return bun.JSC.JSValue.null;
const blocks: []const BasicBlockRange = blocks_ptr[0..function_start_offset];
var function_blocks: []const BasicBlockRange = blocks_ptr[function_start_offset..blocks_len];
if (function_blocks.len > 1) {
function_blocks = function_blocks[1..];
}
var url_slice = source_url.toUTF8(bun.default_allocator);
defer url_slice.deinit();
var report = this.generateCodeCoverageReportFromBlocks(bun.default_allocator, url_slice, blocks, function_blocks, ignore_sourcemap) catch {
globalThis.throwOutOfMemory();
return .zero;
};
defer report.deinit(bun.default_allocator);
var coverage_fraction = CoverageFraction{};
var mutable_str = bun.MutableString.initEmpty(bun.default_allocator);
defer mutable_str.deinit();
var buffered_writer = mutable_str.bufferedWriter();
var writer = buffered_writer.writer();
report.writeFormat(source_url.utf8ByteLength(), &coverage_fraction, "", &writer, false) catch {
globalThis.throwOutOfMemory();
return .zero;
};
buffered_writer.flush() catch {
globalThis.throwOutOfMemory();
return .zero;
};
var str = bun.String.create(mutable_str.toOwnedSliceLeaky());
defer str.deref();
return str.toJS(globalThis);
}
pub fn compute(source_contents: []const u8, source_id: i32, source_url: bun.JSC.ZigString.Slice) ByteRangeMapping {
return ByteRangeMapping{
.line_offset_table = LineOffsetTable.generate(bun.JSC.VirtualMachine.get().allocator, source_contents, 0),
.source_id = source_id,
.source_url = source_url,
};
}
};
comptime {
@export(ByteRangeMapping.generate, .{ .name = "ByteRangeMapping__generate" });
@export(ByteRangeMapping.findExecutedLines, .{ .name = "ByteRangeMapping__findExecutedLines" });
@export(ByteRangeMapping.find, .{ .name = "ByteRangeMapping__find" });
@export(ByteRangeMapping.getSourceID, .{ .name = "ByteRangeMapping__getSourceID" });
}
pub const CoverageFraction = struct {
functions: f64 = 0.9,
lines: f64 = 0.9,
// This metric is less accurate right now
stmts: f64 = 0.75,
failing: bool = false,
};
|