ruby-changes:10692
From: nobu <ko1@a...>
Date: Thu, 12 Feb 2009 19:42:49 +0900 (JST)
Subject: [ruby-changes:10692] Ruby:r22255 (trunk): * compile.c (compile_array_, defined_expr, iseq_compile_each): hide
nobu 2009-02-12 19:42:36 +0900 (Thu, 12 Feb 2009) New Revision: 22255 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22255 Log: * compile.c (compile_array_, defined_expr, iseq_compile_each): hide and freeze internal literal objects, to prevent from modifying. [ruby-dev:37959] * iseq.c (insn_operand_intern): copy internal literal objects. * insns.def (putstring, duparray): ditto. * string.c (rb_str_replace): exported. Modified files: trunk/ChangeLog trunk/compile.c trunk/include/ruby/intern.h trunk/insns.def trunk/iseq.c trunk/string.c Index: include/ruby/intern.h =================================================================== --- include/ruby/intern.h (revision 22254) +++ include/ruby/intern.h (revision 22255) @@ -617,6 +617,7 @@ VALUE rb_str_equal(VALUE str1, VALUE str2); VALUE rb_str_drop_bytes(VALUE, long); void rb_str_update(VALUE, long, long, VALUE); +VALUE rb_str_replace(VALUE, VALUE); VALUE rb_str_inspect(VALUE); VALUE rb_str_dump(VALUE); VALUE rb_str_split(VALUE, const char*); Index: insns.def =================================================================== --- insns.def (revision 22254) +++ insns.def (revision 22255) @@ -373,7 +373,7 @@ () (VALUE val) { - val = rb_str_new3(str); + val = rb_str_replace(rb_str_new(0, 0), str); } /** @@ -460,7 +460,7 @@ () (VALUE val) { - val = rb_ary_dup(ary); + val = rb_ary_replace(rb_ary_new2(0), ary); } /** Index: ChangeLog =================================================================== --- ChangeLog (revision 22254) +++ ChangeLog (revision 22255) @@ -1,3 +1,15 @@ +Thu Feb 12 19:42:33 2009 Nobuyoshi Nakada <nobu@r...> + + * compile.c (compile_array_, defined_expr, iseq_compile_each): hide + and freeze internal literal objects, to prevent from modifying. + [ruby-dev:37959] + + * iseq.c (insn_operand_intern): copy internal literal objects. + + * insns.def (putstring, duparray): ditto. + + * string.c (rb_str_replace): exported. + Thu Feb 12 17:17:51 2009 Nobuyoshi Nakada <nobu@r...> * ext/stringio/stringio.c (strio_ungetc): calculates new position Index: iseq.c =================================================================== --- iseq.c (revision 22254) +++ iseq.c (revision 22255) @@ -21,6 +21,8 @@ VALUE rb_cISeq; +#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass) + static void compile_data_free(struct iseq_compile_data *compile_data) { @@ -699,6 +701,16 @@ op = ID2SYM(op); case TS_VALUE: /* VALUE */ + if (hidden_obj_p(op)) { + switch (BUILTIN_TYPE(op)) { + case T_STRING: + op = rb_str_replace(rb_str_new(0, 0), op); + break; + case T_ARRAY: + op = rb_ary_replace(rb_ary_new2(0), op); + break; + } + } ret = rb_inspect(op); if (CLASS_OF(op) == rb_cISeq) { rb_ary_push(child, op); Index: string.c =================================================================== --- string.c (revision 22254) +++ string.c (revision 22255) @@ -815,14 +815,18 @@ return str; } -static VALUE rb_str_replace(VALUE, VALUE); +static VALUE +str_duplicate(VALUE klass, VALUE str) +{ + VALUE dup = str_alloc(klass); + rb_str_replace(dup, str); + return dup; +} VALUE rb_str_dup(VALUE str) { - VALUE dup = str_alloc(rb_obj_class(str)); - rb_str_replace(dup, str); - return dup; + return str_duplicate(rb_obj_class(str), str); } @@ -3722,7 +3726,7 @@ * s.replace "world" #=> "world" */ -static VALUE +VALUE rb_str_replace(VALUE str, VALUE str2) { long len; @@ -4024,9 +4028,7 @@ rb_str_to_s(VALUE str) { if (rb_obj_class(str) != rb_cString) { - VALUE dup = str_alloc(rb_cString); - rb_str_replace(dup, str); - return dup; + return str_duplicate(rb_cString, str); } return str; } Index: compile.c =================================================================== --- compile.c (revision 22254) +++ compile.c (revision 22255) @@ -295,6 +295,7 @@ #define INIT_ANCHOR(name) \ (name##_body__.last = &name##_body__.anchor, name = &name##_body__) +#define hide_obj(obj) (void)(OBJ_FREEZE(obj), RBASIC(obj)->klass = 0) #include "optinsn.inc" #if OPT_INSTRUCTIONS_UNIFICATION @@ -2230,7 +2231,7 @@ if (opt_p == Qtrue) { if (!poped) { - VALUE ary = rb_ary_new(); + VALUE ary = rb_ary_tmp_new(len); node = node_root; while (node) { rb_ary_push(ary, node->nd_head->nd_lit); @@ -2709,6 +2710,7 @@ if (estr != 0) { if (needstr != Qfalse) { VALUE str = rb_str_new2(estr); + hide_obj(str); ADD_INSN1(ret, nd_line(node), putstring, str); iseq_add_mark_object_compile_time(iseq, str); } @@ -4353,6 +4355,7 @@ case NODE_STR:{ debugp_param("nd_lit", node->nd_lit); if (!poped) { + hide_obj(node->nd_lit); ADD_INSN1(ret, nd_line(node), putstring, node->nd_lit); } break; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/