From 4ed23fe8c5dcd6f1dcfd58cbd0a293225b1e3435 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 11 Feb 2011 03:06:50 +0000 Subject: [PATCH] * ruby.c (add_gems, require_libraries, proc_options): add --require and --gem options. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30836 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ ruby.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 59b875500c..45d572d9a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Feb 11 12:06:48 2011 Nobuyoshi Nakada + + * ruby.c (add_gems, require_libraries, proc_options): add + --require and --gem options. + Fri Feb 11 12:03:24 2011 Nobuyoshi Nakada * configure.in (rubygems): add --disable-rubygems option. diff --git a/ruby.c b/ruby.c index 30ef9c78a2..86d779e63b 100644 --- a/ruby.c +++ b/ruby.c @@ -480,6 +480,31 @@ add_modules(VALUE *req_list, const char *mod) rb_ary_push(list, rb_obj_freeze(rb_str_new2(mod))); } +static void +add_gems(VALUE *req_list, const char *mod) +{ + VALUE list = *req_list; + VALUE gem, ver; + const char *v; + + if (!list) { + *req_list = list = rb_ary_new(); + RBASIC(list)->klass = 0; + } + for (v = mod; *v && !ISSPACE(*v) && !strchr("=!<>~", *v); ++v); + gem = rb_obj_freeze(rb_str_new(mod, v-mod)); + if (*v) { + while (ISSPACE(*v)) ++v; + } + if (*v) { + gem = rb_assoc_new(gem, rb_obj_freeze(rb_str_new2(v))); + } + else { + gem = rb_ary_new4(1, &gem); + } + rb_ary_push(list, rb_obj_freeze(gem)); +} + extern void Init_ext(void); extern VALUE rb_vm_top_self(void); @@ -487,7 +512,8 @@ static void require_libraries(VALUE *req_list) { VALUE list = *req_list; - ID require; + VALUE self = rb_vm_top_self(); + ID require, gem; rb_thread_t *th = GET_THREAD(); rb_block_t *prev_base_block = th->base_block; int prev_parse_in_eval = th->parse_in_eval; @@ -496,9 +522,15 @@ require_libraries(VALUE *req_list) Init_ext(); /* should be called here for some reason :-( */ CONST_ID(require, "require"); + CONST_ID(gem, "gem"); while (list && RARRAY_LEN(list) > 0) { VALUE feature = rb_ary_shift(list); - rb_funcall2(rb_vm_top_self(), require, 1, &feature); + if (RB_TYPE_P(feature, T_ARRAY)) { + rb_funcall2(self, gem, RARRAY_LENINT(feature), RARRAY_PTR(feature)); + } + else { + rb_funcall2(self, require, 1, &feature); + } } *req_list = 0; @@ -1036,6 +1068,12 @@ proc_options(long argc, char **argv, struct cmdline_options *opt, int envopt) set_source_encoding_once(opt, s, 0); } #endif + else if (is_option_with_arg("require", Qfalse, Qtrue)) { + add_modules(&opt->req_list, s); + } + else if (is_option_with_arg("gem", Qfalse, Qtrue)) { + add_gems(&opt->req_list, s); + } else if (strcmp("version", s) == 0) { if (envopt) goto noenvopt_long; opt->dump |= DUMP_BIT(version);