[前][次][番号順一覧][スレッド一覧]

ruby-changes:53961

From: mrkn <ko1@a...>
Date: Tue, 4 Dec 2018 13:22:11 +0900 (JST)
Subject: [ruby-changes:53961] mrkn:r66181 (trunk): Symbol refinements

mrkn	2018-12-04 13:22:06 +0900 (Tue, 04 Dec 2018)

  New Revision: 66181

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66181

  Log:
    Symbol refinements
    
    * vm_args.c (refine_sym_proc_call): enalbe accidentally disabled
      refinements in Symbol#to_proc.   [Bug #15114]
    
    From: osyo (manga osyo) <manga.osyo@g...>

  Modified files:
    trunk/method.h
    trunk/test/ruby/test_symbol.rb
    trunk/vm_args.c
    trunk/vm_method.c
Index: test/ruby/test_symbol.rb
===================================================================
--- test/ruby/test_symbol.rb	(revision 66180)
+++ test/ruby/test_symbol.rb	(revision 66181)
@@ -161,6 +161,32 @@ class TestSymbol < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_symbol.rb#L161
     assert_equal(1, first, bug11594)
   end
 
+  class TestToPRocArgWithRefinements; end
+  def _test_to_proc_arg_with_refinements_call(&block)
+    block.call TestToPRocArgWithRefinements.new
+  end
+  using Module.new {
+    refine TestToPRocArgWithRefinements do
+      def hoge
+        :hoge
+      end
+    end
+  }
+  def test_to_proc_arg_with_refinements
+    assert_equal(:hoge, _test_to_proc_arg_with_refinements_call(&:hoge))
+  end
+
+  using Module.new {
+    refine TestToPRocArgWithRefinements do
+      def hoge
+        :hogehoge
+      end
+    end
+  }
+  def test_to_proc_arg_with_refinements_override
+    assert_equal(:hogehoge, _test_to_proc_arg_with_refinements_call(&:hoge))
+  end
+
   private def return_from_proc
     Proc.new { return 1 }.tap(&:call)
   end
Index: vm_method.c
===================================================================
--- vm_method.c	(revision 66180)
+++ vm_method.c	(revision 66181)
@@ -967,7 +967,7 @@ rb_resolve_refined_method(VALUE refineme https://github.com/ruby/ruby/blob/trunk/vm_method.c#L967
     return resolve_refined_method(refinements, me, NULL);
 }
 
-static const rb_callable_method_entry_t *
+const rb_callable_method_entry_t *
 rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me)
 {
     VALUE defined_class = me->defined_class;
Index: method.h
===================================================================
--- method.h	(revision 66180)
+++ method.h	(revision 66181)
@@ -199,6 +199,7 @@ const rb_method_entry_t *rb_method_entry https://github.com/ruby/ruby/blob/trunk/method.h#L199
 const rb_method_entry_t *rb_method_entry_without_refinements(VALUE klass, ID id, VALUE *defined_class);
 const rb_method_entry_t *rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me);
 RUBY_SYMBOL_EXPORT_BEGIN
+const rb_callable_method_entry_t *rb_resolve_refined_method_callable(VALUE refinements, const rb_callable_method_entry_t *me);
 const rb_method_entry_t *rb_resolve_me_location(const rb_method_entry_t *, VALUE[5]);
 RUBY_SYMBOL_EXPORT_END
 
Index: vm_args.c
===================================================================
--- vm_args.c	(revision 66180)
+++ vm_args.c	(revision 66181)
@@ -851,13 +851,17 @@ refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ https://github.com/ruby/ruby/blob/trunk/vm_args.c#L851
     ID mid;
     const rb_callable_method_entry_t *me;
     rb_execution_context_t *ec;
+    const VALUE symbol = RARRAY_AREF(callback_arg, 0);
+    const VALUE refinements = RARRAY_AREF(callback_arg, 1);
 
     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, NULL);
+
+    mid = SYM2ID(symbol);
+    me = rb_resolve_refined_method_callable(refinements, (const rb_callable_method_entry_t *)rb_method_entry(CLASS_OF(obj), mid));
+
     ec = GET_EC();
     if (!NIL_P(blockarg)) {
 	vm_passed_block_handler_set(ec, blockarg);
@@ -885,12 +889,8 @@ vm_caller_setup_arg_block(const rb_execu https://github.com/ruby/ruby/blob/trunk/vm_args.c#L889
 	    const rb_cref_t *cref = vm_env_cref(reg_cfp->ep);
 	    if (cref && !NIL_P(cref->refinements)) {
 		VALUE ref = cref->refinements;
-		VALUE func = rb_hash_lookup(ref, block_code);
-		if (NIL_P(func)) {
-		    /* TODO: limit cached funcs */
-		    func = rb_func_proc_new(refine_sym_proc_call, block_code);
-		    rb_hash_aset(ref, block_code, func);
-		}
+		VALUE callback_arg = rb_ary_new_from_args(2, block_code, ref);
+		VALUE func = rb_func_proc_new(refine_sym_proc_call, callback_arg);
 		block_code = func;
 	    }
             return block_code;

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]