summaryrefslogtreecommitdiff
path: root/thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'thread.c')
-rw-r--r--thread.c41
1 files changed, 36 insertions, 5 deletions
diff --git a/thread.c b/thread.c
index 432a91d331..36af5af49d 100644
--- a/thread.c
+++ b/thread.c
@@ -2116,11 +2116,9 @@ clear_coverage_i(st_data_t key, st_data_t val, st_data_t dummy)
static void
clear_coverage(void)
{
- if (rb_const_defined_at(rb_cObject, rb_intern("COVERAGE__"))) {
- VALUE hash = rb_const_get_at(rb_cObject, rb_intern("COVERAGE__"));
- if (TYPE(hash) == T_HASH) {
- st_foreach(RHASH_TBL(hash), clear_coverage_i, 0);
- }
+ VALUE coverages = rb_get_coverages();
+ if (RTEST(coverages)) {
+ st_foreach(RHASH_TBL(coverages), clear_coverage_i, 0);
}
}
@@ -3530,3 +3528,36 @@ rb_check_deadlock(rb_vm_t *vm)
rb_thread_raise(2, argv, vm->main_thread);
}
}
+
+VALUE
+rb_get_coverages(void)
+{
+ return GET_VM()->coverages;
+}
+
+static void
+update_coverage(rb_event_flag_t event, VALUE proc, VALUE self, ID id, VALUE klass)
+{
+ rb_control_frame_t *cfp = GET_THREAD()->cfp;
+ VALUE coverage = cfp->iseq->coverage;
+ if (coverage) {
+ long line = vm_get_sourceline(cfp) - 1;
+ long count;
+ if (RARRAY_PTR(coverage)[line] == Qnil) {
+ rb_bug("bug");
+ }
+ count = FIX2LONG(RARRAY_PTR(coverage)[line]) + 1;
+ if (POSFIXABLE(count)) {
+ RARRAY_PTR(coverage)[line] = LONG2FIX(count);
+ }
+ }
+}
+
+void
+rb_enable_coverages(void)
+{
+ if (!RTEST(GET_VM()->coverages)) {
+ GET_VM()->coverages = rb_hash_new();
+ rb_add_event_hook(update_coverage, RUBY_EVENT_COVERAGE, Qnil);
+ }
+}