ruby/ext/coverage/coverage.c

133 строки
3.1 KiB
C
Исходник Обычный вид История

/************************************************
coverage.c -
$Author: $
Copyright (c) 2008 Yusuke Endoh
************************************************/
#include "ruby.h"
#include "vm_core.h"
static VALUE rb_coverages = Qundef;
/*
* call-seq:
* Coverage.start => nil
*
* Enables coverage measurement.
*/
static VALUE
rb_coverage_start(VALUE klass)
{
if (!RTEST(rb_get_coverages())) {
if (rb_coverages == Qundef) {
rb_coverages = rb_hash_new();
* include/ruby/ruby.h: constify RBasic::klass and add RBASIC_CLASS(obj) macro which returns a class of `obj'. This change is a part of RGENGC branch [ruby-trunk - Feature #8339]. * object.c: add new function rb_obj_reveal(). This function reveal interal (hidden) object by rb_obj_hide(). Note that do not change class before and after hiding. Only permitted example is: klass = RBASIC_CLASS(obj); rb_obj_hide(obj); .... rb_obj_reveal(obj, klass); TODO: API design. rb_obj_reveal() should be replaced with others. TODO: modify constified variables using cast may be harmful for compiler's analysis and optimizaton. Any idea to prohibt inserting RBasic::klass directly? If rename RBasic::klass and force to use RBASIC_CLASS(obj), then all codes such as `RBASIC(obj)->klass' will be compilation error. Is it acceptable? (We have similar experience at Ruby 1.9, for example "RARRAY(ary)->ptr" to "RARRAY_PTR(ary)". * internal.h: add some macros. * RBASIC_CLEAR_CLASS(obj) clear RBasic::klass to make it internal object. * RBASIC_SET_CLASS(obj, cls) set RBasic::klass. * RBASIC_SET_CLASS_RAW(obj, cls) same as RBASIC_SET_CLASS without write barrier (planned). * RCLASS_SET_SUPER(a, b) set super class of a. * array.c, class.c, compile.c, encoding.c, enum.c, error.c, eval.c, file.c, gc.c, hash.c, io.c, iseq.c, marshal.c, object.c, parse.y, proc.c, process.c, random.c, ruby.c, sprintf.c, string.c, thread.c, transcode.c, vm.c, vm_eval.c, win32/file.c: Use above macros and functions to access RBasic::klass. * ext/coverage/coverage.c, ext/readline/readline.c, ext/socket/ancdata.c, ext/socket/init.c, * ext/zlib/zlib.c: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-13 14:49:11 +04:00
rb_obj_hide(rb_coverages);
}
rb_set_coverages(rb_coverages);
}
return Qnil;
}
static int
coverage_clear_result_i(st_data_t key, st_data_t val, st_data_t h)
{
VALUE coverage = (VALUE)val;
rb_ary_clear(coverage);
return ST_CONTINUE;
}
static int
coverage_peek_result_i(st_data_t key, st_data_t val, st_data_t h)
{
VALUE path = (VALUE)key;
VALUE coverage = (VALUE)val;
VALUE coverages = (VALUE)h;
coverage = rb_ary_dup(coverage);
rb_ary_freeze(coverage);
rb_hash_aset(coverages, path, coverage);
return ST_CONTINUE;
}
/*
* call-seq:
* Coverage.peek_result => hash
*
* Returns a hash that contains filename as key and coverage array as value.
*/
static VALUE
rb_coverage_peek_result(VALUE klass)
{
VALUE coverages = rb_get_coverages();
VALUE ncoverages = rb_hash_new();
if (!RTEST(coverages)) {
rb_raise(rb_eRuntimeError, "coverage measurement is not enabled");
}
st_foreach(RHASH_TBL(coverages), coverage_peek_result_i, ncoverages);
rb_hash_freeze(ncoverages);
return ncoverages;
}
/*
* call-seq:
* Coverage.result => hash
*
* Returns a hash that contains filename as key and coverage array as value
* and disables coverage measurement.
*/
static VALUE
rb_coverage_result(VALUE klass)
{
VALUE ncoverages = rb_coverage_peek_result(klass);
VALUE coverages = rb_get_coverages();
st_foreach(RHASH_TBL(coverages), coverage_clear_result_i, ncoverages);
rb_reset_coverages();
return ncoverages;
}
/* Coverage provides coverage measurement feature for Ruby.
* This feature is experimental, so these APIs may be changed in future.
*
* = Usage
*
* 1. require "coverage.so"
* 2. do Coverage.start
* 3. require or load Ruby source file
* 4. Coverage.result will return a hash that contains filename as key and
* coverage array as value. A coverage array gives, for each line, the
* number of line execution by the interpreter. A +nil+ value means
* coverage is disabled for this line (lines like +else+ and +end+).
*
* = Example
*
* [foo.rb]
* s = 0
* 10.times do |x|
* s += x
* end
*
* if s == 45
* p :ok
* else
* p :ng
* end
* [EOF]
*
* require "coverage.so"
* Coverage.start
* require "foo.rb"
* p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
*/
void
Init_coverage(void)
{
VALUE rb_mCoverage = rb_define_module("Coverage");
rb_define_module_function(rb_mCoverage, "start", rb_coverage_start, 0);
rb_define_module_function(rb_mCoverage, "result", rb_coverage_result, 0);
rb_define_module_function(rb_mCoverage, "peek_result", rb_coverage_peek_result, 0);
rb_gc_register_address(&rb_coverages);
}