builtin_class_name: add variant that return VALUE

Found that `if (builtin_class_name) { printf } else { printf }` happens
twice.  It would be better if we could eliminate those if statements.
This commit is contained in:
卜部昌平 2020-06-24 10:58:13 +09:00
parent 2071c61e42
commit 801752f577
Notes: git 2020-06-29 11:06:34 +09:00

31
error.c
View File

@ -784,6 +784,17 @@ rb_builtin_type_name(int t)
return 0; return 0;
} }
static VALUE
displaying_class_of(VALUE x)
{
switch (x) {
case Qfalse: return rb_fstring_cstr("false");
case Qnil: return rb_fstring_cstr("nil");
case Qtrue: return rb_fstring_cstr("true");
default: return rb_obj_class(x);
}
}
static const char * static const char *
builtin_class_name(VALUE x) builtin_class_name(VALUE x)
{ {
@ -831,13 +842,8 @@ unexpected_type(VALUE x, int xt, int t)
VALUE mesg, exc = rb_eFatal; VALUE mesg, exc = rb_eFatal;
if (tname) { if (tname) {
const char *cname = builtin_class_name(x); mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
if (cname) displaying_class_of(x), tname);
mesg = rb_sprintf("wrong argument type %s (expected %s)",
cname, tname);
else
mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
rb_obj_class(x), tname);
exc = rb_eTypeError; exc = rb_eTypeError;
} }
else if (xt > T_MASK && xt <= 0x3f) { else if (xt > T_MASK && xt <= 0x3f) {
@ -905,24 +911,23 @@ rb_typeddata_is_instance_of(VALUE obj, const rb_data_type_t *data_type)
void * void *
rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type) rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
{ {
const char *etype; VALUE actual;
if (!RB_TYPE_P(obj, T_DATA)) { if (!RB_TYPE_P(obj, T_DATA)) {
etype = builtin_class_name(obj); actual = displaying_class_of(obj);
} }
else if (!RTYPEDDATA_P(obj)) { else if (!RTYPEDDATA_P(obj)) {
etype = builtin_class_name(obj); actual = displaying_class_of(obj);
} }
else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) { else if (!rb_typeddata_inherited_p(RTYPEDDATA_TYPE(obj), data_type)) {
etype = RTYPEDDATA_TYPE(obj)->wrap_struct_name; const char *name = RTYPEDDATA_TYPE(obj)->wrap_struct_name;
actual = rb_str_new_cstr(name); /* or rb_fstring_cstr? not sure... */
} }
else { else {
return DATA_PTR(obj); return DATA_PTR(obj);
} }
/* rb_obj_classname() cannot be used. A class name can be non-ASCII. */
const char *expected = data_type->wrap_struct_name; const char *expected = data_type->wrap_struct_name;
VALUE actual = (etype) ? rb_str_new_cstr(etype) : rb_obj_class(obj);
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)", rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
actual, expected); actual, expected);
} }