Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c05c1c3

Browse files
authored
Merge pull request #213 from ruby-syntax-tree/tracepoint
Handle tracepoint events except line
2 parents f050888 + af8c520 commit c05c1c3

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

lib/syntax_tree/visitor/compiler.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def offset(index)
285285
# list of instructions along with the metadata pertaining to them. It also
286286
# functions as a builder for the instruction sequence.
287287
class InstructionSequence
288+
MAGIC = "YARVInstructionSequence/SimpleDataFormat"
289+
288290
# The type of the instruction sequence.
289291
attr_reader :type
290292

@@ -363,7 +365,9 @@ def inline_storage_for(name)
363365
end
364366

365367
def length
366-
insns.sum(&:length)
368+
insns.inject(0) do |sum, insn|
369+
insn.is_a?(Array) ? sum + insn.length : sum
370+
end
367371
end
368372

369373
def each_child
@@ -378,7 +382,7 @@ def to_a
378382
versions = RUBY_VERSION.split(".").map(&:to_i)
379383

380384
[
381-
"YARVInstructionSequence/SimpleDataFormat",
385+
MAGIC,
382386
versions[0],
383387
versions[1],
384388
1,
@@ -462,7 +466,13 @@ def initialize(
462466
# This creates a new label at the current length of the instruction
463467
# sequence. It is used as the operand for jump instructions.
464468
def label
465-
:"label_#{iseq.length}"
469+
name = :"label_#{iseq.length}"
470+
iseq.insns.last == name ? name : event(name)
471+
end
472+
473+
def event(name)
474+
iseq.push(name)
475+
name
466476
end
467477

468478
def adjuststack(number)
@@ -1239,8 +1249,10 @@ def visit_block(node)
12391249
current_iseq,
12401250
node
12411251
) do
1252+
builder.event(:RUBY_EVENT_B_CALL)
12421253
visit(node.block_var)
12431254
visit(node.bodystmt)
1255+
builder.event(:RUBY_EVENT_B_RETURN)
12441256
builder.leave
12451257
end
12461258
end
@@ -1429,7 +1441,9 @@ def visit_class(node)
14291441
current_iseq,
14301442
node
14311443
) do
1444+
builder.event(:RUBY_EVENT_CLASS)
14321445
visit(node.bodystmt)
1446+
builder.event(:RUBY_EVENT_END)
14331447
builder.leave
14341448
end
14351449

@@ -1500,7 +1514,9 @@ def visit_def(node)
15001514
node
15011515
) do
15021516
visit(node.params) if node.params
1517+
builder.event(:RUBY_EVENT_CALL)
15031518
visit(node.bodystmt)
1519+
builder.event(:RUBY_EVENT_RETURN)
15041520
builder.leave
15051521
end
15061522

@@ -1628,9 +1644,12 @@ def visit_for(node)
16281644

16291645
local_variable = current_iseq.local_variable(name)
16301646
builder.setlocal(local_variable.index, local_variable.level)
1647+
1648+
builder.event(:RUBY_EVENT_B_CALL)
16311649
builder.nop
16321650

16331651
visit(node.statements)
1652+
builder.event(:RUBY_EVENT_B_RETURN)
16341653
builder.leave
16351654
end
16361655

@@ -1721,8 +1740,10 @@ def visit_lambda(node)
17211740
current_iseq,
17221741
node
17231742
) do
1743+
builder.event(:RUBY_EVENT_B_CALL)
17241744
visit(node.params)
17251745
visit(node.statements)
1746+
builder.event(:RUBY_EVENT_B_RETURN)
17261747
builder.leave
17271748
end
17281749

@@ -1777,7 +1798,9 @@ def visit_module(node)
17771798
current_iseq,
17781799
node
17791800
) do
1801+
builder.event(:RUBY_EVENT_CLASS)
17801802
visit(node.bodystmt)
1803+
builder.event(:RUBY_EVENT_END)
17811804
builder.leave
17821805
end
17831806

@@ -2069,7 +2092,9 @@ def visit_sclass(node)
20692092
current_iseq,
20702093
node
20712094
) do
2095+
builder.event(:RUBY_EVENT_CLASS)
20722096
visit(node.bodystmt)
2097+
builder.event(:RUBY_EVENT_END)
20732098
builder.leave
20742099
end
20752100

test/compiler_test.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,20 @@ def serialize_iseq(iseq)
453453
serialized[4].delete(:node_ids)
454454

455455
serialized[13] = serialized[13].filter_map do |insn|
456-
next unless insn.is_a?(Array)
457-
458-
insn.map do |operand|
459-
if operand.is_a?(Array) &&
460-
operand[0] == "YARVInstructionSequence/SimpleDataFormat"
461-
serialize_iseq(operand)
462-
else
463-
operand
456+
case insn
457+
when Array
458+
insn.map do |operand|
459+
if operand.is_a?(Array) &&
460+
operand[0] == Visitor::Compiler::InstructionSequence::MAGIC
461+
serialize_iseq(operand)
462+
else
463+
operand
464+
end
464465
end
466+
when Integer, :RUBY_EVENT_LINE
467+
# ignore these for now
468+
else
469+
insn
465470
end
466471
end
467472

0 commit comments

Comments
 (0)