diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-01-16 19:32:29 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-01-16 19:32:29 +0900 |
| commit | 8969edf03b2f4ff8537974c2bd30e9abe2002da6 (patch) | |
| tree | 7b656bc07958593fdd70060cca368f2a6d11a9c5 | |
| parent | fa34a8b5be7b88c61d0e3bd110452588654471e1 (diff) | |
| download | mruby-8969edf03b2f4ff8537974c2bd30e9abe2002da6.tar.gz mruby-8969edf03b2f4ff8537974c2bd30e9abe2002da6.zip | |
Avoid runtime evaluation for `MRB_WITHOUT_FLOAT`
| -rw-r--r-- | mrblib/numeric.rb | 9 | ||||
| -rw-r--r-- | src/numeric.c | 17 |
2 files changed, 18 insertions, 8 deletions
diff --git a/mrblib/numeric.rb b/mrblib/numeric.rb index dfdaf9da8..5a3c5eb58 100644 --- a/mrblib/numeric.rb +++ b/mrblib/numeric.rb @@ -104,14 +104,7 @@ module Integral raise ArgumentError, "step can't be 0" if step == 0 return to_enum(:step, num, step) unless block - i = self - if Object.const_defined?(:Float) && - (kind_of?(Float) || num.kind_of?(Float) || step.kind_of?(Float)) - i = i.to_f - num = num.to_f unless num == nil - step = step.to_f - end - + i = __coerce_step_counter(num, step) if num == nil while true block.call(i) diff --git a/src/numeric.c b/src/numeric.c index fc8460300..fa9daf8a7 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -154,6 +154,22 @@ num_div(mrb_state *mrb, mrb_value x) #endif } +static mrb_value +num_coerce_step_counter(mrb_state *mrb, mrb_value self) +{ + mrb_value counter = self, num, step; + + mrb_get_args(mrb, "oo", &num, &step); + +#ifndef MRB_WITHOUT_FLOAT + if (mrb_float_p(self) || mrb_float_p(num) || mrb_float_p(step)) { + counter = mrb_funcall(mrb, counter, "to_f", 0); + } +#endif + + return counter; +} + #ifndef MRB_WITHOUT_FLOAT /******************************************************************** * @@ -1542,6 +1558,7 @@ mrb_init_numeric(mrb_state *mrb) mrb_define_method(mrb, numeric, ">=", num_ge, MRB_ARGS_REQ(1)); mrb_define_method(mrb, numeric, "finite?", num_finite_p, MRB_ARGS_NONE()); mrb_define_method(mrb, numeric, "infinite?",num_infinite_p, MRB_ARGS_NONE()); + mrb_define_method(mrb, numeric, "__coerce_step_counter", num_coerce_step_counter, MRB_ARGS_REQ(2)); /* Integer Class */ integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */ |
