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:
parent
5d0d51b6ba
commit
957d632c52
6 changed files with 60 additions and 7 deletions
|
@ -5,6 +5,7 @@ module ExecJS
|
||||||
|
|
||||||
autoload :ExternalRuntime, "execjs/external_runtime"
|
autoload :ExternalRuntime, "execjs/external_runtime"
|
||||||
autoload :Runtimes, "execjs/runtimes"
|
autoload :Runtimes, "execjs/runtimes"
|
||||||
|
autoload :V8Runtime, "execjs/v8_runtime"
|
||||||
|
|
||||||
def self.exec(source)
|
def self.exec(source)
|
||||||
runtime.exec(source)
|
runtime.exec(source)
|
||||||
|
|
|
@ -24,10 +24,10 @@ module ExecJS
|
||||||
|
|
||||||
def available?
|
def available?
|
||||||
command = @command.split(/\s+/).first
|
command = @command.split(/\s+/).first
|
||||||
binary = `which #{command}`
|
binary = `which #{command}`.strip
|
||||||
if $? == 0
|
if $? == 0
|
||||||
if @test_args
|
if @test_args
|
||||||
output = "#{binary} #{@test_args} 2>&1"
|
output = `#{binary} #{@test_args} 2>&1`
|
||||||
output.match(@test_match)
|
output.match(@test_match)
|
||||||
else
|
else
|
||||||
true
|
true
|
||||||
|
|
|
@ -19,6 +19,9 @@ module ExecJS
|
||||||
end
|
end
|
||||||
|
|
||||||
define_runtime :V8,
|
define_runtime :V8,
|
||||||
|
:as => V8Runtime
|
||||||
|
|
||||||
|
define_runtime :ExternalV8,
|
||||||
:command => "v8",
|
:command => "v8",
|
||||||
:test_args => "--help",
|
:test_args => "--help",
|
||||||
:test_match => /--crankshaft/,
|
:test_match => /--crankshaft/,
|
||||||
|
|
48
lib/execjs/v8_runtime.rb
Normal file
48
lib/execjs/v8_runtime.rb
Normal 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
|
|
@ -12,9 +12,9 @@ class TestExecJS < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_runtime_available
|
def test_runtime_available
|
||||||
runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent")
|
runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent")
|
||||||
assert_equal false, runtime.available?
|
assert !runtime.available?
|
||||||
|
|
||||||
runtime = ExecJS::ExternalRuntime.new(:command => "ruby")
|
runtime = ExecJS::ExternalRuntime.new(:command => "ruby")
|
||||||
assert_equal true, runtime.available?
|
assert runtime.available?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -58,7 +58,8 @@ def test_runtime(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test_runtime :JSC
|
|
||||||
test_runtime :Node
|
|
||||||
test_runtime :Spidermonkey
|
|
||||||
test_runtime :V8
|
test_runtime :V8
|
||||||
|
test_runtime :ExternalV8
|
||||||
|
test_runtime :Node
|
||||||
|
test_runtime :JSC
|
||||||
|
test_runtime :Spidermonkey
|
||||||
|
|
Loading…
Reference in a new issue