therubyrhino 1.72.3-jruby → 1.72.4-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,5 +1,11 @@
1
- === 1.72.3 2009 11-11
2
- * 1 major enhancement
1
+ === 1.72.4 2009-11-12
2
+ * 3 major enhancements:
3
+ * automatically wrap/unwrap ruby and javascript arrays
4
+ * automatically convert ruby method objects and Proc objects into javascript functions
5
+ * Make functions defined in javascript callable from ruby
6
+
7
+ === 1.72.3 2009-11-11
8
+ * 4 major enhancements:
3
9
  * greatly simplified interface to context by unifying context and scope
4
10
  * remove Context#open_std()
5
11
  * remove Context#standard
data/Manifest.txt CHANGED
@@ -5,8 +5,10 @@ Rakefile
5
5
  lib/rhino.rb
6
6
  lib/rhino/context.rb
7
7
  lib/rhino/java.rb
8
+ lib/rhino/native_function.rb
8
9
  lib/rhino/native_object.rb
9
10
  lib/rhino/rhino-1.7R2.jar
11
+ lib/rhino/ruby_function.rb
10
12
  lib/rhino/ruby_object.rb
11
13
  lib/rhino/wormhole.rb
12
14
  script/console
@@ -14,6 +16,7 @@ script/destroy
14
16
  script/generate
15
17
  spec/rhino/context_spec.rb
16
18
  spec/rhino/native_object_spec.rb
19
+ spec/rhino/ruby_object_spec.rb
17
20
  spec/rhino/wormhole_spec.rb
18
21
  spec/spec.opts
19
22
  spec/spec_helper.rb
data/README.rdoc CHANGED
@@ -17,33 +17,31 @@ Embed the Mozilla Rhino Javascript interpreter into Ruby
17
17
  1. Ruby Objects goes into Javascript
18
18
  1. Our shark's in the Javascript!
19
19
 
20
-
21
- include Rhino
22
20
  # evaluate some simple javascript
23
21
 
24
22
  Rhino::Context.open do |context|
25
- context.evaljs("7 * 6") #=> 42
23
+ context.eval("7 * 6") #=> 42
26
24
  end
27
25
 
28
26
  # evaluate a ruby function from javascript
29
27
 
30
28
  Rhino::Context.open do |context|
31
- scope = context.init_standard_objects
32
- scope["say"] = function {|word, times| word * times}
33
- context.evaljs("say("Hello", 3)") #=> HelloHelloHello
29
+ context["say"] = lambda {|word, times| word * times}
30
+ context.eval("say("Hello", 3)") #=> HelloHelloHello
34
31
  end
35
32
 
36
33
  # Configure your embedding setup
37
34
 
38
- Rhino::Context.open do |context|
39
- # Make your standard objects (Object, String, etc...) immutable
40
- scope = context.init_standard_objects(:sealed => true)
41
- context.evaljs("Object.prototype.toString = function() {}") # this is an error!
42
-
43
- #Turn on Java integration from javascript (probably a bad idea)
44
- scope = context.init_standard_objects(:java => true)
45
- context.evaljs("java.lang.System.exit()") #it's dangerous!
35
+ # Make your standard objects (Object, String, etc...) immutable
36
+ Rhino::Context.open(:sealed => true) do |context|
37
+ context.eval("Object.prototype.toString = function() {}") # this is an error!
46
38
  end
39
+
40
+ #Turn on Java integration from javascript (probably a bad idea)
41
+ Rhino::Context.open(:java => true) do |context|
42
+ context.eval("java.lang.System.exit()") #it's dangerous!
43
+ end
44
+
47
45
  == REQUIREMENTS:
48
46
 
49
47
  * JRuby >= 1.3.0
data/lib/rhino.rb CHANGED
@@ -3,10 +3,12 @@ $:.unshift(File.dirname(__FILE__)) unless
3
3
 
4
4
 
5
5
  module Rhino
6
- VERSION = '1.72.3'
6
+ VERSION = '1.72.4'
7
7
  require 'rhino/java'
8
8
  require 'rhino/context'
9
9
  require 'rhino/wormhole'
10
- require 'rhino/native_object'
11
10
  require 'rhino/ruby_object'
11
+ require 'rhino/ruby_function'
12
+ require 'rhino/native_object'
13
+ require 'rhino/native_function'
12
14
  end
data/lib/rhino/context.rb CHANGED
@@ -1,10 +1,6 @@
1
1
  module Rhino
2
- def function(&impl)
3
- Function.new &impl
4
- end
5
-
6
- class Context
7
-
2
+
3
+ class Context
8
4
  attr_reader :scope
