mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
add support for embedding ruby instances
This commit is contained in:
parent
cb516973af
commit
138d41dc98
7 changed files with 73 additions and 11 deletions
|
@ -3,6 +3,7 @@
|
||||||
* easily manipulate javascript objects from ruby (NativeObject)
|
* easily manipulate javascript objects from ruby (NativeObject)
|
||||||
* make NativeObject Enumerable
|
* make NativeObject Enumerable
|
||||||
* to_h and to_json for NativeObject
|
* to_h and to_json for NativeObject
|
||||||
|
* embed ruby instances and call methods from javascript
|
||||||
|
|
||||||
=== 1.72.0 2009-09-24
|
=== 1.72.0 2009-09-24
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,10 @@ $:.unshift(File.dirname(__FILE__)) unless
|
||||||
|
|
||||||
|
|
||||||
module Rhino
|
module Rhino
|
||||||
VERSION = '1.72.0'
|
VERSION = '1.72.1'
|
||||||
require 'rhino/java'
|
require 'rhino/java'
|
||||||
require 'rhino/context'
|
require 'rhino/context'
|
||||||
require 'rhino/wormhole'
|
require 'rhino/wormhole'
|
||||||
require 'rhino/native_object'
|
require 'rhino/native_object'
|
||||||
|
require 'rhino/ruby_object'
|
||||||
end
|
end
|
|
@ -52,12 +52,17 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
class Function < J::BaseFunction
|
class Function < J::BaseFunction
|
||||||
def initialize(&block)
|
def initialize(callable = nil, &block)
|
||||||
@block = block
|
super()
|
||||||
|
@block = callable || block
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(cxt, scope, this, args)
|
def call(cxt, scope, this, args)
|
||||||
@block.call(*args)
|
@block.call(*(args.map {|a| To.ruby(a)}))
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_json(*args)
|
||||||
|
'"[Native Function]"'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,7 +73,7 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
def message
|
def message
|
||||||
@native.message
|
@native.cause.details
|
||||||
end
|
end
|
||||||
|
|
||||||
def javascript_backtrace
|
def javascript_backtrace
|
||||||
|
|
|
@ -13,7 +13,7 @@ module Rhino
|
||||||
end
|
end
|
||||||
|
|
||||||
def []=(k,v)
|
def []=(k,v)
|
||||||
@j.put(k.to_s,@j,v)
|
@j.put(k.to_s,@j,To.javascript(v))
|
||||||
end
|
end
|
||||||
|
|
||||||
def each
|
def each
|
||||||
|
|
35
lib/rhino/ruby_object.rb
Normal file
35
lib/rhino/ruby_object.rb
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
|
||||||
|
module Rhino
|
||||||
|
class RubyObject < J::ScriptableObject
|
||||||
|
include J::Wrapper
|
||||||
|
|
||||||
|
def initialize(object)
|
||||||
|
super()
|
||||||
|
@ruby = object
|
||||||
|
end
|
||||||
|
|
||||||
|
def unwrap
|
||||||
|
@ruby
|
||||||
|
end
|
||||||
|
|
||||||
|
def getClassName()
|
||||||
|
@ruby.class.name
|
||||||
|
end
|
||||||
|
|
||||||
|
def getPrototype()
|
||||||
|
@prototype ||= J::NativeObject.new.tap do |p|
|
||||||
|
p.put("toString", p, Function.new {to_s})
|
||||||
|
for name in @ruby.public_methods(false).reject {|m| m == 'initialize'}
|
||||||
|
method = @ruby.method(name)
|
||||||
|
p.put(name.gsub(/_(\w)/) {$1.upcase}, p, Function.new(method) {})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_s
|
||||||
|
"[Native #{@ruby.class.name}]"
|
||||||
|
end
|
||||||
|
|
||||||
|
alias_method :prototype, :getPrototype
|
||||||
|
end
|
||||||
|
end
|
|
@ -5,9 +5,10 @@ module Rhino
|
||||||
|
|
||||||
def ruby(object)
|
def ruby(object)
|
||||||
case object
|
case object
|
||||||
when *JS_UNDEF then nil
|
when *JS_UNDEF then nil
|
||||||
when J::Wrapper then object.unwrap
|
when Rhino::RubyObject then object
|
||||||
when J::Scriptable then NativeObject.new(object)
|
when J::Wrapper then object.unwrap
|
||||||
|
when J::Scriptable then NativeObject.new(object)
|
||||||
else object
|
else object
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,6 +17,7 @@ module Rhino
|
||||||
case object
|
case object
|
||||||
when NativeObject then object.j
|
when NativeObject then object.j
|
||||||
when J::Scriptable then object
|
when J::Scriptable then object
|
||||||
|
else RubyObject.new(object)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -52,5 +52,23 @@ describe Rhino::To do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "creates a prototype for the object based on its class" do
|
||||||
|
Class.new.tap do |c|
|
||||||
|
c.class_eval do
|
||||||
|
def foo(one, two)
|
||||||
|
"1: #{one}, 2: #{two}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
To.javascript(c.new).tap do |o|
|
||||||
|
o.should be_kind_of(RubyObject)
|
||||||
|
o.prototype.tap do |p|
|
||||||
|
p.should_not be_nil
|
||||||
|
p.get("foo", p).should_not be_nil
|
||||||
|
p.get("toString", p).should_not be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Add table
Add a link
Reference in a new issue