Skip to content

Commit

Permalink
Only pass blocks to super automatically for zsuper
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Aug 2, 2013
1 parent 8e47a09 commit 2404795
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
27 changes: 20 additions & 7 deletions lib/opal/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ def process_super(sexp, level)
args << process(sexp.shift, :expr)
end

js_super [fragment("[", sexp), args, fragment("]", sexp)], sexp
js_super [fragment("[", sexp), args, fragment("]", sexp)], false, sexp
end

# super
Expand All @@ -2092,32 +2092,45 @@ def process_super(sexp, level)
def process_zsuper(exp, level)
if @scope.def?
@scope.uses_zuper = true
js_super fragment("$zuper", exp), exp
js_super fragment("$zuper", exp), true, exp
else
js_super fragment("$slice.call(arguments)", exp), exp
js_super fragment("$slice.call(arguments)", exp), true, exp
end
end

def js_super args, sexp
def js_super args, pass_block, sexp
if @scope.def_in_class?
@scope.uses_block!
mid = @scope.mid.to_s
sid = "super_#{unique_temp}"

@scope.uses_super = sid

[fragment("(#{sid}._p = $iter, #{sid}.apply(#{current_self}, ", sexp), args, fragment("))", sexp)]
if pass_block
@scope.uses_block!
[f("(#{sid}._p = $iter, #{sid}.apply(#{current_self}, ", sexp), args, f("))", sexp)]
else
[f("#{sid}.apply(#{current_self}, ", sexp), args, f(")", sexp)]
end


elsif @scope.type == :def
@scope.uses_block!
@scope.identify!
cls_name = @scope.parent.name || "#{current_self}._klass._proto"
jsid = mid_to_jsid @scope.mid.to_s

if pass_block
@scope.uses_block!
iter = "$iter"
else
iter = "null"
end

if @scope.defs
[f("$opal.dispatch_super(this, #{@scope.mid.to_s.inspect},", sexp), args, f(", $iter, #{cls_name})", sexp)]
[f("$opal.dispatch_super(this, #{@scope.mid.to_s.inspect},", sexp), args, f(", #{iter}, #{cls_name})", sexp)]
else
[fragment("$opal.dispatch_super(#{current_self}, #{@scope.mid.to_s.inspect}, ", sexp), args, fragment(", $iter)", sexp)]
[fragment("$opal.dispatch_super(#{current_self}, #{@scope.mid.to_s.inspect}, ", sexp), args, fragment(", #{iter})", sexp)]
end

elsif @scope.type == :iter
Expand Down
28 changes: 28 additions & 0 deletions spec/opal/language/super_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def self.pass_block(&block)
block_given?
end

def super_args(*a)
block_given?
end

def self.super_args(*a)
block_given?
end

class A < SingletonMethodSuperSpec
def passing_block(*a)
super
Expand All @@ -21,6 +29,14 @@ def passing_block(*a)
def self.pass_block
super
end

def super_args(*a)
super(*a)
end

def self.super_args(*a)
super()
end
end
end

Expand Down Expand Up @@ -52,4 +68,16 @@ def obj.meth; "foo " + super; end
@kls.pass_block { }.should be_true
end
end

describe "with arguments or empty parens" do
before do
@obj = SingletonMethodSuperSpec::A.new
@kls = SingletonMethodSuperSpec::A
end

it "does not pass the block to super" do
@obj.super_args(1, 2, 3) { }.should be_false
@kls.super_args() { }.should be_false
end
end
end

0 comments on commit 2404795

Please sign in to comment.