diff options
| author | KOBAYASHI Shuji <[email protected]> | 2019-01-08 20:31:29 +0900 |
|---|---|---|
| committer | KOBAYASHI Shuji <[email protected]> | 2019-01-08 20:43:23 +0900 |
| commit | 68735d12614ef72b620736a5cd3052fb79445483 (patch) | |
| tree | b829d0e0e27fb250f9df3b873df2e253043c1efd /src/dump.c | |
| parent | 817436c33b556ba4129ae6cf45998d51ff7b9351 (diff) | |
| download | mruby-68735d12614ef72b620736a5cd3052fb79445483.tar.gz mruby-68735d12614ef72b620736a5cd3052fb79445483.zip | |
Fix dump/load float leteral evaluate to infinity
Example:
# example.rb
p(2e308)
p(-2e308)
Good:
$ bin/mruby example.rb
inf
-inf
Bad:
$ bin/mrbc example.rb
$ bin/mruby -b example.mrb
0
-0
Cause:
Float infinity representation is `inf` on dump and it is converted by
corresponding `String#to_f` on load.
Treatment:
- Introduce new representations (`i`: +infinity, `I`: -infinity)
- Allow old representations (`inf`, `-inf`, `infinity`, `-infinity`) too
- Raise error for unknown representations (use corresponding `Kernel#Float`)
Diffstat (limited to 'src/dump.c')
| -rw-r--r-- | src/dump.c | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/dump.c b/src/dump.c index 6ce9c4eb9..11eba4e40 100644 --- a/src/dump.c +++ b/src/dump.c @@ -6,6 +6,7 @@ #include <string.h> #include <limits.h> +#include <math.h> #include <mruby/dump.h> #include <mruby/string.h> #include <mruby/irep.h> @@ -90,6 +91,18 @@ write_iseq_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf, uint8_t flags) return cur - buf; } +#ifndef MRB_WITHOUT_FLOAT +static mrb_value +float_to_str(mrb_state *mrb, mrb_value flt) +{ + mrb_float f = mrb_float(flt); + + if (isinf(f)) { + return f < 0 ? mrb_str_new_lit(mrb, "I") : mrb_str_new_lit(mrb, "i"); + } + return mrb_float_to_str(mrb, flt, MRB_FLOAT_FMT); +} +#endif static size_t get_pool_block_size(mrb_state *mrb, mrb_irep *irep) @@ -116,7 +129,7 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep) #ifndef MRB_WITHOUT_FLOAT case MRB_TT_FLOAT: - str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT); + str = float_to_str(mrb, irep->pool[pool_no]); { mrb_int len = RSTRING_LEN(str); mrb_assert_int_fit(mrb_int, len, size_t, SIZE_MAX); @@ -165,7 +178,7 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, uint8_t *buf) #ifndef MRB_WITHOUT_FLOAT case MRB_TT_FLOAT: cur += uint8_to_bin(IREP_TT_FLOAT, cur); /* data type */ - str = mrb_float_to_str(mrb, irep->pool[pool_no], MRB_FLOAT_FMT); + str = float_to_str(mrb, irep->pool[pool_no]); break; #endif |
