Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions iseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,27 @@ iseqw_inspect(VALUE self)
}
}

static VALUE
iseqw_type(VALUE self)
{
const rb_iseq_t *iseq = iseqw_check(self);
const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);

switch(body->type) {
#define CASE_TYPE(t) case ISEQ_TYPE_ ## t: return ID2SYM(rb_intern(#t)); break;
CASE_TYPE(TOP);
CASE_TYPE(METHOD);
CASE_TYPE(BLOCK);
CASE_TYPE(CLASS);
CASE_TYPE(RESCUE);
CASE_TYPE(ENSURE);
CASE_TYPE(EVAL);
CASE_TYPE(MAIN);
CASE_TYPE(PLAIN);
#undef CASE_TYPE
}
}

/*
* Returns the path of this instruction sequence.
*
Expand Down Expand Up @@ -4000,6 +4021,7 @@ Init_ISeq(void)
rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
rb_undef_alloc_func(rb_cISeq);
rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
rb_define_method(rb_cISeq, "type", iseqw_type, 0);
rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
Expand Down
7 changes: 7 additions & 0 deletions test/ruby/test_iseq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ def obj.foo(*) nil.instance_eval{ ->{super} } end
end
end

def test_type
iseq = RubyVM::InstructionSequence.of(method(__method__))
assert_equal :METHOD, iseq.type
iseq = RubyVM::InstructionSequence.of(->() { })
assert_equal :BLOCK, iseq.type
end

def test_disasm_encoding
src = "\u{3042} = 1; \u{3042}; \u{3043}"
asm = compile(src).disasm
Expand Down