Skip to content

Commit

Permalink
[ruby/prism] Make location methods thread-safe
Browse files Browse the repository at this point in the history
* Before it could result in NoMethodError if multiple threads were
  calling location methods: https://gist.github.com/eregon/b78b7f266d7ee0a278a389cfd1782232

ruby/prism@ff762dcccd
  • Loading branch information
eregon authored and matzbot committed Feb 15, 2024
1 parent bf5cc9e commit 1b9b960
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,9 @@ def deconstruct_keys(keys)

# A Location object representing the location of this token in the source.
def location
return @location if @location.is_a?(Location)
@location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF)
location = @location
return location if location.is_a?(Location)
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

# Implement the pretty print interface for Token.
Expand Down
17 changes: 10 additions & 7 deletions prism/templates/lib/prism/node.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module Prism
# A Location instance that represents the location of this node in the
# source.
def location
return @location if @location.is_a?(Location)
@location = Location.new(source, @location >> 32, @location & 0xFFFFFFFF)
location = @location
return location if location.is_a?(Location)
@location = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end

def newline? # :nodoc:
Expand Down Expand Up @@ -196,18 +197,20 @@ module Prism
<%- case field -%>
<%- when Prism::LocationField -%>
def <%= field.name %>
return @<%= field.name %> if @<%= field.name %>.is_a?(Location)
@<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF)
location = @<%= field.name %>
return location if location.is_a?(Location)
@<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
<%- when Prism::OptionalLocationField -%>
def <%= field.name %>
case @<%= field.name %>
location = @<%= field.name %>
case location
when nil
nil
when Location
@<%= field.name %>
location
else
@<%= field.name %> = Location.new(source, @<%= field.name %> >> 32, @<%= field.name %> & 0xFFFFFFFF)
@<%= field.name %> = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end
end
<%- else -%>
Expand Down

0 comments on commit 1b9b960

Please sign in to comment.