* array.c (rb_ary_sort_bang): respect overridden <=> for String and

Fixnum.  [ruby-core:17708]

* include/ruby/node.h (NOEX_BASIC): basic definition method flag.

* include/ruby/intern.h, vm_method.c (rb_method_basic_definition_p):
  new function to check if the method is not redefined after the
  initialization.

* vm_method.c (rb_obj_respond_to): use rb_method_basic_definition_p.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18360 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-08-04 18:29:55 +00:00
Родитель e370754189
Коммит a58e3d763b
5 изменённых файлов: 65 добавлений и 17 удалений

Просмотреть файл

@ -1,3 +1,16 @@
Tue Aug 5 03:29:52 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_sort_bang): respect overridden <=> for String and
Fixnum. [ruby-core:17708]
* include/ruby/node.h (NOEX_BASIC): basic definition method flag.
* include/ruby/intern.h, vm_method.c (rb_method_basic_definition_p):
new function to check if the method is not redefined after the
initialization.
* vm_method.c (rb_obj_respond_to): use rb_method_basic_definition_p.
Mon Aug 4 20:39:06 2008 Yuki Sonoda (Yugui) <yugui@yugui.jp>
* Makefile.in (update-rubyspec): renamed the rubyspec directory

48
array.c
Просмотреть файл

