diff --git a/gc.c b/gc.c index e2b9598ff0..29b61a0698 100644 --- a/gc.c +++ b/gc.c @@ -3416,18 +3416,40 @@ gc_stat(rb_execution_context_t *ec, VALUE self, VALUE arg) // arg is (nil || has if (NIL_P(arg)) { arg = rb_hash_new(); } + else if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) { + rb_raise(rb_eTypeError, "non-hash or symbol given"); + } - return rb_gc_impl_stat(rb_gc_get_objspace(), arg); + VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg); + + if (ret == Qundef) { + GC_ASSERT(SYMBOL_P(arg)); + + rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg)); + } + + return ret; } size_t -rb_gc_stat(VALUE key) +rb_gc_stat(VALUE arg) { - if (SYMBOL_P(key)) { - return rb_gc_impl_stat(rb_gc_get_objspace(), key); + if (!RB_TYPE_P(arg, T_HASH) && !SYMBOL_P(arg)) { + rb_raise(rb_eTypeError, "non-hash or symbol given"); + } + + VALUE ret = rb_gc_impl_stat(rb_gc_get_objspace(), arg); + + if (ret == Qundef) { + GC_ASSERT(SYMBOL_P(arg)); + + rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg)); + } + + if (SYMBOL_P(arg)) { + return NUM2SIZET(ret); } else { - rb_gc_impl_stat(rb_gc_get_objspace(), key); return 0; } } diff --git a/gc/default.c b/gc/default.c index 9847052d28..aa1e3dd105 100644 --- a/gc/default.c +++ b/gc/default.c @@ -7512,7 +7512,7 @@ rb_gc_impl_stat(void *objspace_ptr, VALUE hash_or_sym) key = hash_or_sym; } else { - rb_raise(rb_eTypeError, "non-hash or symbol given"); + rb_bug("non-hash or symbol given"); } #define SET(name, attr) \ @@ -7566,8 +7566,9 @@ rb_gc_impl_stat(void *objspace_ptr, VALUE hash_or_sym) #endif /* RGENGC_PROFILE */ #undef SET - if (!NIL_P(key)) { /* matched key should return above */ - rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(key)); + if (!NIL_P(key)) { + // Matched key should return above + return Qundef; } #if defined(RGENGC_PROFILE) && RGENGC_PROFILE >= 2