[前][次][番号順一覧][スレッド一覧]

ruby-changes:62077

From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:08:49 +0900 (JST)
Subject: [ruby-changes:62077] 801752f577 (master): builtin_class_name: add variant that return VALUE

https://git.ruby-lang.org/ruby.git/commit/?id=801752f577

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?=
 <shyouhei@r...>
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.

diff --git a/error.c b/error.c
index efe7bb3..5cb808f 100644
--- a/error.c
+++ b/error.c
@@ -784,6 +784,17 @@ rb_builtin_type_name(int t) https://github.com/ruby/ruby/blob/trunk/error.c#L784
     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) https://github.com/ruby/ruby/blob/trunk/error.c#L842
     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) https://github.com/ruby/ruby/blob/trunk/error.c#L911
 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 v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]