From baa00aa2506745308a598ce91e65a727ba25f30f Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 19 Dec 2002 09:20:20 +0000 Subject: * numeric.c (num_step): use DBL_EPSILON. * array.c (rb_check_array_type): new function: return an array (convert if possible), or nil. * string.c (rb_check_string_type): new function: return a string (convert if possible), or nil. * numeric.c (rb_dbl_cmp): returns nil if values are not comparable. * numeric.c (fix_cmp,flo_cmp): use rb_num_coerce_cmp() * bignum.c (rb_big_cmp): ditto. * numeric.c (rb_num_coerce_cmp): new coercing function for "<=>", which does not raise TypeError. * numeric.c (do_coerce): can be supress exception now. * object.c (rb_mod_cmp): should return nil for non class/module objects. * re.c (rb_reg_eqq): return false if the argument is not a string. now returns boolean value. * class.c (rb_include_module): argument should be T_MODULE, not T_class, nor T_ICLASS. * eval.c (is_defined): "defined?" should return "assignment" for attribute assignment (e.g. a.foo=b) and indexed assignment (e.g. a[2] = 44). * parse.y (aryset): use NODE_ATTRASGN. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3169 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 46 ++++++++++++++++++++++++++++++++++++++++++++++ array.c | 9 ++++++++- bignum.c | 3 +-- class.c | 7 +------ configure.in | 2 +- eval.c | 8 +++++--- file.c | 4 +--- intern.h | 3 +++ io.c | 2 +- marshal.c | 2 +- numeric.c | 43 +++++++++++++++++++++++++++++++++---------- object.c | 4 +--- parse.y | 2 +- range.c | 17 +++++++++++------ re.c | 23 ++++++++++++++++++++++- signal.c | 2 +- string.c | 16 +++++++++++++--- variable.c | 12 ++++++++---- 18 files changed, 158 insertions(+), 47 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5ae2d094bb..da89f1e6a2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,7 +1,40 @@ +Thu Dec 19 01:00:09 2002 Yukihiro Matsumoto + + * numeric.c (num_step): use DBL_EPSILON. + + * array.c (rb_check_array_type): new function: return an array + (convert if possible), or nil. + + * string.c (rb_check_string_type): new function: return a string + (convert if possible), or nil. + + * numeric.c (rb_dbl_cmp): returns nil if values are not + comparable. + + * numeric.c (fix_cmp,flo_cmp): use rb_num_coerce_cmp() + + * bignum.c (rb_big_cmp): ditto. + + * numeric.c (rb_num_coerce_cmp): new coercing function for "<=>", + which does not raise TypeError. + + * numeric.c (do_coerce): can be supress exception now. + + * object.c (rb_mod_cmp): should return nil for non class/module + objects. + Thu Dec 19 04:21:10 2002 Akinori MUSHA * lib/open-uri.rb: add a missing ||. (found by: ruby -wc) +Wed Dec 18 17:53:05 2002 Yukihiro Matsumoto + + * re.c (rb_reg_eqq): return false if the argument is not a + string. now returns boolean value. + + * class.c (rb_include_module): argument should be T_MODULE, not + T_class, nor T_ICLASS. + Wed Dec 18 03:52:55 2002 Nobuyoshi Nakada * string.c (rb_str_new4): handle tail shared string. @@ -22,6 +55,14 @@ Tue Dec 17 21:08:29 2002 Nobuyoshi Nakada * re.c (rb_reg_nth_match): ditto. +Tue Dec 17 16:52:38 2002 Yukihiro Matsumoto + + * eval.c (is_defined): "defined?" should return "assignment" for + attribute assignment (e.g. a.foo=b) and indexed assignment + (e.g. a[2] = 44). + + * parse.y (aryset): use NODE_ATTRASGN. + Tue Dec 17 04:03:45 2002 Tanaka Akira * lib/open-uri.rb: new file. @@ -78,6 +119,11 @@ Fri Dec 13 23:42:16 2002 WATANABE Hirofumi * ext/dbm/extconf.rb (db_check): check existence of the function in the specified library before checking it in libc. +Fri Dec 13 17:15:49 2002 Yukihiro Matsumoto + + * variable.c (generic_ivar_get): should always warn uninitialized + instance variables. + Fri Dec 13 12:33:22 2002 Nobuyoshi Nakada * parse.y (expr): rescue clause was ignored. diff --git a/array.c b/array.c index e6c605db14..927d4dcbde 100644 --- a/array.c +++ b/array.c @@ -204,6 +204,13 @@ to_ary(ary) return rb_convert_type(ary, T_ARRAY, "Array", "to_ary"); } +VALUE +rb_check_array_type(ary) + VALUE ary; +{ + return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary"); +} + static VALUE rb_ary_replace _((VALUE, VALUE)); static VALUE @@ -225,7 +232,7 @@ rb_ary_initialize(argc, argv, ary) } if (argc == 1 && !FIXNUM_P(size)) { - val = rb_check_convert_type(size, T_ARRAY, "Array", "to_ary"); + val = rb_check_array_type(size); if (!NIL_P(val)) { rb_ary_replace(ary, val); return ary; diff --git a/bignum.c b/bignum.c index f3ab04ab8e..03c2d7d198 100644 --- a/bignum.c +++ b/bignum.c @@ -853,7 +853,7 @@ rb_big_cmp(x, y) return rb_dbl_cmp(rb_big2dbl(x), RFLOAT(y)->value); default: - return rb_num_coerce_bin(x, y); + return rb_num_coerce_cmp(x, y); } if (RBIGNUM(x)->sign > RBIGNUM(y)->sign) return INT2FIX(1); @@ -1749,7 +1749,6 @@ Init_Bignum() rb_define_method(rb_cBignum, "<=>", rb_big_cmp, 1); rb_define_method(rb_cBignum, "==", rb_big_eq, 1); - rb_define_method(rb_cBignum, "===", rb_big_eq, 1); rb_define_method(rb_cBignum, "eql?", rb_big_eql, 1); rb_define_method(rb_cBignum, "hash", rb_big_hash, 0); rb_define_method(rb_cBignum, "to_f", rb_big_to_f, 0); diff --git a/class.c b/class.c index e680dc7a67..6aed94a992 100644 --- a/class.c +++ b/class.c @@ -365,12 +365,7 @@ rb_include_module(klass, module) if (NIL_P(module)) return; if (klass == module) return; - switch (TYPE(module)) { - case T_MODULE: - case T_CLASS: - case T_ICLASS: - break; - default: + if (TYPE(module) != T_MODULE) { Check_Type(module, T_MODULE); } diff --git a/configure.in b/configure.in index a00dc0d7cb..bc6b58ea38 100644 --- a/configure.in +++ b/configure.in @@ -354,7 +354,7 @@ AC_HEADER_SYS_WAIT AC_CHECK_HEADERS(stdlib.h string.h unistd.h limits.h sys/file.h sys/ioctl.h\ fcntl.h sys/fcntl.h sys/select.h sys/time.h sys/times.h sys/param.h\ syscall.h pwd.h a.out.h utime.h memory.h direct.h sys/resource.h \ - sys/mkdev.h sys/utime.h) + sys/mkdev.h sys/utime.h float.h) dnl Checks for typedefs, structures, and compiler characteristics. AC_TYPE_UID_T diff --git a/eval.c b/eval.c index 86c22ab0d0..b7662c9428 100644 --- a/eval.c +++ b/eval.c @@ -1881,6 +1881,7 @@ is_defined(self, node, buf) goto check_bound; case NODE_CALL: + case NODE_ATTRASGN: PUSH_TAG(PROT_NONE); if ((state = EXEC_TAG()) == 0) { val = rb_eval(self, node->nd_recv); @@ -1909,7 +1910,9 @@ is_defined(self, node, buf) } else if (!rb_method_boundp(val, node->nd_mid, call)) break; - return arg_defined(self, node->nd_args, buf, "method"); + return arg_defined(self, node->nd_args, buf, + nd_type(node) == NODE_ATTRASGN ? + "assignment" : "method"); } break; @@ -1936,7 +1939,6 @@ is_defined(self, node, buf) return "false"; case NODE_ATTRSET: - case NODE_ATTRASGN: case NODE_OP_ASGN1: case NODE_OP_ASGN2: case NODE_MASGN: @@ -5039,7 +5041,7 @@ compile(src, file, line) NODE *node; ruby_nerrs = 0; - Check_Type(src, T_STRING); + StringValue(src); node = rb_compile_string(file, src, line); if (ruby_nerrs == 0) return node; diff --git a/file.c b/file.c index f8a0037e3c..4905de9f0b 100644 --- a/file.c +++ b/file.c @@ -1826,13 +1826,11 @@ test_check(n, argc, argv) for (i=1; ilen != len) goto too_short; if (OBJ_TAINTED(str)) arg->taint = Qtrue; } diff --git a/numeric.c b/numeric.c index c1e064588b..0da0e64279 100644 --- a/numeric.c +++ b/numeric.c @@ -13,10 +13,19 @@ #include "ruby.h" #include #include + #if defined(__FreeBSD__) && __FreeBSD__ < 4 #include #endif +#ifdef HAVE_FLOAT_H +#include +#endif + +#ifndef DBL_EPSILON +#define 2.2204460492503131E-16 +#endif + static ID id_coerce, id_to_i, id_div; VALUE rb_cNumeric; @@ -61,32 +70,46 @@ coerce_rescue(x) return Qnil; /* dummy */ } -static void -do_coerce(x, y) +static int +do_coerce(x, y, err) VALUE *x, *y; + int err; { VALUE ary; VALUE a[2]; a[0] = *x; a[1] = *y; - ary = rb_rescue(coerce_body, (VALUE)a, coerce_rescue, (VALUE)a); + ary = rb_rescue(coerce_body, (VALUE)a, err?coerce_rescue:0, (VALUE)a); if (TYPE(ary) != T_ARRAY || RARRAY(ary)->len != 2) { - rb_raise(rb_eTypeError, "coerce must return [x, y]"); + if (err) { + rb_raise(rb_eTypeError, "coerce must return [x, y]"); + } + return Qfalse; } *x = RARRAY(ary)->ptr[0]; *y = RARRAY(ary)->ptr[1]; + return Qtrue; } VALUE rb_num_coerce_bin(x, y) VALUE x, y; { - do_coerce(&x, &y); + do_coerce(&x, &y, Qtrue); return rb_funcall(x, rb_frame_last_func(), 1, y); } +VALUE +rb_num_coerce_cmp(x, y) + VALUE x, y; +{ + if (do_coerce(&x, &y, Qfalse)) + return rb_funcall(x, rb_frame_last_func(), 1, y); + return Qnil; +} + static VALUE num_copy_object(x, y) VALUE x, y; @@ -110,7 +133,7 @@ num_uminus(num) VALUE zero; zero = INT2FIX(0); - do_coerce(&zero, &num); + do_coerce(&zero, &num, Qtrue); return rb_funcall(zero, '-', 1, num); } @@ -494,7 +517,7 @@ rb_dbl_cmp(a, b) if (a == b) return INT2FIX(0); if (a > b) return INT2FIX(1); if (a < b) return INT2FIX(-1); - rb_raise(rb_eFloatDomainError, "comparing NaN"); + return Qnil; } static VALUE @@ -518,7 +541,7 @@ flo_cmp(x, y) break; default: - return rb_num_coerce_bin(x, y); + return rb_num_coerce_cmp(x, y); } return rb_dbl_cmp(a, b); } @@ -824,7 +847,7 @@ num_step(argc, argv, from) } } else if (TYPE(from) == T_FLOAT || TYPE(to) == T_FLOAT || TYPE(step) == T_FLOAT) { - const double epsilon = 2.2204460492503131E-16; + const double epsilon = DBL_EPSILON; double beg = NUM2DBL(from); double end = NUM2DBL(to); double unit = NUM2DBL(step); @@ -1322,7 +1345,7 @@ fix_cmp(x, y) return INT2FIX(-1); } else { - return rb_num_coerce_bin(x, y); + return rb_num_coerce_cmp(x, y); } } diff --git a/object.c b/object.c index 7eed51b3d8..736348279d 100644 --- a/object.c +++ b/object.c @@ -651,9 +651,7 @@ rb_mod_cmp(mod, arg) case T_CLASS: break; default: - rb_raise(rb_eTypeError, "<=> requires Class or Module (%s given)", - rb_class2name(CLASS_OF(arg))); - break; + return Qnil; } if (rb_mod_le(mod, arg)) { diff --git a/parse.y b/parse.y index 74d4d09039..b1d2bfe8f8 100644 --- a/parse.y +++ b/parse.y @@ -4687,7 +4687,7 @@ aryset(recv, idx) NODE *recv, *idx; { value_expr(recv); - return NEW_CALL(recv, tASET, idx); + return NEW_ATTRASGN(recv, tASET, idx); } ID diff --git a/range.c b/range.c index a004f408ec..bb12bc38c2 100644 --- a/range.c +++ b/range.c @@ -123,6 +123,7 @@ r_lt(a, b) { VALUE r = rb_funcall(a, id_cmp, 1, b); + if (NIL_P(r)) return Qfalse; if (rb_cmpint(r) < 0) return Qtrue; return Qfalse; } @@ -133,16 +134,19 @@ r_le(a, b) { VALUE r = rb_funcall(a, id_cmp, 1, b); + if (NIL_P(r)) return Qfalse; if (rb_cmpint(r) <= 0) return Qtrue; return Qfalse; } + static int r_gt(a,b) VALUE a, b; { VALUE r = rb_funcall(a, id_cmp, 1, b); + if (NIL_P(r)) return Qfalse; if (rb_cmpint(r) > 0) return Qtrue; return Qfalse; } @@ -474,12 +478,13 @@ range_include(range, val) beg = rb_ivar_get(range, id_beg); end = rb_ivar_get(range, id_end); - if (r_gt(beg, val)) return Qfalse; - if (EXCL(range)) { - if (r_lt(val, end)) return Qtrue; - } - else { - if (r_le(val, end)) return Qtrue; + if (r_le(beg, val)) { + if (EXCL(range)) { + if (r_lt(val, end)) return Qtrue; + } + else { + if (r_le(val, end)) return Qtrue; + } } return Qfalse; } diff --git a/re.c b/re.c index 52fca7c2e4..0035f24624 100644 --- a/re.c +++ b/re.c @@ -1109,6 +1109,27 @@ rb_reg_match(re, str) return LONG2FIX(start); } +VALUE +rb_reg_eqq(re, str) + VALUE re, str; +{ + long start; + + if (TYPE(str) != T_STRING) { + str = rb_check_string_type(str); + if (NIL_P(str)) { + rb_backref_set(Qnil); + return Qfalse; + } + } + StringValue(str); + start = rb_reg_search(re, str, 0, 0); + if (start < 0) { + return Qfalse; + } + return Qtrue; +} + VALUE rb_reg_match2(re) VALUE re; @@ -1583,7 +1604,7 @@ Init_Regexp() rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1); rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1); rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1); - rb_define_method(rb_cRegexp, "===", rb_reg_match, 1); + rb_define_method(rb_cRegexp, "===", rb_reg_eqq, 1); rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0); rb_define_method(rb_cRegexp, "match", rb_reg_match_m, 1); rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0); diff --git a/signal.c b/signal.c index 44ada105dc..0526fbfd75 100644 --- a/signal.c +++ b/signal.c @@ -234,7 +234,7 @@ rb_f_kill(argc, argv) { VALUE str; - str = rb_check_convert_type(argv[0], T_STRING, "String", "to_str"); + str = rb_check_string_type(argv[0]); if (!NIL_P(str)) { s = RSTRING(str)->ptr; goto str_signal; diff --git a/string.c b/string.c index 26d1cbf2af..23a804a829 100644 --- a/string.c +++ b/string.c @@ -468,6 +468,13 @@ rb_string_value_ptr(ptr) return RSTRING(s)->ptr; } +VALUE +rb_check_string_type(str) + VALUE str; +{ + return rb_check_convert_type(str, T_STRING, "String", "to_str"); +} + VALUE rb_str_substr(str, beg, len) VALUE str; @@ -763,7 +770,7 @@ rb_str_equal(str1, str2) { if (str1 == str2) return Qtrue; if (TYPE(str2) != T_STRING) { - str2 = rb_check_convert_type(str2, T_STRING, "String", "to_str"); + str2 = rb_check_string_type(str2); if (NIL_P(str2)) return Qfalse; } @@ -794,7 +801,10 @@ rb_str_cmp_m(str1, str2) { int result; - StringValue(str2); + if (TYPE(str2) != T_STRING) { + str2 = rb_check_string_type(str2); + if (NIL_P(str2)) return Qnil; + } result = rb_str_cmp(str1, str2); return INT2FIX(result); } @@ -1428,7 +1438,7 @@ get_pat(pat, quote) break; default: - val = rb_check_convert_type(pat, T_STRING, "String", "to_str"); + val = rb_check_string_type(pat); if (NIL_P(val)) { Check_Type(pat, T_REGEXP); } diff --git a/variable.c b/variable.c index 926cdb9160..aec1d92a64 100644 --- a/variable.c +++ b/variable.c @@ -816,11 +816,15 @@ generic_ivar_get(obj, id) st_table *tbl; VALUE val; - if (!generic_iv_tbl) return Qnil; - if (!st_lookup(generic_iv_tbl, obj, &tbl)) return Qnil; - if (st_lookup(tbl, id, &val)) { - return val; + if (generic_iv_tbl) { + if (st_lookup(generic_iv_tbl, obj, &tbl)) { + if (st_lookup(tbl, id, &val)) { + return val; + } + } } + + rb_warning("instance variable %s not initialized", rb_id2name(id)); return Qnil; } -- cgit v1.2.3