therubyrhino 1.72.2-jruby → 1.72.3-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 1.72.3 2009 11-11
2
+ * 1 major enhancement
3
+ * greatly simplified interface to context by unifying context and scope
4
+ * remove Context#open_std()
5
+ * remove Context#standard
6
+ * remove Context#init_standard_objects
7
+
1
8
  === 1.72.2 2009-11-10
2
9
  * 1 major enhancement:
3
10
  * ability to limit the instruction count for a context
data/Manifest.txt CHANGED
@@ -5,12 +5,18 @@ Rakefile
5
5
  lib/rhino.rb
6
6
  lib/rhino/context.rb
7
7
  lib/rhino/java.rb
8
+ lib/rhino/native_object.rb
8
9
  lib/rhino/rhino-1.7R2.jar
10
+ lib/rhino/ruby_object.rb
11
+ lib/rhino/wormhole.rb
9
12
  script/console
10
13
  script/destroy
11
14
  script/generate
12
15
  spec/rhino/context_spec.rb
16
+ spec/rhino/native_object_spec.rb
17
+ spec/rhino/wormhole_spec.rb
13
18
  spec/spec.opts
14
19
  spec/spec_helper.rb
15
20
  tasks/jruby.rake
16
21
  tasks/rspec.rake
22
+ therubyrhino.gemspec
data/lib/rhino/context.rb CHANGED
@@ -4,41 +4,41 @@ module Rhino
4
4
  end
5
5
 
6
6
  class Context
7
+
8
+ attr_reader :scope
7
9
 
8
10
  class << self
9
- def open
11
+ def open(options = {})
10
12
  ContextFactory.new.call do |native|
11
- yield new(native)
12
- end
13
- end
14
-
15
- def open_std(options = {})
16
- open do |cxt|
17
- yield cxt, cxt.init_standard_objects(options)
13
+ yield new(native, options)
18
14
  end
19
15
  end
20
-
16
+
21
17
  private :new
22
18
  end
23
19
 
24
- def initialize(native) #:nodoc:
20
+ def initialize(native, options) #:nodoc:
25
21
  @native = native
26
- end
27
-
28
- def init_standard_objects(options = {})
29
- NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true)).tap do |objects|
30
- unless options[:java]
31
- for package in ["Packages", "java", "org", "com"]
32
- objects.j.delete(package)
33
- end
22
+ @scope = NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true))
23
+ unless options[:java]
24
+ for package in ["Packages", "java", "org", "com"]
25
+ @scope.j.delete(package)
34
26
  end
35
- end
27
+ end
36
28
  end
37
29
 
38
- def eval(str, scope = @native.initStandardObjects())
30
+ def [](k)
31
+ @scope[k]
32
+ end
33
+
34
+ def []=(k,v)
35
+ @scope[k] = v
36
+ end
37
+
38
+ def eval(str)
39
39
  str = str.to_s
40
40
  begin
41
- To.ruby @native.evaluateString(To.javascript(scope), str, "<eval>", 1, nil)
41
+ To.ruby @native.evaluateString(@scope.j, str, "<eval>", 1, nil)
42
42
  rescue J::RhinoException => e
43
43
  raise Rhino::RhinoError, e
44
44
  end
@@ -48,11 +48,7 @@ module Rhino
48
48
  @native.setInstructionObserverThreshold(limit);
49
49
  @native.factory.instruction_limit = limit
50
50
  end
51
-
52
- def standard
53
- yield @native.initStandardObjects()
54
- end
55
-
51
+
56
52
  end
57
53
 
58
54
  class Function < J::BaseFunction
