mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* proc.c (proc_binding): allow proc from method. [ruby-core:25589]
* vm.c (collect_local_variables_in_env): block iseq can be NULL. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25015 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
3a7c1e25c4
commit
f331b5584e
4 changed files with 31 additions and 13 deletions
|
@ -1,3 +1,9 @@
|
||||||
|
Mon Sep 21 17:12:10 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
|
* proc.c (proc_binding): allow proc from method. [ruby-core:25589]
|
||||||
|
|
||||||
|
* vm.c (collect_local_variables_in_env): block iseq can be NULL.
|
||||||
|
|
||||||
Mon Sep 21 10:13:22 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Sep 21 10:13:22 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* cont.c (cont_new, cont_capture, fiber_t_alloc): needs already
|
* cont.c (cont_new, cont_capture, fiber_t_alloc): needs already
|
||||||
|
|
16
proc.c
16
proc.c
|
@ -33,6 +33,8 @@ rb_iseq_t *rb_method_get_iseq(VALUE method);
|
||||||
|
|
||||||
/* Proc */
|
/* Proc */
|
||||||
|
|
||||||
|
#define IS_METHOD_PROC_NODE(node) (nd_type(node) == NODE_IFUNC && (node)->nd_cfnc == bmcall)
|
||||||
|
|
||||||
static void
|
static void
|
||||||
proc_free(void *ptr)
|
proc_free(void *ptr)
|
||||||
{
|
{
|
||||||
|
@ -633,7 +635,7 @@ rb_proc_arity(VALUE self)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
NODE *node = (NODE *)iseq;
|
NODE *node = (NODE *)iseq;
|
||||||
if (nd_type(node) == NODE_IFUNC && node->nd_cfnc == bmcall) {
|
if (IS_METHOD_PROC_NODE(node)) {
|
||||||
/* method(:foo).to_proc.arity */
|
/* method(:foo).to_proc.arity */
|
||||||
return method_arity(node->nd_tval);
|
return method_arity(node->nd_tval);
|
||||||
}
|
}
|
||||||
|
@ -654,7 +656,7 @@ get_proc_iseq(VALUE self, int *is_proc)
|
||||||
if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
|
if (!RUBY_VM_NORMAL_ISEQ_P(iseq)) {
|
||||||
NODE *node = (NODE *)iseq;
|
NODE *node = (NODE *)iseq;
|
||||||
iseq = 0;
|
iseq = 0;
|
||||||
if (nd_type(node) == NODE_IFUNC && node->nd_cfnc == bmcall) {
|
if (IS_METHOD_PROC_NODE(node)) {
|
||||||
/* method(:foo).to_proc */
|
/* method(:foo).to_proc */
|
||||||
iseq = rb_method_get_iseq(node->nd_tval);
|
iseq = rb_method_get_iseq(node->nd_tval);
|
||||||
if (is_proc) *is_proc = 0;
|
if (is_proc) *is_proc = 0;
|
||||||
|
@ -1807,16 +1809,18 @@ static VALUE
|
||||||
proc_binding(VALUE self)
|
proc_binding(VALUE self)
|
||||||
{
|
{
|
||||||
rb_proc_t *proc;
|
rb_proc_t *proc;
|
||||||
VALUE bindval = binding_alloc(rb_cBinding);
|
VALUE bindval;
|
||||||
rb_binding_t *bind;
|
rb_binding_t *bind;
|
||||||
|
|
||||||
GetProcPtr(self, proc);
|
GetProcPtr(self, proc);
|
||||||
GetBindingPtr(bindval, bind);
|
|
||||||
|
|
||||||
if (TYPE(proc->block.iseq) == T_NODE) {
|
if (TYPE(proc->block.iseq) == T_NODE) {
|
||||||
rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
|
if (!IS_METHOD_PROC_NODE((NODE *)proc->block.iseq)) {
|
||||||
|
rb_raise(rb_eArgError, "Can't create Binding from C level Proc");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bindval = binding_alloc(rb_cBinding);
|
||||||
|
GetBindingPtr(bindval, bind);
|
||||||
bind->env = proc->envval;
|
bind->env = proc->envval;
|
||||||
return bindval;
|
return bindval;
|
||||||
}
|
}
|
||||||
|
|
|
@ -144,6 +144,7 @@ class TestProc < Test::Unit::TestCase
|
||||||
def test_method_to_proc
|
def test_method_to_proc
|
||||||
b = block()
|
b = block()
|
||||||
assert_equal "OK", b.call
|
assert_equal "OK", b.call
|
||||||
|
assert_instance_of(Binding, b.binding, '[ruby-core:25589]')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_curry
|
def test_curry
|
||||||
|
|
21
vm.c
21
vm.c
|
@ -381,19 +381,26 @@ vm_make_env_each(rb_thread_t * const th, rb_control_frame_t * const cfp,
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
collect_local_variables_in_env(rb_env_t * const env, const VALUE ary)
|
collect_local_variables_in_iseq(rb_iseq_t *iseq, const VALUE ary)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < env->block.iseq->local_table_size; i++) {
|
if (!iseq) return 0;
|
||||||
ID lid = env->block.iseq->local_table[i];
|
for (i = 0; i < iseq->local_table_size; i++) {
|
||||||
|
ID lid = iseq->local_table[i];
|
||||||
if (rb_is_local_id(lid)) {
|
if (rb_is_local_id(lid)) {
|
||||||
rb_ary_push(ary, ID2SYM(lid));
|
rb_ary_push(ary, ID2SYM(lid));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (env->prev_envval) {
|
return 1;
|
||||||
rb_env_t *prevenv;
|
}
|
||||||
GetEnvPtr(env->prev_envval, prevenv);
|
|
||||||
collect_local_variables_in_env(prevenv, ary);
|
static int
|
||||||
|
collect_local_variables_in_env(rb_env_t * env, const VALUE ary)
|
||||||
|
{
|
||||||
|
|
||||||
|
while (collect_local_variables_in_iseq(env->block.iseq, ary),
|
||||||
|
env->prev_envval) {
|
||||||
|
GetEnvPtr(env->prev_envval, env);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue