From 801752f577712b1eb81de224743865fce8f21adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= Date: Wed, 24 Jun 2020 10:58:13 +0900 Subject: 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. --- error.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'error.c') diff --git a/error.c b/error.c index efe7bb39ed..5cb808fb44 100644 --- a/error.c +++ b/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); } -- cgit v1.2.3