ruby-changes:64298
From: Jeremy <ko1@a...>
Date: Sat, 19 Dec 2020 02:54:34 +0900 (JST)
Subject: [ruby-changes:64298] 6ced55b07c (master): Make warning_categories a map of category symbols to category numbers
https://git.ruby-lang.org/ruby.git/commit/?id=6ced55b07c From 6ced55b07cf00e4ce8ffac5762f10e8ff1ecc59b Mon Sep 17 00:00:00 2001 From: Jeremy Evans <code@j...> Date: Wed, 16 Dec 2020 08:37:04 -0800 Subject: Make warning_categories a map of category symbols to category numbers Use this to simplify rb_warning_category_from_name. This also adds support for using the :experimental category in Kernel#warn and Warning.warn. diff --git a/error.c b/error.c index 2b63742..7a7d562 100644 --- a/error.c +++ b/error.c @@ -63,6 +63,7 @@ https://github.com/ruby/ruby/blob/trunk/error.c#L63 VALUE rb_iseqw_local_variables(VALUE iseqval); VALUE rb_iseqw_new(const rb_iseq_t *); int rb_str_end_with_asciichar(VALUE str, int c); +VALUE rb_ident_hash_new(void); long rb_backtrace_length_limit = -1; VALUE rb_eEAGAIN; @@ -161,18 +162,13 @@ rb_warning_category_mask(VALUE category) https://github.com/ruby/ruby/blob/trunk/error.c#L162 rb_warning_category_t rb_warning_category_from_name(VALUE category) { - rb_warning_category_t cat = RB_WARN_CATEGORY_NONE; + VALUE cat_value; Check_Type(category, T_SYMBOL); - if (category == ID2SYM(id_deprecated)) { - cat = RB_WARN_CATEGORY_DEPRECATED; - } - else if (category == ID2SYM(id_experimental)) { - cat = RB_WARN_CATEGORY_EXPERIMENTAL; - } - else { + cat_value = rb_hash_aref(warning_categories, category); + if (cat_value == Qnil) { rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category); } - return cat; + return NUM2INT(cat_value); } void @@ -318,7 +314,7 @@ rb_warn_category(VALUE str, VALUE category) https://github.com/ruby/ruby/blob/trunk/error.c#L314 { if (category != Qnil) { category = rb_to_symbol_type(category); - if (rb_hash_aref(warning_categories, category) != Qtrue) { + if (!RTEST(rb_hash_aref(warning_categories, category))) { rb_raise(rb_eArgError, "invalid warning category used: %s", rb_id2name(SYM2ID(category))); } } @@ -2836,16 +2832,17 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2832 sym_category = ID2SYM(id_category); - warning_categories = rb_hash_new(); + warning_categories = rb_ident_hash_new(); rb_gc_register_mark_object(warning_categories); - rb_hash_aset(warning_categories, ID2SYM(rb_intern_const("deprecated")), Qtrue); + rb_hash_aset(warning_categories, ID2SYM(id_deprecated), INT2NUM(RB_WARN_CATEGORY_DEPRECATED)); + rb_hash_aset(warning_categories, ID2SYM(id_experimental), INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL)); rb_obj_freeze(warning_categories); - warning_category_t_map = rb_hash_new(); + warning_category_t_map = rb_ident_hash_new(); rb_gc_register_mark_object(warning_category_t_map); rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_NONE), Qnil); - rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_DEPRECATED), ID2SYM(rb_intern_const("deprecated"))); - rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL), ID2SYM(rb_intern_const("experimental"))); + rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_DEPRECATED), ID2SYM(id_deprecated)); + rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL), ID2SYM(id_experimental)); rb_obj_freeze(warning_category_t_map); } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/