therubyrhino 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/Gemfile +5 -2
- data/{History.txt → History.md} +41 -24
- data/README.md +262 -0
- data/Rakefile +1 -3
- data/lib/rhino/context.rb +1 -1
- data/lib/rhino/version.rb +1 -1
- data/lib/rhino/wormhole.rb +11 -13
- data/spec/spec_helper.rb +7 -3
- data/therubyrhino.gemspec +7 -8
- metadata +14 -35
- data/README.rdoc +0 -246
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color --format documentation
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
|
3
3
|
gemspec :name => "therubyrhino"
|
4
4
|
|
@@ -15,4 +15,7 @@ group :test do
|
|
15
15
|
gem 'therubyrhino_jar', :path => '.'
|
16
16
|
end
|
17
17
|
gem 'less', '>= 2.2.1', :require => nil
|
18
|
-
end
|
18
|
+
end
|
19
|
+
|
20
|
+
gem 'rake', :require => false, :group => :development
|
21
|
+
gem 'jruby-openssl', :group => :development if JRUBY_VERSION < '1.7'
|
data/{History.txt → History.md}
RENAMED
@@ -1,55 +1,63 @@
|
|
1
|
-
|
1
|
+
## 2.0.3 2014-02-20
|
2
|
+
|
3
|
+
* minor fix for time_to_js when scope not set (#27)
|
4
|
+
* Jar path now avoids using a glob, which doesn't work inside a jar (#26)
|
5
|
+
* use Rhino.warn explicitly otherwise it's a Kernel.warn
|
6
|
+
|
7
|
+
## 2.0.2 2012-12-05
|
2
8
|
|
3
9
|
* handle Rhino's 64K code generation (method) limit on the fly (#23)
|
4
10
|
* correct explicit Ruby equality == and eql? (JRuby 1.7.1 compat)
|
5
11
|
* allow to set javascript version programatically - globally also allow
|
6
12
|
reading it from system properties by default
|
7
|
-
* allow to set optimization level globally also allow reading it from
|
8
|
-
system properties by default
|
9
|
-
* make sure Ruby function wrapper has (Ruby) #call semantics just like
|
13
|
+
* allow to set optimization level globally also allow reading it from
|
14
|
+
system properties by default
|
15
|
+
* make sure Ruby function wrapper has (Ruby) #call semantics just like
|
10
16
|
JavaScript functions exposed into the Ruby side
|
11
17
|
* function's return value should be converted to Ruby (Ruby #call style)
|
12
18
|
|
13
|
-
|
19
|
+
## 2.0.1 2012-08-24
|
14
20
|
|
15
21
|
* JSError improvement to preserve nested Ruby error message
|
16
22
|
* jar-1.7.4 regression fix e.g when loading less (#25)
|
17
23
|
* error.message should be a String value (1.9.3 compat)
|
18
24
|
|
19
|
-
|
25
|
+
## jar-1.7.4 2012-08-02
|
20
26
|
|
21
27
|
* updated to new Mozilla Rhino 1.7R4 release, notes:
|
22
28
|
https://developer.mozilla.org/en/New_in_Rhino_1.7R4
|
23
29
|
|
24
|
-
|
25
|
-
|
30
|
+
## 2.0.0 2012-08-02
|
31
|
+
|
32
|
+
This release is functionally the same as therubyrhino-1.73.5 as long
|
26
33
|
as the therubyrhino_jar-1.7.3 gem dependency is used along with it.
|
27
34
|
|
28
35
|
* new versioning scheme - old scheme used for the jar gem
|
29
36
|
* moved out the rhino.jar into a separate therubyrhino_jar gem
|
30
37
|
|
31
|
-
|
38
|
+
## jar-1.7.3 2012-08-02
|
32
39
|
|
33
40
|
* therubyrhino_jar gem packaged with Mozilla Rhino 1.7R3
|
34
41
|
|
35
42
|
|
36
|
-
|
43
|
+
## 1.73.5 2012-07-25
|
37
44
|
|
38
45
|
* #to_s functionName typo fix for org.mozilla.javascript.ScriptStackElement
|
39
46
|
* make sure thrown values are correctly raised from inside JS functions
|
40
47
|
* a better #inspect for native rhino objects
|
41
48
|
* correct JavaScript error handling for Function#apply
|
42
49
|
|
43
|
-
|
50
|
+
## 1.73.4 2012-05-21
|
44
51
|
|
45
52
|
* allow rhino.jar path overrides with Rhino::JAR_PATH
|
46
53
|
* 'correct' JSError#inspect - show thrown value
|
47
|
-
* fix JSError#javascript_backtrace to be an array and add it on top of the
|
54
|
+
* fix JSError#javascript_backtrace to be an array and add it on top of the
|
48
55
|
(ruby) backtrace
|
49
56
|
* make sure JSError#cause always points to native rhino cause (#19)
|
50
57
|
* avoid using instance variables with 'native' JS::Context (JRuby 1.7 warnings)
|
51
58
|
|
52
|
-
|
59
|
+
## 1.73.3 2012-04-23
|
60
|
+
|
53
61
|
RedJS 0.4 compatible
|
54
62
|
|
55
63
|
* allow try-catch-ing ScriptError (besides StandardError) in JS
|
@@ -59,7 +67,8 @@ RedJS 0.4 compatible
|
|
59
67
|
* Ruby StandardError wrapping so they can be try-catched as JS "error" values
|
60
68
|
* Rhino::Context.new is expected to yield when block passed
|
61
69
|
|
62
|
-
|
70
|
+
## 1.73.2 2012-04-11
|
71
|
+
|
63
72
|
RedJS 0.2.1 compatible
|
64
73
|
|
65
74
|
* improve JSError#message + add JSError#value to reflect throw JS value
|
@@ -69,7 +78,8 @@ RedJS 0.2.1 compatible
|
|
69
78
|
* restrictable limits now require Contex.new(:restrictable => true)
|
70
79
|
* added Context#timeout_limit (to complete instruction_limit)
|
71
80
|
|
72
|
-
|
81
|
+
## 1.73.1 2011-11-28
|
82
|
+
|
73
83
|
NOTE: this is a "major" code update from 1.73.0 with some incompatibilities
|
74
84
|
although keeping the bits backward compatible as much as possible :
|
75
85
|
|
@@ -90,51 +100,58 @@ although keeping the bits backward compatible as much as possible :
|
|
90
100
|
* support for setting JS version via Context.version
|
91
101
|
* Rhino::J gets deprecated it's now know as Rhino::JS
|
92
102
|
|
93
|
-
|
103
|
+
## 1.73.0 2011-11-28
|
94
104
|
|
95
105
|
* upgrade to rhino-1.7R3
|
96
106
|
* cache objects passed to context - same object passed twice ends up the same
|
97
107
|
* properly map ruby Time objects to javascript Date
|
98
108
|
* jruby --1.9 improvements
|
99
109
|
|
100
|
-
|
110
|
+
## 1.72.8 2011-06-26
|
111
|
+
|
101
112
|
* fix passing of options hash to ruby.
|
102
113
|
|
103
|
-
|
114
|
+
## 1.72.6 2009-11-30
|
115
|
+
|
104
116
|
* 2 major enhancements:
|
105
117
|
* evaluate an IO object such as a file or an socket input stream
|
106
118
|
* load javascript sources directly into the context with the file system
|
107
119
|
|
108
|
-
|
120
|
+
## 1.72.5 2009-11-12
|
121
|
+
|
109
122
|
* 2 major enhancements:
|
110
123
|
* evaluate javascript with a ruby object as it's scope using Context#open(:with => object)
|
111
124
|
* add eval_js() method to Object to evaluate in the context of that object
|
112
125
|
|
113
|
-
|
126
|
+
## 1.72.4 2009-11-12
|
127
|
+
|
114
128
|
* 3 major enhancements:
|
115
129
|
* automatically wrap/unwrap ruby and javascript arrays
|
116
130
|
* automatically convert ruby method objects and Proc objects into javascript functions
|
117
131
|
* Make functions defined in javascript callable from ruby
|
118
132
|
|
119
|
-
|
133
|
+
## 1.72.3 2009-11-11
|
134
|
+
|
120
135
|
* 4 major enhancements:
|
121
136
|
* greatly simplified interface to context by unifying context and scope
|
122
137
|
* remove Context#open_std()
|
123
138
|
* remove Context#standard
|
124
139
|
* remove Context#init_standard_objects
|
125
140
|
|
126
|
-
|
141
|
+
## 1.72.2 2009-11-10
|
142
|
+
|
127
143
|
* 1 major enhancement:
|
128
144
|
* ability to limit the instruction count for a context
|
129
145
|
|
130
|
-
|
146
|
+
## 1.72.1 2009-11-09
|
147
|
+
|
131
148
|
* 4 major enhancements:
|
132
149
|
* easily manipulate javascript objects from ruby (NativeObject)
|
133
150
|
* make NativeObject Enumerable
|
134
151
|
* to_h and to_json for NativeObject
|
135
152
|
* embed ruby instances and call methods from javascript
|
136
153
|
|
137
|
-
|
154
|
+
## 1.72.0 2009-09-24
|
138
155
|
|
139
156
|
* 2 major enhancements:
|
140
157
|
* evaluate javascript in jruby
|
data/README.md
ADDED
@@ -0,0 +1,262 @@
|
|
1
|
+
# therubyrhino
|
2
|
+
|
3
|
+
Embed the Mozilla Rhino JavaScript interpreter into Ruby
|
4
|
+
|
5
|
+
## REQUIREMENTS:
|
6
|
+
|
7
|
+
* JRuby >= 1.6.8
|
8
|
+
|
9
|
+
## INSTALL:
|
10
|
+
|
11
|
+
`jruby -S gem install therubyrhino`
|
12
|
+
|
13
|
+
## FEATURES/PROBLEMS:
|
14
|
+
|
15
|
+
* Evaluate JavaScript from with in Ruby
|
16
|
+
* Embed your Ruby objects into the JavaScript world
|
17
|
+
|
18
|
+
## SYNOPSIS:
|
19
|
+
|
20
|
+
1. JavaScript goes into Ruby
|
21
|
+
2. Ruby Objects goes into JavaScript
|
22
|
+
3. Our shark's in the JavaScript!
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'rhino'
|
26
|
+
```
|
27
|
+
|
28
|
+
* evaluate some simple JavaScript using `eval_js`:
|
29
|
+
```ruby
|
30
|
+
eval_js "7 * 6" #=> 42
|
31
|
+
```
|
32
|
+
|
33
|
+
* that's quick and dirty, but if you want more control over your environment,
|
34
|
+
use a `Context`:
|
35
|
+
```ruby
|
36
|
+
Rhino::Context.open do |context|
|
37
|
+
context['foo'] = "bar"
|
38
|
+
context.eval('foo') # => "bar"
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
* evaluate a Ruby function from JavaScript:
|
43
|
+
```ruby
|
44
|
+
Rhino::Context.open do |context|
|
45
|
+
context["say"] = lambda {|word, times| word * times}
|
46
|
+
context.eval("say("Hello", 3)") #=> HelloHelloHello
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
* embed a Ruby object into your JavaScript environment
|
51
|
+
```ruby
|
52
|
+
class MyMath
|
53
|
+
def plus(a, b)
|
54
|
+
a + b
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
Rhino::Context.open do |context|
|
59
|
+
context["math"] = MyMath.new
|
60
|
+
context.eval("math.plus(20, 22)") #=> 42
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
* make a Ruby object be your JavaScript environment
|
65
|
+
```ruby
|
66
|
+
math = MyMath.new
|
67
|
+
Rhino::Context.open(:with => math) do |context|
|
68
|
+
context.eval("plus(20, 22)") #=> 42
|
69
|
+
end
|
70
|
+
|
71
|
+
# or the equivalent
|
72
|
+
|
73
|
+
math.eval_js("plus(20, 22)")
|
74
|
+
```
|
75
|
+
|
76
|
+
### Context Configuration
|
77
|
+
|
78
|
+
* make your standard objects (Object, String, etc...) immutable:
|
79
|
+
```ruby
|
80
|
+
Rhino::Context.open(:sealed => true) do |context|
|
81
|
+
context.eval("Object.prototype.toString = function() {}") # this is an error!
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
* turn on Java integration from JavaScript (probably a bad idea):
|
86
|
+
```ruby
|
87
|
+
Rhino::Context.open(:java => true) do |context|
|
88
|
+
context.eval("java.lang.System.exit()") # it's dangerous!
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
* limit the number of instructions that can be executed to prevent rogue code:
|
93
|
+
```ruby
|
94
|
+
Rhino::Context.open(:restrictable => true) do |context|
|
95
|
+
context.instruction_limit = 100000
|
96
|
+
context.eval("while (true);") # => Rhino::RunawayScriptError
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
* limit the time a script executes (rogue scripts):
|
101
|
+
```ruby
|
102
|
+
Rhino::Context.open(:restrictable => true, :java => true) do |context|
|
103
|
+
context.timeout_limit = 1.5 # seconds
|
104
|
+
context.eval %Q{
|
105
|
+
for (var i = 0; i < 100; i++) {
|
106
|
+
java.lang.Thread.sleep(100);
|
107
|
+
}
|
108
|
+
} # => Rhino::ScriptTimeoutError
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
### Loading JavaScript Source
|
113
|
+
|
114
|
+
In addition to just evaluating strings, you can also use streams such as files:
|
115
|
+
|
116
|
+
* evaluate bytes read from any File/IO object:
|
117
|
+
```ruby
|
118
|
+
File.open("mysource.js") do |file|
|
119
|
+
eval_js file, "mysource.js"
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
123
|
+
* or load it by filename:
|
124
|
+
```ruby
|
125
|
+
Rhino::Context.open do |context|
|
126
|
+
context.load("mysource.js")
|
127
|
+
end
|
128
|
+
```
|
129
|
+
|
130
|
+
### Configurable Ruby access
|
131
|
+
|
132
|
+
By default accessing Ruby objects from JavaScript is compatible with *therubyracer*:
|
133
|
+
https://github.com/cowboyd/therubyracer/wiki/Accessing-Ruby-Objects-From-JavaScript
|
134
|
+
|
135
|
+
Thus you end-up calling arbitrary no-arg methods as if they were JavaScript properties,
|
136
|
+
since instance accessors (properties) and methods (functions) are indistinguishable:
|
137
|
+
```ruby
|
138
|
+
Rhino::Context.open do |context|
|
139
|
+
context['Time'] = Time
|
140
|
+
context.eval('Time.now')
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
However, you can customize this behavior and there's another access implementation
|
145
|
+
that attempts to mirror only attributes as properties as close as possible:
|
146
|
+
```ruby
|
147
|
+
class Foo
|
148
|
+
attr_accessor :bar
|
149
|
+
|
150
|
+
def initialize
|
151
|
+
@bar = "bar"
|
152
|
+
end
|
153
|
+
|
154
|
+
def check_bar
|
155
|
+
bar == "bar"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
Rhino::Ruby::Scriptable.access = :attribute
|
160
|
+
Rhino::Context.open do |context|
|
161
|
+
context['Foo'] = Foo
|
162
|
+
context.eval('var foo = new Foo()')
|
163
|
+
context.eval('foo.bar') # get property using reader
|
164
|
+
context.eval('foo.bar = null') # set property using writer
|
165
|
+
context.eval('foo.check_bar()') # called like a function
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
If you happen to come up with your own access strategy, just set it directly :
|
170
|
+
```ruby
|
171
|
+
Rhino::Ruby::Scriptable.access = FooApp::BarAccess.instance
|
172
|
+
```
|
173
|
+
|
174
|
+
## Safe by default
|
175
|
+
|
176
|
+
The Ruby Rhino is designed to let you evaluate JavaScript as safely as possible
|
177
|
+
unless you tell it to do something more dangerous. The default context is a
|
178
|
+
hermetically sealed JavaScript environment with only the standard objects and
|
179
|
+
functions. Nothing from the Ruby world is accessible.
|
180
|
+
|
181
|
+
For Ruby objects that you explicitly embed into JavaScript, only the +public+
|
182
|
+
methods "defined in their classes" are exposed by default e.g.
|
183
|
+
```ruby
|
184
|
+
class A
|
185
|
+
def a; 'a'; end
|
186
|
+
end
|
187
|
+
|
188
|
+
class B < A
|
189
|
+
def b; 'b'; end
|
190
|
+
end
|
191
|
+
|
192
|
+
Rhino::Context.open do |context|
|
193
|
+
context['a'] = A.new
|
194
|
+
context['b'] = B.new
|
195
|
+
context.eval("a.a()") # => 'a'
|
196
|
+
context.eval("b.b()") # => 'b'
|
197
|
+
context.eval("b.a()") # => 'TypeError: undefined property 'a' is not a function'
|
198
|
+
end
|
199
|
+
```
|
200
|
+
|
201
|
+
## Context Customizations
|
202
|
+
|
203
|
+
Just like the JVM packaged Rhino scripting engine, therubyrhino gem supports
|
204
|
+
specifying JavaScript context properies (optimization level and language version)
|
205
|
+
using system properties e.g. to force interpreted mode :
|
206
|
+
|
207
|
+
`jruby -J-Drhino.opt.level=-1 -rtherubyrhino -S ...`
|
208
|
+
|
209
|
+
You might also set these programatically as a default for all created contexts :
|
210
|
+
```ruby
|
211
|
+
Rhino::Context.default_optimization_level = 1
|
212
|
+
Rhino::Context.default_javascript_version = 1.6
|
213
|
+
```
|
214
|
+
|
215
|
+
Or using plain old JAVA_OPTS e.g. when setting JavaScript version :
|
216
|
+
|
217
|
+
`-Drhino.js.version=1.7`
|
218
|
+
|
219
|
+
## Rhino
|
220
|
+
|
221
|
+
Rhino is currently maintained at https://github.com/mozilla/rhino
|
222
|
+
Release downloads are available at http://www.mozilla.org/rhino/download.html
|
223
|
+
Rhino is licensed under the MPL 1.1/GPL 2.0 license.
|
224
|
+
|
225
|
+
### Using a custom Rhino version
|
226
|
+
|
227
|
+
Officially supported versions of Rhino's _js.jar_ are packaged separately as
|
228
|
+
**therubyrhino_jar** gem. Make sure you're using the latest gem version if you
|
229
|
+
feel like missing something available with Rhino. For experimenters the jar can
|
230
|
+
be overriden by defining a `Rhino::JAR_PATH` before `require 'rhino'` e.g. :
|
231
|
+
```ruby
|
232
|
+
module Rhino
|
233
|
+
JAR_PATH = File.expand_path('lib/rhino/build/rhino1_7R5pre/js.jar')
|
234
|
+
end
|
235
|
+
# ...
|
236
|
+
require 'rhino'
|
237
|
+
```
|
238
|
+
|
239
|
+
## LICENSE:
|
240
|
+
|
241
|
+
(The MIT License)
|
242
|
+
|
243
|
+
Copyright (c) 2009-2013 Charles Lowell
|
244
|
+
|
245
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
246
|
+
a copy of this software and associated documentation files (the
|
247
|
+
'Software'), to deal in the Software without restriction, including
|
248
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
249
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
250
|
+
permit persons to whom the Software is furnished to do so, subject to
|
251
|
+
the following conditions:
|
252
|
+
|
253
|
+
The above copyright notice and this permission notice shall be
|
254
|
+
included in all copies or substantial portions of the Software.
|
255
|
+
|
256
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
257
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
258
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
259
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
260
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
261
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
262
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
data/lib/rhino/context.rb
CHANGED
@@ -245,7 +245,7 @@ module Rhino
|
|
245
245
|
do_open(&block)
|
246
246
|
rescue JS::RhinoException => e
|
247
247
|
if code_generation_error?(e)
|
248
|
-
warn "[INFO] Rhino byte-code generation failed forcing #{@native} into interpreted mode"
|
248
|
+
Rhino.warn "[INFO] Rhino byte-code generation failed forcing #{@native} into interpreted mode"
|
249
249
|
self.optimization_level = -1
|
250
250
|
retry
|
251
251
|
end
|
data/lib/rhino/version.rb
CHANGED
data/lib/rhino/wormhole.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
module Rhino
|
3
3
|
module To
|
4
|
-
|
4
|
+
|
5
5
|
def to_ruby(object)
|
6
6
|
case object
|
7
7
|
when JS::Scriptable::NOT_FOUND, JS::Undefined then nil
|
@@ -33,13 +33,13 @@ module Rhino
|
|
33
33
|
def args_to_ruby(args)
|
34
34
|
args.map { |arg| to_ruby(arg) }
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def args_to_javascript(args, scope = nil)
|
38
38
|
args.map { |arg| to_javascript(arg, scope) }.to_java
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
private
|
42
|
-
|
42
|
+
|
43
43
|
def array_to_ruby(js_array)
|
44
44
|
js_array.length.times.map { |i| to_ruby( js_array.get(i, js_array) ) }
|
45
45
|
end
|
@@ -60,7 +60,7 @@ module Rhino
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def hash_to_javascript(rb_hash, scope = nil)
|
63
|
-
js_object =
|
63
|
+
js_object =
|
64
64
|
if scope && context = JS::Context.getCurrentContext
|
65
65
|
context.newObject(scope)
|
66
66
|
else
|
@@ -68,29 +68,27 @@ module Rhino
|
|
68
68
|
end
|
69
69
|
# JS::NativeObject implements Map put it's #put does :
|
70
70
|
# throw new UnsupportedOperationException(); thus no []=
|
71
|
-
rb_hash.each_pair do |key, val|
|
71
|
+
rb_hash.each_pair do |key, val|
|
72
72
|
js_val = to_javascript(val, scope)
|
73
73
|
JS::ScriptableObject.putProperty(js_object, key.to_s, js_val)
|
74
74
|
end
|
75
75
|
js_object
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
def time_to_javascript(time, scope = nil)
|
79
79
|
millis = time.to_f * 1000
|
80
80
|
if scope && context = JS::Context.getCurrentContext
|
81
81
|
JS::ScriptRuntime.newObject(context, scope, 'Date', [ millis ].to_java)
|
82
82
|
else
|
83
83
|
# the pure reflection way - god I love Java's private :
|
84
|
-
js_klass = JS::NativeObject.java_class
|
85
|
-
new = js_klass.getDeclaredConstructor
|
86
|
-
new.setAccessible(true)
|
84
|
+
js_klass = JS::NativeObject.java_class.to_java
|
85
|
+
new = js_klass.getDeclaredConstructor; new.setAccessible(true)
|
87
86
|
js_date = new.newInstance
|
88
|
-
date = js_klass.getDeclaredField(:date)
|
89
|
-
date.setAccessible(true)
|
87
|
+
date = js_klass.getDeclaredField(:date); date.setAccessible(true)
|
90
88
|
date.setDouble(js_date, millis)
|
91
89
|
js_date
|
92
90
|
end
|
93
91
|
end
|
94
|
-
|
92
|
+
|
95
93
|
end
|
96
94
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,11 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
|
3
1
|
require 'rhino'
|
4
2
|
|
5
|
-
|
3
|
+
begin
|
4
|
+
require 'mocha/api'
|
5
|
+
rescue LoadError
|
6
|
+
require 'mocha'
|
7
|
+
end
|
8
|
+
|
6
9
|
require 'redjs'
|
7
10
|
|
8
11
|
module RedJS
|
@@ -32,4 +35,5 @@ end
|
|
32
35
|
RSpec.configure do |config|
|
33
36
|
config.filter_run_excluding :compat => /(0.5.0)|(0.6.0)/ # RedJS
|
34
37
|
config.include Rhino::SpecHelpers
|
38
|
+
config.deprecation_stream = 'spec/deprecations.log'
|
35
39
|
end
|
data/therubyrhino.gemspec
CHANGED
@@ -7,21 +7,20 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Rhino::VERSION
|
8
8
|
s.authors = ["Charles Lowell"]
|
9
9
|
s.email = %q{cowboyd@thefrontside.net}
|
10
|
-
|
10
|
+
|
11
11
|
s.summary = %q{Embed the Rhino JavaScript interpreter into JRuby}
|
12
12
|
s.description = %q{Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.}
|
13
13
|
s.homepage = %q{http://github.com/cowboyd/therubyrhino}
|
14
14
|
s.rubyforge_project = %q{therubyrhino}
|
15
|
-
s.extra_rdoc_files = ["README.
|
16
|
-
|
15
|
+
s.extra_rdoc_files = ["README.md"]
|
16
|
+
s.license = "MIT"
|
17
|
+
|
17
18
|
s.require_paths = ["lib"]
|
18
19
|
s.files = `git ls-files`.split("\n").sort.
|
19
20
|
reject { |file| file == 'therubyrhino_jar.gemspec' || file =~ /^jar\// }
|
20
21
|
|
21
22
|
s.add_dependency "therubyrhino_jar", '>= 1.7.3'
|
22
|
-
|
23
|
-
s.add_development_dependency "
|
24
|
-
s.add_development_dependency "
|
25
|
-
s.add_development_dependency "mocha"
|
26
|
-
s.add_development_dependency "jruby-openssl"
|
23
|
+
|
24
|
+
s.add_development_dependency "rspec", "~> 2.14.1"
|
25
|
+
s.add_development_dependency "mocha", "~> 0.13.3"
|
27
26
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: therubyrhino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 2.0.
|
5
|
+
version: 2.0.3
|
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: 2014-02-20 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: therubyrhino_jar
|
@@ -24,49 +24,27 @@ dependencies:
|
|
24
24
|
prerelease: false
|
25
25
|
type: :runtime
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
|
-
name:
|
27
|
+
name: rspec
|
28
28
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 2.14.1
|
34
34
|
requirement: *id002
|
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:
|
42
42
|
- - ~>
|
43
43
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
44
|
+
version: 0.13.3
|
45
45
|
requirement: *id003
|
46
46
|
prerelease: false
|
47
47
|
type: :development
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: mocha
|
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
|
59
|
-
- !ruby/object:Gem::Dependency
|
60
|
-
name: jruby-openssl
|
61
|
-
version_requirements: &id005 !ruby/object:Gem::Requirement
|
62
|
-
none: false
|
63
|
-
requirements:
|
64
|
-
- - ">="
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
version: "0"
|
67
|
-
requirement: *id005
|
68
|
-
prerelease: false
|
69
|
-
type: :development
|
70
48
|
description: Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.
|
71
49
|
email: cowboyd@thefrontside.net
|
72
50
|
executables: []
|
@@ -74,13 +52,14 @@ executables: []
|
|
74
52
|
extensions: []
|
75
53
|
|
76
54
|
extra_rdoc_files:
|
77
|
-
- README.
|
55
|
+
- README.md
|
78
56
|
files:
|
79
57
|
- .gitignore
|
58
|
+
- .rspec
|
80
59
|
- .travis.yml
|
81
60
|
- Gemfile
|
82
|
-
- History.
|
83
|
-
- README.
|
61
|
+
- History.md
|
62
|
+
- README.md
|
84
63
|
- Rakefile
|
85
64
|
- lib/rhino.rb
|
86
65
|
- lib/rhino/context.rb
|
@@ -112,8 +91,8 @@ files:
|
|
112
91
|
- spec/spec_helper.rb
|
113
92
|
- therubyrhino.gemspec
|
114
93
|
homepage: http://github.com/cowboyd/therubyrhino
|
115
|
-
licenses:
|
116
|
-
|
94
|
+
licenses:
|
95
|
+
- MIT
|
117
96
|
post_install_message:
|
118
97
|
rdoc_options: []
|
119
98
|
|
@@ -140,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
119
|
requirements: []
|
141
120
|
|
142
121
|
rubyforge_project: therubyrhino
|
143
|
-
rubygems_version: 1.8.
|
122
|
+
rubygems_version: 1.8.29
|
144
123
|
signing_key:
|
145
124
|
specification_version: 3
|
146
125
|
summary: Embed the Rhino JavaScript interpreter into JRuby
|
data/README.rdoc
DELETED
@@ -1,246 +0,0 @@
|
|
1
|
-
= therubyrhino
|
2
|
-
|
3
|
-
* http://github.com/cowboyd/therubyrhino
|
4
|
-
* irc://irc.freenode.net/therubyrhino
|
5
|
-
|
6
|
-
== DESCRIPTION:
|
7
|
-
|
8
|
-
Embed the Mozilla Rhino JavaScript interpreter into Ruby
|
9
|
-
|
10
|
-
== FEATURES/PROBLEMS:
|
11
|
-
|
12
|
-
* Evaluate JavaScript from with in Ruby
|
13
|
-
* Embed your Ruby objects into the JavaScript world
|
14
|
-
|
15
|
-
== SYNOPSIS:
|
16
|
-
|
17
|
-
1. JavaScript goes into Ruby
|
18
|
-
2. Ruby Objects goes into JavaScript
|
19
|
-
3. Our shark's in the JavaScript!
|
20
|
-
|
21
|
-
require 'rhino'
|
22
|
-
|
23
|
-
# evaluate some simple javascript
|
24
|
-
eval_js "7 * 6" #=> 42
|
25
|
-
|
26
|
-
# that's quick and dirty, but if you want more control over your
|
27
|
-
# environment, use a Context:
|
28
|
-
Rhino::Context.open do |cxt|
|
29
|
-
cxt['foo'] = "bar"
|
30
|
-
cxt.eval('foo') # => "bar"
|
31
|
-
end
|
32
|
-
|
33
|
-
# evaluate a ruby function from JS
|
34
|
-
|
35
|
-
Rhino::Context.open do |context|
|
36
|
-
context["say"] = lambda {|word, times| word * times}
|
37
|
-
context.eval("say("Hello", 3)") #=> HelloHelloHello
|
38
|
-
end
|
39
|
-
|
40
|
-
# embed a ruby object into your JS environment
|
41
|
-
|
42
|
-
class MyMath
|
43
|
-
def plus(lhs, rhs)
|
44
|
-
lhs + rhs
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
Rhino::Context.open do |context|
|
49
|
-
context["math"] = MyMath.new
|
50
|
-
context.eval("math.plus(20, 22)") #=> 42
|
51
|
-
end
|
52
|
-
|
53
|
-
# make a ruby object *be* your JS environment
|
54
|
-
math = MyMath.new
|
55
|
-
Rhino::Context.open(:with => math) do |context|
|
56
|
-
context.eval("plus(20, 22)") #=> 42
|
57
|
-
end
|
58
|
-
|
59
|
-
#or the equivalent
|
60
|
-
|
61
|
-
math.eval_js("plus(20, 22)")
|
62
|
-
|
63
|
-
# Configure your embedding setup
|
64
|
-
|
65
|
-
# Make your standard objects (Object, String, etc...) immutable
|
66
|
-
Rhino::Context.open(:sealed => true) do |context|
|
67
|
-
context.eval("Object.prototype.toString = function() {}") # this is an error!
|
68
|
-
end
|
69
|
-
|
70
|
-
#Turn on Java integration from javascript (probably a bad idea)
|
71
|
-
Rhino::Context.open(:java => true) do |context|
|
72
|
-
context.eval("java.lang.System.exit()") # it's dangerous!
|
73
|
-
end
|
74
|
-
|
75
|
-
#limit the number of instructions that can be executed in order to prevent
|
76
|
-
#rogue scripts
|
77
|
-
Rhino::Context.open(:restrictable => true) do |context|
|
78
|
-
context.instruction_limit = 100000
|
79
|
-
context.eval("while (true);") # => Rhino::RunawayScriptError
|
80
|
-
end
|
81
|
-
|
82
|
-
#limit the time a script executes
|
83
|
-
#rogue scripts
|
84
|
-
Rhino::Context.open(:restrictable => true, :java => true) do |context|
|
85
|
-
context.timeout_limit = 1.5 # seconds
|
86
|
-
context.eval %Q{
|
87
|
-
for (var i = 0; i < 100; i++) {
|
88
|
-
java.lang.Thread.sleep(100);
|
89
|
-
}
|
90
|
-
} # => Rhino::ScriptTimeoutError
|
91
|
-
end
|
92
|
-
|
93
|
-
==== Different ways of loading JavaScript source
|
94
|
-
|
95
|
-
In addition to just evaluating strings, you can also use streams such as files.
|
96
|
-
|
97
|
-
# evaluate bytes read from any File/IO object:
|
98
|
-
File.open("mysource.js") do |file|
|
99
|
-
eval_js file, "mysource.js"
|
100
|
-
end
|
101
|
-
|
102
|
-
# or load it by filename
|
103
|
-
Rhino::Context.open do |context|
|
104
|
-
context.load("mysource.js")
|
105
|
-
end
|
106
|
-
|
107
|
-
==== Configurable Ruby access
|
108
|
-
|
109
|
-
By default accessing Ruby objects from JavaScript is compatible with *therubyracer*:
|
110
|
-
https://github.com/cowboyd/therubyracer/wiki/Accessing-Ruby-Objects-From-JavaScript
|
111
|
-
|
112
|
-
Thus you end-up calling arbitrary no-arg methods as if they were JavaScript properties,
|
113
|
-
since instance accessors (properties) and methods (functions) are indistinguishable:
|
114
|
-
|
115
|
-
Rhino::Context.open do |context|
|
116
|
-
context['Time'] = Time
|
117
|
-
context.eval('Time.now')
|
118
|
-
end
|
119
|
-
|
120
|
-
However, you can customize this behavior and there's another access implementation
|
121
|
-
that attempts to mirror only attributes as properties as close as possible:
|
122
|
-
|
123
|
-
class Foo
|
124
|
-
attr_accessor :bar
|
125
|
-
|
126
|
-
def initialize
|
127
|
-
@bar = "bar"
|
128
|
-
end
|
129
|
-
|
130
|
-
def check_bar
|
131
|
-
bar == "bar"
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
Rhino::Ruby::Scriptable.access = :attribute
|
136
|
-
Rhino::Context.open do |context|
|
137
|
-
context['Foo'] = Foo
|
138
|
-
context.eval('var foo = new Foo()')
|
139
|
-
context.eval('foo.bar') # get property using reader
|
140
|
-
context.eval('foo.bar = null') # set property using writer
|
141
|
-
context.eval('foo.check_bar()') # called like a function
|
142
|
-
end
|
143
|
-
|
144
|
-
If you happen to come up with your own access strategy, just set it directly :
|
145
|
-
|
146
|
-
Rhino::Ruby::Scriptable.access = FooApp::BarAccess.instance
|
147
|
-
|
148
|
-
=== Safe by default
|
149
|
-
|
150
|
-
The Ruby Rhino is designed to let you evaluate JavaScript as safely as possible
|
151
|
-
unless you tell it to do something more dangerous. The default context is a
|
152
|
-
hermetically sealed JavaScript environment with only the standard objects and
|
153
|
-
functions. Nothing from the Ruby world is accessible at all.
|
154
|
-
|
155
|
-
For Ruby objects that you explicitly embed into JavaScript, only the +public+
|
156
|
-
methods "defined in their classes" are exposed by default e.g.
|
157
|
-
|
158
|
-
class A
|
159
|
-
def a
|
160
|
-
"a"
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
class B < A
|
165
|
-
def b
|
166
|
-
"b"
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
|
171
|
-
Rhino::Context.open do |cxt|
|
172
|
-
cxt['a'] = A.new
|
173
|
-
cxt['b'] = B.new
|
174
|
-
cxt.eval("a.a()") # => 'a'
|
175
|
-
cxt.eval("b.b()") # => 'b'
|
176
|
-
cxt.eval("b.a()") # => 'TypeError: undefined property 'a' is not a function'
|
177
|
-
end
|
178
|
-
|
179
|
-
==== Using a custom Rhino version
|
180
|
-
|
181
|
-
Officially supported versions of Rhino's _js.jar_ are packaged separately as
|
182
|
-
*therubyrhino_jar* gem. Make sure you're using the latest gem version if you
|
183
|
-
feel like missing something available with Rhino. For experimenters the jar can
|
184
|
-
be overriden by defining a +Rhino::JAR_PATH+ before +require 'rhino'+ e.g. :
|
185
|
-
|
186
|
-
module Rhino
|
187
|
-
JAR_PATH = File.expand_path('lib/rhino/build/rhino1_7R5pre/js.jar')
|
188
|
-
end
|
189
|
-
# ...
|
190
|
-
require 'rhino'
|
191
|
-
|
192
|
-
==== Context customizations
|
193
|
-
|
194
|
-
Just like the JVM packaged Rhino scripting engine, therubyrhino gem supports
|
195
|
-
specifying JavaScript context properies (optimization level and language version)
|
196
|
-
using system properties e.g. to force interpreted mode :
|
197
|
-
|
198
|
-
jruby -J-Drhino.opt.level=-1 -rtherubyrhino -S ...
|
199
|
-
|
200
|
-
You might also set these programatically as a default for all created contexts :
|
201
|
-
|
202
|
-
Rhino::Context.default_optimization_level = 1
|
203
|
-
Rhino::Context.default_javascript_version = 1.6
|
204
|
-
|
205
|
-
Or using plain old JAVA_OPTS e.g. when setting JavaScript version :
|
206
|
-
|
207
|
-
-Drhino.js.version=1.7
|
208
|
-
|
209
|
-
== Rhino
|
210
|
-
|
211
|
-
Rhino is currently maintained at https://github.com/mozilla/rhino
|
212
|
-
Release downloads are available at http://www.mozilla.org/rhino/download.html
|
213
|
-
Rhino is licensed under the MPL 1.1/GPL 2.0 license.
|
214
|
-
|
215
|
-
== REQUIREMENTS:
|
216
|
-
|
217
|
-
* JRuby >= 1.6
|
218
|
-
|
219
|
-
== INSTALL:
|
220
|
-
|
221
|
-
* jruby -S gem install therubyrhino
|
222
|
-
|
223
|
-
== LICENSE:
|
224
|
-
|
225
|
-
(The MIT License)
|
226
|
-
|
227
|
-
Copyright (c) 2009-2012 Charles Lowell
|
228
|
-
|
229
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
230
|
-
a copy of this software and associated documentation files (the
|
231
|
-
'Software'), to deal in the Software without restriction, including
|
232
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
233
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
234
|
-
permit persons to whom the Software is furnished to do so, subject to
|
235
|
-
the following conditions:
|
236
|
-
|
237
|
-
The above copyright notice and this permission notice shall be
|
238
|
-
included in all copies or substantial portions of the Software.
|
239
|
-
|
240
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
241
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
242
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
243
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
244
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
245
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
246
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|