diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-18 02:27:13 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-18 02:27:13 +0000 |
commit | e994503d851aa18c010dee6a1eb718c14a97276c (patch) | |
tree | 6889f0e7778c43813320ce40dadf72818d643309 | |
parent | a0d40a1de2d5e5ee0c121e6b6aded91b3d6c3a25 (diff) | |
download | ruby-e994503d851aa18c010dee6a1eb718c14a97276c.tar.gz |
refinements in string interpolation
* compile.c (iseq_compile_each0): insert to_s method call, so that
refinements activated at the caller should take place.
[Feature #13812]
* insns.def (tostring): fix up converted object to a string,
infect and fallback.
* insns.def (branchiftype): new instruction for conversion.
branches if TOS is an instance of the given type.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59950 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | compile.c | 8 | ||||
-rw-r--r-- | insns.def | 22 | ||||
-rw-r--r-- | string.c | 8 | ||||
-rw-r--r-- | test/ruby/test_refinement.rb | 13 |
5 files changed, 51 insertions, 2 deletions
@@ -18,6 +18,8 @@ with all sufficient information, see the ChangeLog file or Redmine * rescue/else/ensure are allowed inside do/end blocks. [Feature #12906] +* refinements take place in string interpolations. [Feature #13812] + === Core classes updates (outstanding ones only) * Array @@ -6180,7 +6180,15 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, NODE *node, int popp ADD_INSN(ret, line, pop); } else { + const unsigned int flag = VM_CALL_FCALL; + LABEL *isstr = NEW_LABEL(line); + ADD_INSN(ret, line, dup); + ADD_INSN2(ret, line, branchiftype, INT2FIX(T_STRING), isstr); + LABEL_REF(isstr); + ADD_INSN(ret, line, dup); + ADD_SEND_R(ret, line, idTo_s, INT2FIX(0), NULL, INT2FIX(flag), NULL); ADD_INSN(ret, line, tostring); + ADD_LABEL(ret, isstr); } break; } @@ -356,10 +356,11 @@ concatstrings DEFINE_INSN tostring () -(VALUE val) +(VALUE val, VALUE str) (VALUE val) { - val = rb_obj_as_string(val); + VALUE rb_obj_as_string_result(VALUE str, VALUE obj); + val = rb_obj_as_string_result(str, val); } /** @@ -1038,6 +1039,23 @@ branchnil } } +/** + @c jump + @e if val is type, set PC to (PC + dst). + @j もし val が type ならば、PC を (PC + dst) にする。 + */ +DEFINE_INSN +branchiftype +(rb_num_t type, OFFSET dst) +(VALUE val) +() +{ + if (TYPE(val) == (int)type) { + RUBY_VM_CHECK_INTS(th); + JUMP(dst); + } +} + /**********************************************************/ /* for optimize */ @@ -1404,6 +1404,8 @@ str_shared_replace(VALUE str, VALUE str2) } } +VALUE rb_obj_as_string_result(VALUE str, VALUE obj); + VALUE rb_obj_as_string(VALUE obj) { @@ -1413,6 +1415,12 @@ rb_obj_as_string(VALUE obj) return obj; } str = rb_funcall(obj, idTo_s, 0); + return rb_obj_as_string_result(str, obj); +} + +VALUE +rb_obj_as_string_result(VALUE str, VALUE obj) +{ if (!RB_TYPE_P(str, T_STRING)) return rb_any_to_s(obj); if (!FL_TEST_RAW(str, RSTRING_FSTR) && FL_ABLE(obj)) diff --git a/test/ruby/test_refinement.rb b/test/ruby/test_refinement.rb index d79eb2b187..31925f3933 100644 --- a/test/ruby/test_refinement.rb +++ b/test/ruby/test_refinement.rb @@ -1961,6 +1961,19 @@ class TestRefinement < Test::Unit::TestCase end end + class ToString + c = self + using Module.new {refine(c) {def to_s; "ok"; end}} + def string + "#{self}" + end + end + + def test_tostring + assert_equal("ok", ToString.new.string) + assert_predicate(ToString.new.taint.string, :tainted?) + end + private def eval_using(mod, s) |