9
5
 
10
6
  class << self
@@ -50,22 +46,7 @@ module Rhino
50
46
  end
51
47
 
52
48
  end
53
-
54
- class Function < J::BaseFunction
55
- def initialize(callable = nil, &block)
56
- super()
57
- @block = callable || block
58
- end
59
-
60
- def call(cxt, scope, this, args)
61
- @block.call(*(args.map {|a| To.ruby(a)}))
62
- end
63
-
64
- def to_json(*args)
65
- '"[Native Function]"'
66
- end
67
- end
68
-
49
+
69
50
  class ContextFactory < J::ContextFactory
70
51
 
71
52
  def observeInstructionCount(cxt, count)
@@ -0,0 +1,14 @@
1
+
2
+ module Rhino
3
+ class NativeFunction < NativeObject
4
+ def call(*args)
5
+ begin
6
+ cxt = J::Context.enter()
7
+ scope = @j.getParentScope() || cxt.initStandardObjects()
8
+ @j.call(cxt, scope, scope, args.map {|o| To.javascript(o)})
9
+ ensure
10
+ J::Context.exit()
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module Rhino
3
+ class RubyFunction < J::BaseFunction
4
+
5
+ def initialize(callable)
6
+ super()
7
+ @callable = callable
8
+ end
9
+
10
+ def call(cxt, scope, this, args)
11
+ To.javascript @callable.call(*args.map {|a| To.ruby(a)})
12
+ end
13
+ end
14
+ end
@@ -17,13 +17,7 @@ module Rhino
17
17
  end
18
18
 
19
19
  def getPrototype()
20
- @prototype ||= J::NativeObject.new.tap do |p|
21
- p.put("toString", p, Function.new {to_s})
22
- for name in @ruby.public_methods(false).reject {|m| m == 'initialize'}
23
- method = @ruby.method(name)
24
- p.put(name.gsub(/_(\w)/) {$1.upcase}, p, Function.new(method) {})
25
- end
26
- end
20
+ Prototype::Generic
27
21
  end
28
22
 
29
23
  def to_s
@@ -31,5 +25,27 @@ module Rhino
31
25
  end
32
26
 
33
27
  alias_method :prototype, :getPrototype
28
+
29
+
30
+ class Prototype < J::ScriptableObject
31
+
32
+ def get(name, start)
33
+ robject = To.ruby(start)
34
+ rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
35
+ if (robject.public_methods(false).include?(rb_name))
36
+ method = robject.method(rb_name)
37
+ RubyFunction.new(method)
38
+ else
39
+ super(name, start)
40
+ end
41
+ end
42
+
43
+ def has(name, start)
44
+ rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
45
+ To.ruby(start).public_methods(false).respond_to?(rb_name) ? true : super(name,start)
46
+ end
47
+
48
+ Generic = new
49
+ end
34
50
  end
35
51
  end
@@ -7,6 +7,8 @@ module Rhino
7
7
  case object
8
8
  when *JS_UNDEF then nil
9
9
  when J::Wrapper then object.unwrap
10
+ when J::NativeArray then array(object)
11
+ when J::Function then NativeFunction.new(object)
10
12
  when J::Scriptable then NativeObject.new(object)
11
13
  else object
12
14
  end
@@ -14,12 +16,20 @@ module Rhino
14
16
 
15
17
  def javascript(object)
16
18
  case object
17
- when NativeObject then object.j
18
- when J::Scriptable then object
19
+ when String,Numeric then object
20
+ when TrueClass,FalseClass then object
21
+ when Array then J::NativeArray.new(object.to_java)
22
+ when Proc,Method then RubyFunction.new(object)
23
+ when NativeObject then object.j
24
+ when J::Scriptable then object
19
25
  else RubyObject.new(object)
20
26
  end
21
27
  end
22
28
 
23
- module_function :ruby, :javascript
29
+ def array(native)
30
+ native.length.times.map {|i| native.get(i,native)}
31
+ end
32
+
33
+ module_function :ruby, :javascript, :array
24
34
  end
25
35
  end
@@ -66,7 +66,7 @@ describe Rhino::Context do
66
66
 
67
67
  it "can call ruby functions from javascript" do
68
68
  Context.open do |cxt|
69
- cxt["say"] = function {|word, times| word * times}
69
+ cxt["say"] = lambda {|word, times| word * times}
70
70
  cxt.eval("say('Hello',2)").should == "HelloHello"
71
71
  end
72
72
  end
