Permalink
Browse files

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
  • Loading branch information...
1 parent 69692d4 commit 35a29390197750abf97ef16fa0740e377764daef @nobu nobu committed Oct 15, 2016
Showing with 46 additions and 0 deletions.
  1. +8 −0 ChangeLog
  2. +2 −0 NEWS
  3. +12 −0 test/ruby/test_symbol.rb
  4. +24 −0 vm_args.c
View
@@ -1,3 +1,11 @@
+Sat Oct 15 14:17:05 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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 <nobu@ruby-lang.org>
* process.c (proc_exec_cmd): use UTF-8 version aspawn.
View
@@ -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
@@ -435,4 +435,16 @@ def test_not_freeze
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
View
@@ -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 {

0 comments on commit 35a2939

Please sign in to comment.