From 388d26d77027feaa3e107abf7209e2681868bbe6 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 4 Nov 2017 11:20:04 +0900 Subject: Reimplement `block_given?`; ref #3841 Make `block_given?` to search for the top of the scope first. The top of the scope means either: * the top method body * the enclosing class body * the top-level The special case is the method defined by `define_method` with a block as in #3841. In cases like this, the method body (given by a block) is not considered as the top of the scope. You need to use `&block` in the block parameter if you want to know if a block is given to the method. This commit also changes the behavior of `MRB_PROC_SCOPE` flag. Now it is only set if the `proc` is either a class body or a method body defined in Ruby. It is no longer set for a block that given to `define_method`. --- src/class.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/class.c') diff --git a/src/class.c b/src/class.c index 77a7050da..30faa85e9 100644 --- a/src/class.c +++ b/src/class.c @@ -436,7 +436,6 @@ mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, struct RPro k = kh_put(mt, mrb, h, mid); kh_value(h, k) = p; if (p) { - p->flags |= MRB_PROC_SCOPE; p->c = NULL; mrb_field_write_barrier(mrb, (struct RBasic*)c, (struct RBasic*)p); MRB_PROC_SET_TARGET_CLASS(p, c); @@ -453,6 +452,7 @@ mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t f p = mrb_proc_new_cfunc(mrb, func); MRB_PROC_SET_TARGET_CLASS(p, c); + p->flags |= MRB_PROC_SCOPE; mrb_define_method_raw(mrb, c, mid, p); mrb_gc_arena_restore(mrb, ai); } -- cgit v1.2.3