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

ruby-changes:40176

From: ko1 <ko1@a...>
Date: Sat, 24 Oct 2015 04:03:19 +0900 (JST)
Subject: [ruby-changes:40176] ko1:r52257 (trunk): * ruby.c: introduce --enable-frozen-string-literal-debug option.

ko1	2015-10-24 04:02:55 +0900 (Sat, 24 Oct 2015)

  New Revision: 52257

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

  Log:
    * ruby.c: introduce --enable-frozen-string-literal-debug option.
      If this option is enabled, the modify error will be:
        can't modify frozen String (RuntimeError) =>
        can't modify frozen String, created at test.rb:3 (RuntimeError)
    
    * iseq.h: add compile option frozen_string_literal_debug.
    
    * compile.c: catch up this fix.
    
    * error.c (rb_error_frozen): ditto.
    
    * iseq.c (set_compile_option_from_hash): ditto.
    
    * test/ruby/test_rubyoptions.rb: add a test for this fix.

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/error.c
    trunk/iseq.c
    trunk/iseq.h
    trunk/ruby.c
    trunk/test/ruby/test_rubyoptions.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52256)
+++ ChangeLog	(revision 52257)
@@ -1,3 +1,20 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Oct 24 03:58:02 2015  Koichi Sasada  <ko1@a...>
+
+	* ruby.c: introduce --enable-frozen-string-literal-debug option.
+	  If this option is enabled, the modify error will be:
+	    can't modify frozen String (RuntimeError) =>
+	    can't modify frozen String, created at test.rb:3 (RuntimeError)
+
+	* iseq.h: add compile option frozen_string_literal_debug.
+
+	* compile.c: catch up this fix.
+
+	* error.c (rb_error_frozen): ditto.
+
+	* iseq.c (set_compile_option_from_hash): ditto.
+
+	* test/ruby/test_rubyoptions.rb: add a test for this fix.
+
 Sat Oct 24 02:02:24 2015  Koichi Sasada  <ko1@a...>
 
 	* vm_insnhelper.c: introduce new call handler for simple ISeqs.
Index: iseq.c
===================================================================
--- iseq.c	(revision 52256)
+++ iseq.c	(revision 52257)
@@ -366,6 +366,7 @@ set_compile_option_from_hash(rb_compile_ https://github.com/ruby/ruby/blob/trunk/iseq.c#L366
     SET_COMPILE_OPTION(option, opt, stack_caching);
     SET_COMPILE_OPTION(option, opt, trace_instruction);
     SET_COMPILE_OPTION(option, opt, frozen_string_literal);
+    SET_COMPILE_OPTION(option, opt, frozen_string_literal_debug);
     SET_COMPILE_OPTION_NUM(option, opt, debug_level);
 #undef SET_COMPILE_OPTION
 #undef SET_COMPILE_OPTION_NUM
Index: iseq.h
===================================================================
--- iseq.h	(revision 52256)
+++ iseq.h	(revision 52257)
@@ -67,6 +67,7 @@ struct rb_compile_option_struct { https://github.com/ruby/ruby/blob/trunk/iseq.h#L67
     int stack_caching;
     int trace_instruction;
     int frozen_string_literal;
+    int frozen_string_literal_debug;
     int debug_level;
 };
 
Index: compile.c
===================================================================
--- compile.c	(revision 52256)
+++ compile.c	(revision 52257)
@@ -5107,11 +5107,19 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ https://github.com/ruby/ruby/blob/trunk/compile.c#L5107
 	debugp_param("nd_lit", node->nd_lit);
 	if (!poped) {
 	    node->nd_lit = rb_fstring(node->nd_lit);
-	    if (iseq->compile_data->option->frozen_string_literal) {
-		ADD_INSN1(ret, line, putobject, node->nd_lit); /* already frozen */
+	    if (!iseq->compile_data->option->frozen_string_literal) {
+		ADD_INSN1(ret, line, putstring, node->nd_lit);
 	    }
 	    else {
-		ADD_INSN1(ret, line, putstring, node->nd_lit);
+		if (!iseq->compile_data->option->frozen_string_literal_debug) {
+		    ADD_INSN1(ret, line, putobject, node->nd_lit); /* already frozen */
+		}
+		else {
+		    VALUE str = rb_str_dup(node->nd_lit);
+		    rb_iv_set(str, "__object_created_path__", iseq->body->location.path);
+		    rb_iv_set(str, "__object_created_line__", INT2FIX(line));
+		    ADD_INSN1(ret, line, putobject, rb_obj_freeze(str));
+		}
 	    }
 	}
 	break;
Index: error.c
===================================================================
--- error.c	(revision 52256)
+++ error.c	(revision 52257)
@@ -2226,8 +2226,16 @@ rb_error_frozen(const char *what) https://github.com/ruby/ruby/blob/trunk/error.c#L2226
 void
 rb_error_frozen_object(VALUE frozen_obj)
 {
-    rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE,
-	     CLASS_OF(frozen_obj));
+    VALUE path, line;
+    if ((path = rb_iv_get(frozen_obj, "__object_created_path__")) != Qnil &&
+	(line = rb_iv_get(frozen_obj, "__object_created_line__")) != Qnil) {
+	rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE", created at %"PRIsVALUE":%"PRIsVALUE,
+		 CLASS_OF(frozen_obj), path, line);
+    }
+    else {
+	rb_raise(rb_eRuntimeError, "can't modify frozen %"PRIsVALUE,
+		 CLASS_OF(frozen_obj));
+    }
 }
 
 #undef rb_check_frozen
