Refactor check before `mrb_class_ptr()`; ref
MODIFY
src/class.c
src/class.c
@@ -123,6 +123,19 @@ module_from_sym(mrb_state *mrb, struct RClass *klass, mrb_sym id)
return mrb_class_ptr(c);
}
+static mrb_bool
+class_ptr_p(mrb_value obj)
+{
+ switch (mrb_type(obj)) {
+ case MRB_TT_CLASS:
+ case MRB_TT_SCLASS:
+ case MRB_TT_MODULE:
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
MRB_API struct RClass*
mrb_class_outer_module(mrb_state *mrb, struct RClass *c)
{
@@ -137,14 +150,8 @@ mrb_class_outer_module(mrb_state *mrb, struct RClass *c)
mrb_value klass;
klass = mrb_obj_iv_get(mrb, (struct RObject *)cls,
mrb_intern_lit(mrb, "__attached__"));
- switch (mrb_type(klass)) {
- case MRB_TT_CLASS:
- case MRB_TT_SCLASS:
- case MRB_TT_MODULE:
+ if (class_ptr_p(klass)) {
cls = mrb_class_ptr(klass);
- break;
- default:
- break;
}
}
return cls;
@@ -153,12 +160,7 @@ mrb_class_outer_module(mrb_state *mrb, struct RClass *c)
static void
check_if_class_or_module(mrb_state *mrb, mrb_value obj)
{
- switch (mrb_type(obj)) {
- case MRB_TT_CLASS:
- case MRB_TT_SCLASS:
- case MRB_TT_MODULE:
- return;
- default:
+ if (!class_ptr_p(obj)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a class/module", mrb_inspect(mrb, obj));
}
}
@@ -616,14 +618,8 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
mrb_value ss;
ss = ARGV[arg_i++];
- switch (mrb_type(ss)) {
- case MRB_TT_CLASS:
- case MRB_TT_MODULE:
- case MRB_TT_SCLASS:
- break;
- default:
+ if (!class_ptr_p(ss)) {
mrb_raisef(mrb, E_TYPE_ERROR, "%S is not class/module", ss);
- break;
}
*p = ss;
i++;
@@ -1772,15 +1768,11 @@ mrb_mod_to_s(mrb_state *mrb, mrb_value klass)
str = mrb_str_new_lit(mrb, "#<Class:");
- switch (mrb_type(v)) {
- case MRB_TT_CLASS:
- case MRB_TT_MODULE:
- case MRB_TT_SCLASS:
+ if (class_ptr_p(v)) {
mrb_str_cat_str(mrb, str, mrb_inspect(mrb, v));
- break;
- default:
+ }
+ else {
mrb_str_cat_str(mrb, str, mrb_any_to_s(mrb, v));
- break;
}
return mrb_str_cat_lit(mrb, str, ">");
}

Refactor check before `mrb_class_ptr()`; ref #3602