1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Add Rhino runtime

This commit is contained in:
Joshua Peek 2011-02-07 09:01:34 -06:00
parent c00ee391d2
commit cc7dfe6610
5 changed files with 55 additions and 3 deletions

View file

@ -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"

View file

@ -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"

View file

@ -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

View file

@ -38,5 +38,8 @@ module ExecJS
define_runtime :Spidermonkey,
:command => "js",
:runner_path => runner_path("basic.js")
define_runtime :Rhino,
:as => RhinoRuntime
end
end

View file

@ -63,3 +63,4 @@ test_runtime :ExternalV8
test_runtime :Node
test_runtime :JSC
test_runtime :Spidermonkey
test_runtime :Rhino