Index: ruby.c
===================================================================
--- ruby.c	(revision 52256)
+++ ruby.c	(revision 52257)
@@ -69,6 +69,7 @@ enum feature_flag_bits { https://github.com/ruby/ruby/blob/trunk/ruby.c#L69
     feature_did_you_mean,
     feature_rubyopt,
     feature_frozen_string_literal,
+    feature_frozen_string_literal_debug,
     feature_flag_count
 };
 
@@ -126,6 +127,7 @@ cmdline_options_init(struct cmdline_opti https://github.com/ruby/ruby/blob/trunk/ruby.c#L127
     opt->features &= ~FEATURE_BIT(gems);
 #endif
     opt->features &= ~FEATURE_BIT(frozen_string_literal);
+    opt->features &= ~FEATURE_BIT(frozen_string_literal_debug);
     return opt;
 }
 
@@ -739,6 +741,7 @@ enable_option(const char *str, int len, https://github.com/ruby/ruby/blob/trunk/ruby.c#L741
     SET_WHEN_ENABLE(did_you_mean);
     SET_WHEN_ENABLE(rubyopt);
     SET_WHEN_ENABLE(frozen_string_literal);
+    SET_WHEN_ENABLE(frozen_string_literal_debug);
     if (NAME_MATCH_P("all", str, len)) {
 	*(unsigned int *)arg = ~0U;
 	return;
@@ -754,6 +757,7 @@ disable_option(const char *str, int len, https://github.com/ruby/ruby/blob/trunk/ruby.c#L757
     UNSET_WHEN_DISABLE(did_you_mean);
     UNSET_WHEN_DISABLE(rubyopt);
     UNSET_WHEN_DISABLE(frozen_string_literal);
+    UNSET_WHEN_DISABLE(frozen_string_literal_debug);
     if (NAME_MATCH_P("all", str, len)) {
 	*(unsigned int *)arg = 0U;
 	return;
@@ -1474,6 +1478,11 @@ process_options(int argc, char **argv, s https://github.com/ruby/ruby/blob/trunk/ruby.c#L1478
 	rb_hash_aset(option, ID2SYM(rb_intern_const("frozen_string_literal")), Qtrue);
 	rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option);
     }
+    if (opt->features & FEATURE_BIT(frozen_string_literal_debug)) {
+	VALUE option = rb_hash_new();
+	rb_hash_aset(option, ID2SYM(rb_intern_const("frozen_string_literal_debug")), Qtrue);
+	rb_funcallv(rb_cISeq, rb_intern_const("compile_option="), 1, &option);
+    }
 #if UTF8_PATH
     opt->script_name = str_conv_enc(opt->script_name, rb_utf8_encoding(), lenc);
     opt->script = RSTRING_PTR(opt->script_name);
Index: test/ruby/test_rubyoptions.rb
===================================================================
--- test/ruby/test_rubyoptions.rb	(revision 52256)
+++ test/ruby/test_rubyoptions.rb	(revision 52257)
@@ -804,4 +804,15 @@ class TestRubyOptions < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L804
       end
     end
   end
+
+  def test_frozen_string_literal_debug
+    assert_in_out_err(["--disable=gems", "--enable-frozen-string-literal",  "--enable-frozen-string-literal-debug" ], '"foo" << "bar"', [], /created at/)
+    assert_in_out_err(["--disable=gems", "--enable-frozen-string-literal",  "--disable-frozen-string-literal-debug"], '"foo" << "bar"', [], /can\'t modify frozen String \(RuntimeError\)\n\z/)
+    assert_in_out_err(["--disable=gems", "--disable-frozen-string-literal", "--enable-frozen-string-literal-debug" ], '"foo" << "bar"', [], [])
+    assert_in_out_err(["--disable=gems", "--disable-frozen-string-literal", "--disable-frozen-string-literal-debug"], '"foo" << "bar"', [], [])
+    assert_in_out_err(["--disable=gems",                                    "--enable-frozen-string-literal-debug" ], '"foo" << "bar"', [], [])
+    assert_in_out_err(["--disable=gems",                                    "--disable-frozen-string-literal-debug"], '"foo" << "bar"', [], [])
+    assert_in_out_err(["--disable=gems", "--enable-frozen-string-literal",                                         ], '"foo" << "bar"', [], /can\'t modify frozen String \(RuntimeError\)\n\z/)
+    assert_in_out_err(["--disable=gems", "--enable-frozen-string-literal",                                         ], '"foo" << "bar"', [], /can\'t modify frozen String \(RuntimeError\)\n\z/)
+  end
 end

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

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