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

ruby-changes:61990

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:06:44 +0900 (JST)
Subject: [ruby-changes:61990] cc1e9b8e11 (master): compile_break: do not goto into a branch

https://git.ruby-lang.org/ruby.git/commit/?id=cc1e9b8e11

From cc1e9b8e1178cf4284fbcae1a94950e580491a05 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?=
 <shyouhei@r...>
Date: Thu, 11 Jun 2020 15:18:42 +0900
Subject: compile_break: do not goto into a branch

I'm not necessarily against every goto in general, but jumping into a
branch is definitely a bad idea.  Better refactor.

diff --git a/compile.c b/compile.c
index 48b3afd..5bf72e1 100644
--- a/compile.c
+++ b/compile.c
@@ -6579,22 +6579,8 @@ compile_break(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, i https://github.com/ruby/ruby/blob/trunk/compile.c#L6579
 	    ADD_INSN(ret, line, putnil);
 	}
     }
-    else if (iseq->body->type == ISEQ_TYPE_BLOCK) {
-      break_by_insn:
-	/* escape from block */
-	CHECK(COMPILE(ret, "break val (block)", node->nd_stts));
-        ADD_INSN1(ret, line, throw, INT2FIX(throw_flag | TAG_BREAK));
-	if (popped) {
-	    ADD_INSN(ret, line, pop);
-	}
-    }
-    else if (iseq->body->type == ISEQ_TYPE_EVAL) {
-      break_in_eval:
-	COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with break");
-	return COMPILE_NG;
-    }
     else {
-	const rb_iseq_t *ip = iseq->body->parent_iseq;
+        const rb_iseq_t *ip = iseq;
 
 	while (ip) {
 	    if (!ISEQ_COMPILE_DATA(ip)) {
@@ -6604,16 +6590,26 @@ compile_break(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, i https://github.com/ruby/ruby/blob/trunk/compile.c#L6590
 
 	    if (ISEQ_COMPILE_DATA(ip)->redo_label != 0) {
                 throw_flag = VM_THROW_NO_ESCAPE_FLAG;
-		goto break_by_insn;
 	    }
 	    else if (ip->body->type == ISEQ_TYPE_BLOCK) {
-		goto break_by_insn;
+                throw_flag = 0;
 	    }
 	    else if (ip->body->type == ISEQ_TYPE_EVAL) {
-		goto break_in_eval;
+                COMPILE_ERROR(ERROR_ARGS "Can't escape from eval with break");
+                return COMPILE_NG;
 	    }
+            else {
+                ip = ip->body->parent_iseq;
+                continue;
+            }
 
-	    ip = ip->body->parent_iseq;
+            /* escape from block */
+            CHECK(COMPILE(ret, "break val (block)", node->nd_stts));
+            ADD_INSN1(ret, line, throw, INT2FIX(throw_flag | TAG_BREAK));
+            if (popped) {
+                ADD_INSN(ret, line, pop);
+            }
+            return COMPILE_OK;
 	}
 	COMPILE_ERROR(ERROR_ARGS "Invalid break");
 	return COMPILE_NG;
-- 
cgit v0.10.2


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

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