ruby-changes:46823
From: nobu <ko1@a...>
Date: Sun, 28 May 2017 18:49:36 +0900 (JST)
Subject: [ruby-changes:46823] nobu:r58938 (trunk): ruby.c: file in load_file argument
nobu 2017-05-28 18:49:30 +0900 (Sun, 28 May 2017) New Revision: 58938 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58938 Log: ruby.c: file in load_file argument * ruby.c (load_file): move opened file to an argument, to reduce open/close calls in the near future. Modified files: trunk/ruby.c Index: ruby.c =================================================================== --- ruby.c (revision 58937) +++ ruby.c (revision 58938) @@ -177,7 +177,9 @@ cmdline_options_init(ruby_cmdline_option https://github.com/ruby/ruby/blob/trunk/ruby.c#L177 return opt; } -static NODE *load_file(VALUE, VALUE, int, ruby_cmdline_options_t *); +static NODE *load_file(VALUE parser, VALUE fname, VALUE f, int script, + ruby_cmdline_options_t *opt); +static VALUE open_load_file(VALUE fname_v, int *xflag); static void forbid_setid(const char *, const ruby_cmdline_options_t *); #define forbid_setid(s) forbid_setid((s), opt) @@ -1649,9 +1651,11 @@ process_options(int argc, char **argv, r https://github.com/ruby/ruby/blob/trunk/ruby.c#L1651 tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1); } else { + VALUE f; base_block = toplevel_context(toplevel_binding); rb_parser_set_context(parser, base_block, TRUE); - tree = load_file(parser, opt->script_name, 1, opt); + f = open_load_file(opt->script_name, &opt->xflag); + tree = load_file(parser, opt->script_name, f, 1, opt); } ruby_set_script_name(opt->script_name); if (dump & DUMP_BIT(yydebug)) { @@ -1747,7 +1751,6 @@ struct load_file_arg { https://github.com/ruby/ruby/blob/trunk/ruby.c#L1751 VALUE parser; VALUE fname; int script; - int xflag; ruby_cmdline_options_t *opt; VALUE f; }; @@ -1765,7 +1768,6 @@ load_file_internal(VALUE argp_v) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1768 NODE *tree = 0; rb_encoding *enc; ID set_encoding; - int xflag = argp->xflag; CONST_ID(set_encoding, "set_encoding"); if (script) { @@ -1780,7 +1782,7 @@ load_file_internal(VALUE argp_v) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1782 enc = rb_ascii8bit_encoding(); rb_funcall(f, set_encoding, 1, rb_enc_from_encoding(enc)); - if (xflag || opt->xflag) { + if (opt->xflag) { line_start--; search_shebang: while (!NIL_P(line = rb_io_gets(f))) { @@ -1889,7 +1891,8 @@ load_file_internal(VALUE argp_v) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1891 static VALUE open_load_file(VALUE fname_v, int *xflag) { - const char *fname = StringValueCStr(fname_v); + const char *fname = (fname_v = rb_str_encode_ospath(fname_v), + StringValueCStr(fname_v)); long flen = RSTRING_LEN(fname_v); VALUE f; int e; @@ -1975,15 +1978,14 @@ restore_load_file(VALUE arg) https://github.com/ruby/ruby/blob/trunk/ruby.c#L1978 } static NODE * -load_file(VALUE parser, VALUE fname, int script, ruby_cmdline_options_t *opt) +load_file(VALUE parser, VALUE fname, VALUE f, int script, ruby_cmdline_options_t *opt) { struct load_file_arg arg; arg.parser = parser; arg.fname = fname; arg.script = script; arg.opt = opt; - arg.xflag = 0; - arg.f = open_load_file(rb_str_encode_ospath(fname), &arg.xflag); + arg.f = f; return (NODE *)rb_ensure(load_file_internal, (VALUE)&arg, restore_load_file, (VALUE)&arg); } @@ -1998,17 +2000,15 @@ rb_load_file(const char *fname) https://github.com/ruby/ruby/blob/trunk/ruby.c#L2000 void * rb_load_file_str(VALUE fname_v) { - ruby_cmdline_options_t opt; - - return load_file(rb_parser_new(), fname_v, 0, cmdline_options_init(&opt)); + return rb_parser_load_file(rb_parser_new(), fname_v); } void * rb_parser_load_file(VALUE parser, VALUE fname_v) { ruby_cmdline_options_t opt; - - return load_file(parser, fname_v, 0, cmdline_options_init(&opt)); + VALUE f = open_load_file(fname_v, &cmdline_options_init(&opt)->xflag); + return load_file(parser, fname_v, f, 0, &opt); } /* -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/