Skip to content

Commit 901c223

Browse files
committedNov 27, 2014
Ensure a module can redefine a method it defined before
1 parent 6bac833 commit 901c223

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed
 

‎lib/opal/nodes/def.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def compile
108108
elsif scope.top?
109109
unshift "Opal.Object.$$proto#{jsid} = "
110110
else
111-
raise "Unknown def type for `#{jsid}'"
111+
unshift "def#{jsid} = "
112112
end
113113

114114
wrap '(', ", nil) && '#{mid}'" if expr?

‎opal/corelib/module.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,12 @@ def define_method(name, method = undefined, &block)
233233
block.$$s = null;
234234
block.$$def = block;
235235
236-
Opal.defn(self, jsid, block);
236+
if (self.$$is_singleton) {
237+
self.$$proto[jsid] = block;
238+
}
239+
else {
240+
Opal.defn(self, jsid, block);
241+
}
237242
238243
return name;
239244
}

‎opal/corelib/runtime.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,9 @@
975975
}
976976

977977
// only redefine method on class if the module was included AFTER
978-
// the module which defined the current method body
979-
if (current_owner_index < module_index) {
978+
// the module which defined the current method body. Also make sure
979+
// a module can overwrite a method it defined before
980+
if (current_owner_index <= module_index) {
980981
dest[jsid] = body;
981982
dest[jsid].$$donated = true;
982983
}

‎spec/opal/core/runtime/donate_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ module A
55
def baz
66
'a'
77
end
8+
9+
def woosh
10+
:kapow
11+
end
812
end
913

1014
module B
@@ -25,6 +29,7 @@ def baz; 'b'; end
2529

2630
module A
2731
def bar; 'a'; end
32+
def woosh; :kaplunk; end
2833
end
2934
end
3035

@@ -41,4 +46,8 @@ def bar; 'a'; end
4146
@c.bar.should == 'b'
4247
@c.baz.should == 'b'
4348
end
49+
50+
it 'allows a module to override a method it previously defined' do
51+
@c.woosh.should == :kaplunk
52+
end
4453
end

0 commit comments

Comments
 (0)
Please sign in to comment.