Skip to content

Commit

Permalink
Allow super to be called in singleton object method (fixes #335)
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Aug 2, 2013
1 parent 20d3593 commit 8c43aa4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion corelib/opal/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,15 @@
};

// Super dispatcher
Opal.dispatch_super = function(obj, jsid, args) {
Opal.dispatch_super = function(obj, jsid, args, defs) {
if (defs) {
if (obj._isClass) {
return defs._super['$' + jsid].apply(obj, args);
}
else {
return obj._klass._proto['$' + jsid].apply(obj, args);
}
}
if (obj._isClass) {
return obj._klass['$' + jsid].apply(obj, args);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ def js_super args, sexp
jsid = mid_to_jsid @scope.mid.to_s

if @scope.defs
[fragment(("%s._super%s.apply(this, " % [cls_name, jsid]), sexp), args, fragment(")", sexp)]
[f("$opal.dispatch_super(this, #{@scope.mid.to_s.inspect},", sexp), args, f(", #{cls_name})", sexp)]
else
[fragment("$opal.dispatch_super(#{current_self}, #{@scope.mid.to_s.inspect}, ", sexp), args, fragment(")", sexp)]
end
Expand Down
12 changes: 12 additions & 0 deletions spec/opal/language/super_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'spec_helper'

class SingletonMethodSuperSpec
def meth
"bar"
end
end

# FIXME: we cant make a better test case than this??? For some reason, a single test cannot be deduced
describe "The 'super' keyword" do
it "passes the right arguments when a variable rewrites special `arguments` js object" do
Struct.new(:a, :b, :c).new(1, 2, 3).b.should == 2
end

it "calls super method on object that defines singleton method calling super" do
obj = SingletonMethodSuperSpec.new
def obj.meth; "foo " + super; end
obj.meth.should == "foo bar"
end
end

0 comments on commit 8c43aa4

Please sign in to comment.