ruby-changes:2618
From: ko1@a...
Date: 5 Dec 2007 16:19:11 +0900
Subject: [ruby-changes:2618] nobu - Ruby:r14109 (trunk): * proc.c (rb_proc_s_new): call initialize.
nobu 2007-12-05 16:18:52 +0900 (Wed, 05 Dec 2007) New Revision: 14109 Modified files: trunk/ChangeLog trunk/proc.c trunk/thread.c trunk/vm.c Log: * proc.c (rb_proc_s_new): call initialize. [ruby-core:13824] * proc.c (rb_proc_location): return file name and line number where the proc is defined. * thread.c (thread_s_new): call initialize. [ruby-core:13835] * thread.c (thread_initialize): split initialize method. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14109&r2=14108 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/thread.c?r1=14109&r2=14108 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/proc.c?r1=14109&r2=14108 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/vm.c?r1=14109&r2=14108 Index: ChangeLog =================================================================== --- ChangeLog (revision 14108) +++ ChangeLog (revision 14109) @@ -1,3 +1,14 @@ +Wed Dec 5 16:18:50 2007 Nobuyoshi Nakada <nobu@r...> + + * proc.c (rb_proc_s_new): call initialize. [ruby-core:13824] + + * proc.c (rb_proc_location): return file name and line number where + the proc is defined. + + * thread.c (thread_s_new): call initialize. [ruby-core:13835] + + * thread.c (thread_initialize): split initialize method. + Wed Dec 5 15:25:55 2007 Nobuyoshi Nakada <nobu@r...> * parse.y (rb_intern3): fix to changing encoding to default, and Index: proc.c =================================================================== --- proc.c (revision 14108) +++ proc.c (revision 14109) @@ -304,9 +304,12 @@ */ static VALUE -rb_proc_s_new(VALUE klass) +rb_proc_s_new(int argc, VALUE *argv, VALUE klass) { - return proc_new(klass, Qfalse); + VALUE block = proc_new(klass, Qfalse); + + rb_obj_call_init(block, argc, argv); + return block; } /* @@ -471,6 +474,36 @@ return FIX2INT(proc_arity(proc)); } +static rb_iseq_t * +get_proc_iseq(VALUE self) +{ + rb_proc_t *proc; + rb_iseq_t *iseq; + + GetProcPtr(self, proc); + iseq = proc->block.iseq; + if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) + return 0; + return iseq; +} + +VALUE +rb_proc_location(VALUE self) +{ + rb_iseq_t *iseq = get_proc_iseq(self); + VALUE loc[2]; + + if (!iseq) return Qnil; + loc[0] = iseq->filename; + if (iseq->insn_info_table) { + loc[1] = INT2FIX(iseq->insn_info_table[0].line_no); + } + else { + loc[1] = Qnil; + } + return rb_ary_new4(2, loc); +} + /* * call-seq: * prc == other_proc => true or false @@ -1438,7 +1471,7 @@ /* Proc */ rb_cProc = rb_define_class("Proc", rb_cObject); rb_undef_alloc_func(rb_cProc); - rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, 0); + rb_define_singleton_method(rb_cProc, "new", rb_proc_s_new, -1); rb_define_method(rb_cProc, "call", proc_call, -1); rb_define_method(rb_cProc, "[]", proc_call, -1); rb_define_method(rb_cProc, "yield", proc_yield, -1); Index: thread.c =================================================================== --- thread.c (revision 14108) +++ thread.c (revision 14109) @@ -373,13 +373,10 @@ } static VALUE -thread_create_core(VALUE klass, VALUE args, VALUE (*fn)(ANYARGS)) +thread_create_core(VALUE thval, VALUE args, VALUE (*fn)(ANYARGS)) { rb_thread_t *th; - VALUE thval; - /* create thread object */ - thval = rb_thread_alloc(klass); GetThreadPtr(thval, th); /* setup thread environment */ @@ -407,15 +404,55 @@ */ static VALUE -thread_s_new(VALUE klass, VALUE args) +thread_s_new(int argc, VALUE *argv, VALUE klass) { - return thread_create_core(klass, args, 0); + rb_thread_t *th; + VALUE thread = rb_thread_alloc(klass); + rb_obj_call_init(thread, argc, argv); + GetThreadPtr(thread, th); + if (!th->first_args) { + rb_raise(rb_eThreadError, "uninitialized thread - check `%s#initialize'", + rb_class2name(klass)); + } + return thread; } +static VALUE +thread_start(VALUE klass, VALUE args) +{ + return thread_create_core(rb_thread_alloc(klass), args, 0); +} + +static VALUE +thread_initialize(VALUE thread, VALUE args) +{ + rb_thread_t *th; + if (!rb_block_given_p()) { + rb_raise(rb_eThreadError, "must be called with a block"); + } + GetThreadPtr(thread, th); + if (th->first_args) { + VALUE rb_proc_location(VALUE self); + VALUE proc = th->first_proc, line, loc; + const char *file; + if (!proc || !RTEST(loc = rb_proc_location(proc))) { + rb_raise(rb_eThreadError, "already initialized thread"); + } + fn = RSTRING_PTR(RARRAY_PTR(loc)[0]); + if (NIL_P(line = RARRAY_PTR(loc)[1])) { + rb_raise(rb_eThreadError, "already initialized thread - %s", + file); + } + rb_raise(rb_eThreadError, "already initialized thread - %s:%d", + file, NUM2INT(line)); + } + return thread_create_core(thread, args, 0); +} + VALUE rb_thread_create(VALUE (*fn)(ANYARGS), void *arg) { - return thread_create_core(rb_cThread, (VALUE)arg, fn); + return thread_create_core(rb_thread_alloc(rb_cThread), (VALUE)arg, fn); } @@ -2986,9 +3023,9 @@ { VALUE cThGroup; - rb_define_singleton_method(rb_cThread, "new", thread_s_new, -2); - rb_define_singleton_method(rb_cThread, "start", thread_s_new, -2); - rb_define_singleton_method(rb_cThread, "fork", thread_s_new, -2); + rb_define_singleton_method(rb_cThread, "new", thread_s_new, -1); + rb_define_singleton_method(rb_cThread, "start", thread_start, -2); + rb_define_singleton_method(rb_cThread, "fork", thread_start, -2); rb_define_singleton_method(rb_cThread, "main", rb_thread_s_main, 0); rb_define_singleton_method(rb_cThread, "current", thread_s_current, 0); rb_define_singleton_method(rb_cThread, "stop", rb_thread_stop, 0); @@ -3005,6 +3042,7 @@ rb_define_singleton_method(rb_cThread, "DEBUG=", rb_thread_s_debug_set, 1); #endif + rb_define_method(rb_cThread, "initialize", thread_initialize, -2); rb_define_method(rb_cThread, "raise", thread_raise_m, -1); rb_define_method(rb_cThread, "join", thread_join_m, -1); rb_define_method(rb_cThread, "value", thread_value, 0); Index: vm.c =================================================================== --- vm.c (revision 14108) +++ vm.c (revision 14109) @@ -1664,11 +1664,14 @@ thread_alloc(VALUE klass) { VALUE volatile obj; - //rb_thread_t *th = thread_recycle_struct(); - //obj = Data_Wrap_Struct(klass, rb_thread_mark, thread_free, th); +#ifdef USE_THREAD_RECYCLE + rb_thread_t *th = thread_recycle_struct(); + obj = Data_Wrap_Struct(klass, rb_thread_mark, thread_free, th); +#else rb_thread_t *th; obj = Data_Make_Struct(klass, rb_thread_t, rb_thread_mark, thread_free, th); +#endif return obj; } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml