ruby-changes:70314
From: Koichi <ko1@a...>
Date: Sun, 19 Dec 2021 05:16:50 +0900 (JST)
Subject: [ruby-changes:70314] 89a02d8932 (master): add `rb_iseq_type()` to return iseq type in Symbol
https://git.ruby-lang.org/ruby.git/commit/?id=89a02d8932 From 89a02d8932774f740013fe2a829faa9c40a1cfd1 Mon Sep 17 00:00:00 2001 From: Koichi Sasada <ko1@a...> Date: Sun, 19 Dec 2021 03:20:00 +0900 Subject: add `rb_iseq_type()` to return iseq type in Symbol It is shorthand `ISeq#to_a[9]`. --- iseq.c | 89 +++++++++++++++++++++++++++++++++++++++--------------------------- iseq.h | 1 + 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/iseq.c b/iseq.c index 9538847577a..351c1e815fe 100644 --- a/iseq.c +++ b/iseq.c @@ -1194,6 +1194,14 @@ rb_iseq_code_location(const rb_iseq_t *iseq, int *beg_pos_lineno, int *beg_pos_c https://github.com/ruby/ruby/blob/trunk/iseq.c#L1194 if (end_pos_column) *end_pos_column = loc->end_pos.column; } +static VALUE iseq_type_sym(enum iseq_type type); + +VALUE +rb_iseq_type(const rb_iseq_t *iseq) +{ + return iseq_type_sym(iseq->body->type); +} + VALUE rb_iseq_coverage(const rb_iseq_t *iseq) { @@ -2712,6 +2720,46 @@ static const rb_data_type_t label_wrapper = { https://github.com/ruby/ruby/blob/trunk/iseq.c#L2720 0, 0, RUBY_TYPED_FREE_IMMEDIATELY }; +static VALUE +iseq_type_sym(enum iseq_type type) +{ + DECL_SYMBOL(top); + DECL_SYMBOL(method); + DECL_SYMBOL(block); + DECL_SYMBOL(class); + DECL_SYMBOL(rescue); + DECL_SYMBOL(ensure); + DECL_SYMBOL(eval); + DECL_SYMBOL(main); + DECL_SYMBOL(plain); + + if (sym_top == 0) { + INIT_SYMBOL(top); + INIT_SYMBOL(method); + INIT_SYMBOL(block); + INIT_SYMBOL(class); + INIT_SYMBOL(rescue); + INIT_SYMBOL(ensure); + INIT_SYMBOL(eval); + INIT_SYMBOL(main); + INIT_SYMBOL(plain); + } + + switch (type) { + case ISEQ_TYPE_TOP: return sym_top; + case ISEQ_TYPE_METHOD: return sym_method; + case ISEQ_TYPE_BLOCK: return sym_block; + case ISEQ_TYPE_CLASS: return sym_class; + case ISEQ_TYPE_RESCUE: return sym_rescue; + case ISEQ_TYPE_ENSURE: return sym_ensure; + case ISEQ_TYPE_EVAL: return sym_eval; + case ISEQ_TYPE_MAIN: return sym_main; + case ISEQ_TYPE_PLAIN: return sym_plain; + }; + + rb_bug("unsupported iseq type: %d", (int)type); +} + static VALUE iseq_data_to_ary(const rb_iseq_t *iseq) { @@ -2736,45 +2784,15 @@ iseq_data_to_ary(const rb_iseq_t *iseq) https://github.com/ruby/ruby/blob/trunk/iseq.c#L2784 struct st_table *labels_table = st_init_numtable(); VALUE labels_wrapper = TypedData_Wrap_Struct(0, &label_wrapper, labels_table); - DECL_SYMBOL(top); - DECL_SYMBOL(method); - DECL_SYMBOL(block); - DECL_SYMBOL(class); - DECL_SYMBOL(rescue); - DECL_SYMBOL(ensure); - DECL_SYMBOL(eval); - DECL_SYMBOL(main); - DECL_SYMBOL(plain); - - if (sym_top == 0) { - int i; - for (i=0; i<numberof(insn_syms); i++) { + if (insn_syms[0] == 0) { + int i; + for (i=0; i<numberof(insn_syms); i++) { insn_syms[i] = rb_intern(insn_name(i)); - } - INIT_SYMBOL(top); - INIT_SYMBOL(method); - INIT_SYMBOL(block); - INIT_SYMBOL(class); - INIT_SYMBOL(rescue); - INIT_SYMBOL(ensure); - INIT_SYMBOL(eval); - INIT_SYMBOL(main); - INIT_SYMBOL(plain); + } } /* type */ - switch (iseq_body->type) { - case ISEQ_TYPE_TOP: type = sym_top; break; - case ISEQ_TYPE_METHOD: type = sym_method; break; - case ISEQ_TYPE_BLOCK: type = sym_block; break; - case ISEQ_TYPE_CLASS: type = sym_class; break; - case ISEQ_TYPE_RESCUE: type = sym_rescue; break; - case ISEQ_TYPE_ENSURE: type = sym_ensure; break; - case ISEQ_TYPE_EVAL: type = sym_eval; break; - case ISEQ_TYPE_MAIN: type = sym_main; break; - case ISEQ_TYPE_PLAIN: type = sym_plain; break; - default: rb_bug("unsupported iseq type: %d", (int)iseq_body->type); - }; + type = iseq_type_sym(iseq_body->type); /* locals */ for (i=0; i<iseq_body->local_table_size; i++) { @@ -3788,7 +3806,6 @@ Init_ISeq(void) https://github.com/ruby/ruby/blob/trunk/iseq.c#L3806 rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1); rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1); - /* location APIs */ rb_define_method(rb_cISeq, "path", iseqw_path, 0); rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0); diff --git a/iseq.h b/iseq.h index cc68b8d3f8a..fc61d03b76b 100644 --- a/iseq.h +++ b/iseq.h @@ -198,6 +198,7 @@ const rb_iseq_t *rb_iseqw_to_iseq(VALUE iseqw); https://github.com/ruby/ruby/blob/trunk/iseq.h#L198 VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq); /* obsolete */ int rb_iseq_from_eval_p(const rb_iseq_t *iseq); +VALUE rb_iseq_type(const rb_iseq_t *iseq); VALUE rb_iseq_label(const rb_iseq_t *iseq); VALUE rb_iseq_base_label(const rb_iseq_t *iseq); VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq); -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/