therubyrhino 1.72.1-jruby

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ === 1.72.1 2009-10-XX
2
+ * N major enhancements:
3
+ * easily manipulate javascript objects from ruby (NativeObject)
4
+ * make NativeObject Enumerable
5
+ * to_h and to_json for NativeObject
6
+ * embed ruby instances and call methods from javascript
7
+
8
+ === 1.72.0 2009-09-24
9
+
10
+ * 2 major enhancements:
11
+ * evaluate javascript in jruby
12
+ * embed callable ruby objects in javascript
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/rhino.rb
6
+ lib/rhino/context.rb
7
+ lib/rhino/java.rb
8
+ lib/rhino/rhino-1.7R2.jar
9
+ script/console
10
+ script/destroy
11
+ script/generate
12
+ spec/rhino/context_spec.rb
13
+ spec/spec.opts
14
+ spec/spec_helper.rb
15
+ tasks/jruby.rake
16
+ tasks/rspec.rake
@@ -0,0 +1,78 @@
1
+ = therubyrhino
2
+
3
+ * http://github.com/cowboyd/therubyrhino
4
+
5
+ == DESCRIPTION:
6
+
7
+ Embed the Mozilla Rhino Javascript interpreter into Ruby
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Evaluate Javascript from with in Ruby
12
+ * Embed your Ruby objects into the Javascript world
13
+
14
+ == SYNOPSIS:
15
+
16
+ 1. Javascript goes into Ruby
17
+ 1. Ruby Objects goes into Javascript
18
+ 1. Our shark's in the Javascript!
19
+
20
+
21
+ include Rhino
22
+ # evaluate some simple javascript
23
+
24
+ Rhino::Context.open do |context|
25
+ context.evaljs("7 * 6") #=> 42
26
+ end
27
+
28
+ # evaluate a ruby function from javascript
29
+
30
+ Rhino::Context.open do |context|
31
+ scope = context.init_standard_objects
32
+ scope["say"] = function {|word, times| word * times}
33
+ context.evaljs("say("Hello", 3)") #=> HelloHelloHello
34
+ end
35
+
36
+ # Configure your embedding setup
37
+
38
+ Rhino::Context.open do |context|
39
+ # Make your standard objects (Object, String, etc...) immutable
40
+ scope = context.init_standard_objects(:sealed => true)
41
+ context.evaljs("Object.prototype.toString = function() {}") # this is an error!
42
+
43
+ #Turn on Java integration from javascript (probably a bad idea)
44
+ scope = context.init_standard_objects(:java => true)
45
+ context.evaljs("java.lang.System.exit()") #it's dangerous!
46
+ end
47
+ == REQUIREMENTS:
48
+
49
+ * JRuby >= 1.3.0
50
+
51
+ == INSTALL:
52
+
53
+ * jgem install therubyrhino
54
+
55
+ == LICENSE:
56
+
57
+ (The MIT License)
58
+
59
+ Copyright (c) 2009 Charles Lowell
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ 'Software'), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
75
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
76
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
77
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
78
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/rhino'
6
+
7
+ Hoe.plugin :newgem
8
+
9
+ # Generate all the Rake tasks
10
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
11
+ $hoe = Hoe.spec 'therubyrhino' do
12
+ self.developer 'Charles Lowell', 'cowboyd@thefrontside.net'
13
+ self.rubyforge_name = self.name
14
+ self.summary = "Embed the Rhino Javascript engine into JRuby"
15
+
16
+ self.spec_extras['platform'] = 'jruby' # JRuby gem created, e.g. therubyrhino-X.Y.Z-jruby.gem
17
+ end
18
+
19
+ require 'newgem/tasks'
20
+ Dir['tasks/**/*.rake'].each { |t| load t }
21
+
@@ -0,0 +1,12 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+
5
+ module Rhino
6
+ VERSION = '1.72.1'
7
+ require 'rhino/java'
8
+ require 'rhino/context'
9
+ require 'rhino/wormhole'
10
+ require 'rhino/native_object'
11
+ require 'rhino/ruby_object'
12
+ end
@@ -0,0 +1,83 @@
1
+ module Rhino
2
+
3
+ def function(&impl)
4
+ Function.new &impl
5
+ end
6
+
7
+ class Context
8
+
9
+ class << self
10
+ def open
11
+ J::ContextFactory.new.call do |native|
12
+ yield new(native)
13
+ end
14
+ end
15
+
16
+ def open_std(options = {})
17
+ open do |cxt|
18
+ yield cxt, cxt.init_standard_objects(options)
19
+ end
20
+ end
21
+
22
+ private :new
23
+ end
24
+
25
+ def initialize(native) #:nodoc:
26
+ @native = native
27
+ end
28
+
29
+ def init_standard_objects(options = {})
30
+ NativeObject.new(@native.initStandardObjects(nil, options[:sealed] == true)).tap do |objects|
31
+ unless options[:java]
32
+ for package in ["Packages", "java", "org", "com"]
33
+ objects.j.delete(package)
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def eval(str, scope = @native.initStandardObjects())
40
+ str = str.to_s
41
+ begin
42
+ To.ruby @native.evaluateString(To.javascript(scope), str, "<eval>", 1, nil)
43
+ rescue J::RhinoException => e
44
+ raise Rhino::RhinoError, e
45
+ end
46
+ end
47
+
48
+ def standard
49
+ yield @native.initStandardObjects()
50
+ end
51
+
52
+ end
53
+
54
+ class Function < J::BaseFunction
55
+ def initialize(callable = nil, &block)
56
+ super()
57
+ @block = callable || block
58
+ end
59
+
60
+ def call(cxt, scope, this, args)
61
+ @block.call(*(args.map {|a| To.ruby(a)}))
62
+ end
63
+
64
+ def to_json(*args)
65
+ '"[Native Function]"'
66
+ end
67
+ end
68
+
69
+
70
+ class RhinoError < StandardError
71
+ def initialize(native)
72
+ @native = native
73
+ end
74
+
75
+ def message
76
+ @native.cause.details
77
+ end
78
+
79
+ def javascript_backtrace
80
+ @native.getScriptStackTrace()
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,17 @@
1
+ require 'java'
2
+ require 'rhino/rhino-1.7R2.jar'
3
+
4
+ module Rhino
5
+ module J
6
+ import "org.mozilla.javascript"
7
+ end
8
+ end
9
+
10
+ unless Object.method_defined?(:tap)
11
+ class Object
12
+ def tap
13
+ yield self
14
+ self
15
+ end
16
+ end
17
+ end
Binary file
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env jruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rhino.rb'}"
9
+ puts "Loading therubyrhino gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ include Rhino
4
+
5
+ describe Rhino::Context do
6
+
7
+ it "can evaluate some javascript" do
8
+ Context.open do |cxt|
9
+ cxt.eval("5 + 3").should == 8
10
+ end
11
+ end
12
+
13
+ it "treats nil and the empty string as the same thing when it comes to eval" do
14
+ Context.open do |cxt|
15
+ cxt.eval(nil).should == cxt.eval('')
16
+ end
17
+ end
18
+
19
+ it "can embed primitive ruby object into javascript" do
20
+ Context.open do |cxt|
21
+ cxt.init_standard_objects.tap do |scope|
22
+ scope["foo"] = "Hello World"
23
+ cxt.eval("foo", scope).should == "Hello World"
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "Initalizing Standard Javascript Objects" do
29
+ it "provides the standard objects without java integration by default" do
30
+ 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
41
+ end
42
+ end
43
+
44
+ it "provides unsealed standard object by default" do
45
+ 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
50
+ end
51
+ end
52
+
53
+ 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
65
+ end
66
+ end
67
+
68
+ 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"
81
+ end
82
+ end
83
+ end
84
+
85
+
86
+ it "can call ruby functions from javascript" do
87
+ 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
92
+ end
93
+ end
94
+
95
+ it "has a private constructor" do
96
+ lambda {
97
+ Context.new(nil)
98
+ }.should raise_error
99
+ end
100
+ end
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'rhino'
@@ -0,0 +1,7 @@
1
+ if RUBY_PLATFORM =~ /java/
2
+ require 'java'
3
+ else
4
+ puts "Java RubyGem only! You are not running within jruby."
5
+ puts "Try: jruby -S rake #{ARGV.join(' ')}"
6
+ exit(1)
7
+ end
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems' unless ENV['NO_RUBYGEMS']
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec/models"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/**/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: therubyrhino
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.72.1
5
+ platform: jruby
6
+ authors:
7
+ - Charles Lowell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-09 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: Embed the Mozilla Rhino Javascript interpreter into Ruby
26
+ email:
27
+ - cowboyd@thefrontside.net
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/rhino.rb
41
+ - lib/rhino/context.rb
42
+ - lib/rhino/java.rb
43
+ - lib/rhino/rhino-1.7R2.jar
44
+ - script/console
45
+ - script/destroy
46
+ - script/generate
47
+ - spec/rhino/context_spec.rb
48
+ - spec/spec.opts
49
+ - spec/spec_helper.rb
50
+ - tasks/jruby.rake
51
+ - tasks/rspec.rake
52
+ has_rdoc: true
53
+ homepage: http://github.com/cowboyd/therubyrhino
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --main
59
+ - README.rdoc
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ requirements: []
75
+
76
+ rubyforge_project: therubyrhino
77
+ rubygems_version: 1.3.5
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Embed the Rhino Javascript engine into JRuby
81
+ test_files: []
82
+