Skip to content

Commit 35a2939

Browse files
committed
vm_args.c: allow refinements in Symbol proc
* 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] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 69692d4 commit 35a2939

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Sat Oct 15 14:17:05 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
2+
3+
* vm_args.c (refine_sym_proc_call): search and call method with
4+
refinements.
5+
6+
* vm_args.c (vm_caller_setup_arg_block): enable refinements when
7+
enabled in the caller. [Feature #9451]
8+
19
Sat Oct 15 00:54:01 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
210

311
* process.c (proc_exec_cmd): use UTF-8 version aspawn.

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ with all sufficient information, see the ChangeLog file or Redmine
1717
* Multiple assignment in conditional expression is now allowed.
1818
[Feature #10617]
1919

20+
* Refinments is enabled at method by Symbol#to_proc. [Feature #9451]
21+
2022
=== Core classes updates (outstanding ones only)
2123

2224
* Array

test/ruby/test_symbol.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,4 +435,16 @@ def test_not_freeze
435435
assert_equal str, str.to_sym.to_s
436436
assert_not_predicate(str, :frozen?, bug11721)
437437
end
438+
439+
module WithRefinements
440+
using Module.new {refine(Integer) {alias inc succ}}
441+
def mapinc(a)
442+
a.map(&:inc)
443+
end
444+
end
445+
446+
def test_proc_with_refinements
447+
obj = Object.new.extend WithRefinements
448+
assert_equal [*1..3], obj.mapinc(0..2)
449+
end
438450
end

vm_args.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,26 @@ vm_to_proc(VALUE proc)
794794
}
795795
}
796796

797+
static VALUE
798+
refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg))
799+
{
800+
VALUE obj;
801+
ID mid;
802+
const rb_callable_method_entry_t *me;
803+
804+
if (argc-- < 1) {
805+
rb_raise(rb_eArgError, "no receiver given");
806+
}
807+
obj = *argv++;
808+
mid = SYM2ID(callback_arg);
809+
me = rb_callable_method_entry_with_refinements(CLASS_OF(obj), mid);
810+
if (!me) {
811+
/* fallback to funcall (e.g. method_missing) */
812+
return rb_funcall_with_block(obj, mid, argc, argv, blockarg);
813+
}
814+
return vm_call0(GET_THREAD(), obj, mid, argc, argv, me);
815+
}
816+
797817
static void
798818
vm_caller_setup_arg_block(const rb_thread_t *th, rb_control_frame_t *reg_cfp,
799819
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,
806826
}
807827
else {
808828
if (SYMBOL_P(block_code) && rb_method_basic_definition_p(rb_cSymbol, idTo_proc)) {
829+
const rb_cref_t *cref = vm_env_cref(reg_cfp->ep);
830+
if (cref && !NIL_P(cref->refinements)) {
831+
block_code = rb_func_proc_new(refine_sym_proc_call, block_code);
832+
}
809833
calling->block_handler = block_code;
810834
}
811835
else {

0 commit comments

Comments
 (0)