From 188b67397315283a86388531f87bac4384a5af00 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 18 Dec 2018 13:59:46 +0000 Subject: Enable refinements on symbol-proc in ruby-level methods * vm_args.c (refine_sym_proc_call): resolve refinements when the proc is invoked, instead of resolving at making the proc, to enable refinements on symbol-proc in ruby-level methods * vm.c (vm_cref_dup): clear cached symbol-procs when duplicating. [Bug #15114] [Fix GH-2039] From: manga_osyo git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66439 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- method.h | 1 + test/ruby/test_symbol.rb | 30 ++++++++++++++++++++++++++++++ vm.c | 10 +++++++++- vm_args.c | 12 +++++++++--- vm_method.c | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/method.h b/method.h index bea630f4e2..771e77e889 100644 --- a/method.h +++ b/method.h @@ -199,6 +199,7 @@ const rb_method_entry_t *rb_method_entry(VALUE klass, ID id); 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 diff --git a/test/ruby/test_symbol.rb b/test/ruby/test_symbol.rb index 36cbf4a710..6438ec8771 100644 --- a/test/ruby/test_symbol.rb +++ b/test/ruby/test_symbol.rb @@ -161,6 +161,36 @@ class TestSymbol < Test::Unit::TestCase 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 + + def self._test_to_proc_arg_with_refinements_call(&block) + block.call TestToPRocArgWithRefinements.new + end + _test_to_proc_arg_with_refinements_call(&:hoge) + 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 diff --git a/vm.c b/vm.c index 7b79ca9890..d5434697b2 100644 --- a/vm.c +++ b/vm.c @@ -229,6 +229,12 @@ vm_cref_new_use_prev(VALUE klass, rb_method_visibility_t visi, int module_func, return vm_cref_new0(klass, visi, module_func, prev_cref, pushed_by_eval, TRUE); } +static int +ref_delete_symkey(VALUE key, VALUE value, VALUE unused) +{ + return SYMBOL_P(key) ? ST_DELETE : ST_CONTINUE; +} + static rb_cref_t * vm_cref_dup(const rb_cref_t *cref) { @@ -240,7 +246,9 @@ vm_cref_dup(const rb_cref_t *cref) new_cref = vm_cref_new(klass, visi->method_visi, visi->module_func, next_cref, pushed_by_eval); if (!NIL_P(CREF_REFINEMENTS(cref))) { - CREF_REFINEMENTS_SET(new_cref, rb_hash_dup(CREF_REFINEMENTS(cref))); + VALUE ref = rb_hash_dup(CREF_REFINEMENTS(cref)); + rb_hash_foreach(ref, ref_delete_symkey, Qnil); + CREF_REFINEMENTS_SET(new_cref, ref); CREF_OMOD_SHARED_UNSET(new_cref); } diff --git a/vm_args.c b/vm_args.c index 16284c0f33..af192da979 100644 --- a/vm_args.c +++ b/vm_args.c @@ -851,13 +851,18 @@ refine_sym_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)) 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_callable_method_entry(CLASS_OF(obj), mid); + me = rb_resolve_refined_method_callable(refinements, me); + ec = GET_EC(); if (!NIL_P(blockarg)) { vm_passed_block_handler_set(ec, blockarg); @@ -888,7 +893,8 @@ vm_caller_setup_arg_block(const rb_execution_context_t *ec, rb_control_frame_t * 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); + VALUE callback_arg = rb_ary_new_from_args(2, block_code, ref); + func = rb_func_proc_new(refine_sym_proc_call, callback_arg); rb_hash_aset(ref, block_code, func); } block_code = func; diff --git a/vm_method.c b/vm_method.c index ffe6f12166..8fe5c0868f 100644 --- a/vm_method.c +++ b/vm_method.c @@ -973,7 +973,7 @@ rb_resolve_refined_method(VALUE refinements, const rb_method_entry_t *me) 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; -- cgit v1.2.3