therubyrhino 1.72.8 → 1.73.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/Rakefile +4 -4
- data/lib/rhino.rb +0 -4
- data/lib/rhino/context.rb +13 -11
- data/lib/rhino/java.rb +1 -1
- data/lib/rhino/native_function.rb +16 -10
- data/lib/rhino/rhino-1.7R3.jar +0 -0
- data/lib/rhino/ruby_function.rb +1 -1
- data/lib/rhino/ruby_object.rb +26 -18
- data/lib/rhino/version.rb +1 -1
- data/lib/rhino/wormhole.rb +46 -12
- data/spec/rhino/context_spec.rb +21 -12
- data/spec/spec_helper.rb +0 -8
- data/therubyrhino.gemspec +1 -0
- metadata +17 -10
- data/lib/rhino/rhino-1.7R2.jar +0 -0
- data/tasks/jruby.rake +0 -7
- data/tasks/rspec.rake +0 -13
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
+
require 'java'
|
1
2
|
require 'bundler/setup'
|
2
3
|
Bundler::GemHelper.install_tasks
|
3
4
|
|
4
|
-
for file in Dir['tasks/*.rake']
|
5
|
-
load file
|
6
|
-
end
|
7
|
-
|
8
5
|
desc "remove all build artifacts"
|
9
6
|
task :clean do
|
10
7
|
sh "rm -rf pkg/"
|
11
8
|
end
|
9
|
+
|
10
|
+
require 'rspec/core/rake_task'
|
11
|
+
RSpec::Core::RakeTask.new
|
data/lib/rhino.rb
CHANGED
data/lib/rhino/context.rb
CHANGED
@@ -5,23 +5,23 @@ module Rhino
|
|
5
5
|
# ==Overview
|
6
6
|
# All Javascript must be executed in a context which represents the execution environment in
|
7
7
|
# which scripts will run. The environment consists of the standard javascript objects
|
8
|
-
# and functions like Object, String, Array, etc... as well as any objects or functions which
|
8
|
+
# and functions like Object, String, Array, etc... as well as any objects or functions which
|
9
9
|
# have been defined in it. e.g.
|
10
|
-
#
|
10
|
+
#
|
11
11
|
# Context.open do |cxt|
|
12
12
|
# cxt['num'] = 5
|
13
13
|
# cxt.eval('num + 5') #=> 10
|
14
14
|
# end
|
15
|
-
#
|
15
|
+
#
|
16
16
|
# == Multiple Contexts.
|
17
|
-
# The same object may appear in any number of contexts, but only one context may be executing javascript code
|
17
|
+
# The same object may appear in any number of contexts, but only one context may be executing javascript code
|
18
18
|
# in any given thread. If a new context is opened in a thread in which a context is already opened, the second
|
19
19
|
# context will "mask" the old context e.g.
|
20
20
|
#
|
21
21
|
# six = 6
|
22
22
|
# Context.open do |cxt|
|
23
23
|
# cxt['num'] = 5
|
24
|
-
# cxt.eval('num') # => 5
|
24
|
+
# cxt.eval('num') # => 5
|
25
25
|
# Context.open do |cxt|
|
26
26
|
# cxt['num'] = 10
|
27
27
|
# cxt.eval('num') # => 10
|
@@ -44,11 +44,11 @@ module Rhino
|
|
44
44
|
def open(options = {}, &block)
|
45
45
|
new(options).open(&block)
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
def eval(javascript)
|
49
49
|
new.eval(javascript)
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
end
|
53
53
|
|
54
54
|
# Create a new javascript environment for executing javascript and ruby code.
|
@@ -78,8 +78,8 @@ module Rhino
|
|
78
78
|
@scope[k]
|
79
79
|
end
|
80
80
|
|
81
|
-
# Set a value in the global scope of this context. This value will be visible to all the
|
82
|
-
# javascript that is executed in this context.
|
81
|
+
# Set a value in the global scope of this context. This value will be visible to all the
|
82
|
+
# javascript that is executed in this context.
|
83
83
|
def []=(k, v)
|
84
84
|
@scope[k] = v
|
85
85
|
end
|
@@ -103,7 +103,7 @@ module Rhino
|
|
103
103
|
end
|
104
104
|
end
|
105
105
|
end
|
106
|
-
|
106
|
+
|
107
107
|
def evaluate(*args) # :nodoc:
|
108
108
|
self.eval(*args)
|
109
109
|
end
|
@@ -186,7 +186,7 @@ module Rhino
|
|
186
186
|
end
|
187
187
|
|
188
188
|
class ContextError < StandardError # :nodoc:
|
189
|
-
|
189
|
+
|
190
190
|
end
|
191
191
|
|
192
192
|
class JavascriptError < StandardError # :nodoc:
|
@@ -203,6 +203,8 @@ module Rhino
|
|
203
203
|
end
|
204
204
|
end
|
205
205
|
|
206
|
+
JSError = JavascriptError
|
207
|
+
|
206
208
|
class RunawayScriptError < StandardError # :nodoc:
|
207
209
|
end
|
208
210
|
end
|
data/lib/rhino/java.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
module Rhino
|
3
|
-
|
4
|
-
# Wraps a function that has been defined in Javascript so that it can
|
3
|
+
|
4
|
+
# Wraps a function that has been defined in Javascript so that it can
|
5
5
|
# be referenced and called from javascript. e.g.
|
6
6
|
#
|
7
7
|
# plus = Rhino::Context.open do |cx|
|
@@ -9,15 +9,21 @@ module Rhino
|
|
9
9
|
# end
|
10
10
|
# plus.call(5,4) # => 9
|
11
11
|
#
|
12
|
-
class NativeFunction < NativeObject
|
12
|
+
class NativeFunction < NativeObject
|
13
13
|
def call(*args)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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()
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
Binary file
|
data/lib/rhino/ruby_function.rb
CHANGED
data/lib/rhino/ruby_object.rb
CHANGED
@@ -2,44 +2,53 @@
|
|
2
2
|
module Rhino
|
3
3
|
class RubyObject < J::ScriptableObject
|
4
4
|
include J::Wrapper
|
5
|
-
|
5
|
+
|
6
6
|
def initialize(object)
|
7
7
|
super()
|
8
8
|
@ruby = object
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def unwrap
|
12
12
|
@ruby
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def getClassName()
|
16
16
|
@ruby.class.name
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def getPrototype()
|
20
20
|
Prototype::Generic
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
|
+
def put(key, start, value)
|
24
|
+
if @ruby.respond_to?("#{key}=")
|
25
|
+
@ruby.send("#{key}=", To.ruby(value))
|
26
|
+
value
|
27
|
+
else
|
28
|
+
super
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
23
32
|
def getIds()
|
24
33
|
@ruby.public_methods(false).map {|m| m.gsub(/(.)_(.)/) {java.lang.String.new("#{$1}#{$2.upcase}")}}.to_java
|
25
34
|
end
|
26
|
-
|
35
|
+
|
27
36
|
def to_s
|
28
37
|
"[Native #{@ruby.class.name}]"
|
29
38
|
end
|
30
|
-
|
39
|
+
|
31
40
|
alias_method :prototype, :getPrototype
|
32
|
-
|
33
|
-
|
41
|
+
|
42
|
+
|
34
43
|
class Prototype < J::ScriptableObject
|
35
|
-
|
44
|
+
|
36
45
|
def get(name, start)
|
37
46
|
robject = To.ruby(start)
|
38
|
-
if name == "toString"
|
47
|
+
if name == "toString"
|
39
48
|
return RubyFunction.new(lambda { "[Ruby #{robject.class.name}]"})
|
40
49
|
end
|
41
|
-
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
|
42
|
-
if (robject.public_methods(false).include?(rb_name))
|
50
|
+
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}.to_sym
|
51
|
+
if (robject.public_methods(false).collect(&:to_sym).include?(rb_name))
|
43
52
|
method = robject.method(rb_name)
|
44
53
|
if method.arity == 0
|
45
54
|
To.javascript(method.call)
|
@@ -50,14 +59,13 @@ module Rhino
|
|
50
59
|
super(name, start)
|
51
60
|
end
|
52
61
|
end
|
53
|
-
|
62
|
+
|
54
63
|
def has(name, start)
|
55
|
-
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}
|
56
|
-
To.ruby(start).public_methods(false).
|
64
|
+
rb_name = name.gsub(/([a-z])([A-Z])/) {"#{$1}_#{$2.downcase}"}.to_sym
|
65
|
+
To.ruby(start).public_methods(false).collect(&:to_sym).include?(rb_name) ? true : super(name,start)
|
57
66
|
end
|
58
|
-
|
67
|
+
|
59
68
|
Generic = new
|
60
|
-
|
61
69
|
end
|
62
70
|
end
|
63
71
|
end
|
data/lib/rhino/version.rb
CHANGED
data/lib/rhino/wormhole.rb
CHANGED
@@ -1,33 +1,36 @@
|
|
1
1
|
|
2
2
|
module Rhino
|
3
|
-
module To
|
3
|
+
module To
|
4
4
|
JS_UNDEF = [J::Scriptable::NOT_FOUND, J::Undefined]
|
5
|
-
|
5
|
+
|
6
|
+
module_function
|
7
|
+
|
6
8
|
def ruby(object)
|
7
9
|
case object
|
8
10
|
when *JS_UNDEF then nil
|
9
11
|
when J::Wrapper then object.unwrap
|
10
12
|
when J::NativeArray then array(object)
|
13
|
+
when J::NativeDate then Time.at(object.getJSTimeValue() / 1000)
|
11
14
|
when J::Regexp::NativeRegExp then object
|
12
|
-
when J::Function then NativeFunction.new(
|
13
|
-
when J::Scriptable then NativeObject.new(
|
15
|
+
when J::Function then j2r(object) {|o| NativeFunction.new(o)}
|
16
|
+
when J::Scriptable then j2r(object) {|o| NativeObject.new(o)}
|
14
17
|
else object
|
15
|
-
end
|
18
|
+
end
|
16
19
|
end
|
17
|
-
|
20
|
+
|
18
21
|
def javascript(object)
|
19
22
|
case object
|
20
23
|
when String,Numeric then object
|
21
24
|
when TrueClass,FalseClass then object
|
22
25
|
when Array then J::NativeArray.new(object.to_java)
|
23
26
|
when Hash then ruby_hash_to_native(object)
|
24
|
-
when Proc,Method then RubyFunction.new(
|
25
|
-
when NativeObject then object.j
|
27
|
+
when Proc,Method then r2j(object, object.to_s) {|o| RubyFunction.new(o)}
|
28
|
+
when NativeObject then object.j
|
26
29
|
when J::Scriptable then object
|
27
|
-
else RubyObject.new(
|
30
|
+
else r2j(object) {|o| RubyObject.new(o)}
|
28
31
|
end
|
29
32
|
end
|
30
|
-
|
33
|
+
|
31
34
|
def array(native)
|
32
35
|
native.length.times.map {|i| ruby(native.get(i,native))}
|
33
36
|
end
|
@@ -41,7 +44,38 @@ module Rhino
|
|
41
44
|
|
42
45
|
native_object.j
|
43
46
|
end
|
44
|
-
|
45
|
-
|
47
|
+
|
48
|
+
@@j2r = {}
|
49
|
+
def j2r(value)
|
50
|
+
key = value.object_id
|
51
|
+
if ref = @@j2r[key]
|
52
|
+
if peer = ref.get()
|
53
|
+
return peer
|
54
|
+
else
|
55
|
+
@@j2r.delete(key)
|
56
|
+
return j2r(value) {|o| yield o}
|
57
|
+
end
|
58
|
+
else
|
59
|
+
yield(value).tap do |peer|
|
60
|
+
@@j2r[key] = java.lang.ref.WeakReference.new(peer)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
@@r2j = {}
|
66
|
+
def r2j(value, key = value.object_id)
|
67
|
+
if ref = @@r2j[key]
|
68
|
+
if peer = ref.get()
|
69
|
+
return peer
|
70
|
+
else
|
71
|
+
@@r2j.delete(key)
|
72
|
+
return r2j(value, key) {|o| yield o}
|
73
|
+
end
|
74
|
+
else
|
75
|
+
yield(value).tap do |peer|
|
76
|
+
@@r2j[key] = java.lang.ref.WeakReference.new(peer)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
46
80
|
end
|
47
81
|
end
|
data/spec/rhino/context_spec.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../spec_helper'
|
2
2
|
|
3
3
|
include Rhino
|
4
|
-
|
5
|
-
describe Rhino::Context do
|
6
|
-
|
4
|
+
|
5
|
+
describe Rhino::Context do
|
6
|
+
|
7
7
|
describe "Initalizing Standard Javascript Objects" do
|
8
8
|
it "provides the standard objects without java integration by default" do
|
9
9
|
Context.open do |cxt|
|
@@ -17,30 +17,39 @@ describe Rhino::Context do
|
|
17
17
|
cxt["com"].should be_nil
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it "provides unsealed standard object by default" do
|
22
22
|
Context.open do |cxt|
|
23
23
|
cxt.eval("Object.foop = 'blort'")
|
24
24
|
cxt["Object"]['foop'].should == 'blort'
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
|
+
it "allows you to scope the context to an object" do
|
29
|
+
class MyScope
|
30
|
+
def foo; proc { 'bar' }; end
|
31
|
+
end
|
32
|
+
Context.open(:with => MyScope.new) do |ctx|
|
33
|
+
ctx.eval("foo()").should == 'bar'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
28
37
|
it "allows you to seal the standard objects so that they cannot be modified" do
|
29
38
|
Context.open(:sealed => true) do |cxt|
|
30
39
|
lambda {
|
31
|
-
cxt.eval("Object.foop = 'blort'")
|
32
|
-
}.should raise_error(Rhino::
|
33
|
-
|
40
|
+
cxt.eval("Object.foop = 'blort'")
|
41
|
+
}.should raise_error(Rhino::JSError)
|
42
|
+
|
34
43
|
lambda {
|
35
|
-
cxt.eval("Object.prototype.toString = function() {}")
|
36
|
-
}.should raise_error(Rhino::
|
44
|
+
cxt.eval("Object.prototype.toString = function() {}")
|
45
|
+
}.should raise_error(Rhino::JSError)
|
37
46
|
end
|
38
47
|
end
|
39
|
-
|
48
|
+
|
40
49
|
it "allows java integration to be turned on when initializing standard objects" do
|
41
50
|
Context.open(:java => true) do |cxt|
|
42
51
|
cxt["Packages"].should_not be_nil
|
43
52
|
end
|
44
|
-
end
|
53
|
+
end
|
45
54
|
end
|
46
55
|
end
|
data/spec/spec_helper.rb
CHANGED
data/therubyrhino.gemspec
CHANGED
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
s.rubyforge_project = %q{therubyrhino}
|
16
16
|
s.summary = %q{Embed the Rhino JavaScript interpreter into JRuby}
|
17
17
|
|
18
|
+
s.add_development_dependency "rake"
|
18
19
|
s.add_development_dependency "rspec"
|
19
20
|
s.add_development_dependency "jruby-openssl"
|
20
21
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: therubyrhino
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.73.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Charles Lowell
|
@@ -10,11 +10,10 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-11-28 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
16
|
+
name: rake
|
18
17
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
19
18
|
none: false
|
20
19
|
requirements:
|
@@ -25,7 +24,7 @@ dependencies:
|
|
25
24
|
prerelease: false
|
26
25
|
type: :development
|
27
26
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
27
|
+
name: rspec
|
29
28
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
30
29
|
none: false
|
31
30
|
requirements:
|
@@ -35,6 +34,17 @@ dependencies:
|
|
35
34
|
requirement: *id002
|
36
35
|
prerelease: false
|
37
36
|
type: :development
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jruby-openssl
|
39
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
requirement: *id003
|
46
|
+
prerelease: false
|
47
|
+
type: :development
|
38
48
|
description: Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript.
|
39
49
|
email: cowboyd@thefrontside.net
|
40
50
|
executables: []
|
@@ -56,7 +66,7 @@ files:
|
|
56
66
|
- lib/rhino/native_function.rb
|
57
67
|
- lib/rhino/native_object.rb
|
58
68
|
- lib/rhino/object.rb
|
59
|
-
- lib/rhino/rhino-1.
|
69
|
+
- lib/rhino/rhino-1.7R3.jar
|
60
70
|
- lib/rhino/ruby_function.rb
|
61
71
|
- lib/rhino/ruby_object.rb
|
62
72
|
- lib/rhino/version.rb
|
@@ -66,10 +76,7 @@ files:
|
|
66
76
|
- spec/rhino/wormhole_spec.rb
|
67
77
|
- spec/spec.opts
|
68
78
|
- spec/spec_helper.rb
|
69
|
-
- tasks/jruby.rake
|
70
|
-
- tasks/rspec.rake
|
71
79
|
- therubyrhino.gemspec
|
72
|
-
has_rdoc: true
|
73
80
|
homepage: http://github.com/cowboyd/therubyrhino
|
74
81
|
licenses: []
|
75
82
|
|
@@ -99,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
106
|
requirements: []
|
100
107
|
|
101
108
|
rubyforge_project: therubyrhino
|
102
|
-
rubygems_version: 1.
|
109
|
+
rubygems_version: 1.8.9
|
103
110
|
signing_key:
|
104
111
|
specification_version: 3
|
105
112
|
summary: Embed the Rhino JavaScript interpreter into JRuby
|
data/lib/rhino/rhino-1.7R2.jar
DELETED
Binary file
|
data/tasks/jruby.rake
DELETED
data/tasks/rspec.rake
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'spec'
|
3
|
-
require 'spec/rake/spectask'
|
4
|
-
desc "Run the specs under spec/models"
|
5
|
-
Spec::Rake::SpecTask.new do |t|
|
6
|
-
t.spec_opts = ['--options', "spec/spec.opts"]
|
7
|
-
t.spec_files = FileList['spec/**/*_spec.rb']
|
8
|
-
end
|
9
|
-
rescue LoadError
|
10
|
-
desc "bundle install to run rspecs"
|
11
|
-
task :spec
|
12
|
-
end
|
13
|
-
|