@@ -0,0 +1,37 @@
1
+
2
+ module Rhino
3
+ class NativeObject
4
+ include Enumerable
5
+ attr_reader :j
6
+
7
+ def initialize(j)
8
+ @j = j
9
+ end
10
+
11
+ def [](k)
12
+ To.ruby @j.get(k.to_s, @j)
13
+ end
14
+
15
+ def []=(k,v)
16
+ @j.put(k.to_s,@j,To.javascript(v))
17
+ end
18
+
19
+ def each
20
+ for id in @j.getAllIds() do
21
+ yield id,@j.get(id,@j)
22
+ end
23
+ end
24
+
25
+ def to_h
26
+ {}.tap do |h|
27
+ each do |k,v|
28
+ h[k] = self.class === v ? v.to_h : v
29
+ end
30
+ end
31
+ end
32
+
33
+ def to_json(*args)
34
+ to_h.to_json(*args)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+
2
+ module Rhino
3
+ class RubyObject < J::ScriptableObject
4
+ include J::Wrapper
5
+
6
+ def initialize(object)
7
+ super()
8
+ @ruby = object
9
+ end
10
+
11
+ def unwrap
12
+ @ruby
13
+ end
14
+
15
+ def getClassName()
16
+ @ruby.class.name
17
+ end
18
+
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
27
+ end
28
+
29
+ def to_s
30
+ "[Native #{@ruby.class.name}]"
31
+ end
32
+
33
+ alias_method :prototype, :getPrototype
34
+ end
35
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Rhino
3
+ module To
4
+ JS_UNDEF = [J::Scriptable::NOT_FOUND, J::Undefined]
5
+
6
+ def ruby(object)
7
+ case object
8
+ when *JS_UNDEF then nil
9
+ when J::Wrapper then object.unwrap
10
+ when J::Scriptable then NativeObject.new(object)
11
+ else object
12
+ end
13
+ end
14
+
15
+ def javascript(object)
16
+ case object
17
+ when NativeObject then object.j
18
+ when J::Scriptable then object
19
+ else RubyObject.new(object)
20
+ end
21
+ end
22
+
23
+ module_function :ruby, :javascript
24
+ end
25
+ end
data/lib/rhino.rb CHANGED
@@ -3,7 +3,7 @@ $:.unshift(File.dirname(__FILE__)) unless
3
3
 
4
4
 
5
5
  module Rhino
6
- VERSION = '1.72.2'
6
+ VERSION = '1.72.3'
7
7
  require 'rhino/java'
8
8
  require 'rhino/context'
9
9
  require 'rhino/wormhole'
@@ -18,66 +18,47 @@ describe Rhino::Context do
18
18
 
19
19
  it "can embed primitive ruby object into javascript" do
20
20
  Context.open do |cxt|
21
- cxt.init_standard_objects.tap do |scope|
22
- scope["foo"] = "Hello World"
23
- cxt.eval("foo", scope).unwrap.should == "Hello World"
24
- end
21
+ cxt['foo'] = "Hello World"
22
+ cxt.eval("foo").should == "Hello World"
25
23
  end
26
24
  end
27
25
 
28
26
  describe "Initalizing Standard Javascript Objects" do
29
27
  it "provides the standard objects without java integration by default" do
30
28
  Context.open do |cxt|
31
- cxt.init_standard_objects.tap do |scope|
32
- scope["Object"].should_not be_nil
33
- scope["Math"].should_not be_nil
34
- scope["String"].should_not be_nil
35
- scope["Function"].should_not be_nil
36
- scope["Packages"].should be_nil
37
- scope["java"].should be_nil
38
- scope["org"].should be_nil
39
- scope["com"].should be_nil
40
- end
29
+ cxt["Object"].should_not be_nil
30
+ cxt["Math"].should_not be_nil
31
+ cxt["String"].should_not be_nil
32
+ cxt["Function"].should_not be_nil
33
+ cxt["Packages"].should be_nil
34
+ cxt["java"].should be_nil
35
+ cxt["org"].should be_nil
36
+ cxt["com"].should be_nil
41
37
  end
42
38
  end
43
39
 
44
40
  it "provides unsealed standard object by default" do
45
41
  Context.open do |cxt|
46
- cxt.init_standard_objects.tap do |scope|
47
- cxt.eval("Object.foop = 'blort'", scope)
48
- scope["Object"]['foop'].should == 'blort'
49
- end
42
+ cxt.eval("Object.foop = 'blort'")
43
+ cxt["Object"]['foop'].should == 'blort'
50
44
  end
51
45
  end
52
46
 
53
47
  it "allows you to seal the standard objects so that they cannot be modified" do
