commit b04ca685ccbfde924430c5a53a3eeff421edec32 Author: Charles Lowell Date: Thu Sep 24 19:06:31 2009 -0500 basic javascript access from jruby diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8a662 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +pkg \ No newline at end of file diff --git a/History.txt b/History.txt new file mode 100644 index 0000000..6f3cf72 --- /dev/null +++ b/History.txt @@ -0,0 +1,5 @@ +=== 1.72.0 2009-09-24 + +* 2 major enhancements: + * evaluate javascript in jruby + * embed callable ruby objects in javascript diff --git a/Manifest.txt b/Manifest.txt new file mode 100644 index 0000000..efba7d1 --- /dev/null +++ b/Manifest.txt @@ -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 diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 0000000..2e648f4 --- /dev/null +++ b/README.rdoc @@ -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. \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..f4a9e6a --- /dev/null +++ b/Rakefile @@ -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 } + diff --git a/lib/rhino.rb b/lib/rhino.rb new file mode 100644 index 0000000..ac610ef --- /dev/null +++ b/lib/rhino.rb @@ -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 \ No newline at end of file diff --git a/lib/rhino/context.rb b/lib/rhino/context.rb new file mode 100644 index 0000000..4276e4d --- /dev/null +++ b/lib/rhino/context.rb @@ -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, "", 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 \ No newline at end of file diff --git a/lib/rhino/java.rb b/lib/rhino/java.rb new file mode 100644 index 0000000..2d86427 --- /dev/null +++ b/lib/rhino/java.rb @@ -0,0 +1,8 @@ +require 'java' +require 'rhino/rhino-1.7R2.jar' + +module Rhino + module J + import "org.mozilla.javascript" + end +end \ No newline at end of file diff --git a/lib/rhino/rhino-1.7R2.jar b/lib/rhino/rhino-1.7R2.jar new file mode 100644 index 0000000..2369f99 Binary files /dev/null and b/lib/rhino/rhino-1.7R2.jar differ diff --git a/script/console b/script/console new file mode 100755 index 0000000..e0e580a --- /dev/null +++ b/script/console @@ -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" \ No newline at end of file diff --git a/script/destroy b/script/destroy new file mode 100755 index 0000000..e48464d --- /dev/null +++ b/script/destroy @@ -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) diff --git a/script/generate b/script/generate new file mode 100755 index 0000000..c27f655 --- /dev/null +++ b/script/generate @@ -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) diff --git a/spec/rhino/context_spec.rb b/spec/rhino/context_spec.rb new file mode 100644 index 0000000..641d1e8 --- /dev/null +++ b/spec/rhino/context_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/spec.opts b/spec/spec.opts new file mode 100644 index 0000000..cf6add7 --- /dev/null +++ b/spec/spec.opts @@ -0,0 +1 @@ +--colour \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..83c9a74 --- /dev/null +++ b/spec/spec_helper.rb @@ -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' diff --git a/tasks/jruby.rake b/tasks/jruby.rake new file mode 100644 index 0000000..60a9556 --- /dev/null +++ b/tasks/jruby.rake @@ -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 diff --git a/tasks/rspec.rake b/tasks/rspec.rake new file mode 100644 index 0000000..31a99b0 --- /dev/null +++ b/tasks/rspec.rake @@ -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