From 89a02d8932774f740013fe2a829faa9c40a1cfd1 Mon Sep 17 00:00:00 2001 From: Koichi Sasada 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 +++++++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 36 deletions(-) (limited to 'iseq.c') diff --git a/iseq.c b/iseq.c index 9538847577..351c1e815f 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 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 = { 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) 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; itype) { - 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; ilocal_table_size; i++) { @@ -3788,7 +3806,6 @@ Init_ISeq(void) 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); -- cgit v1.2.3