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

ruby-changes:48836

From: nobu <ko1@a...>
Date: Fri, 1 Dec 2017 12:54:54 +0900 (JST)
Subject: [ruby-changes:48836] nobu:r60953 (trunk): prelude.c.tmpl: split prelude code

nobu	2017-12-01 12:54:50 +0900 (Fri, 01 Dec 2017)

  New Revision: 60953

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

  Log:
    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.

  Modified files:
    trunk/template/prelude.c.tmpl
Index: template/prelude.c.tmpl
===================================================================
--- template/prelude.c.tmpl	(revision 60952)
+++ template/prelude.c.tmpl	(revision 60953)
@@ -6,6 +6,8 @@ https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L6
 # Ruby 1.9 feature should not be used.
 
 class Prelude
+  LINE_LIMIT = 509 # by C89
+
   C_ESC = {
     "\\" => "\\\\",
     '"' => '\"',
@@ -42,7 +44,10 @@ class Prelude https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L44
     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 https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L62
           orig
         end
       end
-      lines << c_esc(line)
+      lines << [line, comment]
     end
     result
   end
@@ -67,7 +72,7 @@ Prelude.new(output && output[/\w+(?=_pre https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L72
 /* -*-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+(?=_pre https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L84
 % 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) https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L175
 %   @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) https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L209
 % 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) https://github.com/ruby/ruby/blob/trunk/template/prelude.c.tmpl#L219
 
 #if 0
 % preludes.length.times {|i|
-    puts(prelude_code<%=i%><%=%>);
+    printf("%.*s", (int)sizeof(prelude_code<%=i%><%=%>), prelude_code<%=i%><%=%>.L0);
 % }
 #endif
 %end

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

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