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

Add therubyracer V8 runtime implementation

This commit is contained in:
Sam Stephenson 2011-02-06 22:52:13 -06:00
parent 5d0d51b6ba
commit 957d632c52
6 changed files with 60 additions and 7 deletions

View file

@ -5,6 +5,7 @@ module ExecJS
autoload :ExternalRuntime, "execjs/external_runtime"
autoload :Runtimes, "execjs/runtimes"
autoload :V8Runtime, "execjs/v8_runtime"
def self.exec(source)
runtime.exec(source)

View file

@ -24,10 +24,10 @@ module ExecJS
def available?
command = @command.split(/\s+/).first
binary = `which #{command}`
binary = `which #{command}`.strip
if $? == 0
if @test_args
output = "#{binary} #{@test_args} 2>&1"
output = `#{binary} #{@test_args} 2>&1`
output.match(@test_match)
else
true

View file

@ -19,6 +19,9 @@ module ExecJS
end
define_runtime :V8,
:as => V8Runtime
define_runtime :ExternalV8,
:command => "v8",
:test_args => "--help",
:test_match => /--crankshaft/,

48
lib/execjs/v8_runtime.rb Normal file
View file

@ -0,0 +1,48 @@
module ExecJS
class V8Runtime
def initialize(options)
end
def exec(source)
if /\S/ =~ source
eval "(function(){#{source}})()"
end
end
def eval(source)
if /\S/ =~ source
context = ::V8::Context.new
unbox context.eval("(#{source})")
end
rescue ::V8::JSError => e
if e.value["name"] == "SyntaxError"
raise RuntimeError, e
else
raise ProgramError, e
end
end
def available?
require "v8"
true
rescue LoadError
false
end
def unbox(value)
case value
when ::V8::Function
nil
when ::V8::Array
value.map { |v| unbox(v) }
when ::V8::Object
value.inject({}) do |vs, (k, v)|
vs[k] = unbox(v) unless v.is_a?(::V8::Function)
vs
end
else
value
end
end
end
end

View file

@ -12,9 +12,9 @@ class TestExecJS < Test::Unit::TestCase
def test_runtime_available
runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent")
assert_equal false, runtime.available?
assert !runtime.available?
runtime = ExecJS::ExternalRuntime.new(:command => "ruby")
assert_equal true, runtime.available?
assert runtime.available?
end
end

View file

@ -58,7 +58,8 @@ def test_runtime(name)
end
end
test_runtime :JSC
test_runtime :Node
test_runtime :Spidermonkey
test_runtime :V8
test_runtime :ExternalV8
test_runtime :Node
test_runtime :JSC
test_runtime :Spidermonkey