Skip to content
Browse files

#def, #include and #extend for the interop API

  • Loading branch information...
1 parent e465c9d commit 61f4c9f90799f2e371b9b1d82aec5aec5a9fec98 @txus committed Jan 27, 2012
Showing with 45 additions and 1 deletion.
  1. +13 −0 lib/noscript/runtime.rb
  2. +32 −1 test/interop_test.rb
View
13 lib/noscript/runtime.rb
@@ -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)
@@ -141,6 +148,7 @@ def noscript_send(name, *args)
end
noscript_alias [:==, :"!="]
+ noscript_alias [:include, :extend, :def]
noscript_def("@!") { !self }
noscript_alias :nil?
@@ -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
View
33 test/interop_test.rb
@@ -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.
Something went wrong with that request. Please try again.