2014-11-11 04:09:14 -05:00
|
|
|
<%
|
2007-11-10 11:33:30 -05:00
|
|
|
# This file is interpreted by $(BASERUBY) and miniruby.
|
2007-11-14 21:54:55 -05:00
|
|
|
# $(BASERUBY) is used for miniprelude.c.
|
|
|
|
# miniruby is used for prelude.c.
|
2007-11-10 11:33:30 -05:00
|
|
|
# Since $(BASERUBY) may be older than Ruby 1.9,
|
|
|
|
# Ruby 1.9 feature should not be used.
|
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
class Prelude
|
|
|
|
C_ESC = {
|
|
|
|
"\\" => "\\\\",
|
|
|
|
'"' => '\"',
|
|
|
|
"\n" => '\n',
|
|
|
|
}
|
2007-11-10 04:22:59 -05:00
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
|
|
|
|
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
|
|
|
|
C_ESC_PAT = Regexp.union(*C_ESC.keys)
|
2007-11-10 04:22:59 -05:00
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
def c_esc(str)
|
|
|
|
'"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
|
|
|
|
end
|
|
|
|
def prelude_base(filename)
|
2014-11-11 04:09:14 -05:00
|
|
|
filename.chomp(".rb")
|
2010-03-11 17:15:11 -05:00
|
|
|
end
|
|
|
|
def prelude_name(filename)
|
|
|
|
"<internal:" + prelude_base(filename) + ">"
|
|
|
|
end
|
2007-08-24 11:26:28 -04:00
|
|
|
|
2014-11-11 04:09:14 -05:00
|
|
|
def initialize(init_name, preludes, vpath)
|
|
|
|
@init_name = init_name
|
2010-03-11 17:15:11 -05:00
|
|
|
@have_sublib = false
|
2014-11-11 04:09:14 -05:00
|
|
|
@vpath = vpath
|
2010-03-11 17:15:11 -05:00
|
|
|
@preludes = {}
|
|
|
|
@mains = preludes.map {|filename| translate(filename)[0]}
|
2014-11-11 04:09:14 -05:00
|
|
|
@preludes.delete_if {|_, (_, _, lines, sub)| sub && lines.empty?}
|
2010-03-11 17:15:11 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def translate(filename, sub = false)
|
|
|
|
idx = @preludes[filename]
|
|
|
|
return idx if idx
|
|
|
|
lines = []
|
2014-11-18 20:08:00 -05:00
|
|
|
result = [@preludes.size, @vpath.strip(filename), lines, sub]
|
2014-11-11 04:09:14 -05:00
|
|
|
@vpath.foreach(filename) do |line|
|
|
|
|
@preludes[filename] ||= result
|
2014-12-03 18:25:01 -05:00
|
|
|
line.sub!(/(?:^|\s+)\#(?:$|[#\s].*)/, '')
|
|
|
|
line.sub!(/require(_relative)?\s*\(?\s*(["'])(.*?)(?:\.rb)?\2\)?/) do
|
|
|
|
orig, rel, path = $&, $2, $3
|
|
|
|
if rel
|
|
|
|
path = File.join(File.dirname(filename), path)
|
|
|
|
nil while path.gsub!(%r'(\A|/)(?!\.\.?/)[^/]+/\.\.(?:/|\z)', '')
|
|
|
|
end
|
|
|
|
path = translate("#{path}.rb", true) rescue nil
|
2014-11-11 04:09:14 -05:00
|
|
|
if path
|
2010-03-11 17:15:11 -05:00
|
|
|
@have_sublib = true
|
2014-11-11 04:09:14 -05:00
|
|
|
"TMP_RUBY_PREFIX.require(#{path[0]})"
|
2010-03-11 17:15:11 -05:00
|
|
|
else
|
|
|
|
orig
|
2009-07-31 02:09:13 -04:00
|
|
|
end
|
2009-07-30 10:12:25 -04:00
|
|
|
end
|
2009-07-31 02:09:13 -04:00
|
|
|
lines << c_esc(line)
|
|
|
|
end
|
2010-03-11 17:15:11 -05:00
|
|
|
result
|
2007-11-14 21:54:55 -05:00
|
|
|
end
|
2014-11-11 04:09:14 -05:00
|
|
|
end
|
|
|
|
Prelude.new(output && output[/\w+(?=_prelude.c\b)/] || 'prelude', ARGV, vpath).instance_eval do
|
|
|
|
-%>
|
2009-07-30 10:12:25 -04:00
|
|
|
/* -*-c-*-
|
2014-11-11 04:09:14 -05:00
|
|
|
THIS FILE WAS AUTOGENERATED BY template/prelude.c.tmpl. DO NOT EDIT.
|
2009-07-30 10:12:25 -04:00
|
|
|
|
2010-04-19 20:29:26 -04:00
|
|
|
sources: <%= @preludes.map {|n,*| prelude_base(n)}.join(', ') %>
|
2009-07-30 10:12:25 -04:00
|
|
|
*/
|
2014-11-27 03:10:21 -05:00
|
|
|
%unless @preludes.empty?
|
2007-08-24 11:26:28 -04:00
|
|
|
#include "ruby/ruby.h"
|
* internal.h: declare internal functions here.
* node.h: declare NODE dependent internal functions here.
* iseq.h: declare rb_iseq_t dependent internal functions here.
* vm_core.h: declare rb_thread_t dependent internal functions here.
* bignum.c, class.c, compile.c, complex.c, cont.c, dir.c, encoding.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c, io.c,
iseq.c, load.c, marshal.c, math.c, numeric.c, object.c, parse.y,
proc.c, process.c, range.c, rational.c, re.c, ruby.c, string.c,
thread.c, time.c, transcode.c, variable.c, vm.c,
tool/compile_prelude.rb: don't declare internal functions declared
in above headers. include above headers if required.
Note that rb_thread_mark() was declared as
void rb_thread_mark(rb_thread_t *th) in cont.c but defined as
void rb_thread_mark(void *ptr) in vm.c. Now it is declared as
the later in internal.h.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32156 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2011-06-17 18:43:38 -04:00
|
|
|
#include "internal.h"
|
2007-08-24 19:49:19 -04:00
|
|
|
#include "vm_core.h"
|
2015-11-12 02:16:23 -05:00
|
|
|
#include "iseq.h"
|
2007-08-24 19:49:19 -04:00
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
% preludes = @preludes.values.sort
|
|
|
|
% preludes.each {|i, prelude, lines, sub|
|
|
|
|
|
2015-09-26 09:33:17 -04:00
|
|
|
static const char prelude_name<%=i%><%=%>[] = <%=c_esc(prelude_name(*prelude))%><%=%>;
|
|
|
|
static const char prelude_code<%=i%><%=%>[] =
|
2010-03-11 17:15:11 -05:00
|
|
|
% lines.each {|line|
|
2015-09-26 09:33:17 -04:00
|
|
|
<%=line%><%=%>
|
2007-12-20 19:27:40 -05:00
|
|
|
% }
|
2007-08-24 11:26:28 -04:00
|
|
|
;
|
2007-12-20 19:27:40 -05:00
|
|
|
% }
|
2007-08-24 21:20:30 -04:00
|
|
|
|
2014-11-25 10:22:01 -05:00
|
|
|
% if @have_sublib
|
2014-11-27 03:10:21 -05:00
|
|
|
#define PRELUDE_COUNT <%=preludes.size%>
|
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
struct prelude_env {
|
|
|
|
volatile VALUE prefix_path;
|
|
|
|
#if PRELUDE_COUNT > 0
|
|
|
|
char loaded[PRELUDE_COUNT];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
prelude_prefix_path(VALUE self)
|
|
|
|
{
|
|
|
|
struct prelude_env *ptr = DATA_PTR(self);
|
|
|
|
return ptr->prefix_path;
|
|
|
|
}
|
|
|
|
% end
|
|
|
|
|
2010-04-27 20:49:25 -04:00
|
|
|
% unless preludes.empty?
|
|
|
|
static void
|
2015-11-12 02:16:23 -05:00
|
|
|
prelude_eval(VALUE code, VALUE name, int line)
|
2010-04-27 20:49:25 -04:00
|
|
|
{
|
2015-11-12 02:16:23 -05:00
|
|
|
static const rb_compile_option_t optimization = {
|
|
|
|
TRUE, /* int inline_const_cache; */
|
|
|
|
TRUE, /* int peephole_optimization; */
|
2016-10-18 11:40:31 -04:00
|
|
|
TRUE, /* int tailcall_optimization; */
|
2015-11-12 02:16:23 -05:00
|
|
|
TRUE, /* int specialized_instruction; */
|
|
|
|
TRUE, /* int operands_unification; */
|
|
|
|
TRUE, /* int instructions_unification; */
|
|
|
|
TRUE, /* int stack_caching; */
|
2016-10-18 11:40:31 -04:00
|
|
|
FALSE, /* int trace_instruction; */
|
|
|
|
TRUE, /* int frozen_string_literal; */
|
|
|
|
FALSE, /* int debug_frozen_string_literal; */
|
2015-11-12 02:16:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
NODE *node = rb_parser_compile_string_path(rb_parser_new(), name, code, line);
|
|
|
|
if (!node) rb_exc_raise(rb_errinfo());
|
|
|
|
rb_iseq_eval(rb_iseq_new_with_opt(node, name, name, Qnil, INT2FIX(line),
|
|
|
|
NULL, ISEQ_TYPE_TOP, &optimization));
|
2010-04-27 20:49:25 -04:00
|
|
|
}
|
|
|
|
% end
|
|
|
|
|
2010-03-11 17:15:11 -05:00
|
|
|
% if @have_sublib
|
|
|
|
static VALUE
|
|
|
|
prelude_require(VALUE self, VALUE nth)
|
|
|
|
{
|
|
|
|
struct prelude_env *ptr = DATA_PTR(self);
|
|
|
|
VALUE code, name;
|
|
|
|
int n = FIX2INT(nth);
|
|
|
|
|
|
|
|
if (n > PRELUDE_COUNT) return Qfalse;
|
|
|
|
if (ptr->loaded[n]) return Qfalse;
|
|
|
|
ptr->loaded[n] = 1;
|
|
|
|
switch (n) {
|
|
|
|
% @preludes.each_value do |i, prelude, lines, sub|
|
|
|
|
% if sub
|
2015-09-26 09:33:17 -04:00
|
|
|
case <%=i%><%=%>:
|
|
|
|
code = rb_usascii_str_new(prelude_code<%=i%><%=%>, sizeof(prelude_code<%=i%><%=%>) - 1);
|
|
|
|
name = rb_usascii_str_new(prelude_name<%=i%><%=%>, sizeof(prelude_name<%=i%><%=%>) - 1);
|
2010-03-11 17:15:11 -05:00
|
|
|
break;
|
|
|
|
% end
|
|
|
|
% end
|
|
|
|
default:
|
|
|
|
return Qfalse;
|
|
|
|
}
|
2015-11-12 02:16:23 -05:00
|
|
|
prelude_eval(code, name, 1);
|
2010-03-11 17:15:11 -05:00
|
|
|
return Qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
% end
|
2014-11-27 03:10:21 -05:00
|
|
|
%end
|
2007-08-24 11:26:28 -04:00
|
|
|
void
|
2015-09-26 09:33:17 -04:00
|
|
|
Init_<%=@init_name%><%=%>(void)
|
2007-08-24 11:26:28 -04:00
|
|
|
{
|
2014-11-27 03:10:21 -05:00
|
|
|
%unless @preludes.empty?
|
2014-11-25 10:22:01 -05:00
|
|
|
% if @have_sublib
|
2010-03-11 17:15:11 -05:00
|
|
|
struct prelude_env memo;
|
|
|
|
ID name = rb_intern("TMP_RUBY_PREFIX");
|
|
|
|
VALUE prelude = Data_Wrap_Struct(rb_cData, 0, 0, &memo);
|
|
|
|
|
|
|
|
memo.prefix_path = rb_const_remove(rb_cObject, name);
|
|
|
|
rb_const_set(rb_cObject, name, prelude);
|
|
|
|
rb_define_singleton_method(prelude, "to_s", prelude_prefix_path, 0);
|
|
|
|
% end
|
|
|
|
% if @have_sublib
|
|
|
|
memset(memo.loaded, 0, sizeof(memo.loaded));
|
|
|
|
rb_define_singleton_method(prelude, "require", prelude_require, 1);
|
|
|
|
% end
|
|
|
|
% preludes.each do |i, prelude, lines, sub|
|
|
|
|
% next if sub
|
2010-04-27 20:49:25 -04:00
|
|
|
prelude_eval(
|
2015-09-26 09:33:17 -04:00
|
|
|
rb_usascii_str_new(prelude_code<%=i%><%=%>, sizeof(prelude_code<%=i%><%=%>) - 1),
|
|
|
|
rb_usascii_str_new(prelude_name<%=i%><%=%>, sizeof(prelude_name<%=i%><%=%>) - 1),
|
2010-04-27 20:49:25 -04:00
|
|
|
INT2FIX(1));
|
2010-03-11 17:15:11 -05:00
|
|
|
% end
|
2014-11-25 10:22:01 -05:00
|
|
|
% if @have_sublib
|
2010-03-11 17:15:11 -05:00
|
|
|
rb_gc_force_recycle(prelude);
|
|
|
|
% end
|
2007-08-24 19:49:19 -04:00
|
|
|
|
|
|
|
#if 0
|
2007-12-20 19:27:40 -05:00
|
|
|
% preludes.length.times {|i|
|
2015-09-26 09:33:17 -04:00
|
|
|
puts(prelude_code<%=i%><%=%>);
|
2007-12-20 19:27:40 -05:00
|
|
|
% }
|
2007-08-24 19:49:19 -04:00
|
|
|
#endif
|
2014-11-27 03:10:21 -05:00
|
|
|
%end
|
2007-08-24 11:26:28 -04:00
|
|
|
}
|
2014-11-11 04:09:14 -05:00
|
|
|
<%end -%>
|