therubyrhino 1.73.0 → 1.73.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rhino.rb +27 -9
- data/lib/rhino/context.rb +127 -100
- data/lib/rhino/deprecations.rb +52 -0
- data/lib/rhino/error.rb +44 -0
- data/lib/rhino/object.rb +10 -1
- data/lib/rhino/rhino_ext.rb +237 -0
- data/lib/rhino/ruby.rb +225 -0
- data/lib/rhino/ruby/access.rb +8 -0
- data/lib/rhino/ruby/attribute_access.rb +55 -0
- data/lib/rhino/ruby/default_access.rb +54 -0
- data/lib/rhino/version.rb +1 -1
- data/lib/rhino/wormhole.rb +63 -57
- data/spec/rhino/access_spec.rb +69 -0
- data/spec/rhino/context_spec.rb +24 -10
- data/spec/rhino/deprecations_spec.rb +41 -0
- data/spec/rhino/error_spec.rb +38 -0
- data/spec/rhino/rhino_ext_spec.rb +234 -0
- data/spec/rhino/ruby_spec.rb +390 -0
- data/spec/rhino/wormhole_spec.rb +99 -78
- data/spec/spec_helper.rb +1 -0
- data/therubyrhino.gemspec +1 -0
- metadata +26 -8
- data/lib/rhino/java.rb +0 -24
- data/lib/rhino/native_function.rb +0 -29
- data/lib/rhino/native_object.rb +0 -71
- data/lib/rhino/ruby_function.rb +0 -14
- data/lib/rhino/ruby_object.rb +0 -71
data/spec/rhino/wormhole_spec.rb
CHANGED
@@ -1,44 +1,38 @@
|
|
1
|
-
require File.dirname(__FILE__)
|
2
|
-
|
3
|
-
include Rhino
|
1
|
+
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
4
2
|
|
5
3
|
describe Rhino::To do
|
4
|
+
|
6
5
|
describe "ruby translation" do
|
6
|
+
|
7
7
|
it "converts javascript NOT_FOUND to ruby nil" do
|
8
|
-
|
8
|
+
Rhino.to_ruby(Rhino::JS::Scriptable::NOT_FOUND).should be_nil
|
9
9
|
end
|
10
10
|
|
11
|
-
it "converts javascript
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
it "converts javascript undefined into nil" do
|
12
|
+
Rhino.to_ruby(Rhino::JS::Undefined.instance).should be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it "does return javascript object" do
|
16
|
+
Rhino::JS::NativeObject.new.tap do |js_obj|
|
17
|
+
Rhino.to_ruby(js_obj).tap do |rb_obj|
|
18
|
+
rb_obj.should be(js_obj)
|
16
19
|
end
|
17
20
|
end
|
18
21
|
end
|
19
22
|
|
20
23
|
it "wraps native javascript arrays into a ruby NativeArray wrapper" do
|
21
|
-
|
22
|
-
|
24
|
+
Rhino::JS::NativeArray.new([1,2,4].to_java).tap do |js_array|
|
25
|
+
Rhino.to_ruby(js_array).should == [1,2,4]
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
26
|
-
it "
|
29
|
+
it "does return javascript function" do
|
27
30
|
|
28
|
-
|
29
|
-
self.tap do
|
30
|
-
def call(cxt, scope, this, args)
|
31
|
-
args.join(',')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
31
|
+
klass = Class.new(Rhino::JS::BaseFunction)
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
o.should be_kind_of(NativeObject)
|
40
|
-
o.should be_respond_to(:call)
|
41
|
-
o.call(1,2,3).should == "1,2,3"
|
33
|
+
klass.new.tap do |js_fn|
|
34
|
+
Rhino.to_ruby(js_fn).tap do |rb_fn|
|
35
|
+
rb_fn.should be(js_fn)
|
42
36
|
end
|
43
37
|
end
|
44
38
|
|
@@ -46,97 +40,124 @@ describe Rhino::To do
|
|
46
40
|
|
47
41
|
it "leaves native ruby objects alone" do
|
48
42
|
Object.new.tap do |o|
|
49
|
-
|
43
|
+
Rhino.to_ruby(o).should be(o)
|
50
44
|
end
|
51
45
|
end
|
52
46
|
|
53
47
|
it "it unwraps wrapped java objects" do
|
54
|
-
Context.open do |cx|
|
48
|
+
Rhino::Context.open do |cx|
|
55
49
|
scope = cx.scope
|
56
|
-
java.lang.String.new("Hello World")
|
57
|
-
|
58
|
-
|
59
|
-
end
|
50
|
+
j_str = java.lang.String.new("Hello World")
|
51
|
+
Rhino::JS::NativeJavaObject.new(scope, j_str, j_str.getClass()).tap do |o|
|
52
|
+
Rhino.to_ruby(o).should == "Hello World"
|
60
53
|
end
|
61
54
|
end
|
62
55
|
end
|
63
56
|
|
64
|
-
it "converts javascript undefined into nil" do
|
65
|
-
To.ruby(J::Undefined.instance).should be_nil
|
66
|
-
end
|
67
57
|
end
|
68
58
|
|
69
59
|
describe "javascript translation" do
|
70
60
|
|
71
61
|
it "passes primitives through to the js layer to let jruby and rhino do he thunking" do
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
it "unwraps wrapped ruby objects before passing them to the javascript runtime" do
|
80
|
-
J::NativeObject.new.tap do |o|
|
81
|
-
To.javascript(NativeObject.new(o)).should be(o)
|
82
|
-
end
|
62
|
+
Rhino.to_javascript(1).should be(1)
|
63
|
+
Rhino.to_javascript(2.5).should == 2.5
|
64
|
+
Rhino.to_javascript("foo").should == "foo"
|
65
|
+
Rhino.to_javascript(true).should be(true)
|
66
|
+
Rhino.to_javascript(false).should be(false)
|
67
|
+
Rhino.to_javascript(nil).should be_nil
|
83
68
|
end
|
84
69
|
|
85
70
|
it "leaves native javascript objects alone" do
|
86
|
-
|
87
|
-
|
71
|
+
Rhino::JS::NativeObject.new.tap do |o|
|
72
|
+
Rhino.to_javascript(o).should be(o)
|
88
73
|
end
|
89
74
|
end
|
90
75
|
|
91
76
|
it "converts ruby arrays into javascript arrays" do
|
92
|
-
|
93
|
-
a.should be_kind_of(
|
77
|
+
Rhino.to_javascript([1,2,3,4,5]).tap do |a|
|
78
|
+
a.should be_kind_of(Rhino::JS::NativeArray)
|
94
79
|
a.get(0,a).should be(1)
|
95
80
|
a.get(1,a).should be(2)
|
96
81
|
a.get(2,a).should be(3)
|
97
82
|
a.get(3,a).should be(4)
|
83
|
+
a.prototype.should be_nil # this is how Rhino works !
|
98
84
|
end
|
99
85
|
end
|
100
|
-
|
86
|
+
|
101
87
|
it "converts ruby hashes into native objects" do
|
102
|
-
|
103
|
-
h.should be_kind_of(
|
88
|
+
Rhino.to_javascript({ :bare => true }).tap do |h|
|
89
|
+
h.should be_kind_of(Rhino::JS::NativeObject)
|
104
90
|
h.get("bare", h).should be(true)
|
91
|
+
h.prototype.should be_nil # this is how Rhino works !
|
105
92
|
end
|
106
93
|
end
|
107
94
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
95
|
+
describe "with a scope" do
|
96
|
+
|
97
|
+
before do
|
98
|
+
factory = Rhino::JS::ContextFactory.new
|
99
|
+
context = nil
|
100
|
+
factory.call do |ctx|
|
101
|
+
context = ctx
|
102
|
+
@scope = context.initStandardObjects(nil, false)
|
103
|
+
end
|
104
|
+
factory.enterContext(context)
|
116
105
|
end
|
117
|
-
end
|
118
106
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
107
|
+
after do
|
108
|
+
Rhino::JS::Context.exit
|
109
|
+
end
|
110
|
+
|
111
|
+
it "converts ruby arrays into javascript arrays" do
|
112
|
+
Rhino.to_javascript([1,2,3,4,5], @scope).tap do |a|
|
113
|
+
a.should be_kind_of(Rhino::JS::NativeArray)
|
114
|
+
a.get(0,a).should be(1)
|
115
|
+
a.get(1,a).should be(2)
|
116
|
+
a.get(2,a).should be(3)
|
117
|
+
a.get(3,a).should be(4)
|
118
|
+
a.prototype.should_not be_nil
|
125
119
|
end
|
120
|
+
end
|
126
121
|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
p.get("toString", p).should_not be_nil
|
133
|
-
end
|
122
|
+
it "converts ruby hashes into native objects" do
|
123
|
+
Rhino.to_javascript({ :bare => true }, @scope).tap do |h|
|
124
|
+
h.should be_kind_of(Rhino::JS::NativeObject)
|
125
|
+
h.get("bare", h).should be(true)
|
126
|
+
h.prototype.should_not be_nil
|
134
127
|
end
|
135
128
|
end
|
136
|
-
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
it "converts procs and methods into native functions" do
|
133
|
+
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
134
|
+
f.should be_kind_of(Rhino::JS::Function)
|
135
|
+
f.call(nil, nil, nil, [7,6]).should be(42)
|
136
|
+
end
|
137
|
+
|
138
|
+
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |m|
|
139
|
+
m.should be_kind_of(Rhino::JS::Function)
|
140
|
+
Rhino.to_ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# it "creates a prototype for the object based on its class" do
|
145
|
+
# klass = Class.new do
|
146
|
+
# def foo(one, two)
|
147
|
+
# "1: #{one}, 2: #{two}"
|
148
|
+
# end
|
149
|
+
# end
|
150
|
+
#
|
151
|
+
# Rhino.to_javascript(klass.new).tap do |o|
|
152
|
+
# o.should be_kind_of(Rhino::RubyObject)
|
153
|
+
# o.prototype.tap do |p|
|
154
|
+
# p.should_not be_nil
|
155
|
+
# p.get("foo", p).should_not be_nil
|
156
|
+
# p.get("toString", p).should_not be_nil
|
157
|
+
# end
|
158
|
+
# end
|
159
|
+
# end
|
160
|
+
|
137
161
|
end
|
138
162
|
|
139
|
-
def to(object)
|
140
|
-
To.javascript(object)
|
141
|
-
end
|
142
163
|
end
|
data/spec/spec_helper.rb
CHANGED
data/therubyrhino.gemspec
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: therubyrhino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.73.
|
5
|
+
version: 1.73.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Charles Lowell
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2012-01-24 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -35,7 +35,7 @@ dependencies:
|
|
35
35
|
prerelease: false
|
36
36
|
type: :development
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
|
-
name:
|
38
|
+
name: mocha
|
39
39
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
@@ -45,6 +45,17 @@ dependencies:
|
|
45
45
|
requirement: *id003
|
46
46
|
prerelease: false
|
47
47
|
type: :development
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jruby-openssl
|
50
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
requirement: *id004
|
57
|
+
prerelease: false
|
58
|
+
type: :development
|
48
59
|
description: Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.
|
49
60
|
email: cowboyd@thefrontside.net
|
50
61
|
executables: []
|
@@ -62,17 +73,24 @@ files:
|
|
62
73
|
- Rakefile
|
63
74
|
- lib/rhino.rb
|
64
75
|
- lib/rhino/context.rb
|
65
|
-
- lib/rhino/
|
66
|
-
- lib/rhino/
|
67
|
-
- lib/rhino/native_object.rb
|
76
|
+
- lib/rhino/deprecations.rb
|
77
|
+
- lib/rhino/error.rb
|
68
78
|
- lib/rhino/object.rb
|
69
79
|
- lib/rhino/rhino-1.7R3.jar
|
70
|
-
- lib/rhino/
|
71
|
-
- lib/rhino/
|
80
|
+
- lib/rhino/rhino_ext.rb
|
81
|
+
- lib/rhino/ruby.rb
|
82
|
+
- lib/rhino/ruby/access.rb
|
83
|
+
- lib/rhino/ruby/attribute_access.rb
|
84
|
+
- lib/rhino/ruby/default_access.rb
|
72
85
|
- lib/rhino/version.rb
|
73
86
|
- lib/rhino/wormhole.rb
|
74
87
|
- spec/redjs_helper.rb
|
88
|
+
- spec/rhino/access_spec.rb
|
75
89
|
- spec/rhino/context_spec.rb
|
90
|
+
- spec/rhino/deprecations_spec.rb
|
91
|
+
- spec/rhino/error_spec.rb
|
92
|
+
- spec/rhino/rhino_ext_spec.rb
|
93
|
+
- spec/rhino/ruby_spec.rb
|
76
94
|
- spec/rhino/wormhole_spec.rb
|
77
95
|
- spec/spec.opts
|
78
96
|
- spec/spec_helper.rb
|
data/lib/rhino/java.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'java'
|
2
|
-
require 'rhino/rhino-1.7R3.jar'
|
3
|
-
|
4
|
-
module Rhino
|
5
|
-
# This module contains all the native Rhino objects implemented in Java
|
6
|
-
# e.g.
|
7
|
-
# Rhino::J::NativeObject # => org.mozilla.javascript.NativeObject
|
8
|
-
module J
|
9
|
-
import "org.mozilla.javascript"
|
10
|
-
|
11
|
-
module Regexp
|
12
|
-
import "org.mozilla.javascript.regexp"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
unless Object.method_defined?(:tap)
|
18
|
-
class Object #:nodoc:
|
19
|
-
def tap
|
20
|
-
yield self
|
21
|
-
self
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,29 +0,0 @@
|
|
1
|
-
|
2
|
-
module Rhino
|
3
|
-
|
4
|
-
# Wraps a function that has been defined in Javascript so that it can
|
5
|
-
# be referenced and called from javascript. e.g.
|
6
|
-
#
|
7
|
-
# plus = Rhino::Context.open do |cx|
|
8
|
-
# cx.eval('function(lhs, rhs) {return lhs + rhs}')
|
9
|
-
# end
|
10
|
-
# plus.call(5,4) # => 9
|
11
|
-
#
|
12
|
-
class NativeFunction < NativeObject
|
13
|
-
def call(*args)
|
14
|
-
cxt = J::Context.enter()
|
15
|
-
scope = @j.getParentScope() || cxt.initStandardObjects()
|
16
|
-
@j.call(cxt, scope, scope, args.map {|o| To.javascript(o)})
|
17
|
-
ensure
|
18
|
-
J::Context.exit()
|
19
|
-
end
|
20
|
-
|
21
|
-
def methodcall(this, *args)
|
22
|
-
cxt = J::Context.enter()
|
23
|
-
scope = @j.getParentScope() || cxt.initStandardObjects()
|
24
|
-
@j.call(cxt, scope, To.javascript(this), args.map {|o| To.javascript(o)})
|
25
|
-
ensure
|
26
|
-
J::Context.exit()
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
data/lib/rhino/native_object.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
|
2
|
-
module Rhino
|
3
|
-
# Wraps a javascript object and makes its properties available from ruby.
|
4
|
-
class NativeObject
|
5
|
-
include Enumerable
|
6
|
-
|
7
|
-
# The native java object wrapped by this NativeObject. This will generally
|
8
|
-
# be an instance of org.mozilla.javascript.Scriptable
|
9
|
-
attr_reader :j
|
10
|
-
|
11
|
-
def initialize(j=nil) # :nodoc:
|
12
|
-
@j = j || J::NativeObject.new
|
13
|
-
end
|
14
|
-
|
15
|
-
# get a property from this javascript object, where +k+ is a string or symbol
|
16
|
-
# corresponding to the property name
|
17
|
-
# e.g.
|
18
|
-
# jsobject = Context.open do |cxt|
|
19
|
-
# cxt.eval('({foo: 'bar', 'Take me to': 'a funky town'})')
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# jsobject[:foo] # => 'bar'
|
23
|
-
# jsobject['foo'] # => 'bar'
|
24
|
-
# jsobject['Take me to'] # => 'a funky town'
|
25
|
-
def [](k)
|
26
|
-
To.ruby J::ScriptableObject.getProperty(@j,k.to_s)
|
27
|
-
end
|
28
|
-
|
29
|
-
# set a property on the javascript object, where +k+ is a string or symbol corresponding
|
30
|
-
# to the property name, and +v+ is the value to set. e.g.
|
31
|
-
#
|
32
|
-
# jsobject = eval_js "new Object()"
|
33
|
-
# jsobject['foo'] = 'bar'
|
34
|
-
# Context.open(:with => jsobject) do |cxt|
|
35
|
-
# cxt.eval('foo') # => 'bar'
|
36
|
-
# end
|
37
|
-
|
38
|
-
def []=(k,v)
|
39
|
-
J::ScriptableObject.putProperty(@j, k.to_s, To.javascript(v))
|
40
|
-
end
|
41
|
-
|
42
|
-
# enumerate the key value pairs contained in this javascript object. e.g.
|
43
|
-
#
|
44
|
-
# eval_js("{foo: 'bar', baz: 'bang'}").each do |key,value|
|
45
|
-
# puts "#{key} -> #{value} "
|
46
|
-
# end
|
47
|
-
#
|
48
|
-
# outputs foo -> bar baz -> bang
|
49
|
-
def each
|
50
|
-
for id in @j.getAllIds() do
|
51
|
-
yield id,@j.get(id,@j)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
# Converts the native object to a hash. This isn't really a stretch since it's
|
56
|
-
# pretty much a hash in the first place.
|
57
|
-
def to_h
|
58
|
-
{}.tap do |h|
|
59
|
-
each do |k,v|
|
60
|
-
v = To.ruby(v)
|
61
|
-
h[k] = self.class === v ? v.to_h : v
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
# Convert this javascript object into a json string.
|
67
|
-
def to_json(*args)
|
68
|
-
to_h.to_json(*args)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|