Skip to content

Commit 145bf2b

Browse files
committedOct 24, 2013
Partial rewrite of CallNode
1 parent 33264fb commit 145bf2b

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed
 

Diff for: ‎lib/opal/nodes/call.rb

+24-25
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,15 @@ class CallNode < Base
88
children :recvr, :meth, :arglist, :iter
99

1010
def compile
11-
if handled = self.handle_special
12-
push handled
13-
return
14-
end
15-
16-
mid = mid_to_jsid meth.to_s
11+
# if we handle this call specially, just do that and return early
12+
return if self.handle_special
1713

1814
compiler.method_calls << meth.to_sym
1915

20-
# trying to access an lvar in irb mode
21-
if using_irb?
22-
with_temp do |tmp|
23-
lvar = variable(meth)
24-
call = s(:call, s(:self), meth.intern, s(:arglist))
25-
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
26-
end
16+
# if trying to access an lvar in irb mode
17+
return compile_irb_var if using_irb?
2718

28-
return
29-
end
19+
mid = mid_to_jsid meth.to_s
3020

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

@@ -60,9 +50,7 @@ def compile
6050
end
6151

6252
if splat
63-
push ".apply("
64-
push(tmprecv || recv_code)
65-
push ", ", args, ")"
53+
push ".apply(", (tmprecv || recv_code), ", ", args, ")"
6654
elsif tmpfunc
6755
push ".call(", args, ")"
6856
else
@@ -76,6 +64,17 @@ def recv_sexp
7664
recvr || s(:self)
7765
end
7866

67+
# Used to generate the code to use this sexp as an ivar var reference
68+
def compile_irb_var
69+
with_temp do |tmp|
70+
lvar = variable(meth)
71+
call = s(:call, s(:self), meth.intern, s(:arglist))
72+
push "((#{tmp} = $opal.irb_vars.#{lvar}) == null ? ", expr(call), " : #{tmp})"
73+
end
74+
end
75+
76+
# a variable reference in irb mode in top scope might be a var ref,
77+
# or it might be a method call
7978
def using_irb?
8079
@compiler.irb? and scope.top? and arglist == s(:arglist) and recvr.nil? and iter.nil?
8180
end
@@ -84,11 +83,9 @@ def using_irb?
8483
# this method. If this method returns nil, then the method will continue
8584
# to be generated by CallNode.
8685
def handle_special
87-
case meth
88-
when :require then handle_require
89-
when :autoload then handle_autoload
90-
when :block_given? then handle_block_given
91-
when :__method__, :__callee__ then handle_callee
86+
if respond_to? "handle_#{meth}"
87+
push __send__("handle_#{meth}")
88+
return true
9289
end
9390
end
9491

@@ -106,18 +103,20 @@ def handle_autoload
106103
end
107104
end
108105

109-
def handle_block_given
106+
def handle_block_given?
110107
compiler.handle_block_given_call @sexp
111108
end
112109

113-
def handle_callee
110+
def handle___callee__
114111
if scope.def?
115112
fragment scope.mid.to_s.inspect
116113
else
117114
fragment 'nil'
118115
end
119116
end
120117

118+
alias handle___method__ handle___callee__
119+
121120
class DependencyResolver
122121
def initialize(compiler, sexp)
123122
@compiler = compiler

0 commit comments

Comments
 (0)