# Patch for ruby-core:14813

**URL:** <https://www.ruby-forum.com/t/patch-for-ruby-core-14813/134943>\
**Category:** Development\
**Created:** [April 16, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/patch-for-ruby-core-14813/134943 "2008-04-16T13:56:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wanabe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/51bf81/32.png) [@wanabe](https://www.ruby-forum.com/u/wanabe)\
**Post date:** [April 16, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/patch-for-ruby-core-14813/134943/1 "2008-04-16T13:56:42Z")

</div>

e$B%o%J%Y$H?=$7$^$9!#e(B

[ruby-core:14813] e$B$KBP$9$k%Q%C%A$r=q$-$^$7$?!#e(B  
e$B%V%m%C%/0z?t$,M?$($i$l$?%V%m%C%/!"$\*$h$Se(B define\_method  
e$B$N%V%m%C%/Cf$G$Ne(B  
given\_block? e$B$He(B yield e$B$N5sF0$,JQ$o$j$^$9!#e(B  
e$B%V%m%C%/%U%l!\<%`$Ne(B bp  
e$B$r2!$7\>e$2!"$G$-$?7d4V$K6/0z$K%V%m%C%/0z?t$r;}$?$;$F$$$^$9!#e(B

# Index: vm\_core.h

— vm\_core.h (revision 15984)  
+++ vm\_core.h (working copy)  
@@ -608,6 +608,7 @@  
VALUE vm\_yield(rb\_thread\_t \*th, int argc, VALUE \*argv);  
VALUE vm\_call0(rb\_thread\_t \*th, VALUE klass, VALUE recv, VALUE id, ID  
oid,  
int argc, const VALUE \*argv, NODE \*body, int nosuper);  
+rb\_block\_t \*vm\_get\_block(rb\_control\_frame\_t \*cfp);

int vm\_get\_sourceline(rb\_control\_frame\_t \*);

# Index: eval.c

— eval.c (revision 15984)  
+++ eval.c (working copy)  
@@ -837,11 +837,10 @@  
JUMP\_TAG(tag);  
}

-int  
-rb\_block\_given\_p(void)  
+static inline VALUE  
+block\_given\_p(rb\_control\_frame\_t \*cfp)  
{

- rb\_thread\_t \*th = GET\_THREAD();
- if (GC\_GUARDED\_PTR\_REF(th-\>cfp-\>lfp[0])) {

- if (vm\_get\_block(cfp)) {  
return Qtrue;  
}  
else {  
@@ -850,6 +849,12 @@  
}

int  
+rb\_block\_given\_p(void)  
+{

- return block\_given\_p(GET\_THREAD()-\>cfp);  
+}
- 

+int  
rb\_iterator\_p()  
{  
return rb\_block\_given\_p();  
@@ -882,13 +887,9 @@  
{  
rb\_thread\_t \*th = GET\_THREAD();  
rb\_control\_frame\_t \*cfp = th-\>cfp;  
+  
cfp = vm\_get\_ruby\_level\_cfp(th,  
RUBY\_VM\_PREVIOUS\_CONTROL\_FRAME(cfp));

- if (GC\_GUARDED\_PTR\_REF(cfp-\>lfp[0])) {
- return Qtrue;
- }
- else {
- return Qfalse;
- }

- return block\_given\_p(cfp);  
}

# VALUE rb\_eThreadError; Index: vm.c

— vm.c (revision 15984)  
+++ vm.c (working copy)  
@@ -539,6 +539,30 @@  
return vm\_call\_super(GET\_THREAD(), argc, argv);  
}

+rb\_block\_t \*  
+vm\_get\_block(rb\_control\_frame\_t \*cfp) {

- rb\_block\_t \*block = 0;
- 
- if (RUBY\_VM\_NORMAL\_ISEQ\_P(cfp-\>iseq)) {
- if (cfp-\>iseq-\>type == ISEQ\_TYPE\_BLOCK) {
- 

```
 block = ((rb_block_t *)(GC_GUARDED_PTR_REF(cfp->dfp[1])));

```

- 

```
 if (cfp->iseq->defined_method_id) {

```

- return block;
- 

```
 }

```

- }
- else if (RUBY\_VM\_NORMAL\_ISEQ\_P(cfp-\>iseq-\>local\_iseq)) {
- 

```
 if (cfp->iseq->local_iseq->type != ISEQ_TYPE_METHOD) {

```

- return 0;
- 

```
 }

```

- }
- }
- if (!block) {
- block = ((rb\_block\_t \*)(GC\_GUARDED\_PTR\_REF(cfp-\>lfp[0])));
- }
- 
- return block;  
+}
- 

/\* C -\> Ruby: block \*/

static VALUE  
@@ -568,6 +592,7 @@  
self, GC\_GUARDED\_PTR(block-\>dfp),  
iseq-\>iseq\_encoded + opt\_pc, cfp-\>sp + arg\_size, block-\>lfp,  
iseq-\>local\_size - arg\_size);

- vm\_add\_specval(th, GC\_GUARDED\_PTR(blockptr));

- 

```
 vm_add_specval(th, 0);

 state = 0;
 th->errinfo = Qnil;

```

# Index: vm\_insnhelper.c

— vm\_insnhelper.c (revision 15984)  
+++ vm\_insnhelper.c (working copy)  
@@ -68,6 +68,14 @@  
}

static inline void  
+vm\_add\_specval(rb\_thread\_t \*th, VALUE specval)  
+{

- \*th-\>cfp-\>bp = specval;
- th-\>cfp-\>sp++;
- th-\>cfp-\>bp++;  
+}
- 

+static inline void  
vm\_pop\_frame(rb\_thread\_t \*th)  
{  
#if COLLECT\_PROFILE  
@@ -792,11 +800,12 @@  
vm\_invoke\_block(rb\_thread\_t \*th, rb\_control\_frame\_t \*reg\_cfp,  
rb\_num\_t num, rb\_num\_t flag)  
{  
VALUE val;

- rb\_block\_t \*block = GET\_BLOCK\_PTR();

- rb\_block\_t \*block = 0;  
rb\_iseq\_t \*iseq;  
int argc = num;

- if (GET\_ISEQ()-\>local\_iseq-\>type != ISEQ\_TYPE\_METHOD || block == 0)  
{

- block = vm\_get\_block(reg\_cfp);

- if (block == 0) {  
vm\_localjump\_error(“no block given (yield)”, Qnil, 0);  
}  
iseq = block-\>iseq;  
@@ -817,6 +826,7 @@  
FRAME\_MAGIC\_BLOCK, block-\>self, (VALUE) block-\>dfp,  
iseq-\>iseq\_encoded + opt\_pc, rsp + arg\_size, block-\>lfp,  
iseq-\>local\_size - arg\_size);

- vm\_add\_specval(th, 0);
