Skip to content

Commit 3538cf4

Browse files
committedJan 26, 2015
Raise if Class#native_alias is used on undef meth
fixes #692
1 parent d5a5607 commit 3538cf4

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed
 

Diff for: ‎spec/opal/stdlib/native/native_alias_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'native'
2+
3+
describe "Class#native_alias" do
4+
it "exposes a method to javascript without the '$' prefix" do
5+
klass = Class.new {
6+
def a
7+
2
8+
end
9+
10+
native_alias :a, :a
11+
}
12+
instance = klass.new
13+
`instance.a()`.should == 2
14+
end
15+
16+
it "raises if the aliased method isn't defined" do
17+
lambda { Class.new { native_alias :a, :not_a_method } }.should raise_error(NameError)
18+
end
19+
end

Diff for: ‎spec/opal/stdlib/native/native_module_spec.rb

Whitespace-only changes.

Diff for: ‎stdlib/native.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,14 @@ def native_module
538538
end
539539

540540
class Class
541-
def native_alias(jsid, mid)
542-
`#{self}.$$proto[#{jsid}] = #{self}.$$proto['$' + #{mid}]`
541+
def native_alias(new_jsid, existing_mid)
542+
%x{
543+
var aliased = #{self}.$$proto['$' + #{existing_mid}];
544+
if (!aliased) {
545+
#{raise NameError, "undefined method `#{existing_mid}' for class `#{inspect}'"};
546+
}
547+
#{self}.$$proto[#{new_jsid}] = aliased;
548+
}
543549
end
544550

545551
alias native_class native_module

1 commit comments

Comments
 (1)

matthewp commented on Jan 26, 2015

@matthewp

Good change!

Please sign in to comment.