aboutsummaryrefslogtreecommitdiffstats
path: root/error.c
diff options
context:
space:
mode:
author卜部昌平 <shyouhei@ruby-lang.org>2020-06-24 10:58:13 +0900
committer卜部昌平 <shyouhei@ruby-lang.org>2020-06-29 11:05:41 +0900
commit801752f577712b1eb81de224743865fce8f21adf (patch)
tree6b76bfec4fe7cc5fdc6145e1f199dad16abf1b94 /error.c
parent2071c61e4216b5de347b327acd60fa1b4affeec2 (diff)
downloadruby-801752f577712b1eb81de224743865fce8f21adf.tar.gz
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.
Diffstat (limited to 'error.c')
-rw-r--r--error.c31
1 files changed, 18 insertions, 13 deletions
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);
}