Skip to content

Commit

Permalink
support info ivars obj
Browse files Browse the repository at this point in the history
`info ivars obj` shows the instance variables of `obj`.

Note that this command notation also support `/pattern/` like
`info ivars obj /pattern/` so that we can not show ivars of
Regexp literal objects (but I believe nobody use it).
  • Loading branch information
ko1 committed Nov 29, 2022
1 parent 9a9cc33 commit 4b012f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
6 changes: 4 additions & 2 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ def register_default_command
# * It includes `self` as `%self` and a return value as `%return`.
# * `i[nfo] i[var[s]]` or `i[nfo] instance`
# * Show information about instance variables about `self`.
# * `info ivars <expr>` shows the instance variables of the result of `<expr>`.
# * `i[nfo] c[onst[s]]` or `i[nfo] constant[s]`
# * Show information about accessible constants except toplevel constants.
# * `i[nfo] g[lobal[s]]`
Expand All @@ -759,8 +760,9 @@ def register_default_command
request_tc [:show, :default, pat] # something useful
when 'l', /^locals?/
request_tc [:show, :locals, pat]
when 'i', /^ivars?/i, /^instance[_ ]variables?/i
request_tc [:show, :ivars, pat]
when /^i\b/, /^ivars?\b/i, /^instance[_ ]variables?\b/i
expr = $~&.post_match&.strip
request_tc [:show, :ivars, pat, expr]
when 'c', /^consts?/i, /^constants?/i
request_tc [:show, :consts, pat]
when 'g', /^globals?/i, /^global[_ ]variables?/i
Expand Down
19 changes: 14 additions & 5 deletions lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,18 @@ def show_locals pat
end
end

def show_ivars pat
if s = current_frame&.self
M_INSTANCE_VARIABLES.bind_call(s).sort.each{|iv|
value = M_INSTANCE_VARIABLE_GET.bind_call(s, iv)
def show_ivars pat, expr = nil

if expr && !expr.empty?
_self = frame_eval(expr);
elsif _self = current_frame&.self
else
_self = nil
end

if _self
M_INSTANCE_VARIABLES.bind_call(_self).sort.each{|iv|
value = M_INSTANCE_VARIABLE_GET.bind_call(_self, iv)
puts_variable_info iv, value, pat
}
end
Expand Down Expand Up @@ -1088,7 +1096,8 @@ def wait_next_action_

when :ivars
pat = args.shift
show_ivars pat
expr = args.shift
show_ivars pat, expr

when :consts
pat = args.shift
Expand Down
47 changes: 47 additions & 0 deletions test/console/info_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,51 @@ def test_info_constant
end
end
end

class InfoIvarsTest < ConsoleTestCase
def program
<<~RUBY
1| class C
2| def initialize
3| @a = :a
4| @b = :b
5| end
6| end
7| c = C.new
8| c
RUBY
end

def test_ivar
debug_code program do
type 'b 5'
type 'c'
assert_line_num 5
type 'info ivars'
assert_line_text(/@a/)
assert_line_text(/@b/)
type 'info ivars /b/'
assert_no_line_text(/@a/)
assert_line_text(/@b/)
type 'c'
end
end

def test_ivar_obj
debug_code program do
type 'u 8'
assert_line_num 8
type 'info ivars'
assert_no_line_text /@/
type 'info ivars c'
assert_line_text /@a/
assert_line_text /@b/
type 'info ivars c /b/'
assert_no_line_text /@a/
assert_line_text /@b/
type 'c'
end
end

end
end

0 comments on commit 4b012f5

Please sign in to comment.