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

ruby-changes:39873

From: nobu <ko1@a...>
Date: Sun, 27 Sep 2015 15:47:14 +0900 (JST)
Subject: [ruby-changes:39873] nobu:r51954 (trunk): ruby.c: frozen-string-literal option

nobu	2015-09-27 15:47:00 +0900 (Sun, 27 Sep 2015)

  New Revision: 51954

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

  Log:
    ruby.c: frozen-string-literal option
    
    * ruby.c (process_options): add an option to enable/disable
      frozen-string-literal.  [Feature #8976]

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/ruby.c
    trunk/test/ruby/test_rubyoptions.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51953)
+++ ChangeLog	(revision 51954)
@@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Sun Sep 27 15:43:59 2015  Nobuyoshi Nakada  <nobu@r...>
+Sun Sep 27 15:46:58 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* ruby.c (process_options): add an option to enable/disable
+	  frozen-string-literal.  [Feature #8976]
 
 	* compile.c (iseq_compile_each): override compile option by option
 	  given by pragma.
Index: NEWS
===================================================================
--- NEWS	(revision 51953)
+++ NEWS	(revision 51954)
@@ -15,6 +15,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L15
 
 * frozen-string-literal pragma:
   * new pragma, frozen-string-literal has been experimentally introduced.
+  * besides, --enable/--disable=frozen-string-literal options also have
+    been introduced.
 
 === Core classes updates (outstanding ones only)
 
Index: ruby.c
===================================================================
--- ruby.c	(revision 51953)
+++ ruby.c	(revision 51954)
@@ -64,6 +64,7 @@ enum feature_flag_bits { https://github.com/ruby/ruby/blob/trunk/ruby.c#L64
     feature_gems,
     feature_did_you_mean,
     feature_rubyopt,
+    feature_frozen_string_literal,
     feature_flag_count
 };
 
@@ -120,6 +121,7 @@ cmdline_options_init(struct cmdline_opti https://github.com/ruby/ruby/blob/trunk/ruby.c#L121
 #if DISABLE_RUBYGEMS
     opt->features &= ~FEATURE_BIT(gems);
 #endif
+    opt->features &= ~FEATURE_BIT(frozen_string_literal);
     return opt;
 }
 
@@ -196,6 +198,7 @@ usage(const char *name, int help) https://github.com/ruby/ruby/blob/trunk/ruby.c#L198
 	M("gems",    "",        "rubygems (default: "DEFAULT_RUBYGEMS_ENABLED")"),
 	M("did_you_mean", "",   "did_you_mean (default: "DEFAULT_RUBYGEMS_ENABLED")"),
 	M("rubyopt", "",        "RUBYOPT environment variable (default: enabled)"),
+	M("frozen-string-literal", "", "freeze all string literals (default: disabled)"),
     };
     int i;
     const int num = numberof(usage_msg) - (help ? 1 : 0);
@@ -733,6 +736,7 @@ enable_option(const char *str, int len, https://github.com/ruby/ruby/blob/trunk/ruby.c#L736
     SET_WHEN_ENABLE(gems);
     SET_WHEN_ENABLE(did_you_mean);
     SET_WHEN_ENABLE(rubyopt);
+    SET_WHEN_ENABLE(frozen_string_literal);
     if (NAME_MATCH_P("all", str, len)) {
 	*(unsigned int *)arg = ~0U;
 	return;
@@ -747,6 +751,7 @@ disable_option(const char *str, int len, https://github.com/ruby/ruby/blob/trunk/ruby.c#L751
     UNSET_WHEN_DISABLE(gems);
     UNSET_WHEN_DISABLE(did_you_mean);
     UNSET_WHEN_DISABLE(rubyopt);
+    UNSET_WHEN_DISABLE(frozen_string_literal);
     if (NAME_MATCH_P("all", str, len)) {
 	*(unsigned int *)arg = 0U;
 	return;
@@ -1462,6 +1467,11 @@ process_options(int argc, char **argv, s https://github.com/ruby/ruby/blob/trunk/ruby.c#L1467
 	rb_define_module("DidYouMean");
     }
     ruby_init_prelude();
+    if (opt->features & FEATURE_BIT(frozen_string_literal)) {
+	VALUE option = rb_hash_new();
+	rb_hash_aset(option, ID2SYM(rb_intern_const("frozen_string_literal")), 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 51953)
+++ test/ruby/test_rubyoptions.rb	(revision 51954)
@@ -783,4 +783,19 @@ class TestRubyOptions < Test::Unit::Test https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rubyoptions.rb#L783
   def test_dump_insns_with_rflag
     assert_norun_with_rflag('--dump=insns')
   end
+
+  def test_frozen_string_literal
+    results = {}
+    %W[frozen_string_literal frozen_string_literal].each do |arg|
+      [["disable", "false"], ["enable", "true"]].each do |opt, exp|
+        key = "#{opt}=#{arg}"
+        begin
+          assert_in_out_err(["--disable=gems", "--#{key}"], 'p("foo".frozen?)', [exp])
+        rescue MiniTest::Assertion => e
+          results[key] = e
+        end
+      end
+    end
+    assert_empty(results)
+  end
 end

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

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