54
- Context.open do |cxt|
55
- cxt.init_standard_objects(:sealed => true).tap do |scope|
56
- lambda {
57
- cxt.eval("Object.foop = 'blort'", scope)
58
- }.should raise_error(Rhino::RhinoError)
59
-
60
- lambda {
61
- cxt.eval("Object.prototype.toString = function() {}", scope)
62
- }.should raise_error(Rhino::RhinoError)
63
-
64
- end
48
+ Context.open(:sealed => true) do |cxt|
49
+ lambda {
50
+ cxt.eval("Object.foop = 'blort'")
51
+ }.should raise_error(Rhino::RhinoError)
52
+
53
+ lambda {
54
+ cxt.eval("Object.prototype.toString = function() {}")
55
+ }.should raise_error(Rhino::RhinoError)
65
56
  end
66
57
  end
67
58
 
68
59
  it "allows java integration to be turned on when initializing standard objects" do
69
- Context.open do |cxt|
70
- cxt.init_standard_objects(:java => true).tap do |scope|
71
- scope["Packages"].should_not be_nil
72
- end
73
- end
74
- end
75
-
76
- it "provides a convenience method for initializing scopes" do
77
- Context.open_std(:sealed => true, :java => true) do |cxt, scope|
78
- scope["Object"].should_not be_nil
79
- scope["java"].should_not be_nil
80
- cxt.eval("new java.lang.String('foo')", scope).should == "foo"
60
+ Context.open(:java => true) do |cxt|
61
+ cxt["Packages"].should_not be_nil
81
62
  end
82
63
  end
83
64
  end
@@ -85,16 +66,14 @@ describe Rhino::Context do
85
66
 
86
67
  it "can call ruby functions from javascript" do
87
68
  Context.open do |cxt|
88
- cxt.standard do |scope|
89
- scope.put("say", scope, function {|word, times| word * times})
90
- cxt.eval("say('Hello',2)", scope).should == "HelloHello"
91
- end
69
+ cxt["say"] = function {|word, times| word * times}
70
+ cxt.eval("say('Hello',2)").should == "HelloHello"
92
71
  end
93
72
  end
94
73
 
95
74
  it "can limit the number of instructions that are executed in the context" do
