Skip to content

Commit

Permalink
let's get rid of Fixnum and Float boxing classes and use NSNumbers
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@3753 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
lrz committed Mar 13, 2010
1 parent d0c3a39 commit fe7c396
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 146 deletions.
8 changes: 1 addition & 7 deletions class.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,9 @@ rb_obj_imp_copyWithZone(void *rcv, SEL sel, void *zone)
static BOOL
rb_obj_imp_isEqual(void *rcv, SEL sel, void *obj)
{
if (obj == NULL)
if (obj == NULL) {
return false;

if (*(Class *)rcv == (Class)rb_cFixnum
&& *(Class *)obj == (Class)rb_cFixnum) {
/* XXX check if Numeric#== is not overriden */
return RFIXNUM(rcv)->value == RFIXNUM(obj)->value;
}

VALUE arg = OC2RB(obj);
return rb_vm_call((VALUE)rcv, selEq, 1, &arg, false) == Qtrue;
}
Expand Down
30 changes: 1 addition & 29 deletions include/ruby/ruby.h
Original file line number Diff line number Diff line change
Expand Up @@ -551,34 +551,10 @@ CFMutableDictionaryRef rb_class_ivar_dict_or_create(VALUE);
void rb_class_ivar_set_dict(VALUE, CFMutableDictionaryRef);
#endif

struct RFloat {
struct RBasic basic;
double float_value;
};

static inline double
__rb_float_value(VALUE v)
{
return FIXFLOAT_P(v) ? FIXFLOAT2DBL(v) : ((struct RFloat *)v)->float_value;
}
#define RFLOAT_VALUE(v) (__rb_float_value((VALUE)v))
#define RFLOAT_VALUE(v) FIXFLOAT2DBL(v)
#define DOUBLE2NUM(dbl) rb_float_new(dbl)
#define DBL2NUM DOUBLE2NUM

#if WITH_OBJC
struct RFixnum {
VALUE klass;
long value;
};

struct RSymbol {
VALUE klass;
char *str;
unsigned int len;
ID id;
};
#endif

#define ELTS_SHARED FL_USER2

#if !WITH_OBJC
Expand Down Expand Up @@ -768,10 +744,6 @@ struct RBignum {
#define R_CAST(st) (struct st*)
#define RBASIC(obj) (R_CAST(RBasic)(obj))
#define ROBJECT(obj) (R_CAST(RObject)(obj))
#define RFLOAT(obj) (R_CAST(RFloat)(obj))
#if WITH_OBJC
# define RFIXNUM(obj) (R_CAST(RFixnum)(obj))
#endif
#define RDATA(obj) (R_CAST(RData)(obj))
#define RSTRUCT(obj) (R_CAST(RStruct)(obj))
#define RBIGNUM(obj) (R_CAST(RBignum)(obj))
Expand Down
122 changes: 12 additions & 110 deletions numeric.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,12 @@ VALUE rb_cFixnum;
VALUE rb_eZeroDivError;
VALUE rb_eFloatDomainError;

static CFMutableDictionaryRef fixnum_cache = NULL;

static inline VALUE
rb_box_fixfloat0(double v)
static VALUE
rb_box_fixfloat0(double value)
{
NEWOBJ(val, struct RFloat);
OBJSETUP(val, rb_cFloat, T_FLOAT);
val->float_value = v;
return (VALUE)val;
CFNumberRef number = CFNumberCreate(NULL, kCFNumberDoubleType, &value);
CFMakeCollectable(number);
return (VALUE)number;
}

VALUE
Expand All @@ -110,25 +107,12 @@ rb_box_fixfloat(VALUE fixfloat)
return rb_box_fixfloat0(NUM2DBL(fixfloat));
}

static inline VALUE
static VALUE
rb_box_fixnum0(long value)
{
if (fixnum_cache == NULL) {
fixnum_cache = CFDictionaryCreateMutable(NULL, 0, NULL,
&kCFTypeDictionaryValueCallBacks);
}

struct RFixnum *val = (struct RFixnum *)CFDictionaryGetValue(fixnum_cache,
(const void *)value);
if (val == NULL) {
NEWOBJ(fixval, struct RFixnum);
fixval->klass = rb_cFixnum;
fixval->value = value;
val = fixval;
CFDictionarySetValue(fixnum_cache, (const void *)value, val);
}

return (VALUE)val;
CFNumberRef number = CFNumberCreate(NULL, kCFNumberLongType, &value);
CFMakeCollectable(number);
return (VALUE)number;
}

VALUE
Expand Down Expand Up @@ -3298,58 +3282,6 @@ fix_even_p(VALUE num, SEL sel)
return Qtrue;
}

