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
Родитель 2071c61e42
Коммит 801752f577
1 изменённых файлов: 18 добавлений и 13 удалений

31
error.c
Просмотреть файл

@ -784,6 +784,17 @@ rb_builtin_type_name(int t)
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 *
builtin_class_name(VALUE x)
{
@ -831,13 +842,8 @@ unexpected_type(VALUE x, int xt, int t)
VALUE mesg, exc = rb_eFatal;
if (tname) {
const char *cname = builtin_class_name(x);
if (cname)
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);
mesg = rb_sprintf("wrong argument type %"PRIsVALUE" (expected %s)",
displaying_class_of(x), tname);
exc = rb_eTypeError;
}
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 *
rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
{
const char *etype;
VALUE actual;
if (!RB_TYPE_P(obj, T_DATA)) {
etype = builtin_class_name(obj);
actual = displaying_class_of(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)) {
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 {
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;
VALUE actual = (etype) ? rb_str_new_cstr(etype) : rb_obj_class(obj);
rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
actual, expected);
}