@@ -12,7 +12,7 @@ describe Rhino::NativeObject do
12
12
 
13
13
  it "wraps a native javascript object" do
14
14
  @o["foo"] = 'bar'
15
- @j.get("foo", @j).unwrap.should == "bar"
15
+ @j.get("foo", @j).should == "bar"
16
16
  @j.put("blue",@j, "blam")
17
17
  @o["blue"].should == "blam"
18
18
  end
@@ -0,0 +1,68 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Rhino::RubyObject do
4
+
5
+ before(:each) do
6
+ @class = Class.new
7
+ @instance = @class.new
8
+ end
9
+
10
+ it "can call public locally defined ruby methods" do
11
+ class_eval do
12
+ def voo
13
+ "doo"
14
+ end
15
+ end
16
+ eval("o.voo").should_not be_nil
17
+ eval("o.voo()").should == "doo"
18
+ end
19
+
20
+ it "translates ruby naming conventions into javascript naming conventions, but you can still access them by their original names" do
21
+ class_eval do
22
+ def my_special_method
23
+ "hello"
24
+ end
25
+ end
26
+ eval("o.mySpecialMethod").should_not be_nil
27
+ eval("o.mySpecialMethod()").should == "hello"
28
+ eval("o.my_special_method").should_not be_nil
29
+ eval("o.my_special_method()").should == "hello"
30
+ end
31
+
32
+ it "hides methods not defined directly on this instance's class" do
33
+ class_eval do
34
+ def bar
35
+ end
36
+ end
37
+ eval("o.to_s").should be_nil
38
+ end
39
+
40
+ it "translated camel case properties are enumerated by default, but perl case are not"
41
+
42
+ it "will see a method that appears after the wrapper was first created" do
43
+ Rhino::Context.open do |cxt|
44
+ cxt['o'] = @instance
45
+ class_eval do
46
+ def bar
47
+ "baz!"
48
+ end
49
+ end
50
+ cxt.eval("o.bar").should_not be_nil
51
+ cxt.eval("o.bar()").should == "baz!"
52
+ end
53
+ end
54
+
55
+ it "allows you to specify which methods should be treated as properties"
56
+
57
+
58
+ def eval(str)
59
+ Rhino::Context.open do |cxt|
60
+ cxt['o'] = @instance
61
+ cxt.eval(str)
62
+ end
63
+ end
64
+
65
+ def class_eval(&body)
66
+ @class.class_eval &body
67
+ end
68
+ end
@@ -8,7 +8,7 @@ describe Rhino::To do
8
8
  To.ruby(J::Scriptable::NOT_FOUND).should be_nil
9
9
  end
10
10
 
11
- it "wraps native javascript objects in a ruby NativeObject wrapper" do
11
+ it "converts javascript arrays to ruby arrays" do
12
12
  J::NativeObject.new.tap do |o|
13
13
  To.ruby(o).tap do |ruby_object|
14
14
  ruby_object.should respond_to(:j)
@@ -17,6 +17,33 @@ describe Rhino::To do
17
17
  end
18
18
  end
19
19
 
20
+ it "wraps native javascript arrays into a ruby NativeArray wrapper" do
21
+ J::NativeArray.new([1,2,4].to_java).tap do |a|
22
+ To.ruby(a).should == [1,2,4]
23
+ end
24
+ end
25
+
26
+ it "wraps native javascript functions into a ruby NativeFunction wrapper" do
27
+
28
+ c = Class.new(J::BaseFunction).class_eval do
29
+ self.tap do
30
+ def call(cxt, scope, this, args)
31
+ args.join(',')
32
+ end
33
+ end
34
+ end
35
+
36
+ c.new.tap do |f|
37
+ To.ruby(f).tap do |o|
38
+ o.should_not be_nil
39
+ o.should be_kind_of(NativeObject)
40
+ o.should be_respond_to(:call)
41
+ o.call(1,2,3).should == "1,2,3"
42
+ end
43
+ end
44
+
45
+ end
46
+
20
47
  it "leaves native ruby objects alone" do
21
48
  Object.new.tap do |o|
22
49
  To.ruby(o).should be(o)
@@ -40,6 +67,15 @@ describe Rhino::To do
40
67
  end
41
68
 
42
69
  describe "javascript translation" do
70
+
71
+ it "passes primitives through to the js layer to let jruby and rhino do he thunking" do
72
+ to(1).should be(1)
73
+ to(2.5).should == 2.5
74
+ to("foo").should == "foo"
75
+ to(true).should be(true)
76
+ to(false).should be(false)
77
+ end
78
+
43
79
  it "unwraps wrapped ruby objects before passing them to the javascript runtime" do
