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

ruby-changes:9539

From: ko1 <ko1@a...>
Date: Sat, 27 Dec 2008 10:16:17 +0900 (JST)
Subject: [ruby-changes:9539] Ruby:r21079 (trunk): * vm.c (Init_VM): create and define TOPLEVEL_BINDING at first.

ko1	2008-12-27 10:15:56 +0900 (Sat, 27 Dec 2008)

  New Revision: 21079

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=21079

  Log:
    * vm.c (Init_VM): create and define TOPLEVEL_BINDING at first.
    * vm.c (vm_set_main_stack, rb_iseq_eval_main): added.
    * parse.y (rb_parser_compile_file): fix to check parse_in_eval flag.
    * eval.c (ruby_exec_node): use rb_iseq_eval_main()
      instead of rb_iseq_eval().
    * iseq.c (rb_iseq_new_main), vm_core.h: added.
      main script (specified by -e or script name) should be run
      under TOPLEVEL_BINDING using Kernel#eval.  Above changes
      simulate Kernel#eval behaviour.  [ruby-dev:37240]
    * compile.c (make_name_for_block): skip iseq except block type.
      this fix is needed for [ruby-dev:37240], and also fixes
      [ruby-dev:35392].

  Modified files:
    trunk/ChangeLog
    trunk/bootstraptest/test_eval.rb
    trunk/compile.c
    trunk/eval.c
    trunk/iseq.c
    trunk/parse.y
    trunk/ruby.c
    trunk/vm.c
    trunk/vm_core.h

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 21078)
+++ ChangeLog	(revision 21079)
@@ -1,3 +1,23 @@
+Sat Dec 27 09:48:54 2008  Koichi Sasada  <ko1@a...>
+
+	* vm.c (Init_VM): create and define TOPLEVEL_BINDING at first.
+
+	* vm.c (vm_set_main_stack, rb_iseq_eval_main): added.
+
+	* parse.y (rb_parser_compile_file): fix to check parse_in_eval flag.
+
+	* eval.c (ruby_exec_node): use rb_iseq_eval_main()
+	  instead of rb_iseq_eval().
+
+	* iseq.c (rb_iseq_new_main), vm_core.h: added.
+	  main script (specified by -e or script name) should be run
+	  under TOPLEVEL_BINDING using Kernel#eval.  Above changes
+	  simulate Kernel#eval behaviour.  [ruby-dev:37240]
+
+	* compile.c (make_name_for_block): skip iseq except block type.
+	  this fix is needed for [ruby-dev:37240], and also fixes
+	  [ruby-dev:35392].
+
 Sat Dec 27 09:14:17 2008  Yuki Sonoda (Yugui)  <yugui@y...>
 
 	* cont.c: rdoc for Fiber. patch by Muhammad Ali.
Index: bootstraptest/test_eval.rb
===================================================================
--- bootstraptest/test_eval.rb	(revision 21078)
+++ bootstraptest/test_eval.rb	(revision 21079)
@@ -286,3 +286,16 @@
 assert_normal_exit %q{
   eval("", method(:proc).call {}.binding)
 }
+
+assert_equal "(eval):1:in `block in <main>': ", %q{
+  b = binding
+  10.times{
+    eval('', b)
+  }
+  begin
+    eval('1.times{raise}', b)
+  rescue => e
+    e.message
+  end
+}, ' [ruby-dev:35392]'
+
Index: vm_core.h
===================================================================
--- vm_core.h	(revision 21078)
+++ vm_core.h	(revision 21079)
@@ -431,6 +431,7 @@
 /* iseq.c */
 VALUE rb_iseq_new(NODE*, VALUE, VALUE, VALUE, VALUE);
 VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE parent);
+VALUE rb_iseq_new_main(NODE *node, VALUE filename);
 VALUE rb_iseq_new_with_bopt(NODE*, VALUE, VALUE, VALUE, VALUE, VALUE);
 VALUE rb_iseq_new_with_opt(NODE*, VALUE, VALUE, VALUE, VALUE, const rb_compile_option_t*);
 VALUE rb_iseq_compile(VALUE src, VALUE file, VALUE line);
