diff --git a/ChangeLog b/ChangeLog index 80600c703a..7a54dcda08 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Sat Oct 15 14:17:05 2016 Nobuyoshi Nakada + + * vm_args.c (refine_sym_proc_call): search and call method with + refinements. + + * vm_args.c (vm_caller_setup_arg_block): enable refinements when + enabled in the caller. [Feature #9451] + Sat Oct 15 00:54:01 2016 Nobuyoshi Nakada * process.c (proc_exec_cmd): use UTF-8 version aspawn. diff --git a/NEWS b/NEWS index 73b5bd21a2..26989c333b 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,8 @@ with all sufficient information, see the ChangeLog file or Redmine * Multiple assignment in conditional expression is now allowed. [Feature #10617] +* Refinments is enabled at method by Symbol#to_proc. [Feature #9451] + === Core classes updates (outstanding ones only) * Array diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index d8c91c1eea..a135338edb 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -435,4 +435,16 @@ class TestSymbol < Test::Unit::TestCase assert_equal str, str.to_sym.to_s assert_not_predicate(str, :frozen?, bug11721) end + + module WithRefinements + using Module.new {refine(Integer) {alias inc succ}} + def mapinc(a) + a.map(&:inc) + end + end + + def test_proc_with_refinements + obj = Object.new.extend WithRefinements + assert_equal [*1..3], obj.mapinc(0..2) + end end diff --git a/vm_args.c b/vm_args.c index 5b7c27b4a8..4a586c9643 100644 --- a/vm_args.c +++ b/vm_args.c @@ -794,6 +794,26 @@ vm_to_proc(VALUE proc) } } +static VALUE +refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)) +{ + VALUE obj; + ID mid; + const rb_callable_method_entry_t *me; + + if (argc-- < 1) { + rb_raise(rb_eArgError, "no receiver given"); + } + obj = *argv++; + mid = SYM2ID(callback_arg); + me = rb_callable_method_entry_with_refinements(CLASS_OF(obj), mid); + if (!me) { + /* fallback to funcall (e.g. method_missing) */ + return rb_funcall_with_block(obj, mid, argc, argv, blockarg); + } + return vm_call0(GET_THREAD(), obj, mid, argc, argv, me); +} + static void vm_caller_setup_arg_block(const rb_thread_t *th, rb_control_frame_t *reg_cfp, struct rb_calling_info *calling, const struct rb_call_info *ci, rb_iseq_t *blockiseq, const int is_super) @@ -806,6 +826,10 @@ vm_caller_setup_arg_block(const rb_thread_t *th, rb_control_frame_t *reg_cfp, } else { if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) { + const rb_cref_t *cref = vm_env_cref(reg_cfp->ep); + if (cref && !NIL_P(cref->refinements)) { + block_code = rb_func_proc_new(refine_sym_proc_call, block_code); + } calling->block_handler = block_code; } else {