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

Runtime -> ExternalRuntime

This commit is contained in:
Sam Stephenson 2011-02-06 20:57:51 -06:00
parent 39b51331ce
commit c5e22c4716
4 changed files with 13 additions and 12 deletions

View file

@ -3,8 +3,8 @@ module ExecJS
class RuntimeError < Error; end
class ProgramError < Error; end
autoload :Runtime, "execjs/runtime"
autoload :Runtimes, "execjs/runtimes"
autoload :ExternalRuntime, "execjs/external_runtime"
autoload :Runtimes, "execjs/runtimes"
def self.exec(source)
runtime.exec(source)

View file

@ -2,24 +2,24 @@ require "json"
require "tempfile"
module ExecJS
class Runtime
class ExternalRuntime
def initialize(options)
@command = options[:command]
@runner_path = options[:runner_path]
end
def exec(source)
compile_to_tempfile(source) do |file|
extract_result(exec_runtime(file.path))
end
end
def eval(source)
if /\S/ =~ source
exec("return eval(#{"(#{source})".to_json})")
end
end
def exec(source)
compile_to_tempfile(source) do |file|
extract_result(exec_runtime(file.path))
end
end
def available?
command = @command.split(/\s+/).first
`which #{command}`

View file

@ -9,7 +9,8 @@ module ExecJS
end
def self.define_runtime(name, options)
runtimes.push runtime = Runtime.new(options)
klass = options[:as] || ExternalRuntime
runtimes.push runtime = klass.new(options)
const_set(name, runtime)
end

View file

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