@@ -576,6 +577,7 @@
 /* functions about thread/vm execution */
 
 VALUE rb_iseq_eval(VALUE iseqval);
+VALUE rb_iseq_eval_main(VALUE iseqval);
 void rb_enable_interrupt(void);
 void rb_disable_interrupt(void);
 int rb_thread_method_id_and_class(rb_thread_t *th, ID *idp, VALUE *klassp);
Index: iseq.c
===================================================================
--- iseq.c	(revision 21078)
+++ iseq.c	(revision 21079)
@@ -310,6 +310,15 @@
 				&COMPILE_OPTION_DEFAULT);
 }
 
+VALUE
+rb_iseq_new_main(NODE *node, VALUE filename)
+{
+    rb_thread_t *th = GET_THREAD();
+    VALUE parent = th->base_block->iseq->self;
+    return rb_iseq_new_with_opt(node, rb_str_new2("<main>"), filename,
+				parent, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
+}
+
 static VALUE
 rb_iseq_new_with_bopt_and_opt(NODE *node, VALUE name, VALUE filename,
 				VALUE parent, VALUE type, VALUE bopt,
Index: compile.c
===================================================================
--- compile.c	(revision 21078)
+++ compile.c	(revision 21079)
@@ -2724,16 +2724,22 @@
 static VALUE
 make_name_for_block(rb_iseq_t *iseq)
 {
-    if (iseq->parent_iseq == 0) {
-	return rb_sprintf("block in %s", RSTRING_PTR(iseq->name));
-    }
-    else {
-	int level = 1;
-	rb_iseq_t *ip = iseq;
+    int level = 1;
+    rb_iseq_t *ip = iseq;
+
+    if (iseq->parent_iseq != 0) {
 	while (ip->local_iseq != ip) {
+	    if (ip->type == ISEQ_TYPE_BLOCK) {
+		level++;
+	    }
 	    ip = ip->parent_iseq;
-	    level++;
 	}
+    }
+
+    if (level == 1) {
+	return rb_sprintf("block in %s", RSTRING_PTR(ip->name));
+    }
+    else {
 	return rb_sprintf("block (%d levels) in %s", level, RSTRING_PTR(ip->name));
     }
 }
@@ -5323,5 +5329,3 @@
 {
     return GET_THREAD()->parse_in_eval != 0;
 }
-
-
Index: eval.c
===================================================================
--- eval.c	(revision 21078)
+++ eval.c	(revision 21079)
@@ -204,7 +204,7 @@
     if ((state = EXEC_TAG()) == 0) {
 	SAVE_ROOT_JMPBUF(th, {
 	    th->base_block = 0;
-	    rb_iseq_eval(iseq);
+	    rb_iseq_eval_main(iseq);
 	});
     }
     POP_TAG();
Index: parse.y
===================================================================
--- parse.y	(revision 21078)
+++ parse.y	(revision 21079)
@@ -5124,6 +5124,7 @@
     lex_gets = lex_io_gets;
     lex_input = file;
     lex_pbeg = lex_p = lex_pend = 0;
+    compile_for_eval = rb_parse_in_eval();
 
     node = yycompile(parser, f, start);
     tmp = vparser; /* prohibit tail call optimization */
Index: vm.c
===================================================================
--- vm.c	(revision 21078)
+++ vm.c	(revision 21079)
@@ -97,6 +97,27 @@
     }
 }
 
+static void
+vm_set_main_stack(rb_thread_t *th, VALUE iseqval)
+{
+    VALUE toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
+    rb_binding_t *bind;
+    rb_iseq_t *iseq;
+    rb_env_t *env;
+
+    GetBindingPtr(toplevel_binding, bind);
+    GetEnvPtr(bind->env, env);
+    th->base_block = &env->block;
+    vm_set_eval_stack(th, iseqval, 0);
+    th->base_block = 0;
+
+    /* save binding */
+    GetISeqPtr(iseqval, iseq);
+    if (bind && iseq->local_size > 0) {
+	bind->env = vm_make_env_object(th, th->cfp);
+    }
+}
+
 rb_control_frame_t *
 vm_get_ruby_level_next_cfp(rb_thread_t *th, rb_control_frame_t *cfp)
 {
@@ -1241,9 +1262,20 @@
 
     vm_set_top_stack(th, iseqval);
 
-    if (!rb_const_defined(rb_cObject, rb_intern("TOPLEVEL_BINDING"))) {
-	rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new());
+    val = vm_exec(th);
+    tmp = iseqval; /* prohibit tail call optimization */
+    return val;
     }
+
+VALUE
+rb_iseq_eval_main(VALUE iseqval)
+{
+    rb_thread_t *th = GET_THREAD();
+    VALUE val;
+    volatile VALUE tmp;
+
+    vm_set_main_stack(th, iseqval);
+
     val = vm_exec(th);
     tmp = iseqval; /* prohibit tail call optimization */
     return val;
@@ -1860,7 +1892,7 @@
     {
 	rb_vm_t *vm = ruby_current_vm;
 	rb_thread_t *th = GET_THREAD();
-	VALUE filename = rb_str_new2("<dummy toplevel>");
+	VALUE filename = rb_str_new2("<main>");
 	volatile VALUE iseqval = rb_iseq_new(0, filename, filename, 0, ISEQ_TYPE_TOP);
         volatile VALUE th_self;
 	rb_iseq_t *iseq;
@@ -1884,6 +1916,9 @@
 	GetISeqPtr(iseqval, iseq);
 	th->cfp->iseq = iseq;
 	th->cfp->pc = iseq->iseq_encoded;
+	th->cfp->self = th->top_self;
+
+	rb_define_global_const("TOPLEVEL_BINDING", rb_binding_new());
     }
     vm_init_redefined_flag();
 }
Index: ruby.c
===================================================================
--- ruby.c	(revision 21078)
+++ ruby.c	(revision 21079)
@@ -1211,6 +1211,8 @@
     const char *s;
     char fbuf[MAXPATHLEN];
     int i = proc_options(argc, argv, opt, 0);
+    rb_thread_t *th = GET_THREAD();
+    rb_env_t *env = 0;
 
     argc -= i;
     argv += i;
@@ -1327,6 +1329,18 @@
     ruby_set_argv(argc, argv);
     process_sflag(opt);
 
+    {
+	/* set eval context */
+	VALUE toplevel_binding = rb_const_get(rb_cObject, rb_intern("TOPLEVEL_BINDING"));
+	rb_binding_t *bind;
+
+	GetBindingPtr(toplevel_binding, bind);
+	GetEnvPtr(bind->env, env);
+
+	th->parse_in_eval++;
+	th->mild_compile_error++;
+    }
+
     if (opt->e_script) {
 	rb_encoding *eenc;
 	if (opt->src.enc.index >= 0) {
@@ -1337,12 +1351,16 @@
 	}
 	rb_enc_associate(opt->e_script, eenc);
 	require_libraries(opt);
+
+	th->base_block = &env->block;
 	tree = rb_parser_compile_string(parser, opt->script, opt->e_script, 1);
     }
     else {
 	if (opt->script[0] == '-' && !opt->script[1]) {
 	    forbid_setid("program input from stdin");
 	}
+
+	th->base_block = &env->block;
 	tree = load_file(parser, opt->script, 1, opt);
     }
     if (opt->dump & DUMP_BIT(yydebug)) return Qtrue;
@@ -1382,8 +1400,10 @@
 	rb_define_global_function("chomp", rb_f_chomp, -1);
     }
 
-    iseq = rb_iseq_new_top(tree, rb_str_new2("<main>"),
-			   opt->script_name, Qfalse);
+    iseq = rb_iseq_new_main(tree, opt->script_name);
+    th->parse_in_eval--;
+    th->mild_compile_error--;
+    th->base_block = 0;
 
     if (opt->dump & DUMP_BIT(insns)) {
 	rb_io_write(rb_stdout, ruby_iseq_disasm(iseq));

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

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