Move error handling for GC.stat_heap to gc.c

This commit is contained in:
Peter Zhu 2024-10-22 11:05:51 -04:00
parent d3aaca9785
commit 5460d5b119
Notes: git 2025-06-06 00:31:58 +00:00
2 changed files with 29 additions and 6 deletions

24
gc.c
View File

@ -3461,7 +3461,29 @@ gc_stat_heap(rb_execution_context_t *ec, VALUE self, VALUE heap_name, VALUE arg)
arg = rb_hash_new();
}
return rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
if (NIL_P(heap_name)) {
if (!RB_TYPE_P(arg, T_HASH)) {
rb_raise(rb_eTypeError, "non-hash given");
}
}
else if (FIXNUM_P(heap_name)) {
if (!SYMBOL_P(arg) && !RB_TYPE_P(arg, T_HASH)) {
rb_raise(rb_eTypeError, "non-hash or symbol given");
}
}
else {
rb_raise(rb_eTypeError, "heap_name must be nil or an Integer");
}
VALUE ret = rb_gc_impl_stat_heap(rb_gc_get_objspace(), heap_name, arg);
if (ret == Qundef) {
GC_ASSERT(SYMBOL_P(arg));
rb_raise(rb_eArgError, "unknown key: %"PRIsVALUE, rb_sym2str(arg));
}
return ret;
}
static VALUE

View File

@ -7635,8 +7635,9 @@ stat_one_heap(rb_heap_t *heap, VALUE hash, VALUE key)
SET(total_freed_objects, heap->total_freed_objects);
#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;
}
return hash;
@ -7651,7 +7652,7 @@ rb_gc_impl_stat_heap(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym)
if (NIL_P(heap_name)) {
if (!RB_TYPE_P(hash_or_sym, T_HASH)) {
rb_raise(rb_eTypeError, "non-hash given");
rb_bug("non-hash given");
}
for (int i = 0; i < HEAP_COUNT; i++) {
@ -7678,11 +7679,11 @@ rb_gc_impl_stat_heap(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym)
return stat_one_heap(&heaps[heap_idx], hash_or_sym, Qnil);
}
else {
rb_raise(rb_eTypeError, "non-hash or symbol given");
rb_bug("non-hash or symbol given");
}
}
else {
rb_raise(rb_eTypeError, "heap_name must be nil or an Integer");
rb_bug("heap_name must be nil or an Integer");
}
return hash_or_sym;