Skip to content

Commit

Permalink
Refactor check before mrb_class_ptr(); ref #3602
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Apr 12, 2017
1 parent a25404c commit 918e7bb
Showing 1 changed file with 21 additions and 29 deletions.
50 changes: 21 additions & 29 deletions src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
Expand All @@ -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));
}
}
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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:
mrb_str_cat_str(mrb, str, mrb_inspect(mrb, v));
break;
default:
mrb_str_cat_str(mrb, str, mrb_any_to_s(mrb, v));
break;
if (class_ptr_p(v)) {
mrb_str_cat_str(mrb, str, mrb_inspect(mrb, v));
}
else {
mrb_str_cat_str(mrb, str, mrb_any_to_s(mrb, v));
}
return mrb_str_cat_lit(mrb, str, ">");
}
Expand Down

0 comments on commit 918e7bb

Please sign in to comment.