From 76da83bda95f1b8f304940a03647c51e1d4481ce 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 --- compile.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index 26ee3b24fa..07cd7a6100 100644 --- a/compile.c +++ b/compile.c @@ -5002,10 +5002,15 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) break; } case NODE_STR:{ - node->nd_lit = rb_fstring(node->nd_lit); debugp_param("nd_lit", node->nd_lit); if (!poped) { - ADD_INSN1(ret, line, putstring, node->nd_lit); + 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 */ + } + else { + ADD_INSN1(ret, line, putstring, node->nd_lit); + } } break; } @@ -5015,6 +5020,11 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) if (poped) { ADD_INSN(ret, line, pop); } + else { + if (iseq->compile_data->option->frozen_string_literal) { + ADD_SEND (ret, line, idFreeze, INT2FIX(0)); + } + } break; } case NODE_XSTR:{ -- cgit v1.2.3