Skip to content

Commit c10dcd5

Browse files
committed
Add signature for RubyVM::InstructionSequence.of
https://docs.ruby-lang.org/en/4.0/RubyVM/InstructionSequence.html#method-c-of `prism` uses it and currently vendors this change. The return type is nillable as per the implementation in ruby but I'm not sure how to actually trigger that for valid input. It may happen with some procs, that's all I know.
1 parent 26b3007 commit c10dcd5

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

core/ruby_vm.rbs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,46 @@ class RubyVM::InstructionSequence < Object
371371
# event_symbol] pair.
372372
#
373373
def trace_points: () -> Array[untyped]
374+
375+
# <!--
376+
# rdoc-file=iseq.c
377+
# - of(p1)
378+
# -->
379+
# Returns the instruction sequence containing the given proc or method.
380+
#
381+
# For example, using irb:
382+
#
383+
# # a proc
384+
# > p = proc { num = 1 + 2 }
385+
# > RubyVM::InstructionSequence.of(p)
386+
# > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
387+
#
388+
# # for a method
389+
# > def foo(bar); puts bar; end
390+
# > RubyVM::InstructionSequence.of(method(:foo))
391+
# > #=> <RubyVM::InstructionSequence:foo@(irb)>
392+
#
393+
# Using ::compile_file:
394+
#
395+
# # /tmp/iseq_of.rb
396+
# def hello
397+
# puts "hello, world"
398+
# end
399+
#
400+
# $a_global_proc = proc { str = 'a' + 'b' }
401+
#
402+
# # in irb
403+
# > require '/tmp/iseq_of.rb'
404+
#
405+
# # first the method hello
406+
# > RubyVM::InstructionSequence.of(method(:hello))
407+
# > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
408+
#
409+
# # then the global proc
410+
# > RubyVM::InstructionSequence.of($a_global_proc)
411+
# > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
412+
#
413+
def self.of: (Proc | Method | UnboundMethod body) -> RubyVM::InstructionSequence?
374414
end
375415

376416
# <!-- rdoc-file=ast.rb -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require_relative "test_helper"
2+
3+
class RubyVM::InstructionSequenceSingletonTest < Test::Unit::TestCase
4+
include TestHelper
5+
6+
testing "singleton(::RubyVM::InstructionSequence)"
7+
8+
def test_of
9+
assert_send_type "(::Method body) -> ::RubyVM::InstructionSequence",
10+
RubyVM::InstructionSequence, :of, method(:test_of)
11+
assert_send_type "(::UnboundMethod body) -> ::RubyVM::InstructionSequence",
12+
RubyVM::InstructionSequence, :of, self.class.instance_method(:test_of)
13+
assert_send_type "(::Proc body) -> ::RubyVM::InstructionSequence",
14+
RubyVM::InstructionSequence, :of, -> { }
15+
end
16+
end

0 commit comments

Comments
 (0)