Skip to content

Commit

Permalink
Continue trace after printing vars
Browse files Browse the repository at this point in the history
- fix issue that trace stopped after printing vars of a traced object
- minor speedup when generate a call signature
- adapt README and example/foo.rb
- bump version to v1.0.2
  • Loading branch information
mblumtritt committed May 13, 2024
1 parent e645544 commit f82649c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 66 deletions.
67 changes: 42 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,31 +108,48 @@ my_foo.foo(3) { puts _1 }
ImLost.vars(my_foo)

# output will look like
# > Foo.create(:foo!)
# /projects/foo.rb25
# > Foo.new(*)
# /projects/foo.rb6
# < Foo.new(*)
# = #<Foo:0x0000000100ab1188 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x0000000100ab1188 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb28
# > Foo#bar()
# /projects/foo.rb15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# = /projects/foo.rb29
# instance variables:
# @value: "1-none-[]-{}-bar"
# = /projects/foo.rb32
# instance variables:
# @value: "2-some-[a,b,c]-{:name=>:value}-bar"
# = /projects/foo.rb35
# instance variables:
# @value: "3--[]-{}-bar"
# > Foo.create(:foo!)
# /projects/foo.rb:25
# > Foo.new(*)
# /projects/foo.rb:6
# < Foo.new(*)
# = #<Foo:0x00000001030810c0 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x00000001030810c0 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb:28
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# = /projects/foo.rb:29
# instance variables:
# @value: "1-none-[]-{}-bar"
# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# /projects/foo.rb:31
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# = "2-some-[a,b,c]-{:name=>:value}-bar"
# = /projects/foo.rb:32
# instance variables:
# @value: "2-some-[a,b,c]-{:name=>:value}-bar"
# > Foo#foo(3, *[], nil, **{}, &#<Proc:0x00000001030aee30 /projects/foo.rb:34>)
# /projects/foo.rb:34
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# 3--[]-{}-bar
# < Foo#foo(3, *[], nil, **{}, &#<Proc:0x00000001030aee30 /projects/foo.rb:34>)
# = nil
# = /projects/foo.rb:35
# instance variables:
# @value: "3--[]-{}-bar"
```

See [examples dir](./examples) for more…
Expand Down
67 changes: 42 additions & 25 deletions examples/foo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,45 @@ def bar = :bar
ImLost.vars(my_foo)

# output will look like
# > Foo.create(:foo!)
# /projects/foo.rb25
# > Foo.new(*)
# /projects/foo.rb6
# < Foo.new(*)
# = #<Foo:0x0000000100ab1188 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x0000000100ab1188 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb28
# > Foo#bar()
# /projects/foo.rb15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# = /projects/foo.rb29
# instance variables:
# @value: "1-none-[]-{}-bar"
# = /projects/foo.rb32
# instance variables:
# @value: "2-some-[a,b,c]-{:name=>:value}-bar"
# = /projects/foo.rb35
# instance variables:
# @value: "3--[]-{}-bar"
# > Foo.create(:foo!)
# /projects/foo.rb:25
# > Foo.new(*)
# /projects/foo.rb:6
# < Foo.new(*)
# = #<Foo:0x00000001030810c0 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x00000001030810c0 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb:28
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# = /projects/foo.rb:29
# instance variables:
# @value: "1-none-[]-{}-bar"
# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# /projects/foo.rb:31
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# = "2-some-[a,b,c]-{:name=>:value}-bar"
# = /projects/foo.rb:32
# instance variables:
# @value: "2-some-[a,b,c]-{:name=>:value}-bar"
# > Foo#foo(3, *[], nil, **{}, &#<Proc:0x00000001030aee30 /projects/foo.rb:34>)
# /projects/foo.rb:34
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# 3--[]-{}-bar
# < Foo#foo(3, *[], nil, **{}, &#<Proc:0x00000001030aee30 /projects/foo.rb:34>)
# = nil
# = /projects/foo.rb:35
# instance variables:
# @value: "3--[]-{}-bar"
36 changes: 21 additions & 15 deletions lib/im-lost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def vars(object)
return unless object.respond_to?(:instance_variables)
_vars(object, Kernel.caller_locations(1, 1)[0])
ensure
@trace[traced] = 1 if traced
@trace[traced] = traced if traced
end

protected
Expand All @@ -272,30 +272,35 @@ def as_sig(prefix, info, args)
private

def _trace(arg)
@trace[arg.__id__] = 1 if self != arg && @output != arg
id = arg.__id__
@trace[id] = id if __id__ != id && @output.__id__ != id
arg
end

def _trace_all(args)
args.each do |arg|
@trace[arg.__id__] = 1 if arg != self && @output != arg
arg = arg.__id__
@trace[arg] = arg if __id__ != arg && @output.__id__ != arg
end
args
end

def _trace_b(arg)
@trace[id = arg.__id__] = 1 if self != arg && @output != arg
yield(arg)
ensure
@trace.delete(id) if id
id = arg.__id__
return yield(arg) if __id__ == id || @output.__id__ == id
begin
@trace[id] = id
yield(arg)
ensure
@trace.delete(id) if id
end
end

def _trace_all_b(args)
ids =
args.filter_map do |arg|
next if self == arg || @output == arg
@trace[id = arg.__id__] = 1
id
arg = arg.__id__
@trace[arg] = arg if __id__ != arg && @output.__id__ != arg
end
yield(args)
ensure
Expand Down Expand Up @@ -332,9 +337,8 @@ def _local_vars(binding)
end

ARG_SIG = { rest: '*', keyrest: '**', block: '&' }.compare_by_identity.freeze
NO_NAME = %i[* ** &].freeze
EX_PREFIX = { raise: 'x', rescue: '!' }.freeze
private_constant :ARG_SIG, :NO_NAME, :EX_PREFIX
NO_NAME = { :* => 1, :** => 1, :& => 1 }.compare_by_identity.freeze
private_constant :ARG_SIG, :NO_NAME

@trace = {}.compare_by_identity
@caller_locations = true
Expand All @@ -354,7 +358,7 @@ def _local_vars(binding)
'>',
tp,
tp.parameters.map do |kind, name|
next name if NO_NAME.include?(name)
next name if NO_NAME.key?(name)
"#{ARG_SIG[kind]}#{ctx.local_variable_get(name).inspect}"
end
)
Expand Down Expand Up @@ -392,7 +396,9 @@ def _local_vars(binding)
@trace_exceptions =
TracePoint.new(*supported) do |tp|
ex = tp.raised_exception.inspect
@output.puts("#{EX_PREFIX[tp.event]} #{ex[0] == '#' ? ex[2..-2] : ex}")
@output.puts(
"#{tp.event == :raise ? 'x' : '!'} #{ex[0] == '#' ? ex[2..-2] : ex}"
)
@output.puts(" #{tp.path}:#{tp.lineno}") if @exception_locations
end

Expand Down
2 changes: 1 addition & 1 deletion lib/im-lost/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module ImLost
# The version number of the gem.
VERSION = '1.0.1'
VERSION = '1.0.2'
end

0 comments on commit f82649c

Please sign in to comment.