* eval.c (rb_mod_refine), vm_eval.c (rb_yield_refine_block):

Module#refine activates all refinements defined in that module
  only in a given block.

* string.c (sym_to_proc, sym_call): don't use refinements.

* test/ruby/test_refinement.rb: related test.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38269 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shugo 2012-12-08 02:37:16 +00:00
parent 3adc9834d1
commit db051011d6
6 changed files with 162 additions and 99 deletions

View file

@ -14,8 +14,7 @@
#include "ruby/ruby.h"
#include "ruby/re.h"
#include "ruby/encoding.h"
#include "node.h"
#include "eval_intern.h"
#include "vm_core.h"
#include "internal.h"
#include "probes.h"
#include <assert.h>
@ -7862,18 +7861,15 @@ sym_to_sym(VALUE sym)
}
static VALUE
sym_call(VALUE args, VALUE p, int argc, VALUE *argv)
sym_call(VALUE args, VALUE sym, int argc, VALUE *argv)
{
VALUE obj;
NODE *memo = RNODE(p);
if (argc < 1) {
rb_raise(rb_eArgError, "no receiver given");
}
obj = argv[0];
return rb_funcall_passing_block_with_refinements(obj, (ID) memo->u1.id,
argc - 1, argv + 1,
memo->u2.value);
return rb_funcall_passing_block(obj, (ID)sym, argc - 1, argv + 1);
}
/*
@ -7893,32 +7889,25 @@ sym_to_proc(VALUE sym)
VALUE proc;
long id, index;
VALUE *aryp;
const NODE *cref = rb_vm_cref();
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
rb_gc_register_mark_object(sym_proc_cache);
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
}
id = SYM2ID(sym);
if (NIL_P(cref->nd_refinements)) {
if (!sym_proc_cache) {
sym_proc_cache = rb_ary_tmp_new(SYM_PROC_CACHE_SIZE * 2);
rb_gc_register_mark_object(sym_proc_cache);
rb_ary_store(sym_proc_cache, SYM_PROC_CACHE_SIZE*2 - 1, Qnil);
}
index = (id % SYM_PROC_CACHE_SIZE) << 1;
index = (id % SYM_PROC_CACHE_SIZE) << 1;
aryp = RARRAY_PTR(sym_proc_cache);
if (aryp[index] == sym) {
return aryp[index + 1];
}
else {
proc = rb_proc_new(sym_call,
(VALUE) NEW_MEMO(id, Qnil, 0));
aryp[index] = sym;
aryp[index + 1] = proc;
return proc;
}
aryp = RARRAY_PTR(sym_proc_cache);
if (aryp[index] == sym) {
return aryp[index + 1];
}
else {
return rb_proc_new(sym_call,
(VALUE) NEW_MEMO(id, cref->nd_refinements, 0));
proc = rb_proc_new(sym_call, (VALUE)id);
aryp[index] = sym;
aryp[index + 1] = proc;
return proc;
}
}