Skip to content

Commit 67f4e73

Browse files
committed
Add InstructionSequence#type method
This method returns a symbol representing the type of the instruction sequence object.
1 parent 22f79ae commit 67f4e73

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

iseq.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,27 @@ iseqw_inspect(VALUE self)
16241624
}
16251625
}
16261626

1627+
static VALUE
1628+
iseqw_type(VALUE self)
1629+
{
1630+
const rb_iseq_t *iseq = iseqw_check(self);
1631+
const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1632+
1633+
switch(body->type) {
1634+
#define CASE_TYPE(t) case ISEQ_TYPE_ ## t: return ID2SYM(rb_intern(#t)); break;
1635+
CASE_TYPE(TOP);
1636+
CASE_TYPE(METHOD);
1637+
CASE_TYPE(BLOCK);
1638+
CASE_TYPE(CLASS);
1639+
CASE_TYPE(RESCUE);
1640+
CASE_TYPE(ENSURE);
1641+
CASE_TYPE(EVAL);
1642+
CASE_TYPE(MAIN);
1643+
CASE_TYPE(PLAIN);
1644+
#undef CASE_TYPE
1645+
}
1646+
}
1647+
16271648
/*
16281649
* Returns the path of this instruction sequence.
16291650
*
@@ -3915,6 +3936,7 @@ Init_ISeq(void)
39153936
rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
39163937
rb_undef_alloc_func(rb_cISeq);
39173938
rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
3939+
rb_define_method(rb_cISeq, "type", iseqw_type, 0);
39183940
rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
39193941
rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
39203942
rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);

test/ruby/test_iseq.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,13 @@ def obj.foo(*) nil.instance_eval{ ->{super} } end
167167
end
168168
end
169169

170+
def test_type
171+
iseq = RubyVM::InstructionSequence.of(method(__method__))
172+
assert_equal :METHOD, iseq.type
173+
iseq = RubyVM::InstructionSequence.of(->() { })
174+
assert_equal :BLOCK, iseq.type
175+
end
176+
170177
def test_disasm_encoding
171178
src = "\u{3042} = 1; \u{3042}; \u{3043}"
172179
asm = compile(src).disasm

0 commit comments

Comments
 (0)