From b7a908f61b90cb8cb15df2f81beec06cfbf3d68a Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 1 Dec 2017 03:54:50 +0000 Subject: prelude.c.tmpl: split prelude code * template/prelude.c.tmpl: split prelude code into blocks so that each elements do not exceed the string literal size limit in C89. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60953 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- template/prelude.c.tmpl | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'template') diff --git a/template/prelude.c.tmpl b/template/prelude.c.tmpl index ef024fd05c..52f5968f19 100644 --- a/template/prelude.c.tmpl +++ b/template/prelude.c.tmpl @@ -6,6 +6,8 @@ # Ruby 1.9 feature should not be used. class Prelude + LINE_LIMIT = 509 # by C89 + C_ESC = { "\\" => "\\\\", '"' => '\"', @@ -42,7 +44,10 @@ class Prelude result = [@preludes.size, @vpath.strip(filename), lines, sub] @vpath.foreach(filename) do |line| @preludes[filename] ||= result - line.sub!(/(?:^|\s+)\#(?:$|[#\s].*)/, '') + comment = ($1 || '' if line.sub!(/(?:^|\s+)\#(?:$|[#\s](.*))/, '')) + if line.size > LINE_LIMIT + raise "#{filename}:#{lines.size+1}: too long line" + end line.sub!(/require(_relative)?\s*\(?\s*(["'])(.*?)(?:\.rb)?\2\)?/) do orig, rel, path = $&, $2, $3 if rel @@ -57,7 +62,7 @@ class Prelude orig end end - lines << c_esc(line) + lines << [line, comment] end result end @@ -67,7 +72,7 @@ Prelude.new(output && output[/\w+(?=_prelude.c\b)/] || 'prelude', ARGV, vpath).i /* -*-c-*- THIS FILE WAS AUTOGENERATED BY template/prelude.c.tmpl. DO NOT EDIT. - sources: <%= @preludes.map {|n,*| prelude_base(n)}.join(', ') %> + sources: <%= @preludes.map {|n,*| prelude_base(n)}.join(', ') %><%=%> */ %unless @preludes.empty? #include "ruby/ruby.h" @@ -79,11 +84,34 @@ Prelude.new(output && output[/\w+(?=_prelude.c\b)/] || 'prelude', ARGV, vpath).i % preludes.each {|i, prelude, lines, sub| static const char prelude_name<%=i%><%=%>[] = <%=c_esc(prelude_name(*prelude))%><%=%>; -static const char prelude_code<%=i%><%=%>[] = -% lines.each {|line| -<%=line%><%=%> +static const struct { +% size = beg = 0 +% lines.each_with_index {|(line, comment), n| +% if size + line.size < Prelude::LINE_LIMIT +% size += line.size +% next +% end + char L<%=beg%><%=%>[<%=size%><%=%>]; /* <%=beg+1%>..<%=n%> */ +% size = line.size +% beg = n % } -; +% if size > 0 + char L<%=beg%><%=%>[<%=size%><%=%>]; /* <%=beg+1%>..<%=lines.size+1%> */ +% end +} prelude_code<%=i%><%=%> = { +% size = 0 +#line 1 <%=c_esc(prelude)%> +% lines.each_with_index {|(line, comment), n| +% if size + line.size >= Prelude::LINE_LIMIT +% size = 0 +, +#line <%=n+1%> <%=c_esc(prelude)%> +% end +% size += line.size +<%=c_esc(line)%><%=%><%if comment%>/* <%=comment%> */<%end%> +% } +#line <%=_erbout.count("\n")+2%> "<%=@init_name%>.c" +}; % } % if @have_sublib @@ -147,7 +175,7 @@ prelude_require(VALUE self, VALUE nth) % @preludes.each_value do |i, prelude, lines, sub| % if sub case <%=i%><%=%>: - code = rb_usascii_str_new(prelude_code<%=i%><%=%>, sizeof(prelude_code<%=i%><%=%>) - 1); + code = rb_usascii_str_new(prelude_code<%=i%><%=%>.L0, sizeof(prelude_code<%=i%><%=%>)); name = rb_usascii_str_new(prelude_name<%=i%><%=%>, sizeof(prelude_name<%=i%><%=%>) - 1); break; % end @@ -181,7 +209,7 @@ Init_<%=@init_name%><%=%>(void) % preludes.each do |i, prelude, lines, sub| % next if sub prelude_eval( - rb_usascii_str_new(prelude_code<%=i%><%=%>, sizeof(prelude_code<%=i%><%=%>) - 1), + rb_usascii_str_new(prelude_code<%=i%><%=%>.L0, sizeof(prelude_code<%=i%><%=%>)), rb_usascii_str_new(prelude_name<%=i%><%=%>, sizeof(prelude_name<%=i%><%=%>) - 1), INT2FIX(1)); % end @@ -191,7 +219,7 @@ Init_<%=@init_name%><%=%>(void) #if 0 % preludes.length.times {|i| - puts(prelude_code<%=i%><%=%>); + printf("%.*s", (int)sizeof(prelude_code<%=i%><%=%>), prelude_code<%=i%><%=%>.L0); % } #endif %end -- cgit v1.2.3