Skip to content

Commit

Permalink
Expose classes, modules and methods to js
Browse files Browse the repository at this point in the history
  • Loading branch information
elia committed Nov 14, 2013
1 parent 71bc1d5 commit c682171
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
33 changes: 33 additions & 0 deletions spec/stdlib/native/exposure_spec.rb
@@ -0,0 +1,33 @@
require 'native'

describe 'Native exposure' do
describe Class do
describe '#native_alias' do
it 'exposes a method to javascript' do
c = Class.new do
def ruby_method
:ruby
end

native_alias :rubyMethod, :ruby_method
end

`#{c.new}.rubyMethod()`.should == :ruby
end
end

describe '#native_class' do
it 'exposes a Class on the JS global object' do
c = Class.new do
def self.name
'Pippo'
end

native_class
end

`Pippo`.should == c
end
end
end
end
14 changes: 14 additions & 0 deletions stdlib/native.rb
Expand Up @@ -479,5 +479,19 @@ def to_n
end
end

class Module
def native_module
`Opal.global[#{self.name}] = #{self}`
end
end

class Class
def native_alias(jsid, mid)
`#{self}._proto[#{jsid}] = #{self}._proto['$' + #{mid}]`
end

alias native_class native_module
end

# native global
$$ = $global = Native(`Opal.global`)
13 changes: 13 additions & 0 deletions stdlib/native_exposure.rb
@@ -0,0 +1,13 @@
class Module
def native_module
`Opal.global[#{self.name}] = #{self}`
end
end

class Class
def native_alias(jsid, mid)
`#{self}._proto[#{jsid}] = #{self}._proto['$' + #{mid}]`
end

alias native_class native_module
end

2 comments on commit c682171

@meh
Copy link
Member

@meh meh commented on c682171 Nov 14, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to delete the old file.

@elia
Copy link
Member Author

@elia elia commented on c682171 Nov 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh, sorry, thanks for the fix :)

Please sign in to comment.