summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--enum.c166
-rw-r--r--eval.c16
-rw-r--r--ruby.h1
4 files changed, 96 insertions, 96 deletions
diff --git a/ChangeLog b/ChangeLog
index 6993af7cb98..677d83b9136 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Wed Jun 8 08:33:10 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * enum.c (enum_min_by, enum_max_by): return nil if no iteration.
+ fixed: [ruby-dev:26245]
+
+ * eval.c (rb_need_block): ensure a block is given.
+
+ * eval.c (backtrace): skip successive frames sharing same node.
+
Wed Jun 8 00:00:01 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
* ext/curses/curses.c (curses_insertln): merged a patch from
diff --git a/enum.c b/enum.c
index a745a6e604e..a61a5c16a36 100644
--- a/enum.c
+++ b/enum.c
@@ -80,11 +80,10 @@ enum_grep(obj, pat)
static VALUE
find_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
if (RTEST(rb_yield(i))) {
- memo->u2.value = Qtrue;
- memo->u1.value = i;
+ *memo = i;
rb_iter_break();
}
return Qnil;
@@ -111,13 +110,13 @@ enum_find(argc, argv, obj)
VALUE* argv;
VALUE obj;
{
- NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, Qfalse, 0);
+ VALUE memo = Qundef;
VALUE if_none;
rb_scan_args(argc, argv, "01", &if_none);
- rb_iterate(rb_each, obj, find_i, (VALUE)memo);
- if (memo->u2.value) {
- return memo->u1.value;
+ rb_iterate(rb_each, obj, find_i, (VALUE)&memo);
+ if (memo != Qundef) {
+ return memo;
}
if (!NIL_P(if_none)) {
return rb_funcall(if_none, rb_intern("call"), 0, 0);
@@ -257,14 +256,13 @@ enum_to_a(obj)
static VALUE
inject_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
- if (memo->u2.value) {
- memo->u2.value = Qfalse;
- memo->u1.value = i;
+ if (*memo == Qundef) {
+ *memo = i;
}
else {
- memo->u1.value = rb_yield_values(2, memo->u1.value, i);
+ *memo = rb_yield_values(2, *memo, i);
}
return Qnil;
}
@@ -305,18 +303,11 @@ enum_inject(argc, argv, obj)
int argc;
VALUE *argv, obj;
{
- NODE *memo;
- VALUE n;
+ VALUE memo = Qundef;
- if (rb_scan_args(argc, argv, "01", &n) == 1) {
- memo = rb_node_newnode(NODE_MEMO, n, Qfalse, 0);
- }
- else {
- memo = rb_node_newnode(NODE_MEMO, Qnil, Qtrue, 0);
- }
- rb_iterate(rb_each, obj, inject_i, (VALUE)memo);
- n = memo->u1.value;
- return n;
+ rb_scan_args(argc, argv, "01", &memo);
+ rb_iterate(rb_each, obj, inject_i, (VALUE)&memo);
+ return memo;
}
static VALUE
@@ -507,10 +498,10 @@ enum_sort_by(obj)
static VALUE
all_iter_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
if (!RTEST(rb_yield(i))) {
- memo->u1.value = Qfalse;
+ *memo = Qfalse;
rb_iter_break();
}
return Qnil;
@@ -519,10 +510,10 @@ all_iter_i(i, memo)
static VALUE
all_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
if (!RTEST(i)) {
- memo->u1.value = Qfalse;
+ *memo = Qfalse;
rb_iter_break();
}
return Qnil;
@@ -549,22 +540,19 @@ static VALUE
enum_all(obj)
VALUE obj;
{
- VALUE result;
- NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
+ VALUE result = Qtrue;
- memo->u1.value = Qtrue;
- rb_iterate(rb_each, obj, rb_block_given_p() ? all_iter_i : all_i, (VALUE)memo);
- result = memo->u1.value;
+ rb_iterate(rb_each, obj, rb_block_given_p() ? all_iter_i : all_i, (VALUE)&result);
return result;
}
static VALUE
any_iter_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
if (RTEST(rb_yield(i))) {
- memo->u1.value = Qtrue;
+ *memo = Qtrue;
rb_iter_break();
}
return Qnil;
@@ -573,10 +561,10 @@ any_iter_i(i, memo)
static VALUE
any_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
if (RTEST(i)) {
- memo->u1.value = Qtrue;
+ *memo = Qtrue;
rb_iter_break();
}
return Qnil;
@@ -604,29 +592,26 @@ static VALUE
enum_any(obj)
VALUE obj;
{
- VALUE result;
- NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
+ VALUE result = Qfalse;
- memo->u1.value = Qfalse;
- rb_iterate(rb_each, obj, rb_block_given_p() ? any_iter_i : any_i, (VALUE)memo);
- result = memo->u1.value;
+ rb_iterate(rb_each, obj, rb_block_given_p() ? any_iter_i : any_i, (VALUE)&result);
return result;
}
static VALUE
min_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
VALUE cmp;
- if (NIL_P(memo->u1.value)) {
- memo->u1.value = i;
+ if (NIL_P(*memo)) {
+ *memo = i;
}
else {
- cmp = rb_funcall(i, id_cmp, 1, memo->u1.value);
- if (rb_cmpint(cmp, i, memo->u1.value) < 0) {
- memo->u1.value = i;
+ cmp = rb_funcall(i, id_cmp, 1, *memo);
+ if (rb_cmpint(cmp, i, *memo) < 0) {
+ *memo = i;
}
}
return Qnil;
@@ -635,17 +620,17 @@ min_i(i, memo)
static VALUE
min_ii(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
VALUE cmp;
- if (NIL_P(memo->u1.value)) {
- memo->u1.value = i;
+ if (NIL_P(*memo)) {
+ *memo = i;
}
else {
- cmp = rb_yield_values(2, i, memo->u1.value);
- if (rb_cmpint(cmp, i, memo->u1.value) < 0) {
- memo->u1.value = i;
+ cmp = rb_yield_values(2, i, *memo);
+ if (rb_cmpint(cmp, i, *memo) < 0) {
+ *memo = i;
}
}
return Qnil;
@@ -670,11 +655,9 @@ static VALUE
enum_min(obj)
VALUE obj;
{
- VALUE result;
- NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
+ VALUE result = Qnil;
- rb_iterate(rb_each, obj, rb_block_given_p() ? min_ii : min_i, (VALUE)memo);
- result = memo->u1.value;
+ rb_iterate(rb_each, obj, rb_block_given_p() ? min_ii : min_i, (VALUE)&result);
return result;
}
@@ -695,17 +678,17 @@ enum_min(obj)
static VALUE
max_i(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
VALUE cmp;
- if (NIL_P(memo->u1.value)) {
- memo->u1.value = i;
+ if (NIL_P(*memo)) {
+ *memo = i;
}
else {
- cmp = rb_funcall(i, id_cmp, 1, memo->u1.value);
- if (rb_cmpint(cmp, i, memo->u1.value) > 0) {
- memo->u1.value = i;
+ cmp = rb_funcall(i, id_cmp, 1, *memo);
+ if (rb_cmpint(cmp, i, *memo) > 0) {
+ *memo = i;
}
}
return Qnil;
@@ -714,17 +697,17 @@ max_i(i, memo)
static VALUE
max_ii(i, memo)
VALUE i;
- NODE *memo;
+ VALUE *memo;
{
VALUE cmp;
- if (NIL_P(memo->u1.value)) {
- memo->u1.value = i;
+ if (NIL_P(*memo)) {
+ *memo = i;
}
else {
- cmp = rb_yield_values(2, i, memo->u1.value);
- if (rb_cmpint(cmp, i, memo->u1.value) > 0) {
- memo->u1.value = i;
+ cmp = rb_yield_values(2, i, *memo);
+ if (rb_cmpint(cmp, i, *memo) > 0) {
+ *memo = i;
}
}
return Qnil;
@@ -748,21 +731,19 @@ static VALUE
enum_max(obj)
VALUE obj;
{
- VALUE result;
- NODE *memo = rb_node_newnode(NODE_MEMO, Qnil, 0, 0);
+ VALUE result = Qnil;
- rb_iterate(rb_each, obj, rb_block_given_p() ? max_ii : max_i, (VALUE)memo);
- result = memo->u1.value;
+ rb_iterate(rb_each, obj, rb_block_given_p() ? max_ii : max_i, (VALUE)&result);
return result;
}
static VALUE
member_i(item, memo)
VALUE item;
- NODE *memo;
+ VALUE *memo;
{
- if (rb_equal(item, memo->u1.value)) {
- memo->u2.value = Qtrue;
+ if (rb_equal(item, memo[0])) {
+ memo[1] = Qtrue;
rb_iter_break();
}
return Qnil;
@@ -785,21 +766,21 @@ static VALUE
enum_member(obj, val)
VALUE obj, val;
{
- VALUE result;
- NODE *memo = rb_node_newnode(NODE_MEMO, val, Qfalse, 0);
+ VALUE memo[2];
+ memo[0] = val;
+ memo[1] = Qfalse;
rb_iterate(rb_each, obj, member_i, (VALUE)memo);
- result = memo->u2.value;
- return result;
+ return memo[1];
}
static VALUE
each_with_index_i(val, memo)
VALUE val;
- NODE *memo;
+ VALUE *memo;
{
- rb_yield_values(2, val, INT2FIX(memo->u3.cnt));
- memo->u3.cnt++;
+ rb_yield_values(2, val, INT2FIX(*memo));
+ ++*memo;
return Qnil;
}
@@ -822,20 +803,21 @@ static VALUE
enum_each_with_index(obj)
VALUE obj;
{
- NODE *memo = rb_node_newnode(NODE_MEMO, 0, 0, 0);
+ VALUE memo = 0;
- rb_iterate(rb_each, obj, each_with_index_i, (VALUE)memo);
+ rb_need_block();
+ rb_iterate(rb_each, obj, each_with_index_i, (VALUE)&memo);
return obj;
}
static VALUE
zip_i(val, memo)
VALUE val;
- NODE *memo;
+ VALUE *memo;
{
- VALUE result = memo->u1.value;
- VALUE args = memo->u2.value;
- int idx = memo->u3.cnt++;
+ VALUE result = memo[0];
+ VALUE args = memo[1];
+ int idx = memo[2]++;
VALUE tmp;
int i;
@@ -884,13 +866,15 @@ enum_zip(argc, argv, obj)
{
int i;
VALUE result;
- NODE *memo;
+ VALUE memo[3];
for (i=0; i<argc; i++) {
argv[i] = rb_convert_type(argv[i], T_ARRAY, "Array", "to_a");
}
result = rb_block_given_p() ? Qnil : rb_ary_new();
- memo = rb_node_newnode(NODE_MEMO, result, rb_ary_new4(argc, argv), 0);
+ memo[0] = result;
+ memo[1] = rb_ary_new4(argc, argv);
+ memo[2] = 0;
rb_iterate(rb_each, obj, zip_i, (VALUE)memo);
return result;
diff --git a/eval.c b/eval.c
index d05216a990d..f2715d400c6 100644
--- a/eval.c
+++ b/eval.c
@@ -4674,6 +4674,14 @@ break_jump(retval)
localjump_error("unexpected break", retval, TAG_BREAK);
}
+void
+rb_need_block()
+{
+ if (!rb_block_given_p()) {
+ localjump_error("no block given", Qnil, 0);
+ }
+}
+
static VALUE
rb_yield_0(val, self, klass, flags, avalue)
VALUE val, self, klass; /* OK */
@@ -4691,9 +4699,7 @@ rb_yield_0(val, self, klass, flags, avalue)
int lambda = flags & YIELD_LAMBDA_CALL;
int state;
- if (!rb_block_given_p()) {
- localjump_error("no block given", Qnil, 0);
- }
+ rb_need_block();
PUSH_VARS();
block = ruby_block;
@@ -6029,8 +6035,9 @@ backtrace(lev)
}
}
}
- while (frame && (n = frame->node)) {
+ for (; frame && (n = frame->node); frame = frame->prev) {
if (frame->prev && frame->prev->last_func) {
+ if (frame->prev->node == n) continue;
snprintf(buf, BUFSIZ, "%s:%d:in `%s'",
n->nd_file, nd_line(n),
rb_id2name(frame->prev->last_func));
@@ -6039,7 +6046,6 @@ backtrace(lev)
snprintf(buf, BUFSIZ, "%s:%d", n->nd_file, nd_line(n));
}
rb_ary_push(ary, rb_str_new2(buf));
- frame = frame->prev;
}
return ary;
diff --git a/ruby.h b/ruby.h
index db3280b905b..e1cbb948283 100644
--- a/ruby.h
+++ b/ruby.h
@@ -543,6 +543,7 @@ VALUE rb_yield _((VALUE));
VALUE rb_yield_values __((int n, ...));
VALUE rb_yield_splat _((VALUE));
int rb_block_given_p _((void));
+void rb_need_block _((void));
VALUE rb_iterate _((VALUE(*)(VALUE),VALUE,VALUE(*)(ANYARGS),VALUE));
VALUE rb_rescue _((VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE));
VALUE rb_rescue2 __((VALUE(*)(ANYARGS),VALUE,VALUE(*)(ANYARGS),VALUE,...));