object.c: add new function `mrb_ensure_integer_type()`.
that checks if the value is an `Integer` which may be a big integer,
where existing `mrb_ensure_int_type()` checks if the value is an Integer
**and** fits in `mrb_int`.
MODIFY
include/mruby.h
include/mruby.h
@@ -1406,6 +1406,9 @@ MRB_API mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
#define mrb_to_str(mrb, str) mrb_ensure_string_type(mrb,str)
/* obsolete: use mrb_obj_as_string() instead */
#define mrb_str_to_str(mrb, str) mrb_obj_as_string(mrb, str)
+/* check if val is an integer (including Bigint) */
+MRB_API mrb_value mrb_ensure_integer_type(mrb_state *mrb, mrb_value val);
+/* check if val fit in mrb_int */
MRB_API mrb_value mrb_ensure_int_type(mrb_state *mrb, mrb_value val);
#define mrb_as_int(mrb, val) mrb_integer(mrb_ensure_int_type(mrb, val))
/* obsolete: use mrb_ensure_int_type() instead */

MODIFY
src/object.c
src/object.c
@@ -497,7 +497,7 @@ mrb_value mrb_complex_to_i(mrb_state *mrb, mrb_value comp);
#endif
MRB_API mrb_value
-mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
+mrb_ensure_integer_type(mrb_state *mrb, mrb_value val)
{
if (!mrb_integer_p(val)) {
#ifndef MRB_NO_FLOAT
@@ -506,6 +506,10 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
}
else {
switch (mrb_type(val)) {
+#ifdef MRB_USE_BIGINT
+ case MRB_TT_BIGINT:
+ return val;
+#endif
#ifdef MRB_USE_RATIONAL
case MRB_TT_RATIONAL:
return mrb_rational_to_i(mrb, val);
@@ -524,6 +528,18 @@ mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
return val;
}
+MRB_API mrb_value
+mrb_ensure_int_type(mrb_state *mrb, mrb_value val)
+{
+ val = mrb_ensure_integer_type(mrb, val);
+#ifdef MRB_USE_BIGINT
+ if (mrb_bigint_p(val)) {
+ return mrb_int_value(mrb, mrb_bint_as_int(mrb, val));
+ }
+#endif
+ return val;
+}
+
#ifndef MRB_NO_FLOAT
MRB_API mrb_value
mrb_ensure_float_type(mrb_state *mrb, mrb_value val)

object.c: add new function `mrb_ensure_integer_type()`.

that checks if the value is an `Integer` which may be a big integer,
where existing `mrb_ensure_int_type()` checks if the value is an Integer
**and** fits in `mrb_int`.