Skip to content

Commit

Permalink
#def, #include and #extend for the interop API
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Jan 27, 2012
1 parent e465c9d commit 61f4c9f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/noscript/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ def noscript_def(name, &block)
end
end

class Class
noscript_alias [:new]
noscript_def 'def' do |name, implementation|
define_method(name, &implementation)
end
end

class Object
include Noscriptable
def noscript_send(name, *args)
Expand Down Expand Up @@ -141,6 +148,7 @@ def noscript_send(name, *args)
end

noscript_alias [:==, :"!="]
noscript_alias [:include, :extend, :def]
noscript_def("@!") { !self }

noscript_alias :nil?
Expand Down Expand Up @@ -193,13 +201,18 @@ class Runtime
class Function
attr_reader :executable
def initialize(blk_env)
@block_environment = blk_env
@executable = blk_env.code
end

def call(this, *args)
@executable.invoke(:anonymous, @executable.scope.module, this, args, nil)
end

def to_proc
Proc.__from_block__(@block_environment)
end

define_method("noscript:call") do |*args|
call(args.shift, *args)
end
Expand Down
33 changes: 32 additions & 1 deletion test/interop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,38 @@ def bar
end
end

def test_call_ruby_from_noscript
def test_toplevel_namespace
assert_equal Fixnum, compile("Ruby.Fixnum")
end

def test_define_ruby_method
compile(<<-CODE)
Ruby.Array.def('sum', ->
@ruby('reduce', '+'.ruby('to_sym'))
end)
CODE

assert_respond_to [], :sum
assert_equal 6, [1,2,3].sum
end

def test_include_ruby_module
compile(<<-CODE)
Ruby.InteropTest.Foo.include(Ruby.InteropTest.Mixin)
CODE

foo = Foo.new
assert_respond_to foo, :mixin_method
assert_equal 1234, foo.mixin_method
end

def test_extend_ruby_module
foo = compile(<<-CODE)
foo = Ruby.InteropTest.Foo.new()
foo.extend(Ruby.InteropTest.Mixin)
CODE

assert_respond_to foo, :mixin_method
assert_equal 1234, foo.mixin_method
end
end

0 comments on commit 61f4c9f

Please sign in to comment.