therubyrhino 1.72.4-jruby → 1.72.5-jruby

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,8 @@
1
+ === 1.72.5 2009-11-12
2
+ * 2 major enhancements:
3
+ * evaluate javascript with a ruby object as it's scope using Context#open(:with => object)
4
+ * add eval_js() method to Object to evaluate in the context of that object
5
+
1
6
  === 1.72.4 2009-11-12
2
7
  * 3 major enhancements:
3
8
  * automatically wrap/unwrap ruby and javascript arrays
@@ -7,6 +7,7 @@ lib/rhino/context.rb
7
7
  lib/rhino/java.rb
8
8
  lib/rhino/native_function.rb
9
9
  lib/rhino/native_object.rb
10
+ lib/rhino/object.rb
10
11
  lib/rhino/rhino-1.7R2.jar
11
12
  lib/rhino/ruby_function.rb
12
13
  lib/rhino/ruby_object.rb
@@ -17,18 +17,40 @@ 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
+ require 'rhino'
21
+
20
22
  # evaluate some simple javascript
21
-
22
- Rhino::Context.open do |context|
23
- context.eval("7 * 6") #=> 42
24
- end
25
-
23
+ eval_js "7 * 6" #=> 42
24
+
26
25
  # evaluate a ruby function from javascript
27
26
 
28
27
  Rhino::Context.open do |context|
29
28
  context["say"] = lambda {|word, times| word * times}
30
29
  context.eval("say("Hello", 3)") #=> HelloHelloHello
31
30
  end
31
+
32
+ # embed a ruby object into your javascript environment
33
+
34
+ class MyMath
35
+ def plus(lhs, rhs)
36
+ lhs + rhs
37
+ end
38
+ end
39
+
40
+ Rhino::Context.open do |context|
41
+ context["math"] = MyMath.new
42
+ context.eval("math.plus(20,22)") #=> 42
43
+ end
44
+
45
+ # make a ruby object *be* your javascript environment
46
+ math = MyMath.new
47
+ Rhino::Context.open(:with => math) do |context|
48
+ context.eval("plus(20,22)") #=> 42
49
+ end
50
+
51
+ #or the equivalent
52
+
53
+ math.eval_js("plus(20,22)")
32
54
 
33
55
  # Configure your embedding setup
34
56
 
@@ -3,8 +3,9 @@ $:.unshift(File.dirname(__FILE__)) unless
3
3
 
4
4
 
5
5
  module Rhino
6
- VERSION = '1.72.4'
6
+ VERSION = '1.72.5'
7
7
  require 'rhino/java'
8
+ require 'rhino/object'
8
9
  require 'rhino/context'
9
10
  require 'rhino/wormhole'
10
11
  require 'rhino/ruby_object'
@@ -15,10 +15,16 @@ module Rhino
15
15
 
16
16
  def initialize(native, options) #:nodoc:
17
17
  @native = native
18
- @scope = NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true))
18
+ @global = NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true))
19
+ if with = options[:with]
20
+ @scope = To.javascript(with)
21
+ @scope.setParentScope(@global.j)
22
+ else
23
+ @scope = @global
24
+ end
19
25
  unless options[:java]
20
26
  for package in ["Packages", "java", "org", "com"]
21
- @scope.j.delete(package)
27
+ @global.j.delete(package)
22
28
  end
23
29
  end
24
30
  end
@@ -34,7 +40,9 @@ module Rhino
34
40
  def eval(str)
35
41
  str = str.to_s
36
42
  begin
37
- To.ruby @native.evaluateString(@scope.j, str, "<eval>", 1, nil)
43
+ scope = To.javascript(@scope)
44
+ result = @native.evaluateString(scope, str, "<eval>", 1, nil)
45
+ To.ruby result
38
46
  rescue J::RhinoException => e
39
47
  raise Rhino::RhinoError, e
40
48
  end
@@ -0,0 +1,8 @@
1
+
2
+ class Object
3
+ def eval_js(source, options = {})
4
+ Rhino::Context.open(options.merge(:with => self)) do |cxt|
5
+ cxt.eval(source)
6
+ end
7
+ end
8
+ end
@@ -20,6 +20,10 @@ module Rhino
20
20
  Prototype::Generic
21
21
  end
22
22
 
23
+ def getIds()
24
+ @ruby.public_methods(false).map {|m| m.gsub(/(.)_(.)/) {"#{$1}#{$2.upcase}"}}.to_java
25
+ end
26
+
23
27
  def to_s
24
28
  "[Native #{@ruby.class.name}]"
25
29
  end
@@ -31,6 +35,9 @@ module Rhino
31
35
 
