object.c: Call functions directly from `mrb_ensure_int_type()`; #5622

parent d34e1cb8
...@@ -117,13 +117,13 @@ mrb_complex_to_f(mrb_state *mrb, mrb_value self) ...@@ -117,13 +117,13 @@ mrb_complex_to_f(mrb_state *mrb, mrb_value self)
return mrb_float_value(mrb, p->real); return mrb_float_value(mrb, p->real);
} }
static mrb_value mrb_value
complex_to_i(mrb_state *mrb, mrb_value self) mrb_complex_to_i(mrb_state *mrb, mrb_value self)
{ {
struct mrb_complex *p = complex_ptr(mrb, self); struct mrb_complex *p = complex_ptr(mrb, self);
if (p->imaginary != 0) { if (p->imaginary != 0) {
mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Float", self); mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %v into Integer", self);
} }
return mrb_int_value(mrb, (mrb_int)p->real); return mrb_int_value(mrb, (mrb_int)p->real);
} }
...@@ -416,7 +416,7 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb) ...@@ -416,7 +416,7 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb)
mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_f", mrb_complex_to_f, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "to_f", mrb_complex_to_f, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_i", complex_to_i, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "to_i", mrb_complex_to_i, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE());
mrb_define_method(mrb, comp, "+", complex_add, MRB_ARGS_REQ(1)); mrb_define_method(mrb, comp, "+", complex_add, MRB_ARGS_REQ(1));
mrb_define_method(mrb, comp, "-", complex_sub, MRB_ARGS_REQ(1)); mrb_define_method(mrb, comp, "-", complex_sub, MRB_ARGS_REQ(1));
......
...@@ -299,8 +299,8 @@ mrb_rational_to_f(mrb_state *mrb, mrb_value self) ...@@ -299,8 +299,8 @@ mrb_rational_to_f(mrb_state *mrb, mrb_value self)
} }
#endif #endif
static mrb_value mrb_value
rational_to_i(mrb_state *mrb, mrb_value self) mrb_rational_to_i(mrb_state *mrb, mrb_value self)
{ {
struct mrb_rational *p = rational_ptr(mrb, self); struct mrb_rational *p = rational_ptr(mrb, self);
if (p->denominator == 0) { if (p->denominator == 0) {
...@@ -714,7 +714,7 @@ void mrb_mruby_rational_gem_init(mrb_state *mrb) ...@@ -714,7 +714,7 @@ void mrb_mruby_rational_gem_init(mrb_state *mrb)
#ifndef MRB_NO_FLOAT #ifndef MRB_NO_FLOAT
mrb_define_method(mrb, rat, "to_f", mrb_rational_to_f, MRB_ARGS_NONE()); mrb_define_method(mrb, rat, "to_f", mrb_rational_to_f, MRB_ARGS_NONE());
#endif #endif
mrb_define_method(mrb, rat, "to_i", rational_to_i, MRB_ARGS_NONE()); mrb_define_method(mrb, rat, "to_i", mrb_rational_to_i, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "to_r", rational_to_r, MRB_ARGS_NONE()); mrb_define_method(mrb, rat, "to_r", rational_to_r, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "negative?", rational_negative_p, MRB_ARGS_NONE()); mrb_define_method(mrb, rat, "negative?", rational_negative_p, MRB_ARGS_NONE());
mrb_define_method(mrb, rat, "==", rational_eq, MRB_ARGS_REQ(1)); mrb_define_method(mrb, rat, "==", rational_eq, MRB_ARGS_REQ(1));
......
...@@ -484,6 +484,17 @@ mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c) ...@@ -484,6 +484,17 @@ mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c)
return FALSE; return FALSE;
} }
#ifdef MRB_USE_RATIONAL
// provided by mruby-rational with MRB_USE_RATIONAL
mrb_value mrb_rational_to_i(mrb_state *mrb, mrb_value rat);
mrb_value mrb_rational_to_f(mrb_state *mrb, mrb_value rat);
#endif
#ifdef MRB_USE_COMPLEX
// provided by mruby-complex with MRB_USE_COMPLEX
mrb_value mrb_complex_to_f(mrb_state *mrb, mrb_value comp);
mrb_value mrb_complex_to_i(mrb_state *mrb, mrb_value comp);
#endif
MRB_API mrb_value MRB_API mrb_value
mrb_ensure_int_type(mrb_state *mrb, mrb_value val) mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
{ {
...@@ -492,6 +503,20 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val) ...@@ -492,6 +503,20 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
if (mrb_float_p(val)) { if (mrb_float_p(val)) {
return mrb_float_to_integer(mrb, val); return mrb_float_to_integer(mrb, val);
} }
else {
switch (mrb_type(val)) {
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_rational_to_i(mrb, val);
#endif
#ifdef MRB_USE_COMPLEX
case MRB_TT_COMPLEX:
return mrb_complex_to_i(mrb, val);
#endif
default:
break;
}
}
#endif #endif
mrb_raisef(mrb, E_TYPE_ERROR, "%Y cannot be converted to Integer", val); mrb_raisef(mrb, E_TYPE_ERROR, "%Y cannot be converted to Integer", val);
} }
...@@ -499,9 +524,6 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val) ...@@ -499,9 +524,6 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
} }
#ifndef MRB_NO_FLOAT #ifndef MRB_NO_FLOAT
mrb_value mrb_complex_to_f(mrb_state *mrb, mrb_value comp); // provided by mruby-complex with MRB_USE_COMPLEX
mrb_value mrb_rational_to_f(mrb_state *mrb, mrb_value rat); // provided by mruby-rational with MRB_USE_RATIONAL
MRB_API mrb_value MRB_API mrb_value
mrb_ensure_float_type(mrb_state *mrb, mrb_value val) mrb_ensure_float_type(mrb_state *mrb, mrb_value val)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment