Skip to content

Commit

Permalink
push js_truthy parts explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 22, 2013
1 parent 87b0524 commit 8f17aae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
23 changes: 9 additions & 14 deletions lib/opal/nodes/helpers.rb
Expand Up @@ -32,26 +32,25 @@ def empty_line
end

def js_truthy(sexp)
if js_truthy_optimize(sexp)
return
if optimize = js_truthy_optimize(sexp)
return optimize
end

with_temp do |tmp|
push "(#{tmp} = ", expr(sexp), ") !== false && #{tmp} !== nil"
[fragment("(#{tmp} = "), expr(sexp), fragment(") !== false && #{tmp} !== nil")]
end
end

def js_falsy(sexp)
if sexp.type == :call
mid = sexp[2]
if mid == :block_given?
push(*@parser.handle_block_given(sexp, true))
return
return @parser.handle_block_given(sexp, true)
end
end

with_temp do |tmp|
push "(#{tmp} = ", expr(sexp), ") === false || #{tmp} === nil"
[fragment("(#{tmp} = "), expr(sexp), fragment(") === false || #{tmp} === nil")]
end
end

Expand All @@ -60,18 +59,14 @@ def js_truthy_optimize(sexp)
mid = sexp[2]

if mid == :block_given?
push expr(sexp)
true
expr(sexp)
elsif Parser::COMPARE.include? mid.to_s
push expr(sexp)
true
expr(sexp)
elsif mid == :"=="
push expr(sexp)
true
expr(sexp)
end
elsif [:lvar, :self].include? sexp.type
push expr(sexp.dup), " !== false && ", expr(sexp.dup), " !== nil"
true
[expr(sexp.dup), fragment(" !== false && "), expr(sexp.dup), fragment(" !== nil")]
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/opal/nodes/if.rb
Expand Up @@ -14,9 +14,9 @@ def compile
if falsy and !truthy
truthy = falsy
falsy = nil
js_falsy test
push js_falsy(test)
else
js_truthy test
push js_truthy(test)
end

push ") {"
Expand Down

0 comments on commit 8f17aae

Please sign in to comment.