mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
accept floats as bit operator operands; fix #3260
This commit is contained in:
+10
-20
@@ -809,17 +809,13 @@ fix_rev(mrb_state *mrb, mrb_value num)
|
||||
return mrb_fixnum_value(~val);
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
bit_coerce(mrb_state *mrb, mrb_value x)
|
||||
{
|
||||
while (!mrb_fixnum_p(x)) {
|
||||
if (mrb_float_p(x)) {
|
||||
mrb_raise(mrb, E_TYPE_ERROR, "can't convert Float into Integer");
|
||||
}
|
||||
x = mrb_to_int(mrb, x);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
static mrb_value flo_and(mrb_state *mrb, mrb_value x);
|
||||
static mrb_value flo_or(mrb_state *mrb, mrb_value x);
|
||||
static mrb_value flo_xor(mrb_state *mrb, mrb_value x);
|
||||
#define bit_op(x,y,op1,op2) do {\
|
||||
if (mrb_fixnum_p(y)) return mrb_fixnum_value(mrb_fixnum(x) op2 mrb_fixnum(y));\
|
||||
return flo_ ## op1(mrb, mrb_float_value(mrb, mrb_fixnum(x)));\
|
||||
} while(0)
|
||||
|
||||
/* 15.2.8.3.9 */
|
||||
/*
|
||||
@@ -835,9 +831,7 @@ fix_and(mrb_state *mrb, mrb_value x)
|
||||
mrb_value y;
|
||||
|
||||
mrb_get_args(mrb, "o", &y);
|
||||
|
||||
y = bit_coerce(mrb, y);
|
||||
return mrb_fixnum_value(mrb_fixnum(x) & mrb_fixnum(y));
|
||||
bit_op(x, y, and, &);
|
||||
}
|
||||
|
||||
/* 15.2.8.3.10 */
|
||||
@@ -854,9 +848,7 @@ fix_or(mrb_state *mrb, mrb_value x)
|
||||
mrb_value y;
|
||||
|
||||
mrb_get_args(mrb, "o", &y);
|
||||
|
||||
y = bit_coerce(mrb, y);
|
||||
return mrb_fixnum_value(mrb_fixnum(x) | mrb_fixnum(y));
|
||||
bit_op(x, y, or, |);
|
||||
}
|
||||
|
||||
/* 15.2.8.3.11 */
|
||||
@@ -873,9 +865,7 @@ fix_xor(mrb_state *mrb, mrb_value x)
|
||||
mrb_value y;
|
||||
|
||||
mrb_get_args(mrb, "o", &y);
|
||||
|
||||
y = bit_coerce(mrb, y);
|
||||
return mrb_fixnum_value(mrb_fixnum(x) ^ mrb_fixnum(y));
|
||||
bit_op(x, y, or, ^);
|
||||
}
|
||||
|
||||
#define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)
|
||||
|
||||
Reference in New Issue
Block a user