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

ruby-changes:43661

From: nobu <ko1@a...>
Date: Sat, 23 Jul 2016 22:43:51 +0900 (JST)
Subject: [ruby-changes:43661] nobu:r55734 (trunk): internal.h: inline Check_Type

nobu	2016-07-23 22:43:44 +0900 (Sat, 23 Jul 2016)

  New Revision: 55734

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=55734

  Log:
    internal.h: inline Check_Type
    
    * internal.h (Check_Type): inline check for the object type.

  Modified files:
    trunk/ChangeLog
    trunk/class.c
    trunk/error.c
    trunk/internal.h
    trunk/vm.c
Index: internal.h
===================================================================
--- internal.h	(revision 55733)
+++ internal.h	(revision 55734)
@@ -1540,6 +1540,12 @@ VALUE rb_str2big_gmp(VALUE arg, int base https://github.com/ruby/ruby/blob/trunk/internal.h#L1540
 
 /* error.c (export) */
 int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data);
+NORETURN(void rb_unexpected_type(VALUE,int));
+#undef Check_Type
+#define Check_Type(v, t) \
+    (!RB_TYPE_P((VALUE)(v), (t)) || \
+     ((t) == RUBY_T_DATA && RTYPEDDATA_P(v)) ? \
+     rb_unexpected_type((VALUE)(v), (t)) : (void)0)
 
 /* file.c (export) */
 #ifdef HAVE_READLINK
Index: class.c
===================================================================
--- class.c	(revision 55733)
+++ class.c	(revision 55734)
@@ -855,11 +855,7 @@ rb_include_module(VALUE klass, VALUE mod https://github.com/ruby/ruby/blob/trunk/class.c#L855
     int changed = 0;
 
     rb_frozen_class_p(klass);
-
-    if (!RB_TYPE_P(module, T_MODULE)) {
-	Check_Type(module, T_MODULE);
-    }
-
+    Check_Type(module, T_MODULE);
     OBJ_INFECT(klass, module);
 
     changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
@@ -971,9 +967,7 @@ rb_prepend_module(VALUE klass, VALUE mod https://github.com/ruby/ruby/blob/trunk/class.c#L967
     int changed = 0;
 
     rb_frozen_class_p(klass);
-
     Check_Type(module, T_MODULE);
-
     OBJ_INFECT(klass, module);
 
     origin = RCLASS_ORIGIN(klass);
Index: error.c
===================================================================
--- error.c	(revision 55733)
+++ error.c	(revision 55734)
@@ -553,34 +553,60 @@ rb_builtin_class_name(VALUE x) https://github.com/ruby/ruby/blob/trunk/error.c#L553
     return etype;
 }
 
+NORETURN(static void unexpected_type(VALUE, int, int));
+#define UNDEF_LEAKED "undef leaked to the Ruby space"
+
+static void
+unexpected_type(VALUE x, int xt, int t)
+{
+    const char *tname = rb_builtin_type_name(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);
+	exc = rb_eTypeError;
+    }
+    else if (xt > T_MASK && xt <= 0x3f) {
+	mesg = rb_sprintf("unknown type 0x%x (0x%x given, probably comes"
+			  " from extension library for ruby 1.8)", t, xt);
+    }
+    else {
+	mesg = rb_sprintf("unknown type 0x%x (0x%x given)", t, xt);
+    }
+    rb_exc_raise(rb_exc_new_str(exc, mesg));
+}
+
 void
 rb_check_type(VALUE x, int t)
 {
     int xt;
 
     if (x == Qundef) {
-	rb_bug("undef leaked to the Ruby space");
+	rb_bug(UNDEF_LEAKED);
     }
 
     xt = TYPE(x);
     if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
-	const char *tname = rb_builtin_type_name(t);
-	if (tname) {
-	    const char *cname = builtin_class_name(x);
-	    if (cname)
-		rb_raise(rb_eTypeError, "wrong argument type %s (expected %s)",
-			 cname, tname);
-	    else
-		rb_raise(rb_eTypeError, "wrong argument type %"PRIsVALUE" (expected %s)",
-			 rb_obj_class(x), tname);
-	}
-	if (xt > T_MASK && xt <= 0x3f) {
-	    rb_fatal("unknown type 0x%x (0x%x given, probably comes from extension library for ruby 1.8)", t, xt);
-	}
-	rb_bug("unknown type 0x%x (0x%x given)", t, xt);
+	unexpected_type(x, xt, t);
     }
 }
 
+void
+rb_unexpected_type(VALUE x, int t)
+{
+    if (x == Qundef) {
+	rb_bug(UNDEF_LEAKED);
+    }
+
+    unexpected_type(x, TYPE(x), t);
+}
+
 int
 rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent)
 {
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 55733)
+++ ChangeLog	(revision 55734)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Jul 23 22:43:41 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* internal.h (Check_Type): inline check for the object type.
+
 Sat Jul 23 04:06:04 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* include/ruby/ruby.h (RTEST, NIL_P): use RUBY prefixed name in
Index: vm.c
===================================================================
--- vm.c	(revision 55733)
+++ vm.c	(revision 55734)
@@ -2583,7 +2583,7 @@ m_core_hash_merge_ptr(int argc, VALUE *a https://github.com/ruby/ruby/blob/trunk/vm.c#L2583
 static int
 kwmerge_i(VALUE key, VALUE value, VALUE hash)
 {
-    if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
+    Check_Type(key, T_SYMBOL);
     rb_hash_aset(hash, key, value);
     return ST_CONTINUE;
 }
@@ -2591,7 +2591,7 @@ kwmerge_i(VALUE key, VALUE value, VALUE https://github.com/ruby/ruby/blob/trunk/vm.c#L2591
 static int
 kwcheck_i(VALUE key, VALUE value, VALUE hash)
 {
-    if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
+    Check_Type(key, T_SYMBOL);
     return ST_CONTINUE;
 }
 

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

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