Skip to content

Commit

Permalink
Partial rewrite of CallNode
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 24, 2013
1 parent 33264fb commit 145bf2b
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions lib/opal/nodes/call.rb
Expand Up @@ -8,25 +8,15 @@ class CallNode < Base
children :recvr, :meth, :arglist, :iter

def compile
if handled = self.handle_special
push handled
return
end

mid = mid_to_jsid meth.to_s
# if we handle this call specially, just do that and return early
return if self.handle_special

compiler.method_calls << meth.to_sym

# trying to access an lvar in irb mode
if using_irb?
with_temp do |tmp|
lvar = variable(meth)
call = s(:call, s(:self), meth.intern, s(:arglist))
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
end
# if trying to access an lvar in irb mode
return compile_irb_var if using_irb?

return
end
mid = mid_to_jsid meth.to_s

splat = arglist[1..-1].any? { |a| a.first == :splat }

Expand Down Expand Up @@ -60,9 +50,7 @@ def compile
end

if splat
push ".apply("
push(tmprecv || recv_code)
push ", ", args, ")"
push ".apply(", (tmprecv || recv_code), ", ", args, ")"
elsif tmpfunc
push ".call(", args, ")"
else
Expand All @@ -76,6 +64,17 @@ def recv_sexp
recvr || s(:self)
end

# Used to generate the code to use this sexp as an ivar var reference
def compile_irb_var
with_temp do |tmp|
lvar = variable(meth)
call = s(:call, s(:self), meth.intern, s(:arglist))
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
end
end

# a variable reference in irb mode in top scope might be a var ref,
# or it might be a method call
def using_irb?
@compiler.irb? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
end
Expand All @@ -84,11 +83,9 @@ def using_irb?
# this method. If this method returns nil, then the method will continue
# to be generated by CallNode.
def handle_special
case meth
when :require then handle_require
when :autoload then handle_autoload
when :block_given? then handle_block_given
when :__method__, :__callee__ then handle_callee
if respond_to? "handle_#{meth}"
push __send__("handle_#{meth}")
return true
end
end

Expand All @@ -106,18 +103,20 @@ def handle_autoload
end
end

def handle_block_given
def handle_block_given?
compiler.handle_block_given_call @sexp
end

def handle_callee
def handle___callee__
if scope.def?
fragment scope.mid.to_s.inspect
else
fragment 'nil'
end
end

alias handle___method__ handle___callee__

class DependencyResolver
def initialize(compiler, sexp)
@compiler = compiler
Expand Down

0 comments on commit 145bf2b

Please sign in to comment.