From 7cf523c7db67c22ffc09b38a9c5bea057f578db2 Mon Sep 17 00:00:00 2001 From: ko1 Date: Fri, 21 Aug 2015 20:47:53 +0000 Subject: * vm_opts.h, iseq.c, iseq.h: add compile option to force frozen string literals. [Feature #11473] This addition is not specification change, but to try frozen string literal world discussed on [Feature #11473]. You can try frozen string literal world using this magical line: RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true} Note that this is a global compilation option, so that you need to compile another script like that: p 'foo'.frozen? #=> false RubyVM::InstructionSequence.compile_option = {frozen_string_literal: true} p 'foo'.frozen? #=> false, because this line is already compiled. p eval("'foo'.frozen?") #=> true Details: * String literals are deduped by rb_fstring(). * Dynamic string literals ("...#{xyz}...") is now only frozen, not deduped. Maybe you have other ideas. Now, please do not use this option on your productions :) Of course, current specification can be changed. * compile.c: ditto. * test/ruby/test_iseq.rb: add a test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_iseq.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'test') diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb index 686646ddd8..3938fb3d48 100644 --- a/test/ruby/test_iseq.rb +++ b/test/ruby/test_iseq.rb @@ -141,4 +141,17 @@ class TestISeq < Test::Unit::TestCase assert_raise(TypeError, bug11159) {ISeq.compile(:foo)} assert_raise(TypeError, bug11159) {ISeq.compile(1)} end + + def test_frozen_string_literal_compile_option + $f = 'f' + line = __LINE__ + 2 + code = <<-'EOS' + ['foo', 'foo', "#{$f}foo"] + EOS + s1, s2, s3 = RubyVM::InstructionSequence.compile(code, __FILE__, __FILE__, line, {frozen_string_literal: true}).eval + assert(s1.frozen?) + assert(s2.frozen?) + assert(s3.frozen?) + assert(s1.object_id == s2.object_id) + end end -- cgit v1.2.3