mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
basic javascript access from jruby
This commit is contained in:
commit
b04ca685cc
17 changed files with 299 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.idea
|
||||||
|
pkg
|
5
History.txt
Normal file
5
History.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
=== 1.72.0 2009-09-24
|
||||||
|
|
||||||
|
* 2 major enhancements:
|
||||||
|
* evaluate javascript in jruby
|
||||||
|
* embed callable ruby objects in javascript
|
16
Manifest.txt
Normal file
16
Manifest.txt
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
History.txt
|
||||||
|
Manifest.txt
|
||||||
|
README.rdoc
|
||||||
|
Rakefile
|
||||||
|
lib/rhino.rb
|
||||||
|
lib/rhino/context.rb
|
||||||
|
lib/rhino/java.rb
|
||||||
|
lib/rhino/rhino-1.7R2.jar
|
||||||
|
script/console
|
||||||
|
script/destroy
|
||||||
|
script/generate
|
||||||
|
spec/rhino/context_spec.rb
|
||||||
|
spec/spec.opts
|
||||||
|
spec/spec_helper.rb
|
||||||
|
tasks/jruby.rake
|
||||||
|
tasks/rspec.rake
|
66
README.rdoc
Normal file
66
README.rdoc
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
= therubyrhino
|
||||||
|
|
||||||
|
* http://github.com/cowboyd/therubyrhino
|
||||||
|
|
||||||
|
== DESCRIPTION:
|
||||||
|
|
||||||
|
Embed the Mozilla Rhino Javascript interpreter into Ruby
|
||||||
|
|
||||||
|
== FEATURES/PROBLEMS:
|
||||||
|
|
||||||
|
* Evaluate Javascript from with in Ruby
|
||||||
|
* Embed your Ruby objects into the Javascript world
|
||||||
|
|
||||||
|
== SYNOPSIS:
|
||||||
|
|
||||||
|
1) Javascript goes into Ruby
|
||||||
|
2) Ruby Objects goes into Javascript
|
||||||
|
3) Our shark's in the Javascript!
|
||||||
|
|
||||||
|
include Rhino
|
||||||
|
|
||||||
|
#evaluate some simple javascript
|
||||||
|
Rhino::Context.open do |context|
|
||||||
|
context.evaljs("7 * 6") #=> 42
|
||||||
|
end
|
||||||
|
|
||||||
|
#Evaluate a ruby function from javascript
|
||||||
|
|
||||||
|
Rhino::Context.open do |context|
|
||||||
|
context.standard do |scope|
|
||||||
|
scope.put("say", scope, function {|word, times| word * times})
|
||||||
|
context.evaljs("say("Hello", 3)") #=> HelloHelloHello
|
||||||
|
end
|
||||||
|
end
|
||||||
|
== REQUIREMENTS:
|
||||||
|
|
||||||
|
* JRuby >= 1.3.1
|
||||||
|
|
||||||
|
== INSTALL:
|
||||||
|
|
||||||
|
* jgem install therubyrhino
|
||||||
|
|
||||||
|
== LICENSE:
|
||||||
|
|
||||||
|
(The MIT License)
|
||||||
|
|
||||||
|
Copyright (c) 2009 Charles Lowell
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
'Software'), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
Rakefile
Normal file
19
Rakefile
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
require 'rubygems'
|
||||||
|
gem 'hoe', '>= 2.1.0'
|
||||||
|
require 'hoe'
|
||||||
|
require 'fileutils'
|
||||||
|
require './lib/rhino'
|
||||||
|
|
||||||
|
Hoe.plugin :newgem
|
||||||
|
|
||||||
|
# Generate all the Rake tasks
|
||||||
|
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
||||||
|
$hoe = Hoe.spec 'therubyrhino' do
|
||||||
|
self.developer 'Charles Lowell', 'cowboyd@thefrontside.net'
|
||||||
|
self.rubyforge_name = self.name
|
||||||
|
self.spec_extras['platform'] = 'jruby' # JRuby gem created, e.g. therubyrhino-X.Y.Z-jruby.gem
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'newgem/tasks'
|
||||||
|
Dir['tasks/**/*.rake'].each { |t| load t }
|
||||||
|
|
9
lib/rhino.rb
Normal file
9
lib/rhino.rb
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
$:.unshift(File.dirname(__FILE__)) unless
|
||||||
|
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
||||||
|
|
||||||
|
|
||||||
|
module Rhino
|
||||||
|
VERSION = '1.72.0'
|
||||||
|
require 'rhino/java'
|
||||||
|
require 'rhino/context'
|
||||||
|
end
|
61
lib/rhino/context.rb
Normal file
61
lib/rhino/context.rb
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
module Rhino
|
||||||
|
|
||||||
|
def function(&impl)
|
||||||
|
Function.new &impl
|
||||||
|
end
|
||||||
|
|
||||||
|
class Context
|
||||||
|
|
||||||
|
class << self
|
||||||
|
private :new
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize(native)
|
||||||
|
@native = native
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.open
|
||||||
|
J::ContextFactory.new.call do |native|
|
||||||
|
yield new(native)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def evaljs(str, scope = @native.initStandardObjects())
|
||||||
|
begin
|
||||||
|
@native.evaluateString(scope, str, "<eval>", 1, nil)
|
||||||
|
rescue J::RhinoException => e
|
||||||
|
raise Rhino::RhinoError, e
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def standard
|
||||||
|
yield @native.initStandardObjects()
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
class Function < J::BaseFunction
|
||||||
|
def initialize(&block)
|
||||||
|
@block = block
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(cxt, scope, this, args)
|
||||||
|
@block.call(*args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
class RhinoError < StandardError
|
||||||
|
def initialize(native)
|
||||||
|
@native = native
|
||||||
|
end
|
||||||
|
|
||||||
|
def message
|
||||||
|
@native.message
|
||||||
|
end
|
||||||
|
|
||||||
|
def javascript_backtrace
|
||||||
|
@native.script_stack_trace
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
8
lib/rhino/java.rb
Normal file
8
lib/rhino/java.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
require 'java'
|
||||||
|
require 'rhino/rhino-1.7R2.jar'
|
||||||
|
|
||||||
|
module Rhino
|
||||||
|
module J
|
||||||
|
import "org.mozilla.javascript"
|
||||||
|
end
|
||||||
|
end
|
BIN
lib/rhino/rhino-1.7R2.jar
Normal file
BIN
lib/rhino/rhino-1.7R2.jar
Normal file
Binary file not shown.
10
script/console
Executable file
10
script/console
Executable file
|
@ -0,0 +1,10 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
# File: script/console
|
||||||
|
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
||||||
|
|
||||||
|
libs = " -r irb/completion"
|
||||||
|
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
||||||
|
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
||||||
|
libs << " -r #{File.dirname(__FILE__) + '/../lib/rhino.rb'}"
|
||||||
|
puts "Loading therubyrhino gem"
|
||||||
|
exec "#{irb} #{libs} --simple-prompt"
|
14
script/destroy
Executable file
14
script/destroy
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'rubigen'
|
||||||
|
rescue LoadError
|
||||||
|
require 'rubygems'
|
||||||
|
require 'rubigen'
|
||||||
|
end
|
||||||
|
require 'rubigen/scripts/destroy'
|
||||||
|
|
||||||
|
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
||||||
|
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
||||||
|
RubiGen::Scripts::Destroy.new.run(ARGV)
|
14
script/generate
Executable file
14
script/generate
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'rubigen'
|
||||||
|
rescue LoadError
|
||||||
|
require 'rubygems'
|
||||||
|
require 'rubigen'
|
||||||
|
end
|
||||||
|
require 'rubigen/scripts/generate'
|
||||||
|
|
||||||
|
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
||||||
|
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
||||||
|
RubiGen::Scripts::Generate.new.run(ARGV)
|
36
spec/rhino/context_spec.rb
Normal file
36
spec/rhino/context_spec.rb
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
|
describe Rhino::Context do
|
||||||
|
include Rhino
|
||||||
|
|
||||||
|
it "can evaluate some javascript" do
|
||||||
|
Rhino::Context.open do |cxt|
|
||||||
|
cxt.evaljs("5 + 3").should == 8
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can embed ruby object into javascript" do
|
||||||
|
Rhino::Context.open do |cxt|
|
||||||
|
cxt.standard do |scope|
|
||||||
|
scope.put("foo", scope, "Hello World")
|
||||||
|
cxt.evaljs("foo", scope).should == "Hello World"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "can call ruby functions from javascript" do
|
||||||
|
Rhino::Context.open do |cxt|
|
||||||
|
cxt.standard do |scope|
|
||||||
|
scope.put("say", scope, function {|word, times| word * times})
|
||||||
|
cxt.evaljs("say('Hello',2)", scope).should == "HelloHello"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "has a private constructor" do
|
||||||
|
lambda {
|
||||||
|
Rhino::Context.new(nil)
|
||||||
|
}.should raise_error
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
1
spec/spec.opts
Normal file
1
spec/spec.opts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
--colour
|
10
spec/spec_helper.rb
Normal file
10
spec/spec_helper.rb
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
begin
|
||||||
|
require 'spec'
|
||||||
|
rescue LoadError
|
||||||
|
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
||||||
|
gem 'rspec'
|
||||||
|
require 'spec'
|
||||||
|
end
|
||||||
|
|
||||||
|
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
||||||
|
require 'rhino'
|
7
tasks/jruby.rake
Normal file
7
tasks/jruby.rake
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
if RUBY_PLATFORM =~ /java/
|
||||||
|
require 'java'
|
||||||
|
else
|
||||||
|
puts "Java RubyGem only! You are not running within jruby."
|
||||||
|
puts "Try: jruby -S rake #{ARGV.join(' ')}"
|
||||||
|
exit(1)
|
||||||
|
end
|
21
tasks/rspec.rake
Normal file
21
tasks/rspec.rake
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
begin
|
||||||
|
require 'spec'
|
||||||
|
rescue LoadError
|
||||||
|
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
||||||
|
require 'spec'
|
||||||
|
end
|
||||||
|
begin
|
||||||
|
require 'spec/rake/spectask'
|
||||||
|
rescue LoadError
|
||||||
|
puts <<-EOS
|
||||||
|
To use rspec for testing you must install rspec gem:
|
||||||
|
gem install rspec
|
||||||
|
EOS
|
||||||
|
exit(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Run the specs under spec/models"
|
||||||
|
Spec::Rake::SpecTask.new do |t|
|
||||||
|
t.spec_opts = ['--options', "spec/spec.opts"]
|
||||||
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
||||||
|
end
|
Loading…
Reference in a new issue