@ -1452,10 +1452,32 @@ rb_ary_reverse_m(VALUE ary)
return rb_ary_reverse(rb_ary_dup(ary));
}
struct ary_sort_data {
VALUE ary;
int opt_methods;
int opt_inited;
};
enum {
sort_opt_Fixnum,
sort_opt_String,
sort_optimizable_count
};
#define STRING_P(s) (TYPE(s) == T_STRING && CLASS_OF(s) == rb_cString)
#define SORT_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(sort_opt_,type))
#define SORT_OPTIMIZABLE(data, type) \
((data->opt_inited & SORT_OPTIMIZABLE_BIT(type)) ? \
(data->opt_methods & SORT_OPTIMIZABLE_BIT(type)) : \
((data->opt_inited |= SORT_OPTIMIZABLE_BIT(type)), \
rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
(data->opt_methods |= SORT_OPTIMIZABLE_BIT(type))))
static VALUE
sort_reentered(VALUE *klass)
sort_reentered(VALUE ary)
{
if (*klass) {
if (RBASIC(ary)->klass) {
rb_raise(rb_eRuntimeError, "sort reentered");
}
return Qnil;
@ -1464,35 +1486,37 @@ sort_reentered(VALUE *klass)
static int
sort_1(const void *ap, const void *bp, void *dummy)
{
VALUE retval = sort_reentered(dummy);
struct ary_sort_data *data = dummy;
VALUE retval = sort_reentered(data->ary);
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
int n;
retval = rb_yield_values(2, a, b);
n = rb_cmpint(retval, a, b);
sort_reentered(dummy);
sort_reentered(data->ary);
return n;
}
static int
sort_2(const void *ap, const void *bp, void *dummy)
{
VALUE retval = sort_reentered(dummy);
struct ary_sort_data *data = dummy;
VALUE retval = sort_reentered(data->ary);
VALUE a = *(const VALUE *)ap, b = *(const VALUE *)bp;
int n;
if (FIXNUM_P(a) && FIXNUM_P(b)) {
if (FIXNUM_P(a) && FIXNUM_P(b) && SORT_OPTIMIZABLE(data, Fixnum)) {
if ((long)a > (long)b) return 1;
if ((long)a < (long)b) return -1;
return 0;
}
if (TYPE(a) == T_STRING) {
if (TYPE(b) == T_STRING) return rb_str_cmp(a, b);
if (STRING_P(a) && STRING_P(b) && SORT_OPTIMIZABLE(data, String)) {
return rb_str_cmp(a, b);
}
retval = rb_funcall(a, id_cmp, 1, b);
n = rb_cmpint(retval, a, b);
sort_reentered(dummy);
sort_reentered(data->ary);
return n;
}
@ -1519,10 +1543,14 @@ rb_ary_sort_bang(VALUE ary)
rb_ary_modify(ary);
if (RARRAY_LEN(ary) > 1) {
VALUE tmp = ary_make_shared(ary);
struct ary_sort_data data;
RBASIC(tmp)->klass = 0;
data.ary = tmp;
data.opt_methods = 0;
data.opt_inited = 0;
ruby_qsort(RARRAY_PTR(tmp), RARRAY_LEN(tmp), sizeof(VALUE),
rb_block_given_p()?sort_1:sort_2, &RBASIC(tmp)->klass);
rb_block_given_p()?sort_1:sort_2, &data);
if (RARRAY(ary)->ptr != RARRAY(tmp)->ptr) {
if (!ARY_SHARED_P(ary)) xfree(RARRAY(ary)->ptr);
RARRAY(ary)->ptr = RARRAY(tmp)->ptr;

Просмотреть файл

@ -250,6 +250,7 @@ void rb_clear_cache_by_class(VALUE);
void rb_alias(VALUE, ID, ID);
void rb_attr(VALUE,ID,int,int,int);
int rb_method_boundp(VALUE, ID, int);
int rb_method_basic_definition_p(VALUE, ID);
VALUE rb_eval_cmd(VALUE, VALUE, int);
int rb_obj_respond_to(VALUE, ID, int);
int rb_respond_to(VALUE, ID);

Просмотреть файл

@ -463,7 +463,8 @@ typedef struct RNode {
#define NOEX_NOSUPER 0x01
#define NOEX_PRIVATE 0x02
#define NOEX_PROTECTED 0x04
#define NOEX_MASK 0x06 /* 1110 */
#define NOEX_MASK 0x06 /* 0110 */
#define NOEX_BASIC 0x08
#define NOEX_UNDEF NOEX_NOSUPER
@ -472,7 +473,7 @@ typedef struct RNode {
#define NOEX_VCALL 0x40
#define NOEX_SAFE(n) (((n) >> 8) & 0x0F)
#define NOEX_WITH(n, s) ((s << 8) | n)
#define NOEX_WITH(n, s) ((s << 8) | (n) | (ruby_running ? 0 : NOEX_BASIC))
#define NOEX_WITH_SAFE(n) NOEX_WITH(n, rb_safe_level())
#define CALL_PUBLIC 0

Просмотреть файл

@ -1044,6 +1044,15 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
return module;
}
int
rb_method_basic_definition_p(VALUE klass, ID id)
{
NODE *node = rb_method_node(klass, id);
if (node && (node->nd_noex & NOEX_BASIC))
return 1;
return 0;
}
/*
* call-seq:
* obj.respond_to?(symbol, include_private=false) => true or false
@ -1053,14 +1062,12 @@ rb_mod_modfunc(int argc, VALUE *argv, VALUE module)
* optional second parameter evaluates to +true+.
*/
static NODE *basic_respond_to = 0;
int
rb_obj_respond_to(VALUE obj, ID id, int priv)
{
VALUE klass = CLASS_OF(obj);
if (rb_method_node(klass, idRespond_to) == basic_respond_to) {
if (rb_method_basic_definition_p(klass, idRespond_to)) {
return rb_method_boundp(klass, id, !priv);
}
else {
@ -1108,8 +1115,6 @@ Init_eval_method(void)
#undef rb_intern
rb_define_method(rb_mKernel, "respond_to?", obj_respond_to, -1);
basic_respond_to = rb_method_node(rb_cObject, idRespond_to);
rb_register_mark_object((VALUE)basic_respond_to);
rb_define_private_method(rb_cModule, "remove_method", rb_mod_remove_method, -1);
rb_define_private_method(rb_cModule, "undef_method", rb_mod_undef_method, -1);