summaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2017-03-12 19:31:28 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-09-01 15:19:09 +0900
commita92fdc90da26be883b71e1f225034237991c5929 (patch)
tree543cdd1b182d6c975614a6664fad9f6cae8889ea /compile.c
parent996489d7e08aeaf90cb44e9c1aec3ac3afccc3bd (diff)
Extract compile_yield from iseq_compile_each0
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4795
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c94
1 files changed, 46 insertions, 48 deletions
diff --git a/compile.c b/compile.c
index 6449b80e88..3e2f6efc78 100644
--- a/compile.c
+++ b/compile.c
@@ -8617,6 +8617,50 @@ compile_super(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, i
return COMPILE_OK;
}
+static int
+compile_yield(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
+{
+ DECL_ANCHOR(args);
+ VALUE argc;
+ unsigned int flag = 0;
+ struct rb_callinfo_kwarg *keywords = NULL;
+
+ INIT_ANCHOR(args);
+
+ switch (iseq->body->local_iseq->body->type) {
+ case ISEQ_TYPE_TOP:
+ case ISEQ_TYPE_MAIN:
+ case ISEQ_TYPE_CLASS:
+ COMPILE_ERROR(ERROR_ARGS "Invalid yield");
+ return COMPILE_NG;
+ default: /* valid */;
+ }
+
+ if (node->nd_head) {
+ argc = setup_args(iseq, args, node->nd_head, &flag, &keywords);
+ CHECK(!NIL_P(argc));
+ }
+ else {
+ argc = INT2FIX(0);
+ }
+
+ ADD_SEQ(ret, args);
+ ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0, FIX2INT(argc), flag, keywords, FALSE));
+
+ if (popped) {
+ ADD_INSN(ret, node, pop);
+ }
+
+ int level = 0;
+ const rb_iseq_t *tmp_iseq = iseq;
+ for (; tmp_iseq != iseq->body->local_iseq; level++ ) {
+ tmp_iseq = tmp_iseq->body->parent_iseq;
+ }
+ if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
+
+ return COMPILE_OK;
+}
+
static int iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped);
/**
compile each node
@@ -8642,19 +8686,6 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, const NODE *node, int poppe
}
static int
-check_yield_place(const rb_iseq_t *iseq)
-{
- switch (iseq->body->local_iseq->body->type) {
- case ISEQ_TYPE_TOP:
- case ISEQ_TYPE_MAIN:
- case ISEQ_TYPE_CLASS:
- return FALSE;
- default:
- return TRUE;
- }
-}
-
-static int
iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, int popped)
{
const int line = (int)nd_line(node);
@@ -8897,42 +8928,9 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const no
case NODE_RETURN:
CHECK(compile_return(iseq, ret, node, popped));
break;
- case NODE_YIELD:{
- DECL_ANCHOR(args);
- VALUE argc;
- unsigned int flag = 0;
- struct rb_callinfo_kwarg *keywords = NULL;
-
- INIT_ANCHOR(args);
-
- if (check_yield_place(iseq) == FALSE) {
- COMPILE_ERROR(ERROR_ARGS "Invalid yield");
- goto ng;
- }
-
- if (node->nd_head) {
- argc = setup_args(iseq, args, node->nd_head, &flag, &keywords);
- CHECK(!NIL_P(argc));
- }
- else {
- argc = INT2FIX(0);
- }
-
- ADD_SEQ(ret, args);
- ADD_INSN1(ret, node, invokeblock, new_callinfo(iseq, 0, FIX2INT(argc), flag, keywords, FALSE));
-
- if (popped) {
- ADD_INSN(ret, node, pop);
- }
-
- int level = 0;
- const rb_iseq_t *tmp_iseq = iseq;
- for (; tmp_iseq != iseq->body->local_iseq; level++ ) {
- tmp_iseq = tmp_iseq->body->parent_iseq;
- }
- if (level > 0) access_outer_variables(iseq, level, rb_intern("yield"), true);
+ case NODE_YIELD:
+ CHECK(compile_yield(iseq, ret, node, popped));
break;
- }
case NODE_LVAR:{
if (!popped) {
compile_lvar(iseq, ret, node, node->nd_vid);