44
80
  J::NativeObject.new.tap do |o|
45
81
  To.javascript(NativeObject.new(o)).should be(o)
@@ -51,6 +87,27 @@ describe Rhino::To do
51
87
  To.javascript(o).should be(o)
52
88
  end
53
89
  end
90
+
91
+ it "converts ruby arrays into javascript arrays" do
92
+ To.javascript([1,2,3,4,5]).tap do |a|
93
+ a.should be_kind_of(J::NativeArray)
94
+ a.get(0,a).should be(1)
95
+ a.get(1,a).should be(2)
96
+ a.get(2,a).should be(3)
97
+ a.get(3,a).should be(4)
98
+ end
99
+ end
100
+
101
+ it "converts procs and methods into native functions" do
102
+ to(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
103
+ f.should be_kind_of(J::Function)
104
+ f.call(nil, nil, nil, [7,6]).should be(42)
105
+ end
106
+ to("foo,bar,baz".method(:split)).tap do |m|
107
+ m.should be_kind_of(J::Function)
108
+ To.ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
109
+ end
110
+ end
54
111
 
55
112
  it "creates a prototype for the object based on its class" do
56
113
  Class.new.tap do |c|
@@ -71,4 +128,8 @@ describe Rhino::To do
71
128
  end
72
129
  end
73
130
  end
131
+
132
+ def to(object)
133
+ To.javascript(object)
134
+ end
74
135
  end
data/therubyrhino.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{therubyrhino}
5
- s.version = "1.72.3"
5
+ s.version = "1.72.4"
6
6
  s.platform = %q{jruby}
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -11,7 +11,7 @@ Gem::Specification.new do |s|
11
11
  s.description = %q{Embed the Mozilla Rhino Javascript interpreter into Ruby}
12
12
  s.email = ["cowboyd@thefrontside.net"]
13
13
  s.extra_rdoc_files = ["History.txt", "Manifest.txt"]
14
- s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/rhino.rb", "lib/rhino/context.rb", "lib/rhino/java.rb", "lib/rhino/native_object.rb", "lib/rhino/rhino-1.7R2.jar", "lib/rhino/ruby_object.rb", "lib/rhino/wormhole.rb", "push.sh", "script/console", "script/destroy", "script/generate", "spec/rhino/context_spec.rb", "spec/rhino/native_object_spec.rb", "spec/rhino/wormhole_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/jruby.rake", "tasks/rspec.rake", "therubyrhino.gemspec"]
14
+ s.files = ["History.txt", "Manifest.txt", "README.rdoc", "Rakefile", "lib/rhino.rb", "lib/rhino/context.rb", "lib/rhino/java.rb", "lib/rhino/native_function.rb", "lib/rhino/native_object.rb", "lib/rhino/rhino-1.7R2.jar", "lib/rhino/ruby_function.rb", "lib/rhino/ruby_object.rb", "lib/rhino/wormhole.rb", "script/console", "script/destroy", "script/generate", "spec/rhino/context_spec.rb", "spec/rhino/native_object_spec.rb", "spec/rhino/ruby_object_spec.rb", "spec/rhino/wormhole_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/jruby.rake", "tasks/rspec.rake", "therubyrhino.gemspec"]
15
15
  s.homepage = %q{http://github.com/cowboyd/therubyrhino}
16
16
  s.rdoc_options = ["--main", "README.rdoc"]
17
17
  s.require_paths = ["lib"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: therubyrhino
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.72.3
4
+ version: 1.72.4
5
5
  platform: jruby
6
6
  authors:
7
7
  - Charles Lowell
@@ -40,8 +40,10 @@ files:
40
40
  - lib/rhino.rb
41
41
  - lib/rhino/context.rb
42
42
  - lib/rhino/java.rb
43
+ - lib/rhino/native_function.rb
43
44
  - lib/rhino/native_object.rb
44
45
  - lib/rhino/rhino-1.7R2.jar
46
+ - lib/rhino/ruby_function.rb
45
47
  - lib/rhino/ruby_object.rb
46
48
  - lib/rhino/wormhole.rb
47
49
  - script/console
@@ -49,6 +51,7 @@ files:
49
51
  - script/generate
50
52
  - spec/rhino/context_spec.rb
51
53
  - spec/rhino/native_object_spec.rb
54
+ - spec/rhino/ruby_object_spec.rb
52
55
  - spec/rhino/wormhole_spec.rb
53
56
  - spec/spec.opts
54
57
  - spec/spec_helper.rb