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

ruby-changes:33776

From: nobu <ko1@a...>
Date: Wed, 7 May 2014 13:27:21 +0900 (JST)
Subject: [ruby-changes:33776] nobu:r45854 (trunk): vm_eval.c: valid name IDs only

nobu	2014-05-07 13:26:49 +0900 (Wed, 07 May 2014)

  New Revision: 45854

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

  Log:
    vm_eval.c: valid name IDs only
    
    * vm.c (vm_collect_local_variables_in_heap): use struct local_var_list
      instead of VALUE directly.
    
    * vm_eval.c (local_var_list_update): add valid name IDs only.

  Modified files:
    trunk/vm.c
    trunk/vm_eval.c
Index: vm_eval.c
===================================================================
--- vm_eval.c	(revision 45853)
+++ vm_eval.c	(revision 45854)
@@ -11,6 +11,10 @@ https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L11
 
 **********************************************************************/
 
+struct local_var_list {
+    VALUE tbl;
+};
+
 static inline VALUE method_missing(VALUE obj, ID id, int argc, const VALUE *argv, int call_status);
 static inline VALUE vm_yield_with_cref(rb_thread_t *th, int argc, const VALUE *argv, const NODE *cref);
 static inline VALUE vm_yield(rb_thread_t *th, int argc, const VALUE *argv);
@@ -18,7 +22,7 @@ static inline VALUE vm_yield_with_block( https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L22
 static NODE *vm_cref_push(rb_thread_t *th, VALUE klass, int noex, rb_block_t *blockptr);
 static VALUE vm_exec(rb_thread_t *th);
 static void vm_set_eval_stack(rb_thread_t * th, VALUE iseqval, const NODE *cref, rb_block_t *base_block);
-static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, VALUE vars);
+static int vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *dfp, const struct local_var_list *vars);
 
 /* vm_backtrace.c */
 VALUE rb_vm_backtrace_str_ary(rb_thread_t *th, int lev, int n);
@@ -1870,6 +1874,15 @@ rb_catch_protect(VALUE t, rb_block_call_ https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1874
     return val;
 }
 
+static void
+local_var_list_add(const struct local_var_list *vars, ID lid)
+{
+    if (lid && rb_id2str(lid)) {
+	/* should skip temporary variable */
+	rb_hash_aset(vars->tbl, ID2SYM(lid), Qtrue);
+    }
+}
+
 /*
  *  call-seq:
  *     local_variables    -> array
@@ -1886,31 +1899,25 @@ rb_catch_protect(VALUE t, rb_block_call_ https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1899
 static VALUE
 rb_f_local_variables(void)
 {
-    VALUE vars = rb_hash_new();
+    struct local_var_list vars;
     VALUE ary;
     rb_thread_t *th = GET_THREAD();
     rb_control_frame_t *cfp =
 	vm_get_ruby_level_caller_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(th->cfp));
     int i;
 
+    vars.tbl = rb_hash_new();
     while (cfp) {
 	if (cfp->iseq) {
 	    for (i = 0; i < cfp->iseq->local_table_size; i++) {
-		ID lid = cfp->iseq->local_table[i];
-		if (lid) {
-		    const char *vname = rb_id2name(lid);
-		    /* should skip temporary variable */
-		    if (vname) {
-			rb_hash_aset(vars, ID2SYM(lid), Qtrue);
-		    }
-		}
+		local_var_list_add(&vars, cfp->iseq->local_table[i]);
 	    }
 	}
 	if (!VM_EP_LEP_P(cfp->ep)) {
 	    /* block */
 	    VALUE *ep = VM_CF_PREV_EP(cfp);
 
-	    if (vm_collect_local_variables_in_heap(th, ep, vars)) {
+	    if (vm_collect_local_variables_in_heap(th, ep, &vars)) {
 		break;
 	    }
 	    else {
@@ -1925,8 +1932,8 @@ rb_f_local_variables(void) https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1932
     }
     /* TODO: rb_hash_keys() directly, or something not to depend on
      * the order of st_table */
-    ary = rb_funcallv(vars, rb_intern("keys"), 0, 0);
-    rb_hash_clear(vars);
+    ary = rb_funcallv(vars.tbl, rb_intern("keys"), 0, 0);
+    rb_hash_clear(vars.tbl);
     return ary;
 }
 
Index: vm.c
===================================================================
--- vm.c	(revision 45853)
+++ vm.c	(revision 45854)
@@ -516,21 +516,18 @@ vm_make_env_each(rb_thread_t * const th, https://github.com/ruby/ruby/blob/trunk/vm.c#L516
 }
 
 static int
-collect_local_variables_in_iseq(rb_iseq_t *iseq, const VALUE vars)
+collect_local_variables_in_iseq(rb_iseq_t *iseq, const struct local_var_list *vars)
 {
     int i;
     if (!iseq) return 0;
     for (i = 0; i < iseq->local_table_size; i++) {
-	ID lid = iseq->local_table[i];
-	if (rb_is_local_id(lid)) {
-	    rb_hash_aset(vars, ID2SYM(lid), Qtrue);
-	}
+	local_var_list_add(vars, iseq->local_table[i]);
     }
     return 1;
 }
 
 static int
-collect_local_variables_in_env(rb_env_t *env, const VALUE vars)
+collect_local_variables_in_env(rb_env_t *env, const struct local_var_list *vars)
 {
 
     while (collect_local_variables_in_iseq(env->block.iseq, vars),
@@ -541,7 +538,7 @@ collect_local_variables_in_env(rb_env_t https://github.com/ruby/ruby/blob/trunk/vm.c#L538
 }
 
 static int
-vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *ep, VALUE vars)
+vm_collect_local_variables_in_heap(rb_thread_t *th, VALUE *ep, const struct local_var_list *vars)
 {
     if (ENV_IN_HEAP_P(th, ep)) {
 	rb_env_t *env;

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

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