internal.h: inline Check_Type

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55734 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-07-23 13:43:44 +00:00
parent c29ad01850
commit 9662ee0584
5 changed files with 54 additions and 24 deletions

View File

@ -1,3 +1,7 @@
Sat Jul 23 22:43:41 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* internal.h (Check_Type): inline check for the object type.
Sat Jul 23 04:06:04 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Jul 23 04:06:04 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/ruby.h (RTEST, NIL_P): use RUBY prefixed name in * include/ruby/ruby.h (RTEST, NIL_P): use RUBY prefixed name in

View File

@ -855,11 +855,7 @@ rb_include_module(VALUE klass, VALUE module)
int changed = 0; int changed = 0;
rb_frozen_class_p(klass); 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); OBJ_INFECT(klass, module);
changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE); changed = include_modules_at(klass, RCLASS_ORIGIN(klass), module, TRUE);
@ -971,9 +967,7 @@ rb_prepend_module(VALUE klass, VALUE module)
int changed = 0; int changed = 0;
rb_frozen_class_p(klass); rb_frozen_class_p(klass);
Check_Type(module, T_MODULE); Check_Type(module, T_MODULE);
OBJ_INFECT(klass, module); OBJ_INFECT(klass, module);
origin = RCLASS_ORIGIN(klass); origin = RCLASS_ORIGIN(klass);

54
error.c
View File

@ -553,32 +553,58 @@ rb_builtin_class_name(VALUE x)
return etype; 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 void
rb_check_type(VALUE x, int t) rb_check_type(VALUE x, int t)
{ {
int xt; int xt;
if (x == Qundef) { if (x == Qundef) {
rb_bug("undef leaked to the Ruby space"); rb_bug(UNDEF_LEAKED);
} }
xt = TYPE(x); xt = TYPE(x);
if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) { if (xt != t || (xt == T_DATA && RTYPEDDATA_P(x))) {
const char *tname = rb_builtin_type_name(t); unexpected_type(x, xt, 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);
} void
rb_bug("unknown type 0x%x (0x%x given)", t, xt); rb_unexpected_type(VALUE x, int t)
{
if (x == Qundef) {
rb_bug(UNDEF_LEAKED);
} }
unexpected_type(x, TYPE(x), t);
} }
int int

View File

@ -1540,6 +1540,12 @@ VALUE rb_str2big_gmp(VALUE arg, int base, int badcheck);
/* error.c (export) */ /* error.c (export) */
int rb_bug_reporter_add(void (*func)(FILE *, void *), void *data); 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) */ /* file.c (export) */
#ifdef HAVE_READLINK #ifdef HAVE_READLINK

4
vm.c
View File

@ -2583,7 +2583,7 @@ m_core_hash_merge_ptr(int argc, VALUE *argv, VALUE recv)
static int static int
kwmerge_i(VALUE key, VALUE value, VALUE hash) 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); rb_hash_aset(hash, key, value);
return ST_CONTINUE; return ST_CONTINUE;
} }
@ -2591,7 +2591,7 @@ kwmerge_i(VALUE key, VALUE value, VALUE hash)
static int static int
kwcheck_i(VALUE key, VALUE value, VALUE hash) 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; return ST_CONTINUE;
} }