From cc7dfe66102f692e36dbd2e70d75a8bb6ff8aec2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 7 Feb 2011 09:01:34 -0600 Subject: [PATCH] Add Rhino runtime --- execjs.gemspec | 7 +++--- lib/execjs.rb | 1 + lib/execjs/rhino_runtime.rb | 46 +++++++++++++++++++++++++++++++++++++ lib/execjs/runtimes.rb | 3 +++ test/test_runtimes.rb | 1 + 5 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 lib/execjs/rhino_runtime.rb diff --git a/execjs.gemspec b/execjs.gemspec index a440c00..fd13f9f 100644 --- a/execjs.gemspec +++ b/execjs.gemspec @@ -10,17 +10,18 @@ Gem::Specification.new do |s| EOS s.files = [ - "lib/execjs", + "lib/execjs.rb", "lib/execjs/external_runtime.rb", + "lib/execjs/rhino_runtime.rb", "lib/execjs/runners", "lib/execjs/runners/basic.js", "lib/execjs/runners/node.js", "lib/execjs/runtimes.rb", - "lib/execjs/v8_runtime.rb", - "lib/execjs.rb" + "lib/execjs/v8_runtime.rb" ] s.add_development_dependency "therubyracer" + s.add_development_dependency "therubyrhino" s.authors = ["Sam Stephenson"] s.email = "sstephenson@gmail.com" diff --git a/lib/execjs.rb b/lib/execjs.rb index e3ef4f7..f310d5d 100644 --- a/lib/execjs.rb +++ b/lib/execjs.rb @@ -4,6 +4,7 @@ module ExecJS class ProgramError < Error; end autoload :ExternalRuntime, "execjs/external_runtime" + autoload :RhinoRuntime, "execjs/rhino_runtime" autoload :Runtimes, "execjs/runtimes" autoload :V8Runtime, "execjs/v8_runtime" diff --git a/lib/execjs/rhino_runtime.rb b/lib/execjs/rhino_runtime.rb new file mode 100644 index 0000000..32caca2 --- /dev/null +++ b/lib/execjs/rhino_runtime.rb @@ -0,0 +1,46 @@ +module ExecJS + class RhinoRuntime + def initialize(options) + end + + def exec(source) + if /\S/ =~ source + eval "(function(){#{source}})()" + end + end + + def eval(source) + if /\S/ =~ source + context = ::Rhino::Context.new + unbox context.eval("(#{source})") + end + rescue ::Rhino::JavascriptError => e + if e.message == "syntax error" + raise RuntimeError, e + else + raise ProgramError, e + end + end + + def available? + require "rhino" + true + rescue LoadError + false + end + + def unbox(value) + case value + when ::Rhino::NativeFunction + nil + when ::Rhino::NativeObject + value.inject({}) do |vs, (k, v)| + vs[k] = unbox(v) unless v.is_a?(::Rhino::NativeFunction) + vs + end + else + value + end + end + end +end diff --git a/lib/execjs/runtimes.rb b/lib/execjs/runtimes.rb index 9100248..3c80065 100644 --- a/lib/execjs/runtimes.rb +++ b/lib/execjs/runtimes.rb @@ -38,5 +38,8 @@ module ExecJS define_runtime :Spidermonkey, :command => "js", :runner_path => runner_path("basic.js") + + define_runtime :Rhino, + :as => RhinoRuntime end end diff --git a/test/test_runtimes.rb b/test/test_runtimes.rb index ace0092..2abe829 100644 --- a/test/test_runtimes.rb +++ b/test/test_runtimes.rb @@ -63,3 +63,4 @@ test_runtime :ExternalV8 test_runtime :Node test_runtime :JSC test_runtime :Spidermonkey +test_runtime :Rhino