ruby-changes:6341
From: mame <ko1@a...>
Date: Thu, 3 Jul 2008 21:55:43 +0900 (JST)
Subject: [ruby-changes:6341] Ruby:r17857 (trunk): * ext/coverage/coverage.c, ext/coverage/extconf.rb: eliminate
mame 2008-07-03 21:55:12 +0900 (Thu, 03 Jul 2008) New Revision: 17857 Added directories: trunk/ext/coverage/ Added files: trunk/ext/coverage/coverage.c trunk/ext/coverage/extconf.rb Modified files: trunk/ChangeLog trunk/include/ruby/intern.h trunk/insns.def trunk/iseq.c trunk/lib/coverage.rb trunk/parse.y trunk/thread.c trunk/vm.c trunk/vm_core.h Log: * ext/coverage/coverage.c, ext/coverage/extconf.rb: eliminate COVERAGE__ and introduce coverage.so instead. How to measure coverage: (1) require "coverage.so", (2) require or load Ruby source file, and (3) Coverage.result will return the same hash as COVERAGE__. [ruby-dev:35324] * thread.c (rb_enable_coverages): start coverage measurement by using rb_add_event_hook. * thread.c (rb_get_coverages): returns current results of coverage measurement. * include/ruby/intern.h: add prototype for above two functions. * vm_core.h, vm.c: add field of coverages to rb_vm_t. * insns.def (trace): remove special handling for COVERAGE__. * iseq.c (prepare_iseq_build): switch COVERAGE__ to rb_get_coverages(). * parse.y (coverage): ditto. * thread.c (clear_coverage): ditto. * lib/coverage.rb: use coverage.so instead of COVERAGE__. Added: trunk/ext/coverage/ http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=17857 Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 17856) +++ include/ruby/intern.h (revision 17857) @@ -626,6 +626,8 @@ VALUE rb_barrier_new(void); VALUE rb_barrier_wait(VALUE self); VALUE rb_barrier_release(VALUE self); +VALUE rb_get_coverages(void); +void rb_enable_coverages(void); /* time.c */ VALUE rb_time_new(time_t, long); VALUE rb_time_nano_new(time_t, long); Index: insns.def =================================================================== --- insns.def (revision 17856) +++ insns.def (revision 17857) @@ -853,23 +853,7 @@ { rb_event_flag_t flag = nf; - if (flag == RUBY_EVENT_COVERAGE) { - VALUE coverage = GET_ISEQ()->coverage; - if (coverage) { - long line = vm_get_sourceline(GET_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); - } - } - } - else { - EXEC_EVENT_HOOK(th, flag, GET_SELF(), 0, 0 /* TODO: id, klass */); - } + EXEC_EVENT_HOOK(th, flag, GET_SELF(), 0, 0 /* TODO: id, klass */); } /**********************************************************/ Index: ChangeLog =================================================================== --- ChangeLog (revision 17856) +++ ChangeLog (revision 17857) @@ -1,3 +1,32 @@ +Thu Jul 3 21:51:21 2008 Yusuke Endoh <mame@t...> + + * ext/coverage/coverage.c, ext/coverage/extconf.rb: eliminate + COVERAGE__ and introduce coverage.so instead. How to measure + coverage: (1) require "coverage.so", (2) require or load Ruby source + file, and (3) Coverage.result will return the same hash as COVERAGE__. + [ruby-dev:35324] + + * thread.c (rb_enable_coverages): start coverage measurement by using + rb_add_event_hook. + + * thread.c (rb_get_coverages): returns current results of coverage + measurement. + + * include/ruby/intern.h: add prototype for above two functions. + + * vm_core.h, vm.c: add field of coverages to rb_vm_t. + + * insns.def (trace): remove special handling for COVERAGE__. + + * iseq.c (prepare_iseq_build): switch COVERAGE__ to + rb_get_coverages(). + + * parse.y (coverage): ditto. + + * thread.c (clear_coverage): ditto. + + * lib/coverage.rb: use coverage.so instead of COVERAGE__. + Thu Jul 3 21:20:45 2008 Yusuke Endoh <mame@t...> * thread.c (thread_initialize): NUM2INT returns long. Index: vm_core.h =================================================================== --- vm_core.h (revision 17856) +++ vm_core.h (revision 17857) @@ -333,6 +333,7 @@ int src_encoding_index; VALUE verbose, debug, progname; + VALUE coverages; #if defined(ENABLE_VM_OBJSPACE) && ENABLE_VM_OBJSPACE struct rb_objspace *objspace; Index: iseq.c =================================================================== --- iseq.c (revision 17856) +++ iseq.c (revision 17857) @@ -194,12 +194,10 @@ iseq->coverage = Qfalse; if (!GET_THREAD()->parse_in_eval) { - 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) { - iseq->coverage = rb_hash_aref(hash, filename); - if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse; - } + VALUE coverages = rb_get_coverages(); + if (RTEST(coverages)) { + iseq->coverage = rb_hash_aref(coverages, filename); + if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse; } } Index: lib/coverage.rb =================================================================== --- lib/coverage.rb (revision 17856) +++ lib/coverage.rb (revision 17857) @@ -1,4 +1,5 @@ -COVERAGE__ ||= {} +require "coverage.so" + ext = ENV["COVERUBY_EXT"] || ".cov" accum = ENV["COVERUBY_ACCUM"] accum = !accum || accum == "" || !(%w(f n 0).include?(accum[0])) @@ -6,7 +7,7 @@ at_exit do Dir.chdir(pwd) do - COVERAGE__.each do |sfile, covs| + Coverage.result.each do |sfile, covs| cfile = sfile + ext writable = proc do |f| Index: thread.c =================================================================== --- thread.c (revision 17856) +++ thread.c (revision 17857) @@ -2116,11 +2116,9 @@ 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_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); + } +} Index: parse.y =================================================================== --- parse.y (revision 17856) +++ parse.y (revision 17857) @@ -4672,17 +4672,15 @@ static VALUE coverage(const char *f, int n) { - 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) { - VALUE fname = rb_str_new2(f); - VALUE lines = rb_ary_new2(n); - int i; - for (i = 0; i < n; i++) RARRAY_PTR(lines)[i] = Qnil; - RARRAY(lines)->len = n; - rb_hash_aset(hash, fname, lines); - return lines; - } + VALUE coverages = rb_get_coverages(); + if (RTEST(coverages)) { + VALUE fname = rb_str_new2(f); + VALUE lines = rb_ary_new2(n); + int i; + for (i = 0; i < n; i++) RARRAY_PTR(lines)[i] = Qnil; + RARRAY(lines)->len = n; + rb_hash_aset(coverages, fname, lines); + return lines; } return 0; } Index: ext/coverage/extconf.rb =================================================================== --- ext/coverage/extconf.rb (revision 0) +++ ext/coverage/extconf.rb (revision 17857) @@ -0,0 +1,2 @@ +require 'mkmf' +create_makefile('coverage') Property changes on: ext/coverage/extconf.rb ___________________________________________________________________ Name: svn:eol-style + LF Index: ext/coverage/coverage.c =================================================================== --- ext/coverage/coverage.c (revision 0) +++ ext/coverage/coverage.c (revision 17857) @@ -0,0 +1,11 @@ +#include "ruby.h" + +VALUE rb_mCoverage; + +void +Init_coverage(void) +{ + rb_enable_coverages(); + rb_mCoverage = rb_define_module("Coverage"); + rb_define_module_function(rb_mCoverage, "result", rb_get_coverages, 0); +} Property changes on: ext/coverage/coverage.c ___________________________________________________________________ Name: svn:eol-style + LF Index: vm.c =================================================================== --- vm.c (revision 17856) +++ vm.c (revision 17857) @@ -1395,6 +1395,7 @@ RUBY_MARK_UNLESS_NULL(vm->load_path); RUBY_MARK_UNLESS_NULL(vm->loaded_features); RUBY_MARK_UNLESS_NULL(vm->top_self); + RUBY_MARK_UNLESS_NULL(vm->coverages); rb_gc_mark_locations(vm->special_exceptions, vm->special_exceptions + ruby_special_error_count - 1); if (vm->loading_table) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/