-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support safe call #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -26,19 +26,22 @@ def parser=(parser) | |||
end | ||||
end | ||||
|
||||
def jail(str, parentheses = false) | ||||
str = parentheses ? "(#{str})." : "#{str}." if str | ||||
def jail(str, parentheses = false, safe_call: false) | ||||
str = if str | ||||
dot = safe_call ? "&." : "." | ||||
parentheses ? "(#{str})#{dot}" : "#{str}#{dot}" | ||||
end | ||||
"#{str}to_jail" | ||||
end | ||||
|
||||
# split up #process_call. see below ... | ||||
def process_call(exp) | ||||
def process_call(exp, safe_call = false) | ||||
exp.shift # remove ":call" symbol | ||||
receiver = jail process_call_receiver(exp) | ||||
receiver = jail(process_call_receiver(exp), safe_call: safe_call) | ||||
name = exp.shift | ||||
args = process_call_args(exp) | ||||
|
||||
process_call_code(receiver, name, args) | ||||
process_call_code(receiver, name, args, safe_call) | ||||
end | ||||
|
||||
def process_fcall(exp) | ||||
|
@@ -159,25 +162,39 @@ def process_call_args(exp) | |||
end | ||||
args << processed unless (processed.nil? or processed.empty?) | ||||
end | ||||
args.empty? ? nil : args.join(", ") | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not quite familiar with what's that for, but why is this change? |
||||
args | ||||
end | ||||
|
||||
def process_call_code(receiver, name, args) | ||||
def process_call_code(receiver, name, args, safe_call) | ||||
case name | ||||
when :<=>, :==, "!=".to_sym, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at https://github.com/seattlerb/ruby2ruby/blob/ddf539eaf348b57c35acd91109fc6d368289b517/lib/ruby2ruby.rb#L39-L40 this implies we'll also support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of completeness, we will support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at safemode/lib/safemode/core_jails.rb Line 30 in 01af658
Before this change when I try to render
With this change the generated code will be |
||||
"(#{receiver} #{name} #{args})" | ||||
when *BINARY then | ||||
if safe_call | ||||
"#{receiver}&.#{name}(#{args.join(", ")})" | ||||
elsif args.length > 1 | ||||
"#{receiver}.#{name}(#{args.join(", ")})" | ||||
else | ||||
"(#{receiver} #{name} #{args.join(", ")})" | ||||
end | ||||
when :[] then | ||||
"#{receiver}[#{args}]" | ||||
receiver ||= "self" | ||||
"#{receiver}[#{args.join(", ")}]" | ||||
when :[]= then | ||||
receiver ||= "self" | ||||
rhs = args.pop | ||||
"#{receiver}[#{args.join(", ")}] = #{rhs}" | ||||
when :"!" then | ||||
"(not #{receiver})" | ||||
when :"-@" then | ||||
"-#{receiver}" | ||||
when :"+@" then | ||||
"+#{receiver}" | ||||
else | ||||
unless receiver.nil? then | ||||
"#{receiver}.#{name}#{args ? "(#{args})" : args}" | ||||
else | ||||
"#{name}#{args ? "(#{args})" : args}" | ||||
end | ||||
args = nil if args.empty? | ||||
args = "(#{args.join(", ")})" if args | ||||
receiver = "#{receiver}." if receiver and not safe_call | ||||
receiver = "#{receiver}&." if receiver and safe_call | ||||
|
||||
"#{receiver}#{name}#{args}" | ||||
end | ||||
end | ||||
|
||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're now more familiar with the lib, when does
safe_call
change totrue
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm reading things right then
&.
https://github.com/seattlerb/ruby_parser/blob/8f419fdbe7e58bf039cd948b426b18c2f9158548/lib/ruby_parser_extras.rb#L882process_*
methods, these methods get called as it walks the tree of sexps (somewhere around here)process_safe_call(exp)
, which just callsprocess_call(exp, :safe)