96
75
  lambda {
97
- Context.open_std do |cxt, scope|
76
+ Context.open do |cxt|
98
77
  cxt.instruction_limit = 100 * 1000
99
78
  timeout(1) do
100
79
  cxt.eval('while (true);')
@@ -0,0 +1,40 @@
1
+
2
+ require File.dirname(__FILE__) + '/../spec_helper'
3
+
4
+ include Rhino
5
+
6
+ describe Rhino::NativeObject do
7
+
8
+ before(:each) do
9
+ @j = J::NativeObject.new
10
+ @o = NativeObject.new(@j)
11
+ end
12
+
13
+ it "wraps a native javascript object" do
14
+ @o["foo"] = 'bar'
15
+ @j.get("foo", @j).unwrap.should == "bar"
16
+ @j.put("blue",@j, "blam")
17
+ @o["blue"].should == "blam"
18
+ end
19
+
20
+ it "doesn't matter if you use a symbol or a string to set a value" do
21
+ @o[:foo] = "bar"
22
+ @o['foo'].should == "bar"
23
+ @o['baz'] = "bang"
24
+ @o[:baz].should == "bang"
25
+ end
26
+
27
+ it "returns nil when the value is null, null, or not defined" do
28
+ @o[:foo].should be_nil
29
+ end
30
+
31
+ describe Enumerable do
32
+ it "enumerates according to native keys and values" do
33
+ @j.put("foo", @j, 'bar')
34
+ @j.put("bang", @j, 'baz')
35
+ @j.put(5, @j, 'flip')
36
+ @o.inject({}) {|i,p| k,v = p; i.tap {i[k] = v}}.should == {"foo" => 'bar', "bang" => 'baz', 5 => 'flip'}
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ include Rhino
4
+
5
+ describe Rhino::To do
6
+ describe "ruby translation" do
7
+ it "converts javascript NOT_FOUND to ruby nil" do
8
+ To.ruby(J::Scriptable::NOT_FOUND).should be_nil
9
+ end
10
+
11
+ it "wraps native javascript objects in a ruby NativeObject wrapper" do
12
+ J::NativeObject.new.tap do |o|
13
+ To.ruby(o).tap do |ruby_object|
14
+ ruby_object.should respond_to(:j)
15
+ ruby_object.j.should be(o)
16
+ end
17
+ end
18
+ end
19
+
20
+ it "leaves native ruby objects alone" do
21
+ Object.new.tap do |o|
22
+ To.ruby(o).should be(o)
23
+ end
24
+ end
25
+
26
+ it "it unwraps wrapped java objects" do
27
+ Context.open do |cx|
28
+ scope = cx.scope
29
+ Java::JavaLang::String.new("Hello World").tap do |str|
30
+ J::NativeJavaObject.new(scope.j, str, str.getClass()).tap do |o|
31
+ To.ruby(o).should == "Hello World"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ it "converts javascript undefined into nil" do
38
+ To.ruby(J::Undefined.instance).should be_nil
39
+ end
40
+ end
41
+
42
+ describe "javascript translation" do
43
+ it "unwraps wrapped ruby objects before passing them to the javascript runtime" do
44
+ J::NativeObject.new.tap do |o|
45
+ To.javascript(NativeObject.new(o)).should be(o)
46
+ end
47
+ end
48
+
49
+ it "leaves native javascript objects alone" do
50
+ J::NativeObject.new.tap do |o|
51
+ To.javascript(o).should be(o)
52
+ end
53
+ end
54
+
55
+ it "creates a prototype for the object based on its class" do
56
+ Class.new.tap do |c|
57
+ c.class_eval do
58
+ def foo(one, two)
59
+ "1: #{one}, 2: #{two}"
60
+ end
61
+ end
62
+
63
+ To.javascript(c.new).tap do |o|
64
+ o.should be_kind_of(RubyObject)
65
+ o.prototype.tap do |p|
66
+ p.should_not be_nil
67
+ p.get("foo", p).should_not be_nil
68
+ p.get("toString", p).should_not be_nil
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{therubyrhino}
5
+ s.version = "1.72.3"
6
+ s.platform = %q{jruby}
7
+
8
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
+ s.authors = ["Charles Lowell"]
10
+ s.date = %q{2009-11-11}
11
+ s.description = %q{Embed the Mozilla Rhino Javascript interpreter into Ruby}
12
+ s.email = ["cowboyd@thefrontside.net"]
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"]
15
+ s.homepage = %q{http://github.com/cowboyd/therubyrhino}
16
+ s.rdoc_options = ["--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{therubyrhino}
19
+ s.rubygems_version = %q{1.3.5}
20
+ s.summary = %q{Embed the Rhino Javascript engine into JRuby}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
28
+ else
29
+ s.add_dependency(%q<hoe>, [">= 2.3.3"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<hoe>, [">= 2.3.3"])
33
+ end
34
+ end
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.2
4
+ version: 1.72.3
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-10 00:00:00 -05:00
12
+ date: 2009-11-11 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,15 +40,21 @@ files:
40
40
  - lib/rhino.rb
41
41
  - lib/rhino/context.rb
42
42
  - lib/rhino/java.rb
43
+ - lib/rhino/native_object.rb
43
44
  - lib/rhino/rhino-1.7R2.jar
45
+ - lib/rhino/ruby_object.rb
46
+ - lib/rhino/wormhole.rb
44
47
  - script/console
45
48
  - script/destroy
46
49
  - script/generate
47
50
  - spec/rhino/context_spec.rb
51
+ - spec/rhino/native_object_spec.rb
52
+ - spec/rhino/wormhole_spec.rb
48
53
  - spec/spec.opts
49
54
  - spec/spec_helper.rb
50
55
  - tasks/jruby.rake
51
56
  - tasks/rspec.rake
57
+ - therubyrhino.gemspec
52
58
  has_rdoc: true
53
59
  homepage: http://github.com/cowboyd/therubyrhino
54
60
  licenses: []