1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

internal/rational.h: insert assertions in RATIONAL_SET_{NUM,DEN}

This commit is contained in:
Kenta Murata 2020-01-17 09:55:50 +09:00
parent fbc00c2d86
commit 07ce51c5aa
No known key found for this signature in database
GPG key ID: CEFE8AFB6081B062
3 changed files with 17 additions and 1 deletions

View file

@ -89,6 +89,7 @@ static inline double rb_float_flonum_value(VALUE v);
static inline double rb_float_noflonum_value(VALUE v);
static inline double rb_float_value_inline(VALUE v);
static inline VALUE rb_float_new_inline(double d);
static inline bool INT_POSITIVE_P(VALUE num);
static inline bool INT_NEGATIVE_P(VALUE num);
static inline bool FLOAT_ZERO_P(VALUE num);
#define rb_float_value rb_float_value_inline
@ -108,6 +109,17 @@ VALUE rb_float_eql(VALUE x, VALUE y);
VALUE rb_fix_aref(VALUE fix, VALUE idx);
MJIT_SYMBOL_EXPORT_END
static inline bool
INT_POSITIVE_P(VALUE num)
{
if (FIXNUM_P(num)) {
return FIXNUM_POSITIVE_P(num);
}
else {
return BIGNUM_POSITIVE_P(num);
}
}
static inline bool
INT_NEGATIVE_P(VALUE num)
{

View file

@ -12,6 +12,8 @@
#include "ruby/config.h" /* for HAVE_LIBGMP */
#include "ruby/ruby.h" /* for struct RBasic */
#include "internal/gc.h" /* for RB_OBJ_WRITE */
#include "internal/numeric.h" /* for INT_POSITIVE_P */
#include "ruby_assert.h" /* for assert */
struct RRational {
struct RBasic basic;
@ -51,12 +53,15 @@ RUBY_SYMBOL_EXPORT_END
static inline void
RATIONAL_SET_NUM(VALUE r, VALUE n)
{
assert(RB_INTEGER_TYPE_P(n));
RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n);
}
static inline void
RATIONAL_SET_DEN(VALUE r, VALUE d)
{
assert(RB_INTEGER_TYPE_P(d));
assert(INT_POSITIVE_P(d));
RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d);
}

View file

@ -37,7 +37,6 @@
#define GMP_GCD_DIGITS 1
#define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x))
#define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x))
VALUE rb_cRational;