static VALUE
imp_rb_float_copyWithZone(void *rcv, SEL sel, void *zone)
{
// XXX honor zone?
return rb_box_fixfloat0(RFLOAT_VALUE(rcv));
}

static VALUE
imp_rb_fixnum_copyWithZone(void *rcv, SEL sel, void *zone)
{
// XXX honor zone?
return rb_box_fixnum0(RFIXNUM(rcv)->value);
}

static const char *
imp_rb_float_objCType(void *rcv, SEL sel)
{
return "d";
}

static const char *
imp_rb_fixnum_objCType(void *rcv, SEL sel)
{
return "l";
}

static void
imp_rb_float_getValue(void *rcv, SEL sel, void *buffer)
{
double v = RFLOAT_VALUE(rcv);
*(double *)buffer = v;
}

static void
imp_rb_fixnum_getValue(void *rcv, SEL sel, void *buffer)
{
long v = RFIXNUM(rcv)->value;
*(long *)buffer = v;
}

static double
imp_rb_float_doubleValue(void *rcv, SEL sel)
{
return RFLOAT_VALUE(rcv);
}

static long long
imp_rb_fixnum_longValue(void *rcv, SEL sel)
{
return RFIXNUM(rcv)->value;
}

static void *
imp_nsnumber_to_int(void *rcv, SEL sel)
{
Expand All @@ -3365,37 +3297,6 @@ imp_nsnumber_to_int(void *rcv, SEL sel)
return (void *)new_num;
}

static void
rb_install_nsnumber_primitives(void)
{
Class klass;

klass = (Class)rb_cFloat;
rb_objc_install_method2(klass, "copyWithZone:",
(IMP)imp_rb_float_copyWithZone);
rb_objc_install_method2(klass, "objCType", (IMP)imp_rb_float_objCType);
rb_objc_install_method2(klass, "getValue:", (IMP)imp_rb_float_getValue);
rb_objc_install_method2(klass, "doubleValue",
(IMP)imp_rb_float_doubleValue);

klass = (Class)rb_cFixnum;
rb_objc_install_method2(klass, "copyWithZone:",
(IMP)imp_rb_fixnum_copyWithZone);
rb_objc_install_method2(klass, "objCType", (IMP)imp_rb_fixnum_objCType);
rb_objc_install_method2(klass, "getValue:", (IMP)imp_rb_fixnum_getValue);
rb_objc_install_method2(klass, "longValue", (IMP)imp_rb_fixnum_longValue);

klass = (Class)rb_cNSNumber;
class_replaceMethod(klass, sel_registerName("to_int"),
(IMP)imp_nsnumber_to_int, "@@:");
}

#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
# define NSCFNUMBER_CNAME "NSCFNumber"
#else
# define NSCFNUMBER_CNAME "__NSCFNumber"
#endif

void
Init_Numeric(void)
{
Expand Down Expand Up @@ -3577,6 +3478,7 @@ Init_Numeric(void)
rb_objc_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
rb_objc_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
rb_objc_define_method(rb_cFloat, "finite?", flo_is_finite_p, 0);

rb_install_nsnumber_primitives();

class_replaceMethod((Class)rb_cNSNumber, sel_registerName("to_int"),
(IMP)imp_nsnumber_to_int, "@@:");
}

0 comments on commit fe7c396

Please sign in to comment.