Skip to content
Prev Previous commit
Next Next commit
Fix opt table to use labels
  • Loading branch information
kddnewton committed Nov 26, 2022
commit 2115177c7f74faafdf6760e9d926417c7c648bde
9 changes: 7 additions & 2 deletions lib/syntax_tree/yarv/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1278,12 +1278,17 @@ def visit_params(node)
iseq.argument_size += 1

unless iseq.argument_options.key?(:opt)
iseq.argument_options[:opt] = [iseq.label_at_index]
start_label = iseq.label
iseq.push(start_label)
iseq.argument_options[:opt] = [start_label]
end

visit(value)
iseq.setlocal(index, 0)
iseq.argument_options[:opt] << iseq.label_at_index

arg_given_label = iseq.label
iseq.push(arg_given_label)
iseq.argument_options[:opt] << arg_given_label
end

visit(node.rest) if node.rest
Expand Down
22 changes: 12 additions & 10 deletions lib/syntax_tree/yarv/instruction_sequence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ def to_a
end
end

dumped_options = argument_options.dup
dumped_options[:opt].map!(&:name) if dumped_options[:opt]

# Next, return the instruction sequence as an array.
[
MAGIC,
Expand All @@ -252,7 +255,7 @@ def to_a
location.start_line,
type,
local_table.names,
argument_options,
dumped_options,
[],
dumped
]
Expand Down Expand Up @@ -308,10 +311,6 @@ def push(insn)
end
end

def label_at_index
push(:"label_#{length}")
end

def event(name)
push(name)
end
Expand Down Expand Up @@ -767,6 +766,11 @@ def toregexp(options, length)
def self.from(source, options = Compiler::Options.new, parent_iseq = nil)
iseq = new(source[9], source[5], parent_iseq, Location.default, options)

# set up the labels object so that the labels are shared between the
# location in the instruction sequence and the instructions that
# reference them
labels = Hash.new { |hash, name| hash[name] = Label.new(name) }

# set up the correct argument size
iseq.argument_size = source[4][:arg_size]

Expand All @@ -775,11 +779,9 @@ def self.from(source, options = Compiler::Options.new, parent_iseq = nil)

# set up the argument options
iseq.argument_options.merge!(source[11])

# set up the labels object so that the labels are shared between the
# location in the instruction sequence and the instructions that
# reference them
labels = Hash.new { |hash, name| hash[name] = Label.new(name) }
if iseq.argument_options[:opt]
iseq.argument_options[:opt].map! { |opt| labels[opt] }
end

# set up all of the instructions
source[13].each do |insn|
Expand Down