32
36
  def get(name, start)
33
37
  robject = To.ruby(start)
38
+ if name == "toString"
39
+ return RubyFunction.new(lambda { "[Ruby #{robject.class.name}]"})
40
+ end
34
41
  rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
35
42
  if (robject.public_methods(false).include?(rb_name))
36
43
  method = robject.method(rb_name)
@@ -44,8 +51,9 @@ module Rhino
44
51
  rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
45
52
  To.ruby(start).public_methods(false).respond_to?(rb_name) ? true : super(name,start)
46
53
  end
54
+
55
+ Generic = new
47
56
 
48
- Generic = new
49
57
  end
50
58
  end
51
59
  end
@@ -27,7 +27,7 @@ module Rhino
27
27
  end
28
28
 
29
29
  def array(native)
30
- native.length.times.map {|i| native.get(i,native)}
30
+ native.length.times.map {|i| native.get(i,native)}
31
31
  end
32
32
 
33
33
  module_function :ruby, :javascript, :array
@@ -71,6 +71,42 @@ describe Rhino::Context do
71
71
  end
72
72
  end
73
73
 
74
+ it "can eval javascript with a given ruby object as the scope." do
75
+ # pending
76
+ scope = Class.new.class_eval do
77
+ def plus(lhs, rhs)
78
+ lhs + rhs
79
+ end
80
+
81
+ def minus(lhs, rhs)
82
+ lhs - rhs
83
+ end
84
+
85
+ new
86
+ end
87
+
88
+ Context.open(:with => scope) do |cxt|
89
+ cxt.eval("plus(1,2)").should == 3
90
+ cxt.eval("minus(10, 20)").should == -10
91
+ cxt.eval("this").should be(scope)
92
+ end
93
+ end
94
+
95
+ it "extends object to allow for the arbitrary execution of javascript with any object as the scope" do
96
+ Class.new.class_eval do
97
+
98
+ def initialize
99
+ @lhs = 5
100
+ end
101
+
102
+ def timesfive(rhs)
103
+ @lhs * rhs
104
+ end
105
+
106
+ new.eval_js("timesfive(6)").should == 30
107
+ end
108
+ end
109
+
74
110
  it "can limit the number of instructions that are executed in the context" do
75
111
  lambda {
76
112
  Context.open do |cxt|
@@ -37,7 +37,23 @@ describe Rhino::RubyObject do
37
37
  eval("o.to_s").should be_nil
38
38
  end
39
39
 
40
- it "translated camel case properties are enumerated by default, but perl case are not"
40
+ it "translated camel case properties are enumerated by default, but perl case are not" do
41
+ class_eval do
42
+ def foo_bar
43
+ end
44
+
45
+ def baz_bang
46
+ end
47
+ end
48
+ pending "why the hell isn't the return value of getIds() being respected?!?"
49
+ eval(<<-EOJS).should == ["fooBar,bazBang"]
50
+ var names = [];
51
+ for (var p in o) {
52
+ names.push(p);
53
+ }
54
+ names;
55
+ EOJS
56
+ end
41
57
 
42
58
  it "will see a method that appears after the wrapper was first created" do
43
59
  Rhino::Context.open do |cxt|
@@ -52,11 +68,13 @@ describe Rhino::RubyObject do
52
68
  end
53
69
  end
54
70
 
55
- it "allows you to specify which methods should be treated as properties"
71
+ it "treats ruby methods that have an arity of 0 as javascript properties by default"
56
72
 
73
+ it "will call ruby accesssor function when setting a property from javascript"
57
74
 
58
75
  def eval(str)
59
76
  Rhino::Context.open do |cxt|
77
+ cxt['puts'] = lambda {|o| puts o.inspect}
60
78
  cxt['o'] = @instance
61
79
  cxt.eval(str)
62
80
  end
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{therubyrhino}
5
- s.version = "1.72.4"
5
+ s.version = "1.72.5"
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=
9
9
  s.authors = ["Charles Lowell"]
10
- s.date = %q{2009-11-11}
10
+ s.date = %q{2009-11-13}
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"]
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.4
4
+ version: 1.72.5
5
5
  platform: jruby
6
6
  authors:
7
7
  - Charles Lowell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-11 00:00:00 -05:00
12
+ date: 2009-11-13 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - lib/rhino/java.rb
43
43
  - lib/rhino/native_function.rb
44
44
  - lib/rhino/native_object.rb
45
+ - lib/rhino/object.rb
45
46
  - lib/rhino/rhino-1.7R2.jar
46
47
  - lib/rhino/ruby_function.rb
47
48
  - lib/rhino/ruby_object.rb